-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
vpc-flow-logs.test.ts
171 lines (147 loc) · 5.35 KB
/
vpc-flow-logs.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { Template } from '@aws-cdk/assertions';
import * as iam from '@aws-cdk/aws-iam';
import * as logs from '@aws-cdk/aws-logs';
import * as s3 from '@aws-cdk/aws-s3';
import { Stack } from '@aws-cdk/core';
import { FlowLog, FlowLogDestination, FlowLogResourceType, Vpc } from '../lib';
describe('vpc flow logs', () => {
test('with defaults set, it successfully creates with cloudwatch logs destination', () => {
const stack = getTestStack();
new FlowLog(stack, 'FlowLogs', {
resourceType: FlowLogResourceType.fromNetworkInterfaceId('eni-123455'),
});
Template.fromStack(stack).hasResourceProperties('AWS::EC2::FlowLog', {
ResourceType: 'NetworkInterface',
TrafficType: 'ALL',
ResourceId: 'eni-123455',
DeliverLogsPermissionArn: {
'Fn::GetAtt': ['FlowLogsIAMRoleF18F4209', 'Arn'],
},
LogGroupName: {
Ref: 'FlowLogsLogGroup9853A85F',
},
});
Template.fromStack(stack).resourceCountIs('AWS::Logs::LogGroup', 1);
Template.fromStack(stack).resourceCountIs('AWS::IAM::Role', 1);
Template.fromStack(stack).resourceCountIs('AWS::S3::Bucket', 0);
});
test('with cloudwatch logs as the destination, allows use of existing resources', () => {
const stack = getTestStack();
new FlowLog(stack, 'FlowLogs', {
resourceType: FlowLogResourceType.fromNetworkInterfaceId('eni-123456'),
destination: FlowLogDestination.toCloudWatchLogs(
new logs.LogGroup(stack, 'TestLogGroup', {
retention: logs.RetentionDays.FIVE_DAYS,
}),
new iam.Role(stack, 'TestRole', {
roleName: 'TestName',
assumedBy: new iam.ServicePrincipal('vpc-flow-logs.amazonaws.com'),
}),
),
});
Template.fromStack(stack).hasResourceProperties('AWS::Logs::LogGroup', {
RetentionInDays: 5,
});
Template.fromStack(stack).hasResourceProperties('AWS::IAM::Role', {
RoleName: 'TestName',
});
Template.fromStack(stack).resourceCountIs('AWS::S3::Bucket', 0);
});
test('with s3 as the destination, allows use of existing resources', () => {
const stack = getTestStack();
new FlowLog(stack, 'FlowLogs', {
resourceType: FlowLogResourceType.fromNetworkInterfaceId('eni-123456'),
destination: FlowLogDestination.toS3(
new s3.Bucket(stack, 'TestBucket', {
bucketName: 'testbucket',
}),
),
});
Template.fromStack(stack).resourceCountIs('AWS::Logs::LogGroup', 0);
Template.fromStack(stack).resourceCountIs('AWS::IAM::Role', 0);
Template.fromStack(stack).hasResourceProperties('AWS::S3::Bucket', {
BucketName: 'testbucket',
});
});
test('with s3 as the destination, allows use of key prefix', () => {
const stack = getTestStack();
new FlowLog(stack, 'FlowLogs', {
resourceType: FlowLogResourceType.fromNetworkInterfaceId('eni-123456'),
destination: FlowLogDestination.toS3(
new s3.Bucket(stack, 'TestBucket', {
bucketName: 'testbucket',
}),
'FlowLogs/',
),
});
Template.fromStack(stack).resourceCountIs('AWS::Logs::LogGroup', 0);
Template.fromStack(stack).resourceCountIs('AWS::IAM::Role', 0);
Template.fromStack(stack).hasResourceProperties('AWS::S3::Bucket', {
BucketName: 'testbucket',
});
});
test('with s3 as the destination and all the defaults set, it successfully creates all the resources', () => {
const stack = getTestStack();
new FlowLog(stack, 'FlowLogs', {
resourceType: FlowLogResourceType.fromNetworkInterfaceId('eni-123456'),
destination: FlowLogDestination.toS3(),
});
Template.fromStack(stack).hasResourceProperties('AWS::EC2::FlowLog', {
ResourceType: 'NetworkInterface',
TrafficType: 'ALL',
ResourceId: 'eni-123456',
LogDestination: {
'Fn::GetAtt': ['FlowLogsBucket87F67F60', 'Arn'],
},
});
Template.fromStack(stack).resourceCountIs('AWS::Logs::LogGroup', 0);
Template.fromStack(stack).resourceCountIs('AWS::IAM::Role', 0);
Template.fromStack(stack).resourceCountIs('AWS::S3::Bucket', 1);
});
test('create with vpc', () => {
const stack = getTestStack();
new Vpc(stack, 'VPC', {
flowLogs: {
flowLogs: {},
},
});
Template.fromStack(stack).resourceCountIs('AWS::EC2::VPC', 1);
Template.fromStack(stack).hasResourceProperties('AWS::EC2::FlowLog', {
ResourceType: 'VPC',
TrafficType: 'ALL',
ResourceId: {
Ref: 'VPCB9E5F0B4',
},
DeliverLogsPermissionArn: {
'Fn::GetAtt': ['VPCflowLogsIAMRole9D21E1A6', 'Arn'],
},
LogGroupName: {
Ref: 'VPCflowLogsLogGroupE900F980',
},
});
});
test('add to vpc', () => {
const stack = getTestStack();
const vpc = new Vpc(stack, 'VPC');
vpc.addFlowLog('FlowLogs');
Template.fromStack(stack).resourceCountIs('AWS::EC2::VPC', 1);
Template.fromStack(stack).hasResourceProperties('AWS::EC2::FlowLog', {
ResourceType: 'VPC',
TrafficType: 'ALL',
ResourceId: {
Ref: 'VPCB9E5F0B4',
},
DeliverLogsPermissionArn: {
'Fn::GetAtt': ['VPCFlowLogsIAMRole55343234', 'Arn'],
},
LogGroupName: {
Ref: 'VPCFlowLogsLogGroupF48E1B0A',
},
});
});
});
function getTestStack(): Stack {
return new Stack(undefined, 'TestStack', {
env: { account: '123456789012', region: 'us-east-1' },
});
}