Search in sources :

Example 1 with HFCAClient

use of org.hyperledger.fabric_ca.sdk.HFCAClient in project fabric-sdk-java by hyperledger.

the class HFCAClientIT method testEnrollUnknownClient.

// Tests enrolling a user to an unknown CA client
@Test
public void testEnrollUnknownClient() throws Exception {
    thrown.expect(EnrollmentException.class);
    thrown.expectMessage("Failed to enroll user");
    CryptoSuite cryptoSuite = CryptoSuite.Factory.getCryptoSuite();
    // This client does not exist
    String clientName = "test CA client";
    HFCAClient clientWithName = HFCAClient.createNewInstance(clientName, testConfig.getIntegrationTestsSampleOrg(TEST_WITH_INTEGRATION_ORG).getCALocation(), testConfig.getIntegrationTestsSampleOrg(TEST_WITH_INTEGRATION_ORG).getCAProperties());
    clientWithName.setCryptoSuite(cryptoSuite);
    clientWithName.enroll(admin.getName(), TEST_ADMIN_PW);
}
Also used : CryptoSuite(org.hyperledger.fabric.sdk.security.CryptoSuite) ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) MockHFCAClient(org.hyperledger.fabric_ca.sdk.MockHFCAClient) HFCAClient(org.hyperledger.fabric_ca.sdk.HFCAClient) Test(org.junit.Test)

Example 2 with HFCAClient

use of org.hyperledger.fabric_ca.sdk.HFCAClient in project fabric-sdk-java by hyperledger.

the class HFCAClientIT method testDeleteAffiliationNotAllowed.

// Tests deleting an affiliation on CA that does not allow affiliation removal
@Test
public void testDeleteAffiliationNotAllowed() throws Exception {
    if (testConfig.isRunningAgainstFabric10()) {
        // needs v1.1
        return;
    }
    thrown.expectMessage("Authorization failure");
    HFCAClient client2 = HFCAClient.createNewInstance(testConfig.getIntegrationTestsSampleOrg(TEST_WITH_INTEGRATION_ORG2).getCALocation(), testConfig.getIntegrationTestsSampleOrg(TEST_WITH_INTEGRATION_ORG2).getCAProperties());
    client2.setCryptoSuite(crypto);
    // SampleUser can be any implementation that implements org.hyperledger.fabric.sdk.User Interface
    SampleUser admin2 = sampleStore.getMember(TEST_ADMIN_NAME, "org2");
    if (!admin2.isEnrolled()) {
        // Preregistered admin only needs to be enrolled with Fabric CA.
        admin2.setEnrollment(client2.enroll(admin2.getName(), TEST_ADMIN_PW));
    }
    HFCAAffiliation aff = client2.newHFCAAffiliation("org6");
    HFCAAffiliationResp resp = aff.delete(admin2);
    assertEquals("Incorrect status code", new Integer(400), new Integer(resp.getStatusCode()));
}
Also used : HFCAAffiliationResp(org.hyperledger.fabric_ca.sdk.HFCAAffiliation.HFCAAffiliationResp) HFCAAffiliation(org.hyperledger.fabric_ca.sdk.HFCAAffiliation) MockHFCAClient(org.hyperledger.fabric_ca.sdk.MockHFCAClient) HFCAClient(org.hyperledger.fabric_ca.sdk.HFCAClient) SampleUser(org.hyperledger.fabric.sdkintegration.SampleUser) Test(org.junit.Test)

Example 3 with HFCAClient

use of org.hyperledger.fabric_ca.sdk.HFCAClient in project fabric-sdk-java by hyperledger.

the class End2endIT method enrollUsersSetup.

/**
 * Will register and enroll users persisting them to samplestore.
 *
 * @param sampleStore
 * @throws Exception
 */
public void enrollUsersSetup(SampleStore sampleStore) throws Exception {
    for (SampleOrg sampleOrg : testSampleOrgs) {
        HFCAClient ca = sampleOrg.getCAClient();
        final String orgName = sampleOrg.getName();
        final String mspid = sampleOrg.getMSPID();
        ca.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());
        if (testConfig.isRunningFabricTLS()) {
            // This shows how to get a client TLS certificate from Fabric CA
            // we will use one client TLS certificate for orderer peers etc.
            final EnrollmentRequest enrollmentRequestTLS = new EnrollmentRequest();
            enrollmentRequestTLS.addHost("localhost");
            enrollmentRequestTLS.setProfile("tls");
            final Enrollment enroll = ca.enroll("admin", "adminpw", enrollmentRequestTLS);
            final String tlsCertPEM = enroll.getCert();
            final String tlsKeyPEM = getPEMStringFromPrivateKey(enroll.getKey());
            final Properties tlsProperties = new Properties();
            tlsProperties.put("clientKeyBytes", tlsKeyPEM.getBytes(UTF_8));
            tlsProperties.put("clientCertBytes", tlsCertPEM.getBytes(UTF_8));
            clientTLSProperties.put(sampleOrg.getName(), tlsProperties);
            // Save in samplestore for follow on tests.
            sampleStore.storeClientPEMTLCertificate(sampleOrg, tlsCertPEM);
            sampleStore.storeClientPEMTLSKey(sampleOrg, tlsKeyPEM);
        }
        // just check if we connect at all.
        HFCAInfo info = ca.info();
        assertNotNull(info);
        String infoName = info.getCAName();
        if (infoName != null && !infoName.isEmpty()) {
            assertEquals(ca.getCAName(), infoName);
        }
        SampleUser admin = sampleStore.getMember(TEST_ADMIN_NAME, orgName);
        if (!admin.isEnrolled()) {
            // Preregistered admin only needs to be enrolled with Fabric caClient.
            admin.setEnrollment(ca.enroll(admin.getName(), "adminpw"));
            admin.setMspId(mspid);
        }
        // The admin of this org --
        sampleOrg.setAdmin(admin);
        SampleUser user = sampleStore.getMember(TESTUSER_1_NAME, sampleOrg.getName());
        if (!user.isRegistered()) {
            // users need to be registered AND enrolled
            RegistrationRequest rr = new RegistrationRequest(user.getName(), "org1.department1");
            user.setEnrollmentSecret(ca.register(rr, admin));
        }
        if (!user.isEnrolled()) {
            user.setEnrollment(ca.enroll(user.getName(), user.getEnrollmentSecret()));
            user.setMspId(mspid);
        }
        // Remember user belongs to this Org
        sampleOrg.addUser(user);
        final String sampleOrgName = sampleOrg.getName();
        final String sampleOrgDomainName = sampleOrg.getDomainName();
        // src/test/fixture/sdkintegration/e2e-2Orgs/channel/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/keystore/
        SampleUser peerOrgAdmin = sampleStore.getMember(sampleOrgName + "Admin", sampleOrgName, sampleOrg.getMSPID(), Util.findFileSk(Paths.get(testConfig.getTestChannelPath(), "crypto-config/peerOrganizations/", sampleOrgDomainName, format("/users/Admin@%s/msp/keystore", sampleOrgDomainName)).toFile()), Paths.get(testConfig.getTestChannelPath(), "crypto-config/peerOrganizations/", sampleOrgDomainName, format("/users/Admin@%s/msp/signcerts/Admin@%s-cert.pem", sampleOrgDomainName, sampleOrgDomainName)).toFile());
        // A special user that can create channels, join peers and install chaincode
        sampleOrg.setPeerAdmin(peerOrgAdmin);
    }
}
Also used : EnrollmentRequest(org.hyperledger.fabric_ca.sdk.EnrollmentRequest) Enrollment(org.hyperledger.fabric.sdk.Enrollment) Properties(java.util.Properties) RegistrationRequest(org.hyperledger.fabric_ca.sdk.RegistrationRequest) HFCAClient(org.hyperledger.fabric_ca.sdk.HFCAClient) HFCAInfo(org.hyperledger.fabric_ca.sdk.HFCAInfo)

Example 4 with HFCAClient

use of org.hyperledger.fabric_ca.sdk.HFCAClient in project fabric-sdk-java by hyperledger.

the class NetworkConfigIT method doMainSetup.

@BeforeClass
public static void doMainSetup() throws Exception {
    out("\n\n\nRUNNING: NetworkConfigIT.\n");
    resetConfig();
    configHelper.customizeConfig();
    // Use the appropriate TLS/non-TLS network config file
    networkConfig = NetworkConfig.fromYamlFile(testConfig.getTestNetworkConfigFileYAML());
    // Check if we get access to defined CAs!
    CAInfo caInfo = networkConfig.getOrganizationInfo("Org1").getCertificateAuthorities().get(0);
    HFCAClient hfcaClient = HFCAClient.createNewInstance(caInfo);
    assertEquals(hfcaClient.getCAName(), caInfo.getCAName());
    // makes actual REST call.
    HFCAInfo info = hfcaClient.info();
    assertEquals(caInfo.getCAName(), info.getCAName());
    // with no caname or the default
    caInfo = networkConfig.getOrganizationInfo("Org2").getCertificateAuthorities().get(0);
    hfcaClient = HFCAClient.createNewInstance(caInfo);
    assertEquals(hfcaClient.getCAName(), caInfo.getCAName());
    assertNull(caInfo.getCAName());
    // makes actual REST call.
    info = hfcaClient.info();
    // means default
    assertEquals(info.getCAName(), "");
    // Ensure the chaincode required for these tests is deployed
    deployChaincodeIfRequired();
}
Also used : HFCAInfo(org.hyperledger.fabric_ca.sdk.HFCAInfo) CAInfo(org.hyperledger.fabric.sdk.NetworkConfig.CAInfo) HFCAClient(org.hyperledger.fabric_ca.sdk.HFCAClient) HFCAInfo(org.hyperledger.fabric_ca.sdk.HFCAInfo) BeforeClass(org.junit.BeforeClass)

Example 5 with HFCAClient

use of org.hyperledger.fabric_ca.sdk.HFCAClient in project fabric-sdk-java by hyperledger.

the class HFCAClientIT method testDeleteIdentityNotAllowed.

// Tests deleting an identity on CA that does not allow identity removal
@Test
public void testDeleteIdentityNotAllowed() throws Exception {
    if (testConfig.isRunningAgainstFabric10()) {
        // needs v1.1
        return;
    }
    thrown.expectMessage("Identity removal is disabled");
    SampleUser user = new SampleUser("testuser5", "org2", sampleStore);
    HFCAClient client2 = HFCAClient.createNewInstance(testConfig.getIntegrationTestsSampleOrg(TEST_WITH_INTEGRATION_ORG2).getCALocation(), testConfig.getIntegrationTestsSampleOrg(TEST_WITH_INTEGRATION_ORG2).getCAProperties());
    client2.setCryptoSuite(crypto);
    // SampleUser can be any implementation that implements org.hyperledger.fabric.sdk.User Interface
    SampleUser admin2 = sampleStore.getMember(TEST_ADMIN_NAME, "org2");
    if (!admin2.isEnrolled()) {
        // Preregistered admin only needs to be enrolled with Fabric CA.
        admin2.setEnrollment(client2.enroll(admin.getName(), TEST_ADMIN_PW));
    }
    HFCAIdentity ident = client2.newHFCAIdentity(user.getName());
    ident.create(admin2);
    ident.delete(admin2);
}
Also used : HFCAIdentity(org.hyperledger.fabric_ca.sdk.HFCAIdentity) MockHFCAClient(org.hyperledger.fabric_ca.sdk.MockHFCAClient) HFCAClient(org.hyperledger.fabric_ca.sdk.HFCAClient) SampleUser(org.hyperledger.fabric.sdkintegration.SampleUser) Test(org.junit.Test)

Aggregations

HFCAClient (org.hyperledger.fabric_ca.sdk.HFCAClient)5 MockHFCAClient (org.hyperledger.fabric_ca.sdk.MockHFCAClient)3 Test (org.junit.Test)3 SampleUser (org.hyperledger.fabric.sdkintegration.SampleUser)2 HFCAInfo (org.hyperledger.fabric_ca.sdk.HFCAInfo)2 Properties (java.util.Properties)1 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)1 Enrollment (org.hyperledger.fabric.sdk.Enrollment)1 CAInfo (org.hyperledger.fabric.sdk.NetworkConfig.CAInfo)1 CryptoSuite (org.hyperledger.fabric.sdk.security.CryptoSuite)1 EnrollmentRequest (org.hyperledger.fabric_ca.sdk.EnrollmentRequest)1 HFCAAffiliation (org.hyperledger.fabric_ca.sdk.HFCAAffiliation)1 HFCAAffiliationResp (org.hyperledger.fabric_ca.sdk.HFCAAffiliation.HFCAAffiliationResp)1 HFCAIdentity (org.hyperledger.fabric_ca.sdk.HFCAIdentity)1 RegistrationRequest (org.hyperledger.fabric_ca.sdk.RegistrationRequest)1 BeforeClass (org.junit.BeforeClass)1