-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigure-tenant.ps1
439 lines (381 loc) · 12.8 KB
/
configure-tenant.ps1
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
Param(
[Parameter(Mandatory=$true)]
[string]$orgId,
[Parameter(Mandatory=$true)]
[string]$apiKey = ""
)
$ErrorActionPreference = "Stop"
$ProgressPreference = "SilentlyContinue"
if ($apiKey -eq "") {
$apiKey = $env:ENCLAVE_API_KEY
}
if ($apiKey -eq "") {
Write-Error "No API key provided; either specify the 'apiKey' argument, or set the ENCLAVE_API_KEY environment variable."
return;
}
$HasPrimaryGateway = $false
$HasSecondaryGateway = $false
$HasEnrolledGateways = $false
$GatewayHostnamePrimary = "gateway-primary"
$GatewayHostnameSecondary = "gateway-secondary"
$systemIdPrimary = ""
$systemIdSecondary = "";
$currentDateTime = Get-Date -Format "yyyy-MM-dd"
$notes = "Internet Gateway resource auto-provisioned by API on $currentDateTime, do not delete."
$headers = @{Authorization = "Bearer $apiKey"}
$contentType = "application/json";
# ------------
function Invoke-EnclaveApi {
param (
[Parameter(Mandatory = $true)]
[string]$Uri,
[Parameter(Mandatory = $true)]
[string]$Method,
[Parameter(Mandatory = $false)]
[object]$Body
)
try {
if ($null -ne $Body) {
return Invoke-RestMethod -ContentType $contentType -Method $Method -Uri $Uri -Headers $headers -Body ($Body | ConvertTo-Json -Depth 10)
} else {
return Invoke-RestMethod -ContentType $contentType -Method $Method -Uri $Uri -Headers $headers
}
} catch {
throw "Request to $Uri failed with error: $($_.Exception.Message)"
}
}
# ------------
# enrolment key
Write-Host "Checking for enrolled Internet Gateways..."
foreach ($system in @($GatewayHostnamePrimary, $GatewayHostnameSecondary))
{
$response = Invoke-EnclaveApi -Method Get -Uri "https://api.enclave.io/org/$orgId/systems?search=$system"
if ($response.items.Count -gt 0) {
$HasEnrolledGateways = $true
if ($system -eq $GatewayHostnamePrimary)
{
$HasPrimaryGateway = $true
}
elseif ($system -eq $GatewayHostnameSecondary)
{
$HasSecondaryGateway = $true
}
}
}
if ($HasEnrolledGateways -eq $false)
{
Write-Host " No enrolled systems found in this tenant with expected hostnames of an Enclave Internet Gateway."
Write-Host " Creating a new Internet Gateway Enrolment Key:"
Write-Host ""
$currentTime = (Get-Date).ToUniversalTime()
$enrolmentKeyPatch = @{
description = "Internet Gateway"
type = "GeneralPurpose"
approvalMode = "Automatic"
notes = "$notes"
usesRemaining = 2
tags = @(
"internet-gateway"
)
autoExpire = @{
timeZoneId = "Etc/UTC"
expiryDateTime = "$($currentTime.AddHours(1).ToString("yyyy-MM-ddTHH:mm"))"
expiryAction = "Delete"
}
}
# create enrolment key
$response = Invoke-EnclaveApi -Method Post -Uri "https://api.enclave.io/org/$orgId/enrolment-keys" -Body $enrolmentKeyPatch
Write-Host " $($response.key)"
Write-Host ""
Write-Host " This key will automatically expire in 1 hour."
Write-Host ""
exit 0
}
# tags
$tags = @(
@{
name = "internet-gateway"
colour = "#EDEDED"
},
@{
name = "internet-gateway-admin"
colour = "#536DFE"
},
@{
name = "internet-gateway-user"
colour = "#DCEDC8"
}
)
Write-Host "Evaluating Tags..."
foreach ($tag in $tags)
{
$tagsPatch = @{
tag = "$($tag.name)"
colour = "$($tag.colour)"
notes = "$notes"
}
$response = Invoke-EnclaveApi -Method Get -Uri "https://api.enclave.io/org/$orgId/tags?search=$($tag.name)"
if ($response.metadata.total -eq 0)
{
# create tag
Write-Host " Creating tag: $($tag.name)"
$null = Invoke-EnclaveApi -Method Post -Uri "https://api.enclave.io/org/$orgId/tags" -Body $tagsPatch
}
else
{
# update tag
$tagRef = $response.items[0].ref
Write-Host " Refreshing tag: $($tag.name)"
$null = Invoke-EnclaveApi -Method Patch -Uri "https://api.enclave.io/org/$orgId/tags/$tagRef" -Body $tagsPatch
}
}
# dns
Write-Host "Evaluating DNS Records..."
foreach ($dnsRecord in $("blocked", "dnsfilter"))
{
$dnsPatch = @{
name = "$dnsRecord"
zoneId = 1
notes = "$notes"
tags = @("internet-gateway")
}
$response = Invoke-EnclaveApi -Method Get -Uri "https://api.enclave.io/org/$orgId/dns/records?hostname=$dnsRecord"
if ($response.metadata.total -eq 0)
{
# create record
Write-Host " Creating DNS record: $($dnsRecord).enclave"
$null = Invoke-EnclaveApi -Method Post -Uri "https://api.enclave.io/org/$orgId/dns/records" -Body $dnsPatch
}
else
{
# update record
$dnsId = $response.items[0].id
Write-Host " Refreshing DNS record: #$($dnsId) $($response.items[0].name).enclave"
$null = Invoke-EnclaveApi -Method Patch -Uri "https://api.enclave.io/org/$orgId/dns/records/$dnsId" -Body $dnsPatch
}
}
# systems
Write-Host "Evaluating Systems..."
foreach ($system in @($GatewayHostnamePrimary, $GatewayHostnameSecondary))
{
$response = Invoke-EnclaveApi -Method Get -Uri "https://api.enclave.io/org/$orgId/systems?search=$system"
if ($response.items.Count -eq 0) {
continue
}
$systemId = $response.items[0].systemId;
# enable system to act as gateway
$systemPatch = @{
gatewayRoutes = @(
@{
subnet = "0.0.0.0/0"
userEntered = $true
weight = 0
name = "Internet"
}
)
tags = @("internet-gateway")
}
Write-Host " Refreshing system: $systemId ($system)"
$null = Invoke-EnclaveApi -Method Patch -Uri "https://api.enclave.io/org/$orgId/systems/$systemId" -Body $systemPatch
if ($system -eq $GatewayHostnamePrimary)
{
$systemIdPrimary = $systemId
}
elseif ($system -eq $GatewayHostnameSecondary)
{
$systemIdSecondary = $systemId
}
}
# policies
Write-Host "Evaluating Policies..."
$policiesModel = @(
@{
type = "General"
description = "(Internet Gateway) - Admin DNS Dashboard"
isEnabled = $true
notes = "$notes This policy grants administrative access to the Internet Gateway servers, be careful who you give admin access to."
senderTags = @(
"internet-gateway-admin"
)
receiverTags = @(
"internet-gateway"
)
acls = @(
@{
protocol = "Tcp"
ports = "80"
description = "HTTP"
},
@{
protocol = "Tcp"
ports = "443"
description = "HTTPS"
},
@{
protocol = "Tcp"
ports = "444"
description = "PiHole"
},
@{
protocol = "Icmp"
description = "PiHole"
}
)
},
@{
type = "General"
description = "(Internet Gateway) - Blocked Page"
isEnabled = $true
notes = "$notes This policy allows your users to reach the blocked page served by the Internet Gateway when DNS filtering prevents an access."
senderTags = @(
"internet-gateway-user"
)
receiverTags = @(
"internet-gateway"
)
acls = @(
@{
protocol = "Tcp"
ports = "80"
description = "HTTP"
},
@{
protocol = "Tcp"
ports = "443"
description = "HTTPS"
}
)
},
@{
type = "General"
description = "(Internet Gateway) - Cluster"
isEnabled = $true
notes = "$notes This policy allows your Internet Gateways to communicate and syncronise configuration."
senderTags = @(
"internet-gateway"
)
receiverTags = @(
"internet-gateway"
)
acls = @(
@{
protocol = "Udp"
ports = "53"
description = "DNS"
},
@{
protocol = "Tcp"
ports = "9999"
description = "PiHole Gravity Database Sync"
},
@{
protocol = "Icmp"
description = "Icmp"
}
)
}
)
if ($HasEnrolledGateways -eq $true)
{
$policiesModel += @{
type = "Gateway"
description = "(Internet Gateway) - Internet Access"
isEnabled = $true
notes = "$notes"
senderTags = @(
"internet-gateway-user"
)
acls = @(
@{
protocol = "Tcp"
ports = "80"
description = "HTTP"
},
@{
protocol = "Tcp"
ports = "443"
description = "HTTPS"
},
@{
protocol = "Icmp"
description = "Icmp"
}
)
gateways = @()
gatewayTrafficDirection = "Exit"
gatewayAllowedIpRanges = @()
gatewayPriority = "Balanced"
}
if ($HasPrimaryGateway -eq $true)
{
$policiesModel[3].gateways += @{
systemId = "$systemIdPrimary"
routes = @("0.0.0.0/0")
}
}
if ($HasSecondaryGateway -eq $true)
{
$policiesModel[3].gateways += @{
systemId = "$systemIdSecondary"
routes = @("0.0.0.0/0")
}
}
}
$response = Invoke-EnclaveApi -Method Get -Uri "https://api.enclave.io/org/$orgId/policies?include_disabled=true"
foreach ($policyModel in $policiesModel)
{
# tight match each returned policy against the model without relying on user configurable values like description
$allMatchingPolicies = $response.items | Where-Object {
$policyModelSubnets = if ($policyModel.gateways -ne $null -and $policyModel.gateways.Count -gt 0) {
($policyModel.gateways | ForEach-Object { $_.routes }) -join ','
} else {
$null
}
$apiSubnets = if ($_.gateways -ne $null -and $_.gateways.Count -gt 0) {
($_.gateways | ForEach-Object { $_.routes }) -join ','
} else {
$null
}
$typeMatch = ($_."type" -eq $policyModel.type) # match policy type
$descriptionMatch = ($_."description" -eq $policyModel.description) # match description
$senderTagsMatch = (($_.senderTags.tag -join ',') -eq ($policyModel.senderTags -join ',')) # match sender tags
$receiverTagsMatch = (($_.receiverTags.tag -join ',') -eq ($policyModel.receiverTags -join ',')) # match receiver tags
# conditionally match subnets defined
if ($policyModelSubnets -ne $null) {
$subnetMatch = ($apiSubnets -eq $policyModelSubnets)
} else {
$subnetMatch = $true
}
# combine all match conditions
$typeMatch -and
$senderTagsMatch -and
$receiverTagsMatch -and
$subnetMatch -or
$descriptionMatch
}
if ($null -ne $allMatchingPolicies)
{
$policyCount = @($allMatchingPolicies).Count
if ($policyCount -ne 1)
{
Write-Host " Multiple ($policyCount) policies found which match the definition for the '$($policyModel.description)' policy. Please review, will not alter existing policy set."
foreach ($item in $allMatchingPolicies)
{
Write-Host " - #$($item.id): $($item.description)"
}
continue
}
$matchedPolicy = $allMatchingPolicies[0]
# update policy
Write-Host " Refreshing policy: #$($matchedPolicy.id) $($matchedPolicy.description)"
#Write-Host $($policyModel | ConvertTo-Json -Depth 10)
$null = Invoke-EnclaveApi -Method Patch -Uri "https://api.enclave.io/org/$orgId/policies/$($matchedPolicy.id)" -Body $policyModel
}
else
{
# create policy
Write-Host " Creating policy: $($policyModel.description)"
$null = Invoke-EnclaveApi -Method Post -Uri "https://api.enclave.io/org/$orgId/policies" -Body $policyModel
}
}
Write-Host "Done"