-
Notifications
You must be signed in to change notification settings - Fork 0
/
essential-firewall-rules.ps1
164 lines (128 loc) · 5.89 KB
/
essential-firewall-rules.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
<#
Script name: essential-firewall-rules.ps1
Last update: 27 Sep 2021
Author: Eric Gray, @eric_gray
Description: Essential NSX-T CGW and MGW firewall rules to enable managing a new VMware Cloud on AWS SDDC
Use optional -Cleanup switch to delete (undo) all rules and groups defined in this script.
The top portion of this script must be edited to match your unique network requirements.
#>
#Requires -Modules VMware.VimAutomation.Vmc, VMware.VMC.NSXT
param ([switch]$Cleanup=$false)
$myIp = Invoke-RestMethod -Uri https://api.ipify.org
$ipOct = $myIp.split(".")
$adminGroupName = "Admin (x.x.{0}.{1})" -f $ipOct[2], $ipOct[3]
$vpcGroupName = "VPC (10.100/21)"
$vpcCidrs = @("10.100.0.0/21")
$computeGroupName = "Compute seg (192.168.1 + .2)"
$computeCidrs = @("192.168.1.0/24","192.168.2.0/24")
$cgwGroups = @()
$cgwGroups += @{Name = $adminGroupName; IPAddress = $myIp }
$cgwGroups += @{Name = $vpcGroupName; IPAddress = $vpcCidrs }
$cgwGroups += @{Name = $computeGroupName; IPAddress = $computeCidrs }
# The MGW and CGW group definitions start out the same, sync them:
$mgwGroups = @() + $cgwGroups
# Create additional MGW or CGW groups/rules as needed:
# $cgwGroups += @{Name = "X"; IPAddress = @("172.21.0.0/16") }
# $mgwGroups += @{Name = "Y"; IPAddress = @("172.22.0.0/16") }
# InfraScope ("Applied To") values:
# CGW: All Uplinks, VPC Interface, Direct Connect Interface, Internet Interface, VPN Tunnel Interface
# MGW: simply uses MGW
$cgwRules = @()
$cgwRules += @{Name = "$computeGroupName to Any" ; SourceGroup = $computeGroupName; DestinationGroup = @("Any");
Service = "ANY"; InfraScope = @("All Uplinks","VPN Tunnel Interface") }
$cgwRules += @{Name = "$vpcGroupName to $computeGroupName" ; SourceGroup = $vpcGroupName; DestinationGroup = $computeGroupName;
Service = "ANY"; }
# MGW services are: HTTPS, ICMP ALL, VMware VMotion, Provisioning & Remote Console
$mgwRules = @()
$mgwRules += @{Name = "$adminGroupName to vCenter" ; SourceGroup = $adminGroupName; DestinationGroup = "vCenter"; Service = @("HTTPS","ICMP ALL") }
$mgwRules += @{Name = "$vpcGroupName to vCenter" ; SourceGroup = $vpcGroupName; DestinationGroup = "vCenter"; Service = @("HTTPS","ICMP ALL") }
$mgwRules += @{Name = "$vpcGroupName to ESXi" ; SourceGroup = $vpcGroupName; DestinationGroup = "ESXi"; Service = @("HTTPS","ICMP ALL") }
$mgwRules += @{Name = "$vpcGroupName to NSX" ; SourceGroup = $vpcGroupName; DestinationGroup = "NSX Manager"; Service = @("HTTPS") }
#### End of configurable section ####
Function Add-Groups {
param(
[Parameter(Mandatory)]$GatewayType,
[Parameter(Mandatory)]$GroupList
)
Write-Host -ForegroundColor Green "Adding $($GatewayType) groups"
foreach ($group in $GroupList) {
Write-Host -ForegroundColor Green "Working on group: $($group.Name)"
if(Get-NSXTGroup -GatewayType $GatewayType -Name $group.Name) {
Write-Host -ForegroundColor Blue "Group exists, skipping"
} else {
Write-Host -ForegroundColor Green "Creating new group..."
New-NSXTGroup -GatewayType $GatewayType -Name $group.Name -IPAddress $group.IPAddress | Out-Null
}
}
}
Function Remove-Groups {
param(
[Parameter(Mandatory)]$GatewayType,
[Parameter(Mandatory)]$GroupList
)
Write-Host -ForegroundColor Green "Removing $($GatewayType) groups"
foreach ($group in $GroupList) {
write-output "Working on group: $($group.Name)"
$fwGroup = Get-NSXTGroup -GatewayType $GatewayType -Name $group.Name
if($fwGroup.ID) {
Write-Host -ForegroundColor Red "Group exists, attempting to remove..."
Remove-NSXTGroup -Id $fwGroup.ID -GatewayType $GatewayType
} else {
Write-Host -ForegroundColor Red "Group not found, skipping removal"
}
}
}
Function Add-Rules {
param(
[Parameter(Mandatory)]$GatewayType,
[Parameter(Mandatory)]$RuleList
)
Write-Host -ForegroundColor Green "Adding $($GatewayType) rules"
$ruleSeq = 0
foreach ($rule in $RuleList) {
Write-Output "Working on rule: $($rule.Name)"
if(Get-NSXTFirewall -GatewayType $GatewayType -Name $rule.Name) {
Write-Host -ForegroundColor Blue "Rule exists, skipping"
} else {
if($GatewayType -eq "MGW"){
$InfraScope = "MGW"
} elseif ($rule.InfraScope) {
$InfraScope = $rule.InfraScope
} else {
$InfraScope = "All Uplinks"
}
Write-Host -ForegroundColor Green "Creating new rule..."
New-NSXTFirewall -GatewayType $GatewayType -Name $rule.Name -SourceGroup $rule.SourceGroup `
-DestinationGroup $rule.DestinationGroup -Service $rule.Service -InfraScope $InfraScope `
-Logged $true -SequenceNumber ($ruleSeq++) -Action ALLOW | Out-Null
}
}
}
Function Remove-Rules {
param(
[Parameter(Mandatory)]$GatewayType,
[Parameter(Mandatory)]$RuleList
)
Write-Host -ForegroundColor Green "Removing $($GatewayType) rules"
foreach ($rule in $RuleList) {
Write-Output "Working on rule: $($rule.Name)"
$fwRule = Get-NSXTFirewall -GatewayType $GatewayType -Name $rule.Name
if($fwRule.ID) {
Write-Host -ForegroundColor Red "Rule exists, attempting to remove..."
Remove-NSXTFirewall -Id $fwRule.ID -GatewayType $GatewayType
} else {
Write-Host -ForegroundColor Red "Rule not found, skipping removal"
}
}
}
if($Cleanup.isPresent) {
Remove-Rules -GatewayType MGW $mgwRules
Remove-Groups -GatewayType MGW $mgwGroups
Remove-Rules -GatewayType CGW $cgwRules
Remove-Groups -GatewayType CGW $cgwGroups
} else {
Add-Groups -GatewayType CGW $cgwGroups
Add-Rules -GatewayType CGW $cgwRules
Add-Groups -GatewayType MGW $mgwGroups
Add-Rules -GatewayType MGW $mgwRules
}