Search in sources :

Example 1 with CertificateAuthority

use of com.google.cloud.security.privateca.v1.CertificateAuthority in project java-security-private-ca by googleapis.

the class CertificateAuthorityServiceClientTest method listCertificateAuthoritiesTest2.

@Test
public void listCertificateAuthoritiesTest2() throws Exception {
    CertificateAuthority responsesElement = CertificateAuthority.newBuilder().build();
    ListCertificateAuthoritiesResponse expectedResponse = ListCertificateAuthoritiesResponse.newBuilder().setNextPageToken("").addAllCertificateAuthorities(Arrays.asList(responsesElement)).build();
    mockCertificateAuthorityService.addResponse(expectedResponse);
    String parent = "parent-995424086";
    ListCertificateAuthoritiesPagedResponse pagedListResponse = client.listCertificateAuthorities(parent);
    List<CertificateAuthority> resources = Lists.newArrayList(pagedListResponse.iterateAll());
    Assert.assertEquals(1, resources.size());
    Assert.assertEquals(expectedResponse.getCertificateAuthoritiesList().get(0), resources.get(0));
    List<AbstractMessage> actualRequests = mockCertificateAuthorityService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    ListCertificateAuthoritiesRequest actualRequest = ((ListCertificateAuthoritiesRequest) actualRequests.get(0));
    Assert.assertEquals(parent, actualRequest.getParent());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : ListCertificateAuthoritiesPagedResponse(com.google.cloud.security.privateca.v1.CertificateAuthorityServiceClient.ListCertificateAuthoritiesPagedResponse) AbstractMessage(com.google.protobuf.AbstractMessage) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Example 2 with CertificateAuthority

use of com.google.cloud.security.privateca.v1.CertificateAuthority in project java-security-private-ca by googleapis.

the class SnippetsIT method testCreateCertificateAuthority.

@Test
public void testCreateCertificateAuthority() throws IOException {
    // Check if the CA created during setup is successful.
    try (CertificateAuthorityServiceClient certificateAuthorityServiceClient = CertificateAuthorityServiceClient.create()) {
        CertificateAuthority response = certificateAuthorityServiceClient.getCertificateAuthority(CertificateAuthorityName.of(PROJECT_ID, LOCATION, CA_POOL_ID, CA_NAME).toString());
        assertThat(response.getName()).contains(CA_NAME);
    }
}
Also used : CertificateAuthorityServiceClient(com.google.cloud.security.privateca.v1.CertificateAuthorityServiceClient) CertificateAuthority(com.google.cloud.security.privateca.v1.CertificateAuthority) Test(org.junit.Test)

Example 3 with CertificateAuthority

use of com.google.cloud.security.privateca.v1.CertificateAuthority in project java-security-private-ca by googleapis.

the class SnippetsIT method testCreateSubordinateCertificateAuthority.

@Test
public void testCreateSubordinateCertificateAuthority() throws IOException {
    try (CertificateAuthorityServiceClient certificateAuthorityServiceClient = CertificateAuthorityServiceClient.create()) {
        CertificateAuthority response = certificateAuthorityServiceClient.getCertificateAuthority(CertificateAuthorityName.of(PROJECT_ID, LOCATION, CA_POOL_ID, SUBORDINATE_CA_NAME).toString());
        Assert.assertTrue(response.hasCreateTime());
    }
}
Also used : CertificateAuthorityServiceClient(com.google.cloud.security.privateca.v1.CertificateAuthorityServiceClient) CertificateAuthority(com.google.cloud.security.privateca.v1.CertificateAuthority) Test(org.junit.Test)

Example 4 with CertificateAuthority

use of com.google.cloud.security.privateca.v1.CertificateAuthority in project java-security-private-ca by googleapis.

the class CreateCertificateAuthority method createCertificateAuthority.

// Create Certificate Authority which is the root CA in the given CA Pool.
public static void createCertificateAuthority(String project, String location, String pool_Id, String certificateAuthorityName) throws InterruptedException, ExecutionException, IOException {
    // clean up any remaining background resources.
    try (CertificateAuthorityServiceClient certificateAuthorityServiceClient = CertificateAuthorityServiceClient.create()) {
        String commonName = "common-name";
        String orgName = "org-name";
        // Validity of this CA in seconds.
        int caDuration = 100000;
        // Set the type of Algorithm.
        KeyVersionSpec keyVersionSpec = KeyVersionSpec.newBuilder().setAlgorithm(SignHashAlgorithm.RSA_PKCS1_4096_SHA256).build();
        // Set CA subject config.
        SubjectConfig subjectConfig = SubjectConfig.newBuilder().setSubject(Subject.newBuilder().setCommonName(commonName).setOrganization(orgName).build()).build();
        // Set the key usage options for X.509 fields.
        X509Parameters x509Parameters = X509Parameters.newBuilder().setKeyUsage(KeyUsage.newBuilder().setBaseKeyUsage(KeyUsageOptions.newBuilder().setCrlSign(true).setCertSign(true).build()).build()).setCaOptions(CaOptions.newBuilder().setIsCa(true).build()).build();
        // Set certificate authority settings.
        CertificateAuthority certificateAuthority = CertificateAuthority.newBuilder().setType(CertificateAuthority.Type.SELF_SIGNED).setKeySpec(keyVersionSpec).setConfig(CertificateConfig.newBuilder().setSubjectConfig(subjectConfig).setX509Config(x509Parameters).build()).setLifetime(Duration.newBuilder().setSeconds(caDuration).build()).build();
        // Create the CertificateAuthorityRequest.
        CreateCertificateAuthorityRequest certificateAuthorityRequest = CreateCertificateAuthorityRequest.newBuilder().setParent(CaPoolName.of(project, location, pool_Id).toString()).setCertificateAuthorityId(certificateAuthorityName).setCertificateAuthority(certificateAuthority).build();
        // Create Certificate Authority.
        ApiFuture<Operation> futureCall = certificateAuthorityServiceClient.createCertificateAuthorityCallable().futureCall(certificateAuthorityRequest);
        Operation response = futureCall.get();
        if (response.hasError()) {
            System.out.println("Error while creating CA !" + response.getError());
            return;
        }
        System.out.println("Certificate Authority created successfully : " + certificateAuthorityName);
    }
}
Also used : SubjectConfig(com.google.cloud.security.privateca.v1.CertificateConfig.SubjectConfig) X509Parameters(com.google.cloud.security.privateca.v1.X509Parameters) CertificateAuthorityServiceClient(com.google.cloud.security.privateca.v1.CertificateAuthorityServiceClient) CreateCertificateAuthorityRequest(com.google.cloud.security.privateca.v1.CreateCertificateAuthorityRequest) KeyVersionSpec(com.google.cloud.security.privateca.v1.CertificateAuthority.KeyVersionSpec) CertificateAuthority(com.google.cloud.security.privateca.v1.CertificateAuthority) Operation(com.google.longrunning.Operation)

Example 5 with CertificateAuthority

use of com.google.cloud.security.privateca.v1.CertificateAuthority in project java-security-private-ca by googleapis.

the class CreateSubordinateCa method createSubordinateCertificateAuthority.

public static void createSubordinateCertificateAuthority(String project, String location, String pool_Id, String subordinateCaName) throws IOException, ExecutionException, InterruptedException {
    // clean up any remaining background resources.
    try (CertificateAuthorityServiceClient certificateAuthorityServiceClient = CertificateAuthorityServiceClient.create()) {
        String commonName = "common-name";
        String orgName = "csr-org-name";
        String domainName = "dns.your-domain.com";
        // Validity of this CA in seconds.
        int caDuration = 100000;
        // Set the type of Algorithm.
        KeyVersionSpec keyVersionSpec = KeyVersionSpec.newBuilder().setAlgorithm(SignHashAlgorithm.RSA_PKCS1_4096_SHA256).build();
        // Set CA subject config.
        SubjectConfig subjectConfig = SubjectConfig.newBuilder().setSubject(Subject.newBuilder().setCommonName(commonName).setOrganization(orgName).build()).setSubjectAltName(SubjectAltNames.newBuilder().addDnsNames(domainName).build()).build();
        // Set the key usage options for X.509 fields.
        X509Parameters x509Parameters = X509Parameters.newBuilder().setKeyUsage(KeyUsage.newBuilder().setBaseKeyUsage(KeyUsageOptions.newBuilder().setCrlSign(true).setCertSign(true).build()).build()).setCaOptions(CaOptions.newBuilder().setIsCa(true).build()).build();
        // Set certificate authority settings.
        CertificateAuthority subCertificateAuthority = CertificateAuthority.newBuilder().setType(CertificateAuthority.Type.SUBORDINATE).setKeySpec(keyVersionSpec).setConfig(CertificateConfig.newBuilder().setSubjectConfig(subjectConfig).setX509Config(x509Parameters).build()).setLifetime(Duration.newBuilder().setSeconds(caDuration).build()).build();
        // Create the CertificateAuthorityRequest.
        CreateCertificateAuthorityRequest subCertificateAuthorityRequest = CreateCertificateAuthorityRequest.newBuilder().setParent(CaPoolName.of(project, location, pool_Id).toString()).setCertificateAuthorityId(subordinateCaName).setCertificateAuthority(subCertificateAuthority).build();
        // Create Subordinate CA.
        ApiFuture<Operation> futureCall = certificateAuthorityServiceClient.createCertificateAuthorityCallable().futureCall(subCertificateAuthorityRequest);
        Operation response = futureCall.get();
        if (response.hasError()) {
            System.out.println("Error while creating Subordinate CA !" + response.getError());
            return;
        }
        System.out.println("Subordinate Certificate Authority created successfully : " + subordinateCaName);
    }
}
Also used : SubjectConfig(com.google.cloud.security.privateca.v1.CertificateConfig.SubjectConfig) X509Parameters(com.google.cloud.security.privateca.v1.X509Parameters) CertificateAuthorityServiceClient(com.google.cloud.security.privateca.v1.CertificateAuthorityServiceClient) CreateCertificateAuthorityRequest(com.google.cloud.security.privateca.v1.CreateCertificateAuthorityRequest) KeyVersionSpec(com.google.cloud.security.privateca.v1.CertificateAuthority.KeyVersionSpec) CertificateAuthority(com.google.cloud.security.privateca.v1.CertificateAuthority) Operation(com.google.longrunning.Operation)

Aggregations

CertificateAuthority (com.google.cloud.security.privateca.v1.CertificateAuthority)5 CertificateAuthorityServiceClient (com.google.cloud.security.privateca.v1.CertificateAuthorityServiceClient)5 Test (org.junit.Test)4 Operation (com.google.longrunning.Operation)3 KeyVersionSpec (com.google.cloud.security.privateca.v1.CertificateAuthority.KeyVersionSpec)2 ListCertificateAuthoritiesPagedResponse (com.google.cloud.security.privateca.v1.CertificateAuthorityServiceClient.ListCertificateAuthoritiesPagedResponse)2 SubjectConfig (com.google.cloud.security.privateca.v1.CertificateConfig.SubjectConfig)2 CreateCertificateAuthorityRequest (com.google.cloud.security.privateca.v1.CreateCertificateAuthorityRequest)2 X509Parameters (com.google.cloud.security.privateca.v1.X509Parameters)2 AbstractMessage (com.google.protobuf.AbstractMessage)2 UpdateCertificateAuthorityRequest (com.google.cloud.security.privateca.v1.UpdateCertificateAuthorityRequest)1 ByteString (com.google.protobuf.ByteString)1