forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathport.ts
227 lines (204 loc) · 4.46 KB
/
port.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import { Token } from '@aws-cdk/core';
/**
* Protocol for use in Connection Rules
*/
export enum Protocol {
ALL = '-1',
TCP = 'tcp',
UDP = 'udp',
ICMP = 'icmp',
ICMPV6 = '58',
ESP = 'esp',
AH = 'ah',
}
/**
* Properties to create a port range
*/
export interface PortProps {
/**
* The protocol for the range
*/
readonly protocol: Protocol;
/**
* The starting port for the range
*
* @default - Not included in the rule
*/
readonly fromPort?: number;
/**
* The ending port for the range
*
* @default - Not included in the rule
*/
readonly toPort?: number;
/**
* String representation for this object
*/
readonly stringRepresentation: string;
}
/**
* Interface for classes that provide the connection-specification parts of a security group rule
*/
export class Port {
/**
* A single TCP port
*/
public static tcp(port: number): Port {
return new Port({
protocol: Protocol.TCP,
fromPort: port,
toPort: port,
stringRepresentation: renderPort(port),
});
}
/**
* A TCP port range
*/
public static tcpRange(startPort: number, endPort: number) {
return new Port({
protocol: Protocol.TCP,
fromPort: startPort,
toPort: endPort,
stringRepresentation: `${renderPort(startPort)}-${renderPort(endPort)}`,
});
}
/**
* Any TCP traffic
*/
public static allTcp() {
return new Port({
protocol: Protocol.TCP,
fromPort: 0,
toPort: 65535,
stringRepresentation: 'ALL PORTS',
});
}
/**
* A single UDP port
*/
public static udp(port: number): Port {
return new Port({
protocol: Protocol.UDP,
fromPort: port,
toPort: port,
stringRepresentation: `UDP ${renderPort(port)}`,
});
}
/**
* A UDP port range
*/
public static udpRange(startPort: number, endPort: number) {
return new Port({
protocol: Protocol.UDP,
fromPort: startPort,
toPort: endPort,
stringRepresentation: `UDP ${renderPort(startPort)}-${renderPort(endPort)}`,
});
}
/**
* Any UDP traffic
*/
public static allUdp() {
return new Port({
protocol: Protocol.UDP,
fromPort: 0,
toPort: 65535,
stringRepresentation: 'UDP ALL PORTS',
});
}
/**
* A specific combination of ICMP type and code
*
* @see https://www.iana.org/assignments/icmp-parameters/icmp-parameters.xhtml
*/
public static icmpTypeAndCode(type: number, code: number) {
return new Port({
protocol: Protocol.ICMP,
fromPort: type,
toPort: code,
stringRepresentation: `ICMP Type ${type} Code ${code}`,
});
}
/**
* All codes for a single ICMP type
*/
public static icmpType(type: number): Port {
return new Port({
protocol: Protocol.ICMP,
fromPort: type,
toPort: -1,
stringRepresentation: `ICMP Type ${type}`,
});
}
/**
* ICMP ping (echo) traffic
*/
public static icmpPing() {
return Port.icmpType(8);
}
/**
* All ICMP traffic
*/
public static allIcmp() {
return new Port({
protocol: Protocol.ICMP,
fromPort: -1,
toPort: -1,
stringRepresentation: 'ALL ICMP',
});
}
/**
* All traffic
*/
public static allTraffic() {
return new Port({
protocol: Protocol.ALL,
stringRepresentation: 'ALL TRAFFIC',
});
}
/**
* A single ESP port
*/
public static esp(): Port {
return new Port({
protocol: Protocol.ESP,
fromPort: 50,
toPort: 50,
stringRepresentation: 'ESP 50',
});
}
/**
* A single AH port
*/
public static ah(): Port {
return new Port({
protocol: Protocol.AH,
fromPort: 51,
toPort: 51,
stringRepresentation: 'AH 51',
});
}
/**
* Whether the rule containing this port range can be inlined into a securitygroup or not.
*/
public readonly canInlineRule: boolean;
constructor(private readonly props: PortProps) {
this.canInlineRule = !Token.isUnresolved(props.fromPort) && !Token.isUnresolved(props.toPort);
}
/**
* Produce the ingress/egress rule JSON for the given connection
*/
public toRuleJson(): any {
return {
ipProtocol: this.props.protocol,
fromPort: this.props.fromPort,
toPort: this.props.toPort,
};
}
public toString(): string {
return this.props.stringRepresentation;
}
}
function renderPort(port: number) {
return Token.isUnresolved(port) ? '{IndirectPort}' : port.toString();
}