Skip to content

Commit d220b94

Browse files
committed
Undo all my hard work
1 parent 7e5698b commit d220b94

10 files changed

Lines changed: 55 additions & 303 deletions

File tree

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,6 @@
9191
"@aws-cdk/assertions/string-width/**",
9292
"@aws-cdk/assertions/table",
9393
"@aws-cdk/assertions/table/**",
94-
"@aws-cdk/aws-s3/yaml",
95-
"@aws-cdk/aws-s3/yaml/**",
9694
"@aws-cdk/aws-codebuild/yaml",
9795
"@aws-cdk/aws-codebuild/yaml/**",
9896
"@aws-cdk/aws-codepipeline-actions/case",

packages/@aws-cdk/aws-s3/NOTICE

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,2 @@
11
AWS Cloud Development Kit (AWS CDK)
22
Copyright 2018-2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3-
4-
-------------------------------------------------------------------------------
5-
6-
The AWS CDK includes the following third-party software/licensing:
7-
8-
** yaml - https://www.npmjs.com/package/yaml
9-
Copyright 2018 Eemeli Aro <eemeli@gmail.com>
10-
11-
Permission to use, copy, modify, and/or distribute this software for any purpose
12-
with or without fee is hereby granted, provided that the above copyright notice
13-
and this permission notice appear in all copies.
14-
15-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
16-
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17-
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
18-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
19-
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20-
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
21-
THIS SOFTWARE.
22-
23-
----------------
Lines changed: 5 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,7 @@
11
// eslint-disable-next-line import/no-extraneous-dependencies
2-
import { S3, CloudFormation } from 'aws-sdk';
3-
import * as yaml from 'yaml';
4-
5-
/**
6-
* The custom resource should clear the bucket in one of the following cases:
7-
*
8-
* - The stack is deleted
9-
* - The target bucket is removed from the template
10-
* - The target bucket is replaced
11-
* - The target bucket is created in a deployment that gets rolled back
12-
*
13-
* In particular, it should NOT clear the bucket in a case the custom resource is deleted
14-
* but the target bucket is unaffected. This could happen in the following cases:
15-
*
16-
* - The autoDelete feature used to be turned on, and now gets turned off (leads to removal
17-
* of the CR from the template, without affecting the bucket)
18-
* - The autoDelete feature used to be turned off, now gets turned on, but the deployment
19-
* gets rolled back (leads to creation and immediate deletion of the CR, without
20-
* affecting the bucket).
21-
*
22-
* The only cases where we might misclassify is when the CR gets deleted. To
23-
* determine whether or not we should empty the bucket, during a `Delete` event
24-
* we will look at the stack state and depending on the state of the stack
25-
* (rolling forward or rolling backward), compare the OLD and NEW templates to
26-
* determine whether the bucket should be present in the final state:
27-
*
28-
* - ROLL FORWARD: delete contents if the bucket is not in the NEW template
29-
* - ROLLBACK: delete contents if the bucket is not in the OLD template
30-
*/
2+
import { S3 } from 'aws-sdk';
313

324
const s3 = new S3();
33-
const cfn = new CloudFormation();
345

356
export async function handler(event: AWSLambda.CloudFormationCustomResourceEvent) {
367
switch (event.RequestType) {
@@ -39,7 +10,7 @@ export async function handler(event: AWSLambda.CloudFormationCustomResourceEvent
3910
case 'Update':
4011
return onUpdate(event);
4112
case 'Delete':
42-
return onDelete(event.StackId, event.ResourceProperties?.BucketLogicalId, event.ResourceProperties?.BucketName);
13+
return onDelete(event.ResourceProperties?.BucketName);
4314
}
4415
}
4516

@@ -53,7 +24,7 @@ async function onUpdate(event: AWSLambda.CloudFormationCustomResourceEvent) {
5324
and create a new one with the new name. So we have to delete the contents of the
5425
bucket so that this operation does not fail. */
5526
if (bucketNameHasChanged) {
56-
return emptyBucket(oldBucketName);
27+
return onDelete(oldBucketName);
5728
}
5829
}
5930

@@ -77,58 +48,9 @@ async function emptyBucket(bucketName: string) {
7748
}
7849
}
7950

80-
async function onDelete(stackId: string, logicalId?: string, bucketName?: string) {
51+
async function onDelete(bucketName?: string) {
8152
if (!bucketName) {
8253
throw new Error('No BucketName was provided.');
8354
}
84-
if (!logicalId) {
85-
throw new Error('No Logical ID was provided.');
86-
}
87-
if (await isBucketAboutToBeDeleted(stackId, logicalId)) {
88-
await emptyBucket(bucketName);
89-
}
55+
await emptyBucket(bucketName);
9056
}
91-
92-
/**
93-
* Go and inspect CloudFormation to see if the target bucket is about to be deleted
94-
*/
95-
async function isBucketAboutToBeDeleted(stackId: string, logicalId: string) {
96-
const stackResponse = await cfn.describeStacks({ StackName: stackId }).promise();
97-
if (!stackResponse.Stacks?.[0]) {
98-
throw new Error(`Could not find stack with ID: ${stackId}`);
99-
}
100-
const stackStatus = stackResponse.Stacks[0].StackStatus;
101-
process.stdout.write(`Stack status: ${stackStatus}\n`);
102-
103-
// Case 1: the stack failed creation.
104-
// Case 2: the stack is being deleted.
105-
// In both cases, by definition the bucket will go bye-bye.
106-
if (stackStatus === 'ROLLBACK_IN_PROGRESS' || stackStatus === 'DELETE_IN_PROGRESS') {
107-
return true;
108-
}
109-
110-
// Case 3: we're cleaning up after a successful rollforward.
111-
// Case 4: we're rolling back a failed update.
112-
// In both cases, either the bucket is also being deleted here, or it's just
113-
// the CR that's being deleted.
114-
// `GetTemplate` will show us the template we are moving to ('new' in case 3,
115-
// 'old' in case 4). We will check if the bucket is in the template returned
116-
// by `GetTemplate` to see if we need to clean it.
117-
const destinationTemplateResponse = await cfn.getTemplate({ StackName: stackId, TemplateStage: 'Processed' }).promise();
118-
let template;
119-
try {
120-
template = yaml.parse(destinationTemplateResponse.TemplateBody ?? '{}', {
121-
schema: 'core',
122-
});
123-
} catch (e) {
124-
throw new Error(`Unable to parse CloudFormation template (is it not YAML?): ${destinationTemplateResponse.TemplateBody}`);
125-
}
126-
127-
if (logicalId in (template.Resources ?? {})) {
128-
process.stdout.write(`Bucket ${logicalId} is in target template, so NOT cleaning.\n`);
129-
return false;
130-
} else {
131-
process.stdout.write(`Bucket ${logicalId} is NOT in target template, so cleaning.\n`);
132-
return true;
133-
}
134-
}

packages/@aws-cdk/aws-s3/lib/bucket.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as events from '@aws-cdk/aws-events';
44
import * as iam from '@aws-cdk/aws-iam';
55
import * as kms from '@aws-cdk/aws-kms';
66
import {
7-
Aws, Fn, IResource, Lazy, RemovalPolicy, Resource, ResourceProps, Stack, Token,
7+
Fn, IResource, Lazy, RemovalPolicy, Resource, ResourceProps, Stack, Token,
88
CustomResource, CustomResourceProvider, CustomResourceProviderRuntime, FeatureFlags,
99
} from '@aws-cdk/core';
1010
import * as cxapi from '@aws-cdk/cx-api';
@@ -460,7 +460,7 @@ export abstract class BucketBase extends Resource implements IBucket {
460460
* Indicates if a bucket resource policy should automatically created upon
461461
* the first call to `addToResourcePolicy`.
462462
*/
463-
protected abstract autoCreatePolicy: boolean;
463+
protected abstract autoCreatePolicy = false;
464464

465465
/**
466466
* Whether to disallow public access
@@ -1443,7 +1443,6 @@ export class Bucket extends BucketBase {
14431443
private readonly metrics: BucketMetrics[] = [];
14441444
private readonly cors: CorsRule[] = [];
14451445
private readonly inventories: Inventory[] = [];
1446-
private readonly _resource: CfnBucket;
14471446

14481447
constructor(scope: Construct, id: string, props: BucketProps = {}) {
14491448
super(scope, id, {
@@ -1471,7 +1470,6 @@ export class Bucket extends BucketBase {
14711470
inventoryConfigurations: Lazy.any({ produce: () => this.parseInventoryConfiguration() }),
14721471
ownershipControls: this.parseOwnershipControls(props),
14731472
});
1474-
this._resource = resource;
14751473

14761474
resource.applyRemovalPolicy(props.removalPolicy);
14771475

@@ -1914,14 +1912,6 @@ export class Bucket extends BucketBase {
19141912
codeDirectory: path.join(__dirname, 'auto-delete-objects-handler'),
19151913
runtime: CustomResourceProviderRuntime.NODEJS_12_X,
19161914
description: `Lambda function for auto-deleting objects in ${this.bucketName} S3 bucket.`,
1917-
policyStatements: [
1918-
// As a reminder: these are not `iam.PolicyStatement`s, but plain JSON IAM statements
1919-
{
1920-
Effect: 'Allow',
1921-
Action: ['cloudformation:DescribeStacks', 'cloudformation:GetTemplate'],
1922-
Resource: Aws.STACK_ID,
1923-
},
1924-
],
19251915
});
19261916

19271917
// Use a bucket policy to allow the custom resource to delete
@@ -1944,7 +1934,6 @@ export class Bucket extends BucketBase {
19441934
serviceToken: provider.serviceToken,
19451935
properties: {
19461936
BucketName: this.bucketName,
1947-
BucketLogicalId: this._resource.logicalId,
19481937
},
19491938
});
19501939

packages/@aws-cdk/aws-s3/package.json

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
},
5656
"cdk-build": {
5757
"cloudformation": "AWS::S3",
58-
"pre": ["./pre-build.sh"],
5958
"env": {
6059
"AWSLINT_BASE_CONSTRUCT": "true"
6160
}
@@ -80,21 +79,16 @@
8079
"@aws-cdk/pkglint": "0.0.0",
8180
"@types/aws-lambda": "^8.10.83",
8281
"@types/jest": "^26.0.24",
83-
"jest": "^26.6.3",
84-
"aws-sdk": "^2"
82+
"jest": "^26.6.3"
8583
},
8684
"dependencies": {
8785
"@aws-cdk/aws-events": "0.0.0",
8886
"@aws-cdk/aws-iam": "0.0.0",
8987
"@aws-cdk/aws-kms": "0.0.0",
9088
"@aws-cdk/core": "0.0.0",
9189
"@aws-cdk/cx-api": "0.0.0",
92-
"constructs": "^3.3.69",
93-
"yaml": "1.10.2"
90+
"constructs": "^3.3.69"
9491
},
95-
"bundledDependencies": [
96-
"yaml"
97-
],
9892
"homepage": "https://github.com/aws/aws-cdk",
9993
"peerDependencies": {
10094
"@aws-cdk/aws-events": "0.0.0",

0 commit comments

Comments
 (0)