Skip to content

Commit 10c7074

Browse files
fix(core): Fixing enabling GuardDuty S3 Protection in Security Account (#512)
* AutoEnable S3 Detection in Admin Account * Exclude regions for s3 protection * Exclude region based on config * Adding delete action for guardDutyAdminSetup and createIamRole Custom resources * Adding permissions related to clean up * GuardDuty deployment for all environments * Prettier * Enavle S3 Production Security Account based on config
1 parent 4b90150 commit 10c7074

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/deployments/cdk/src/deployments/iam/guardduty-roles.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export async function createAdminSetupRole(stack: AccountStack) {
7575
'guardduty:DescribeOrganizationConfiguration',
7676
'guardduty:UpdateMemberDetectors',
7777
'guardduty:DeleteMembers',
78+
'guardduty:UpdateDetector',
7879
],
7980
resources: ['*'],
8081
}),

src/lib/custom-resources/cdk-guardduty-admin-setup/runtime/src/index.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ async function onCreateOrUpdate(
5454
}
5555

5656
const { memberAccounts, s3Protection } = properties;
57+
await updateS3Protection(detectorId, s3Protection);
58+
5759
const isAutoEnabled = await isConfigurationAutoEnabled(detectorId, s3Protection);
5860
if (isAutoEnabled) {
5961
console.log(`GuardDuty is already enabled ORG Level`);
@@ -68,7 +70,7 @@ async function onCreateOrUpdate(
6870

6971
if (memberAccounts.length > 0) {
7072
await createMembers(memberAccounts, detectorId);
71-
await updateDataSource(memberAccounts, detectorId, s3Protection);
73+
await updateMemberDataSource(memberAccounts, detectorId, s3Protection);
7274
}
7375

7476
return {
@@ -153,7 +155,7 @@ async function isConfigurationAutoEnabled(detectorId: string, s3Protection: bool
153155
}
154156
}
155157

156-
async function updateDataSource(memberAccounts: AccountDetail[], detectorId: string, s3Protection: boolean) {
158+
async function updateMemberDataSource(memberAccounts: AccountDetail[], detectorId: string, s3Protection: boolean) {
157159
if (s3Protection) {
158160
return;
159161
}
@@ -180,6 +182,26 @@ async function updateDataSource(memberAccounts: AccountDetail[], detectorId: str
180182
}
181183
}
182184

185+
async function updateS3Protection(detectorId: string, s3Protection: boolean) {
186+
try {
187+
await throttlingBackOff(() =>
188+
guardduty
189+
.updateDetector({
190+
DetectorId: detectorId,
191+
DataSources: {
192+
S3Logs: {
193+
Enable: s3Protection,
194+
},
195+
},
196+
})
197+
.promise(),
198+
);
199+
} catch (error) {
200+
console.warn('Error while calling guardduty.updateDetector');
201+
console.warn(error);
202+
}
203+
}
204+
183205
async function deleteMembers(memberAccounts: AccountDetail[], detectorId: string) {
184206
try {
185207
console.log(`Calling api "guardduty.createMembers()", ${memberAccounts}, ${detectorId}`);
@@ -216,8 +238,9 @@ async function onDelete(event: CloudFormationCustomResourceDeleteEvent) {
216238
const { memberAccounts } = properties;
217239
try {
218240
const detectorId = await getDetectorId();
241+
await updateS3Protection(detectorId!, false);
219242
await updateConfig(detectorId!, false, false);
220-
await updateDataSource(memberAccounts, detectorId!, false);
243+
await updateMemberDataSource(memberAccounts, detectorId!, false);
221244
await deleteMembers(memberAccounts, detectorId!);
222245
} catch (error) {
223246
console.warn('Exception while performing Delete Action');

0 commit comments

Comments
 (0)