Skip to content

Commit

Permalink
Import the generated certificate into ACM
Browse files Browse the repository at this point in the history
  • Loading branch information
miensol committed Jun 25, 2024
1 parent c6eb541 commit ab0fed2
Show file tree
Hide file tree
Showing 7 changed files with 895 additions and 8 deletions.
4 changes: 4 additions & 0 deletions .projen/deps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions .projen/tasks.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .projenrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const project = new awscdk.AwsCdkConstructLibrary({
name: '@brightinventions/cdk-self-signed-certificate',
projenrcTs: true,
repositoryUrl: 'https://github.com/piotr.mionskowski/bright-cdk-self-signed-certificate.git',
bundledDeps: ['selfsigned'],
bundledDeps: ['selfsigned', '@aws-sdk/client-acm'],
// deps: , /* Runtime dependencies of this module. */
// description: undefined, /* The description is just a string that helps people understand the purpose of the package. */
devDeps: ['@types/aws-lambda'], /* Build dependencies for this module. */
Expand Down
2 changes: 2 additions & 0 deletions package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,16 @@ export class SelfSignedCertificate extends Construct {

super(scope, id);
const packageDir = path.dirname(require.resolve('@brightinventions/cdk-self-signed-certificate/package.json'));

this.provider = CustomResourceProvider.getOrCreateProvider(this, CustomResourceType, {
codeDirectory: path.join(packageDir, 'assets', 'self-signed-certificate-lambda'),
runtime: CustomResourceProviderRuntime.NODEJS_20_X,
description: 'Lambda function created by the custom resource provider',
policyStatements: [{
Effect: 'Allow',
Action: 'acm:ImportCertificate',
Resource: '*',
}],
});

new CustomResource(this, 'resource', {
Expand Down
22 changes: 17 additions & 5 deletions src/self-signed-certificate-lambda/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import type { CdkCustomResourceHandler } from 'aws-lambda';
import type { pki } from 'node-forge';
import { generate } from 'selfsigned';
import { ACMClient, ImportCertificateCommand } from '@aws-sdk/client-acm';

const acmClient = new ACMClient({});

export const handler: CdkCustomResourceHandler = async (event) => {
if (event.RequestType == 'Delete') {
// TODO: remove from imports
return {
};
// TODO: remove from imports?
return {};
}

const certificateDetails = event.ResourceProperties.certificateDetails;
Expand All @@ -20,10 +21,21 @@ export const handler: CdkCustomResourceHandler = async (event) => {
days: 365 * 10,
});

const importResult = await acmClient.send(new ImportCertificateCommand({
CertificateArn: event.RequestType == 'Update' ? event.PhysicalResourceId : undefined,
Certificate: new Uint8Array(Buffer.from(generatedCertificate.cert, 'utf-8')),
PrivateKey: new Uint8Array(Buffer.from(generatedCertificate.private, 'utf-8')),
Tags: event.ResourceProperties.tags,
}));

console.log('Import result', importResult.CertificateArn);

return {
PhysicalResourceId: importResult.CertificateArn,
Data: {
public: generatedCertificate.public,
cert: generatedCertificate.cert,
PublicKey: generatedCertificate.public,
Certificate: generatedCertificate.cert,
CertificateArn: importResult.CertificateArn,
},
};
};
Loading

0 comments on commit ab0fed2

Please sign in to comment.