/
ingress-rule.ts
224 lines (197 loc) · 4.75 KB
/
ingress-rule.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
import * as cdk from 'aws-cdk-lib/core';
/**
* Protocol for use in Connection Rules
*
* https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml
*/
export enum Protocol {
TCP = 'TCP',
UDP = 'UDP',
}
/**
* Properties to create a port range
*/
export interface PortProps {
/**
* The protocol for the range
*/
readonly protocol: Protocol;
/**
* A starting value for a range of allowed port numbers.
*
* For fleets using Windows and Linux builds, only ports 1026-60000 are valid.
*/
readonly fromPort: number;
/**
* An ending value for a range of allowed port numbers. Port numbers are end-inclusive.
* This value must be higher than `fromPort`.
*
* For fleets using Windows and Linux builds, only ports 1026-60000 are valid.
*
* @default the `fromPort` value
*/
readonly toPort?: number;
}
/**
* 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,
});
}
/**
* A TCP port range
*/
public static tcpRange(startPort: number, endPort: number) {
return new Port({
protocol: Protocol.TCP,
fromPort: startPort,
toPort: endPort,
});
}
/**
* Any TCP traffic
*/
public static allTcp() {
return new Port({
protocol: Protocol.TCP,
fromPort: 1026,
toPort: 60000,
});
}
/**
* A single UDP port
*/
public static udp(port: number): Port {
return new Port({
protocol: Protocol.UDP,
fromPort: port,
toPort: port,
});
}
/**
* A UDP port range
*/
public static udpRange(startPort: number, endPort: number) {
return new Port({
protocol: Protocol.UDP,
fromPort: startPort,
toPort: endPort,
});
}
/**
* Any UDP traffic
*/
public static allUdp() {
return new Port({
protocol: Protocol.UDP,
fromPort: 1026,
toPort: 60000,
});
}
constructor(private readonly props: PortProps) {}
/**
* Produce the ingress rule JSON for the given connection
*/
public toJson(): any {
return {
protocol: this.props.protocol,
fromPort: this.props.fromPort,
toPort: this.props.toPort,
};
}
}
/**
* Interface for classes that provide the peer-specification parts of an inbound permission
*/
export interface IPeer {
/**
* A unique identifier for this connection peer
*/
readonly uniqueId: string;
/**
* Produce the ingress rule JSON for the given connection
*/
toJson(): any;
}
/**
* Peer object factories
*
* The static methods on this object can be used to create peer objects
* which represent a connection partner in inbound permission rules.
*
* Use this object if you need to represent connection partners using plain IP addresses.
*/
export class Peer {
/**
* Create an IPv4 peer from a CIDR
*/
public static ipv4(cidrIp: string): IPeer {
return new CidrIPv4(cidrIp);
}
/**
* Any IPv4 address
*/
public static anyIpv4(): IPeer {
return new AnyIPv4();
}
protected constructor() {
}
}
/**
* A connection to and from a given IP range
*/
class CidrIPv4 implements IPeer {
public readonly canInlineRule = true;
public readonly uniqueId: string;
constructor(private readonly cidrIp: string) {
if (!cdk.Token.isUnresolved(cidrIp)) {
const cidrMatch = cidrIp.match(/^(\d{1,3}\.){3}\d{1,3}(\/\d+)?$/);
if (!cidrMatch) {
throw new Error(`Invalid IPv4 CIDR: "${cidrIp}"`);
}
if (!cidrMatch[2]) {
throw new Error(`CIDR mask is missing in IPv4: "${cidrIp}". Did you mean "${cidrIp}/32"?`);
}
}
this.uniqueId = cidrIp;
}
/**
* Produce the ingress rule JSON for the given connection
*/
public toJson(): any {
return { ipRange: this.cidrIp };
}
}
/**
* Any IPv4 address
*/
class AnyIPv4 extends CidrIPv4 {
constructor() {
super('0.0.0.0/0');
}
}
/**
* A range of IP addresses and port settings that allow inbound traffic to connect to server processes on an instance in a fleet.
* New game sessions are assigned an IP address/port number combination, which must fall into the fleet's allowed ranges.
*
* Fleets with custom game builds must have permissions explicitly set.
* For Realtime Servers fleets, GameLift automatically opens two port ranges, one for TCP messaging and one for UDP.
*/
export interface IngressRule {
/**
* The port range used for ingress traffic
*/
readonly port: Port;
/**
* A range of allowed IP addresses .
*/
readonly source: IPeer;
}