Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(certificatemanager): unable to set removal policy on DnsValidatedCertificate #22040

Merged
merged 2 commits into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,9 @@ exports.certificateRequestHandler = async function (event, context) {
responseData.Arn = physicalResourceId = certificateArn;
break;
case 'Delete':
if (event.ResourceProperties.RemovalPolicy === 'retain') {
break;
}
physicalResourceId = event.PhysicalResourceId;
// If the resource didn't create correctly, the physical resource ID won't be the
// certificate ARN, so don't try to delete it in that case.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,38 @@ describe('DNS Validated Certificate Handler', () => {
});
});

test('Delete operation does not delete the certificate if RemovalPolicy===retain', () => {
const describeCertificateFake = sinon.fake.resolves({
Certificate: {
CertificateArn: testCertificateArn,
}
});
AWS.mock('ACM', 'describeCertificate', describeCertificateFake);

const deleteCertificateFake = sinon.fake.resolves({});
AWS.mock('ACM', 'deleteCertificate', deleteCertificateFake);

const request = nock(ResponseURL).put('/', body => {
return body.Status === 'SUCCESS';
}).reply(200);

return LambdaTester(handler.certificateRequestHandler)
.event({
RequestType: 'Delete',
RequestId: testRequestId,
PhysicalResourceId: testCertificateArn,
ResourceProperties: {
Region: 'us-east-1',
RemovalPolicy: 'retain',
}
})
.expectResolve(() => {
sinon.assert.notCalled(describeCertificateFake);
sinon.assert.notCalled(deleteCertificateFake);
expect(request.isDone()).toBe(true);
});
});

test('Delete operation is idempotent', () => {
const error = new Error();
error.name = 'ResourceNotFoundException';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export class DnsValidatedCertificate extends CertificateBase implements ICertifi
private normalizedZoneName: string;
private hostedZoneId: string;
private domainName: string;
private _removalPolicy?: cdk.RemovalPolicy;

constructor(scope: Construct, id: string, props: DnsValidatedCertificateProps) {
super(scope, id);
Expand Down Expand Up @@ -132,6 +133,7 @@ export class DnsValidatedCertificate extends CertificateBase implements ICertifi
HostedZoneId: this.hostedZoneId,
Region: props.region,
Route53Endpoint: props.route53Endpoint,
RemovalPolicy: cdk.Lazy.any({ produce: () => this._removalPolicy }),
// Custom resources properties are always converted to strings; might as well be explict here.
CleanupRecords: props.cleanupRoute53Records ? 'true' : undefined,
Tags: cdk.Lazy.list({ produce: () => this.tags.renderTags() }),
Expand All @@ -143,6 +145,10 @@ export class DnsValidatedCertificate extends CertificateBase implements ICertifi
this.node.addValidation({ validate: () => this.validateDnsValidatedCertificate() });
}

public applyRemovalPolicy(policy: cdk.RemovalPolicy): void {
this._removalPolicy = policy;
}

private validateDnsValidatedCertificate(): string[] {
const errors: string[] = [];
// Ensure the zone name is a parent zone of the certificate domain name
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Template } from '@aws-cdk/assertions';
import * as iam from '@aws-cdk/aws-iam';
import { HostedZone, PublicHostedZone } from '@aws-cdk/aws-route53';
import { App, Stack, Token, Tags } from '@aws-cdk/core';
import { App, Stack, Token, Tags, RemovalPolicy } from '@aws-cdk/core';
import { DnsValidatedCertificate } from '../lib/dns-validated-certificate';

test('creates CloudFormation Custom Resource', () => {
Expand Down Expand Up @@ -266,4 +266,36 @@ test('test transparency logging settings is passed to the custom resource', () =
},
CertificateTransparencyLoggingPreference: 'DISABLED',
});
});
});

test('can set removal policy', () => {
const stack = new Stack();

const exampleDotComZone = new PublicHostedZone(stack, 'ExampleDotCom', {
zoneName: 'example.com',
});

const cert = new DnsValidatedCertificate(stack, 'Certificate', {
domainName: 'test.example.com',
hostedZone: exampleDotComZone,
subjectAlternativeNames: ['test2.example.com'],
cleanupRoute53Records: true,
});
cert.applyRemovalPolicy(RemovalPolicy.RETAIN);

Template.fromStack(stack).hasResourceProperties('AWS::CloudFormation::CustomResource', {
DomainName: 'test.example.com',
SubjectAlternativeNames: ['test2.example.com'],
RemovalPolicy: 'retain',
ServiceToken: {
'Fn::GetAtt': [
'CertificateCertificateRequestorFunction5E845413',
'Arn',
],
},
HostedZoneId: {
Ref: 'ExampleDotCom4D1B83AA',
},
CleanupRecords: 'true',
});
});