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(s3): bucket deletion fails if object creation races against cleanup #26875

Merged
merged 19 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -34,6 +34,26 @@ async function onUpdate(event: AWSLambda.CloudFormationCustomResourceEvent) {
}
}

/**
* Set a write deny policy to prevent new object creation while we're emptying the bucket.
*
* @param bucketName the bucket name
*/
async function denyWrites(bucketName: string) {
const policy = {
"Version": "2012-10-17",
"Statement": [
{
"Principal": "*",
"Effect": "Deny",
"Action": ["s3:PutObject"],
"Resource": [`arn:aws:s3:::${bucketName}/*`]
}
]
};
await s3.putBucketPolicy({ Bucket: bucketName, Policy: JSON.stringify(policy) });
}

/**
* Recursively delete all items in the bucket
*
Expand Down Expand Up @@ -63,6 +83,7 @@ async function onDelete(bucketName?: string) {
console.log(`Bucket does not have '${AUTO_DELETE_OBJECTS_TAG}' tag, skipping cleaning.`);
return;
}
await denyWrites(bucketName);
await emptyBucket(bucketName);
} catch (error: any) {
// Bucket doesn't exist, all is well
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const mockS3Client = {
putBucketPolicy: jest.fn(),
listObjectVersions: jest.fn(),
deleteObjects: jest.fn(),
getBucketTagging: jest.fn(),
Expand All @@ -18,6 +19,7 @@ jest.mock('@aws-sdk/client-s3', () => {
});

beforeEach(() => {
mockS3Client.putBucketPolicy.mockReturnThis();
mockS3Client.listObjectVersions.mockReturnThis();
mockS3Client.deleteObjects.mockReturnThis();
givenTaggedForDeletion();
Expand All @@ -41,8 +43,9 @@ test('does nothing on create event', async () => {
await invokeHandler(event);

// THEN
expect(mockS3Client.listObjectVersions).toHaveBeenCalledTimes(0);
expect(mockS3Client.deleteObjects).toHaveBeenCalledTimes(0);
expect(mockS3Client.putBucketPolicy).not.toHaveBeenCalled();
expect(mockS3Client.listObjectVersions).not.toHaveBeenCalled();
expect(mockS3Client.deleteObjects).not.toHaveBeenCalled();
});

test('does nothing on update event when everything remains the same', async () => {
Expand All @@ -63,8 +66,9 @@ test('does nothing on update event when everything remains the same', async () =
await invokeHandler(event);

// THEN
expect(mockS3Client.listObjectVersions).toHaveBeenCalledTimes(0);
expect(mockS3Client.deleteObjects).toHaveBeenCalledTimes(0);
expect(mockS3Client.putBucketPolicy).not.toHaveBeenCalled();
expect(mockS3Client.listObjectVersions).not.toHaveBeenCalled();
expect(mockS3Client.deleteObjects).not.toHaveBeenCalled();
});

test('does nothing on update event when the bucket name remains the same but the service token changes', async () => {
Expand All @@ -85,8 +89,9 @@ test('does nothing on update event when the bucket name remains the same but the
await invokeHandler(event);

// THEN
expect(mockS3Client.listObjectVersions).toHaveBeenCalledTimes(0);
expect(mockS3Client.deleteObjects).toHaveBeenCalledTimes(0);
expect(mockS3Client.putBucketPolicy).not.toHaveBeenCalled();
expect(mockS3Client.listObjectVersions).not.toHaveBeenCalled();
expect(mockS3Client.deleteObjects).not.toHaveBeenCalled();
});

test('does nothing on update event when the old resource properties are absent', async () => {
Expand All @@ -103,8 +108,9 @@ test('does nothing on update event when the old resource properties are absent',
await invokeHandler(event);

// THEN
expect(mockS3Client.listObjectVersions).toHaveBeenCalledTimes(0);
expect(mockS3Client.deleteObjects).toHaveBeenCalledTimes(0);
expect(mockS3Client.putBucketPolicy).not.toHaveBeenCalled();
expect(mockS3Client.listObjectVersions).not.toHaveBeenCalled();
expect(mockS3Client.deleteObjects).not.toHaveBeenCalled();
});

test('does nothing on update event when the new resource properties are absent', async () => {
Expand All @@ -121,8 +127,9 @@ test('does nothing on update event when the new resource properties are absent',
await invokeHandler(event);

// THEN
expect(mockS3Client.listObjectVersions).toHaveBeenCalledTimes(0);
expect(mockS3Client.deleteObjects).toHaveBeenCalledTimes(0);
expect(mockS3Client.putBucketPolicy).not.toHaveBeenCalled();
expect(mockS3Client.listObjectVersions).not.toHaveBeenCalled();
expect(mockS3Client.deleteObjects).not.toHaveBeenCalled();
});

test('deletes all objects when the name changes on update event', async () => {
Expand Down Expand Up @@ -150,6 +157,11 @@ test('deletes all objects when the name changes on update event', async () => {
await invokeHandler(event);

// THEN
expect(mockS3Client.putBucketPolicy).toHaveBeenCalledTimes(1);
expect(mockS3Client.putBucketPolicy).toHaveBeenCalledWith({
Bucket: 'MyBucket',
Policy: '{"Version":"2012-10-17","Statement":[{"Principal":"*","Effect":"Deny","Action":["s3:PutObject"],"Resource":["arn:aws:s3:::MyBucket/*"]}]}'
});
expect(mockS3Client.listObjectVersions).toHaveBeenCalledTimes(1);
expect(mockS3Client.listObjectVersions).toHaveBeenCalledWith({ Bucket: 'MyBucket' });
expect(mockS3Client.deleteObjects).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -179,9 +191,14 @@ test('deletes no objects on delete event when bucket has no objects', async () =
await invokeHandler(event);

// THEN
expect(mockS3Client.putBucketPolicy).toHaveBeenCalledTimes(1);
expect(mockS3Client.putBucketPolicy).toHaveBeenCalledWith({
Bucket: 'MyBucket',
Policy: '{"Version":"2012-10-17","Statement":[{"Principal":"*","Effect":"Deny","Action":["s3:PutObject"],"Resource":["arn:aws:s3:::MyBucket/*"]}]}'
});
expect(mockS3Client.listObjectVersions).toHaveBeenCalledTimes(1);
expect(mockS3Client.listObjectVersions).toHaveBeenCalledWith({ Bucket: 'MyBucket' });
expect(mockS3Client.deleteObjects).toHaveBeenCalledTimes(0);
expect(mockS3Client.deleteObjects).not.toHaveBeenCalled();
});

test('deletes all objects on delete event', async () => {
Expand All @@ -204,6 +221,11 @@ test('deletes all objects on delete event', async () => {
await invokeHandler(event);

// THEN
expect(mockS3Client.putBucketPolicy).toHaveBeenCalledTimes(1);
expect(mockS3Client.putBucketPolicy).toHaveBeenCalledWith({
Bucket: 'MyBucket',
Policy: '{"Version":"2012-10-17","Statement":[{"Principal":"*","Effect":"Deny","Action":["s3:PutObject"],"Resource":["arn:aws:s3:::MyBucket/*"]}]}'
});
expect(mockS3Client.listObjectVersions).toHaveBeenCalledTimes(1);
expect(mockS3Client.listObjectVersions).toHaveBeenCalledWith({ Bucket: 'MyBucket' });
expect(mockS3Client.deleteObjects).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -239,6 +261,7 @@ test('does not empty bucket if it is not tagged', async () => {
await invokeHandler(event);

// THEN
expect(mockS3Client.putBucketPolicy).not.toHaveBeenCalled();
expect(mockS3Client.listObjectVersions).not.toHaveBeenCalled();
});

Expand Down Expand Up @@ -270,6 +293,11 @@ test('delete event where bucket has many objects does recurse appropriately', as
await invokeHandler(event);

// THEN
expect(mockS3Client.putBucketPolicy).toHaveBeenCalledTimes(1);
expect(mockS3Client.putBucketPolicy).toHaveBeenCalledWith({
Bucket: 'MyBucket',
Policy: '{"Version":"2012-10-17","Statement":[{"Principal":"*","Effect":"Deny","Action":["s3:PutObject"],"Resource":["arn:aws:s3:::MyBucket/*"]}]}'
});
expect(mockS3Client.listObjectVersions).toHaveBeenCalledTimes(2);
expect(mockS3Client.listObjectVersions).toHaveBeenCalledWith({ Bucket: 'MyBucket' });
expect(mockS3Client.deleteObjects).toHaveBeenCalledTimes(2);
Expand All @@ -295,7 +323,7 @@ test('delete event where bucket has many objects does recurse appropriately', as

test('does nothing when the bucket does not exist', async () => {
// GIVEN
mockS3Client.listObjectVersions.mockImplementation(async () => {
mockS3Client.putBucketPolicy.mockImplementation(async () => {
const { S3ServiceException } = jest.requireActual('@aws-sdk/client-s3');
return new S3ServiceException({
name: 'NoSuchBucket',
Expand Down
Loading