@@ -182,19 +182,42 @@ export async function step3(props: GuardDutyStep3Props) {
182182export async function enableGuardDutyPolicy ( props : GuardDutyStep3Props ) {
183183 const { logBucket } = props ;
184184
185+ const servicePrincipals = [ new iam . ServicePrincipal ( 'guardduty.amazonaws.com' ) ] ;
186+ const optinRegions = [
187+ 'af-south-1' ,
188+ 'ap-east-1' ,
189+ 'ap-south-2' ,
190+ 'ap-southeast-3' ,
191+ 'ap-southeast-4' ,
192+ 'ca-west-1' ,
193+ 'eu-central-2' ,
194+ 'eu-south-1' ,
195+ 'eu-south-2' ,
196+ 'il-central-1' ,
197+ 'me-central-1' ,
198+ 'me-south-1' ,
199+ ] ;
200+
201+ optinRegions . map ( optinRegion => {
202+ if ( props . config [ 'global-options' ] [ 'supported-regions' ] . includes ( optinRegion ) ) {
203+ // Ideally want to query aws account list-regions --region-opt-status-contains ENABLED, and intersect with what is configured
204+ servicePrincipals . push ( new iam . ServicePrincipal ( `guardduty.${ optinRegion } .amazonaws.com` ) ) ;
205+ }
206+ } ) ;
207+
185208 // Grant GuardDuty permission to logBucket: https://docs.aws.amazon.com/guardduty/latest/ug/guardduty_exportfindings.html
186209 logBucket . addToResourcePolicy (
187210 new iam . PolicyStatement ( {
188211 actions : [ 's3:GetBucketLocation' , 's3:PutObject' ] ,
189- principals : [ new iam . ServicePrincipal ( 'guardduty.amazonaws.com' ) ] ,
212+ principals : servicePrincipals ,
190213 resources : [ logBucket . bucketArn , logBucket . arnForObjects ( '*' ) ] ,
191214 } ) ,
192215 ) ;
193216
194217 logBucket . encryptionKey ?. addToResourcePolicy (
195218 new iam . PolicyStatement ( {
196219 sid : 'Allow Guardduty to use the key' ,
197- principals : [ new iam . ServicePrincipal ( 'guardduty.amazonaws.com' ) ] ,
220+ principals : servicePrincipals ,
198221 actions : [ 'kms:GenerateDataKey' , 'kms:Encrypt' ] ,
199222 resources : [ '*' ] ,
200223 } ) ,
0 commit comments