Skip to content

Commit f6e3aa7

Browse files
authored
(feature) add aws traffic mirroring to cli (#55)
1 parent 12c7a1f commit f6e3aa7

File tree

11 files changed

+1648
-15
lines changed

11 files changed

+1648
-15
lines changed

cli/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@metlo/cli",
3-
"version": "0.0.1",
3+
"version": "0.0.2",
44
"license": "MIT",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
@@ -16,10 +16,13 @@
1616
"devDependencies": {
1717
"@types/axios": "^0.14.0",
1818
"@types/node": "^18.7.16",
19+
"@types/uuid": "^8.3.4",
1920
"prettier": "^2.7.1",
2021
"typescript": "^4.7.4"
2122
},
2223
"dependencies": {
24+
"@aws-sdk/client-ec2": "^3.198.0",
25+
"@aws-sdk/client-sts": "^3.198.0",
2326
"@metlo/testing": "^0.0.3",
2427
"@types/valid-url": "^1.0.3",
2528
"axios": "^0.27.2",

cli/src/aws/constants.ts

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
export const AWS_REGIONS = [
2+
"us-west-1",
3+
"us-west-2",
4+
"us-east-1",
5+
"us-east-2",
6+
"eu-central-1",
7+
"eu-west-1",
8+
"eu-west-2",
9+
"eu-west-3",
10+
"eu-north-1",
11+
]
12+
13+
export const SUPPORTED_AWS_INSTANCES = [
14+
"a1",
15+
"c5",
16+
"c5a",
17+
"c5ad",
18+
"c5d",
19+
"c5n",
20+
"c6a",
21+
"c6g",
22+
"c6gd",
23+
"c6gn",
24+
"c6i",
25+
"c6id",
26+
"d3",
27+
"d3en",
28+
"dl1",
29+
"g4",
30+
"g4ad",
31+
"g5",
32+
"g5g",
33+
"hpc6a",
34+
"i3en",
35+
"i4i",
36+
"im4gn",
37+
"inf1",
38+
"is4gen",
39+
"m5",
40+
"m5a",
41+
"m5ad",
42+
"m5d",
43+
"m5dn",
44+
"m5n",
45+
"m5zn",
46+
"m6a",
47+
"m6g",
48+
"m6gd",
49+
"m6i",
50+
"m6id",
51+
"p3dn.24xlarge",
52+
"p4",
53+
"r5",
54+
"r5a",
55+
"r5ad",
56+
"r5b",
57+
"r5d",
58+
"r5dn",
59+
"r5n",
60+
"r6a",
61+
"r6g",
62+
"r6gd",
63+
"r6i",
64+
"r6id",
65+
"t3",
66+
"t3a",
67+
"t4g",
68+
"u-6tb1.56xlarge",
69+
"u-6tb1.112xlarge",
70+
"u-9tb1.112xlarge",
71+
"u-12tb1.112xlarge",
72+
"vt1",
73+
"x2gd",
74+
"x2idn",
75+
"x2iedn",
76+
"x2iezn",
77+
"z1d",
78+
"a1.metal",
79+
"c5.metal",
80+
"c5d.metal",
81+
"c5n.metal",
82+
"c6a.metal",
83+
"c6g.metal",
84+
"c6gd.metal",
85+
"c6i.metal",
86+
"c6id.metal",
87+
"g4dn.metal",
88+
"g5g.metal",
89+
"i3.metal",
90+
"i3en.metal",
91+
"i4i.metal",
92+
"m5.metal",
93+
"m5d.metal",
94+
"m5dn.metal",
95+
"m5n.metal",
96+
"m5zn.metal",
97+
"m6a.metal",
98+
"m6g.metal",
99+
"m6gd.metal",
100+
"m6i.metal",
101+
"m6id.metal",
102+
"mac1.metal",
103+
"r5.metal",
104+
"r5b.metal",
105+
"r5d.metal",
106+
"r5dn.metal",
107+
"r5n.metal",
108+
"r6a.metal",
109+
"r6g.metal",
110+
"r6gd.metal",
111+
"r6i.metal",
112+
"r6id.metal",
113+
"u-6tb1.metal",
114+
"u-9tb1.metal",
115+
"u-12tb1.metal",
116+
"u-18tb1.metal",
117+
"u-24tb1.metal",
118+
"x2gd.metal",
119+
"x2idn.metal",
120+
"x2iedn.metal",
121+
"x2iezn.metal",
122+
"z1d.metal",
123+
]

cli/src/aws/ec2Utils.ts

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import {
2+
EC2Client,
3+
DescribeInstanceTypesCommand,
4+
DescribeInstanceTypesCommandInput,
5+
DescribeInstancesCommand,
6+
VirtualizationType,
7+
DescribeNetworkInterfacesCommand,
8+
InstanceTypeInfoFromInstanceRequirements,
9+
GetInstanceTypesFromInstanceRequirementsCommand,
10+
GetInstanceTypesFromInstanceRequirementsCommandInput,
11+
} from "@aws-sdk/client-ec2"
12+
import { SUPPORTED_AWS_INSTANCES } from "./constants"
13+
import { MachineSpecifications } from "./types"
14+
15+
export class EC2_CONN {
16+
private access_id: string
17+
private secret_key: string
18+
private region?: string
19+
private conn: EC2Client
20+
constructor(access_id: string, secret_key: string, region?: string) {
21+
this.access_id = access_id
22+
this.secret_key = secret_key
23+
this.region = region
24+
}
25+
26+
public get_conn() {
27+
if (this.conn) {
28+
return this.conn
29+
}
30+
this.conn = new EC2Client({
31+
credentials: {
32+
accessKeyId: this.access_id,
33+
secretAccessKey: this.secret_key,
34+
},
35+
region: this.region,
36+
})
37+
return this.conn
38+
}
39+
40+
public disconnect() {
41+
if (this.conn) this.conn.destroy()
42+
}
43+
44+
public async describe_instance(ec2_instance_id) {
45+
let resp = await this.get_conn().send(
46+
new DescribeInstancesCommand({ InstanceIds: [ec2_instance_id] }),
47+
)
48+
return resp
49+
}
50+
51+
public async describe_interface(ec2_interface_id) {
52+
let resp = await this.get_conn().send(
53+
new DescribeNetworkInterfacesCommand({
54+
NetworkInterfaceIds: [ec2_interface_id],
55+
}),
56+
)
57+
return resp
58+
}
59+
60+
public async describe_type(Instance_type: string) {
61+
let command = new DescribeInstanceTypesCommand({
62+
InstanceTypes: [Instance_type],
63+
} as DescribeInstanceTypesCommandInput)
64+
let resp = await this.get_conn().send(command)
65+
return resp.InstanceTypes[0]
66+
}
67+
68+
public async get_valid_types(
69+
specs?: MachineSpecifications,
70+
vtx_type?: VirtualizationType,
71+
): Promise<Array<InstanceTypeInfoFromInstanceRequirements>> {
72+
let command = new GetInstanceTypesFromInstanceRequirementsCommand({
73+
ArchitectureTypes: ["x86_64"],
74+
VirtualizationTypes: vtx_type ? [vtx_type] : ["hvm", "paravirtual"],
75+
InstanceRequirements: {
76+
VCpuCount: specs
77+
? { Min: specs.minCpu, Max: specs.maxCpu }
78+
: { Min: 0 },
79+
MemoryMiB: specs
80+
? {
81+
Min: specs.minMem * 1024,
82+
Max: specs.maxMem ? specs.maxMem * 1024 : null,
83+
}
84+
: { Min: 0 },
85+
InstanceGenerations: ["current"],
86+
BurstablePerformance: "included",
87+
BareMetal: "included",
88+
ExcludedInstanceTypes: [
89+
"t2.nano",
90+
"t2.micro",
91+
"t2.small",
92+
"t2.medium",
93+
"t2.large",
94+
"t2.xlarge",
95+
"t2.2xlarge",
96+
"c3.large",
97+
"c3.xlarge",
98+
"c3.2xlarge",
99+
"c3.4xlarge",
100+
"c3.8xlarge",
101+
"r3.large",
102+
"r3.xlarge",
103+
"r3.2xlarge",
104+
"r3.4xlarge",
105+
"r3.8xlarge",
106+
"i3.xlarge",
107+
"i3.2xlarge",
108+
"i3.4xlarge",
109+
"i3.8xlarge",
110+
],
111+
},
112+
} as GetInstanceTypesFromInstanceRequirementsCommandInput)
113+
let conn = this.get_conn()
114+
let resp = await conn.send(command)
115+
return resp.InstanceTypes.filter(x => {
116+
let a = SUPPORTED_AWS_INSTANCES.filter(y => x.InstanceType.includes(y))
117+
return a.length > 0
118+
})
119+
}
120+
}

0 commit comments

Comments
 (0)