View a markdown version of this page

Create and activate a CA for AWS IAM for agents and workloads - AWS Private Certificate Authority

Create and activate a CA for AWS IAM for agents and workloads

This Java example creates a root certificate authority (CA) in the service-affiliated mode with the IAM_WORKLOAD_IDENTITY affiliation in US East (N. Virginia) (us-east-1). The CA uses the EC_prime256v1 key algorithm and the SHA256WITHECDSA signing algorithm. The example doesn't configure revocation because the affiliation turns off certificate revocation lists (CRLs) and Online Certificate Status Protocol (OCSP). The example waits for the CA to create its certificate signing request (CSR). It then has the CA sign its own certificate with the RootCACertificate/V1 template for 10 years. It imports that certificate to activate the CA and writes the CA certificate to workload-identity-ca.pem. To replicate the active CA to the other Regions where your workloads run, see Replicate a CA for AWS IAM for agents and workloads.

Run the example as WorkloadIdentityCAActivation common-name, where common-name is the common name of the CA certificate subject. The example prints the CA Amazon Resource Name (ARN), the CA ID, and the multi-Region type (PRIMARY). The CA ID is the value that you give to the workload identity provider's outbound signer in IAM.

Important

This example creates a private CA. Your account is charged a monthly price for each private CA starting from the time that you create it. You are billed for the CA in every status except DELETED and FAILED.

For the latest AWS Private CA pricing information, see AWS Private Certificate Authority Pricing. You can also use the AWS pricing calculator to estimate costs.

package com.amazonaws.samples.workloadidentity; import java.nio.file.Files; import java.nio.file.Path; import software.amazon.awssdk.core.SdkBytes; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.acmpca.AcmPcaClient; import software.amazon.awssdk.services.acmpca.model.ASN1Subject; import software.amazon.awssdk.services.acmpca.model.AcmPcaException; import software.amazon.awssdk.services.acmpca.model.CertificateAuthorityConfiguration; import software.amazon.awssdk.services.acmpca.model.CertificateAuthorityType; import software.amazon.awssdk.services.acmpca.model.CertificateAuthorityUsageMode; import software.amazon.awssdk.services.acmpca.model.CreateCertificateAuthorityRequest; import software.amazon.awssdk.services.acmpca.model.DescribeCertificateAuthorityRequest; import software.amazon.awssdk.services.acmpca.model.GetCertificateAuthorityCsrRequest; import software.amazon.awssdk.services.acmpca.model.GetCertificateRequest; import software.amazon.awssdk.services.acmpca.model.ImportCertificateAuthorityCertificateRequest; import software.amazon.awssdk.services.acmpca.model.IssueCertificateRequest; import software.amazon.awssdk.services.acmpca.model.KeyAlgorithm; import software.amazon.awssdk.services.acmpca.model.ServiceAffiliation; import software.amazon.awssdk.services.acmpca.model.SigningAlgorithm; import software.amazon.awssdk.services.acmpca.model.Validity; import software.amazon.awssdk.services.acmpca.model.ValidityPeriodType; /** * Creates and activates a service-affiliated root CA for AWS IAM for agents and workloads in the * US East (N. Virginia) Region, saves the CA certificate to workload-identity-ca.pem, and prints * the CA ARN and the CA ID that you give to the workload identity provider's outbound signer. */ public class WorkloadIdentityCAActivation { // A CA for AWS IAM for agents and workloads must be created in the US East (N. Virginia) Region. You then // replicate it to the other Regions where your workloads run. private static final Region PRIMARY_REGION = Region.US_EAST_1; // The service affiliation code for AWS IAM for agents and workloads. private static final String IAM_WORKLOAD_IDENTITY = "IAM_WORKLOAD_IDENTITY"; public static void main(String[] args) throws Exception { if (args.length != 1) { System.err.println("Usage: WorkloadIdentityCAActivation <common-name>"); System.exit(1); } String commonName = args[0]; // The client uses the default credential provider chain. The Region is fixed, because // the primary CA must be in the US East (N. Virginia) Region. try (AcmPcaClient client = AcmPcaClient.builder().region(PRIMARY_REGION).build()) { // Create a root CA in the service-affiliated mode with the AWS IAM for agents and workloads // affiliation. Don't configure revocation: the affiliation turns off CRLs and OCSP, // because the CA issues certificates that are valid for at most one day. CreateCertificateAuthorityRequest createRequest = CreateCertificateAuthorityRequest.builder() .certificateAuthorityType(CertificateAuthorityType.ROOT) .usageMode(CertificateAuthorityUsageMode.SERVICE_AFFILIATED) .serviceAffiliation(ServiceAffiliation.builder() .serviceAffiliationCode(IAM_WORKLOAD_IDENTITY) .build()) .certificateAuthorityConfiguration(CertificateAuthorityConfiguration.builder() .keyAlgorithm(KeyAlgorithm.EC_PRIME256_V1) .signingAlgorithm(SigningAlgorithm.SHA256_WITHECDSA) .subject(ASN1Subject.builder().commonName(commonName).build()) .build()) .build(); String caArn = client.createCertificateAuthority(createRequest).certificateAuthorityArn(); // Wait for the CA to create its certificate signing request (CSR), and then get it. GetCertificateAuthorityCsrRequest csrRequest = GetCertificateAuthorityCsrRequest.builder() .certificateAuthorityArn(caArn) .build(); client.waiter().waitUntilCertificateAuthorityCSRCreated(csrRequest); String csr = client.getCertificateAuthorityCsr(csrRequest).csr(); // Have the CA sign its own certificate with the RootCACertificate/V1 template. The // one-day validity limit of the affiliation applies to the certificates that the CA // issues to workloads, not to the CA certificate. IssueCertificateRequest issueRequest = IssueCertificateRequest.builder() .certificateAuthorityArn(caArn) .csr(SdkBytes.fromUtf8String(csr)) .signingAlgorithm(SigningAlgorithm.SHA256_WITHECDSA) .templateArn("arn:aws:acm-pca:::template/RootCACertificate/V1") .validity(Validity.builder().value(10L).type(ValidityPeriodType.YEARS).build()) .build(); String certificateArn = client.issueCertificate(issueRequest).certificateArn(); GetCertificateRequest getRequest = GetCertificateRequest.builder() .certificateAuthorityArn(caArn) .certificateArn(certificateArn) .build(); client.waiter().waitUntilCertificateIssued(getRequest); String caCertificate = client.getCertificate(getRequest).certificate(); // Install the CA certificate. The CA status changes to ACTIVE. A service-affiliated CA // accepts this import exactly once, so keep the CA certificate validity long enough // for the life of the CA. client.importCertificateAuthorityCertificate(ImportCertificateAuthorityCertificateRequest.builder() .certificateAuthorityArn(caArn) .certificate(SdkBytes.fromUtf8String(caCertificate)) .build()); // The CA is a multi-Region primary. Replicas share its CA ID, so IAM identifies the CA // by account ID and CA ID instead of by a Region-specific ARN. String multiRegionType = client.describeCertificateAuthority(DescribeCertificateAuthorityRequest.builder() .certificateAuthorityArn(caArn) .build()) .certificateAuthority() .multiRegionConfiguration() .multiRegionTypeAsString(); Files.writeString(Path.of("workload-identity-ca.pem"), caCertificate); System.out.println("CA ARN: " + caArn); System.out.println("CA ID: " + caArn.substring(caArn.lastIndexOf('/') + 1)); System.out.println("Multi-Region type: " + multiRegionType); } catch (AcmPcaException e) { // Print the error message from AWS Private CA, such as why it rejected the request. System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }