|
| 1 | +import { PolicyStatement, Effect, ServicePrincipal } from '@aws-cdk/aws-iam'; |
| 2 | +import * as s3 from '@aws-cdk/aws-s3'; |
| 3 | +import { App, RemovalPolicy, Stack, StackProps } from '@aws-cdk/core'; |
| 4 | +import { IntegTest } from '@aws-cdk/integ-tests'; |
| 5 | +import { FlowLog, FlowLogDestination, FlowLogResourceType, Vpc, FlowLogMaxAggregationInterval } from '../lib'; |
| 6 | + |
| 7 | +const app = new App(); |
| 8 | + |
| 9 | + |
| 10 | +class TestStack extends Stack { |
| 11 | + constructor(scope: App, id: string, props?: StackProps) { |
| 12 | + super(scope, id, props); |
| 13 | + |
| 14 | + const vpc = new Vpc(this, 'VPC'); |
| 15 | + |
| 16 | + new FlowLog(this, 'FlowLogsCW', { |
| 17 | + resourceType: FlowLogResourceType.fromVpc(vpc), |
| 18 | + maxAggregationInterval: FlowLogMaxAggregationInterval.TEN_MINUTES, |
| 19 | + }); |
| 20 | + |
| 21 | + vpc.addFlowLog('FlowLogsS3', { |
| 22 | + destination: FlowLogDestination.toS3(), |
| 23 | + maxAggregationInterval: FlowLogMaxAggregationInterval.ONE_MINUTE, |
| 24 | + }); |
| 25 | + |
| 26 | + const bucket = new s3.Bucket(this, 'Bucket', { |
| 27 | + removalPolicy: RemovalPolicy.DESTROY, |
| 28 | + autoDeleteObjects: true, |
| 29 | + }); |
| 30 | + bucket.addToResourcePolicy(new PolicyStatement({ |
| 31 | + effect: Effect.ALLOW, |
| 32 | + principals: [new ServicePrincipal('delivery.logs.amazonaws.com')], |
| 33 | + actions: ['s3:PutObject'], |
| 34 | + resources: [bucket.arnForObjects(`AWSLogs/${this.account}/*`)], |
| 35 | + conditions: { |
| 36 | + StringEquals: { |
| 37 | + 's3:x-amz-acl': 'bucket-owner-full-control', |
| 38 | + 'aws:SourceAccount': this.account, |
| 39 | + }, |
| 40 | + ArnLike: { |
| 41 | + 'aws:SourceArn': this.formatArn({ |
| 42 | + service: 'logs', |
| 43 | + resource: '*', |
| 44 | + }), |
| 45 | + }, |
| 46 | + }, |
| 47 | + })); |
| 48 | + bucket.addToResourcePolicy(new PolicyStatement({ |
| 49 | + effect: Effect.ALLOW, |
| 50 | + principals: [new ServicePrincipal('delivery.logs.amazonaws.com')], |
| 51 | + actions: ['s3:GetBucketAcl', 's3:ListBucket'], |
| 52 | + resources: [bucket.bucketArn], |
| 53 | + conditions: { |
| 54 | + StringEquals: { |
| 55 | + 'aws:SourceAccount': this.account, |
| 56 | + }, |
| 57 | + ArnLike: { |
| 58 | + 'aws:SourceArn': this.formatArn({ |
| 59 | + service: 'logs', |
| 60 | + resource: '*', |
| 61 | + }), |
| 62 | + }, |
| 63 | + }, |
| 64 | + })); |
| 65 | + |
| 66 | + vpc.addFlowLog('FlowLogsS3KeyPrefix', { |
| 67 | + destination: FlowLogDestination.toS3(bucket, 'prefix/'), |
| 68 | + }); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | + |
| 73 | +new IntegTest(app, 'FlowLogs', { |
| 74 | + testCases: [ |
| 75 | + new TestStack(app, 'FlowLogsTestStack'), |
| 76 | + ], |
| 77 | +}); |
| 78 | + |
| 79 | +app.synth(); |
0 commit comments