-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #757 from Enterprise-CMCS/main
Release to val
- Loading branch information
Showing
10 changed files
with
72 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,126 +1,6 @@ | ||
import { | ||
S3Client, | ||
ListObjectVersionsCommand, | ||
DeleteObjectsCommand, | ||
ObjectIdentifier, | ||
ObjectVersion, | ||
DeleteMarkerEntry, | ||
PutBucketPolicyCommand, | ||
GetBucketPolicyCommand, | ||
} from "@aws-sdk/client-s3"; | ||
|
||
const s3 = new S3Client({ region: process.env.AWS_REGION }); | ||
|
||
export const handler = async function (event: any) { | ||
console.log("Request received:\n", JSON.stringify(event, null, 2)); | ||
|
||
if (event.RequestType === "Delete") { | ||
const buckets = event.ResourceProperties.Buckets; | ||
for (const bucket of buckets) { | ||
await blockAllUploads(bucket); | ||
await deleteAllObjects(bucket); | ||
} | ||
} | ||
console.log( | ||
"This pattern is OBE. For lifecycle reasons, it will remain until it's released, then removed entirely", | ||
); | ||
}; | ||
|
||
async function blockAllUploads(bucket: string) { | ||
let existingPolicy; | ||
|
||
try { | ||
const getBucketPolicyParams = { | ||
Bucket: bucket, | ||
}; | ||
const response = await s3.send( | ||
new GetBucketPolicyCommand(getBucketPolicyParams), | ||
); | ||
console.log(response); | ||
existingPolicy = response.Policy | ||
? JSON.parse(response.Policy) | ||
: { | ||
Version: "2012-10-17", | ||
Statement: [], | ||
}; | ||
} catch (error) { | ||
if (error.name === "NoSuchBucketPolicy") { | ||
existingPolicy = { | ||
Version: "2012-10-17", | ||
Statement: [], | ||
}; | ||
} else { | ||
throw error; | ||
} | ||
} | ||
|
||
const newStatement = { | ||
Effect: "Deny", | ||
Principal: "*", | ||
Action: "s3:PutObject", | ||
Resource: `arn:aws:s3:::${bucket}/*`, | ||
}; | ||
|
||
existingPolicy.Statement.push(newStatement); | ||
|
||
const putBucketPolicyParams = { | ||
Bucket: bucket, | ||
Policy: JSON.stringify(existingPolicy), | ||
}; | ||
|
||
await s3.send(new PutBucketPolicyCommand(putBucketPolicyParams)); | ||
} | ||
|
||
async function listAllObjects(bucket: string): Promise<ObjectIdentifier[]> { | ||
let contents: ObjectIdentifier[] = []; | ||
let isTruncated = true; | ||
let keyMarker: string | undefined; | ||
let versionIdMarker: string | undefined; | ||
|
||
while (isTruncated) { | ||
const response = await s3.send( | ||
new ListObjectVersionsCommand({ | ||
Bucket: bucket, | ||
KeyMarker: keyMarker, | ||
VersionIdMarker: versionIdMarker, | ||
}), | ||
); | ||
|
||
if (response.Versions) { | ||
contents = contents.concat( | ||
response.Versions.map((version: ObjectVersion) => ({ | ||
Key: version.Key!, | ||
VersionId: version.VersionId, | ||
})), | ||
); | ||
} | ||
|
||
if (response.DeleteMarkers) { | ||
contents = contents.concat( | ||
response.DeleteMarkers.map((marker: DeleteMarkerEntry) => ({ | ||
Key: marker.Key!, | ||
VersionId: marker.VersionId, | ||
})), | ||
); | ||
} | ||
|
||
isTruncated = response.IsTruncated!; | ||
keyMarker = response.NextKeyMarker; | ||
versionIdMarker = response.NextVersionIdMarker; | ||
} | ||
|
||
return contents; | ||
} | ||
|
||
async function deleteAllObjects(bucket: string) { | ||
const objects = await listAllObjects(bucket); | ||
|
||
const chunkSize = 1000; // Max 1000 objects per batch | ||
for (let i = 0; i < objects.length; i += chunkSize) { | ||
const chunk = objects.slice(i, i + chunkSize); | ||
const deleteParams = { | ||
Bucket: bucket, | ||
Delete: { | ||
Objects: chunk, | ||
}, | ||
}; | ||
await s3.send(new DeleteObjectsCommand(deleteParams)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.