-
Notifications
You must be signed in to change notification settings - Fork 332
Expand file tree
/
Copy pathec2-vpc.adapter.spec.ts
More file actions
164 lines (145 loc) · 5.51 KB
/
Copy pathec2-vpc.adapter.spec.ts
File metadata and controls
164 lines (145 loc) · 5.51 KB
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
import {
DescribeFlowLogsCommand,
DescribeInstancesCommand,
DescribeVpcsCommand,
GetEbsEncryptionByDefaultCommand,
DescribeSecurityGroupsCommand,
} from '@aws-sdk/client-ec2';
import { Ec2VpcAdapter } from './ec2-vpc.adapter';
type SendHandler = (command: unknown) => unknown;
function buildClient(handler: SendHandler) {
return {
send: jest.fn((command: unknown) => Promise.resolve(handler(command))),
} as unknown as Parameters<Ec2VpcAdapter['scan']>[0]['credentials'] extends infer _
? import('@aws-sdk/client-ec2').EC2Client
: never;
}
const noopInstancesResponse = { Reservations: [] };
const noopSgResponse = { SecurityGroups: [] };
const encryptedByDefaultResponse = { EbsEncryptionByDefault: true };
describe('Ec2VpcAdapter — checkVpcFlowLogs', () => {
const adapter = new Ec2VpcAdapter();
// Call the private method through scan() with mocked client wiring.
// scan() constructs its own EC2Client; we override EC2Client by spying on send().
// To keep this test focused on flow-log logic, we mock the EC2Client module
// and assert behavior via the returned findings.
function runScanWithFlowLogs(args: {
vpcs: Array<{ VpcId: string; IsDefault?: boolean; Tags?: Array<{ Key: string; Value: string }> }>;
flowLogPages: Array<{ FlowLogs: Array<{ ResourceId: string }>; NextToken?: string }>;
hasRunningInstances?: boolean;
}) {
let flowLogPageIndex = 0;
const handler: SendHandler = (command) => {
if (command instanceof DescribeVpcsCommand) {
return { Vpcs: args.vpcs };
}
if (command instanceof DescribeFlowLogsCommand) {
const page = args.flowLogPages[flowLogPageIndex] ?? { FlowLogs: [] };
flowLogPageIndex += 1;
return page;
}
if (command instanceof DescribeInstancesCommand) {
return args.hasRunningInstances
? { Reservations: [{ Instances: [{ InstanceId: 'i-abc' }] }] }
: noopInstancesResponse;
}
if (command instanceof GetEbsEncryptionByDefaultCommand) {
return encryptedByDefaultResponse;
}
if (command instanceof DescribeSecurityGroupsCommand) {
return noopSgResponse;
}
return {};
};
const client = buildClient(handler);
// Access the private method for focused testing.
const fn = (
adapter as unknown as {
checkVpcFlowLogs: (
c: unknown,
region: string,
accountId?: string,
) => Promise<ReturnType<typeof adapter.scan> extends Promise<infer U> ? U : never>;
}
).checkVpcFlowLogs;
return fn.call(adapter, client, 'us-east-1', '123456789012');
}
it('passes a VPC when a VPC-scope flow log exists for it', async () => {
const findings = await runScanWithFlowLogs({
vpcs: [{ VpcId: 'vpc-abc123', IsDefault: false }],
flowLogPages: [{ FlowLogs: [{ ResourceId: 'vpc-abc123' }] }],
});
expect(findings).toHaveLength(1);
expect(findings[0].id).toBe('vpc-flow-logs-vpc-abc123');
expect(findings[0].passed).toBe(true);
});
it('fails a VPC that only has subnet-scope flow logs', async () => {
const findings = await runScanWithFlowLogs({
vpcs: [{ VpcId: 'vpc-abc123', IsDefault: false }],
flowLogPages: [
{
FlowLogs: [
{ ResourceId: 'subnet-111' },
{ ResourceId: 'subnet-222' },
],
},
],
});
expect(findings).toHaveLength(1);
expect(findings[0].id).toBe('vpc-no-flow-logs-vpc-abc123');
expect(findings[0].passed).toBe(false);
});
it('fails a VPC that only has ENI-scope flow logs', async () => {
const findings = await runScanWithFlowLogs({
vpcs: [{ VpcId: 'vpc-abc123', IsDefault: false }],
flowLogPages: [{ FlowLogs: [{ ResourceId: 'eni-deadbeef' }] }],
});
expect(findings).toHaveLength(1);
expect(findings[0].id).toBe('vpc-no-flow-logs-vpc-abc123');
expect(findings[0].passed).toBe(false);
});
it('fails a VPC when no flow logs exist at all', async () => {
const findings = await runScanWithFlowLogs({
vpcs: [{ VpcId: 'vpc-abc123', IsDefault: false }],
flowLogPages: [{ FlowLogs: [] }],
});
expect(findings).toHaveLength(1);
expect(findings[0].id).toBe('vpc-no-flow-logs-vpc-abc123');
expect(findings[0].passed).toBe(false);
});
it('paginates DescribeFlowLogs and recognizes a VPC-scope flow log on a later page', async () => {
const findings = await runScanWithFlowLogs({
vpcs: [{ VpcId: 'vpc-abc123', IsDefault: false }],
flowLogPages: [
{
FlowLogs: [{ ResourceId: 'subnet-1' }, { ResourceId: 'eni-1' }],
NextToken: 'page-2',
},
{ FlowLogs: [{ ResourceId: 'vpc-abc123' }] },
],
});
expect(findings).toHaveLength(1);
expect(findings[0].id).toBe('vpc-flow-logs-vpc-abc123');
expect(findings[0].passed).toBe(true);
});
it('handles multiple VPCs with mixed scopes correctly', async () => {
const findings = await runScanWithFlowLogs({
vpcs: [
{ VpcId: 'vpc-aaa', IsDefault: false },
{ VpcId: 'vpc-bbb', IsDefault: false },
],
flowLogPages: [
{
FlowLogs: [
{ ResourceId: 'vpc-aaa' }, // vpc-aaa: VPC-scope → pass
{ ResourceId: 'subnet-xyz' }, // subnet for vpc-bbb doesn't count
],
},
],
});
expect(findings).toHaveLength(2);
const byId = Object.fromEntries(findings.map((f) => [f.resourceId, f]));
expect(byId['vpc-aaa'].passed).toBe(true);
expect(byId['vpc-bbb'].passed).toBe(false);
});
});