Public/cmdb/firewall/vip.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
#
# Copyright 2019, Alexis La Goutte <alexis dot lagoutte at gmail dot com>
# Copyright 2019, Benjamin Perrier <ben dot perrier at outlook dot com>
#
# SPDX-License-Identifier: Apache-2.0
#
function Add-FGTFirewallVip {

    <#
        .SYNOPSIS
        Add a FortiGate Virtual IP

        .DESCRIPTION
        Add a FortiGate Virtual IP (VIP) (One to One)

        .EXAMPLE
        Add-FGTFirewallVip -name myVIP1 -type static-nat -extip 192.0.2.1 -mappedip 198.51.100.1

        Add VIP objet type static-nat (One to One) with name myVIP1 with external IP 192.0.2.1 and mapped IP 198.51.100.1

        .EXAMPLE
        Add-FGTFirewallVip -name myVIP2 -type static-nat -extip 192.0.2.2 -mappedip 198.51.100.2 -interface port1 -comment "My FGT VIP"

        Add VIP objet type static-nat (One to One) with name myVIP1 with external IP 192.0.2.1, mapped IP 198.51.100.1, associated to interface port2 and a comment

        .EXAMPLE
        Add-FGTFirewallVip -name myVIP3-8080 -type static-nat -extip 192.0.2.1 -mappedip 198.51.100.1 -portforward -extport 8080

        Add VIP objet type static-nat (One to One) with name myVIP3 with external IP 192.0.2.1 and mapped IP 198.51.100.1 with Port Forward and TCP Port 8080

        .EXAMPLE
        Add-FGTFirewallVip -name myVIP4-5000-6000 -type static-nat -extip 192.0.2.1 -mappedip 198.51.100.1 -portforward -extport 5000 -mappedport 6000 -protocol udp

        Add VIP objet type static-nat (One to One) with name myVIP3 with external IP 192.0.2.1 and mapped IP 198.51.100.1 with Port Forward and UDP Port 5000 mapped to port 6000

    #>


    Param(
        [Parameter (Mandatory = $true)]
        [ValidateSet("static-nat")]
        [string]$type,
        [Parameter (Mandatory = $true)]
        [string]$name,
        [Parameter (Mandatory = $true)]
        [ipaddress]$extip,
        [Parameter (Mandatory = $true)]
        [ipaddress]$mappedip,
        [Parameter (Mandatory = $false)]
        [string]$interface = "any",
        [Parameter (Mandatory = $false)]
        [ValidateLength(0, 255)]
        [string]$comment,
        [Parameter (Mandatory = $false)]
        [switch]$portforward,
        [Parameter (Mandatory = $false)]
        [ValidateSet("tcp", "udp", "sctp", "icmp",IgnoreCase=$false)]
        [string]$protocol = "tcp",
        [Parameter (Mandatory = $false)]
        [string]$extport,
        [Parameter (Mandatory = $false)]
        [string]$mappedport,
        [Parameter (Mandatory = $false)]
        [switch]$skip,
        [Parameter(Mandatory = $false)]
        [String[]]$vdom,
        [Parameter(Mandatory = $false)]
        [psobject]$connection = $DefaultFGTConnection
    )

    Begin {
    }

    Process {

        $invokeParams = @{ }
        if ( $PsBoundParameters.ContainsKey('skip') ) {
            $invokeParams.add( 'skip', $skip )
        }
        if ( $PsBoundParameters.ContainsKey('vdom') ) {
            $invokeParams.add( 'vdom', $vdom )
        }

        if ( Get-FGTFirewallVip -connection $connection @invokeParams -name $name ) {
            Throw "Already a VIP object using the same name"
        }

        $uri = "api/v2/cmdb/firewall/vip"

        $vip = new-Object -TypeName PSObject

        $vip | add-member -name "name" -membertype NoteProperty -Value $name

        $vip | add-member -name "type" -membertype NoteProperty -Value $type

        $vip | add-member -name "extip" -membertype NoteProperty -Value $extip.ToString()

        $range = New-Object -TypeName PSObject

        $range | Add-member -name "range" -membertype NoteProperty -value $mappedip.ToString()
        $vip | add-member -name "mappedip" -membertype NoteProperty -Value @($range)

        #TODO check if the interface (zone ?) is valid
        $vip | add-member -name "extintf" -membertype NoteProperty -Value $interface

        if ( $PsBoundParameters.ContainsKey('comment') ) {
            $vip | add-member -name "comment" -membertype NoteProperty -Value $comment
        }

        if ( $PsBoundParameters.ContainsKey('portforward') -and $portforward -eq $true) {
            #check if export is set before...
            if ( $extport -eq "") {
                throw "you need to set -extport when enable portforward parameter"
            }
            $vip | add-member -name "portforward" -membertype NoteProperty -Value "enable"
            $vip | add-member -name "protocol" -membertype NoteProperty -Value $protocol
            $vip | add-member -name "extport" -membertype NoteProperty -Value $extport
            #if no mappedport use the extport
            if ( $PsBoundParameters.ContainsKey('mappedport') ) {
                $vip | add-member -name "mappedport" -membertype NoteProperty -Value $mappedport
            }
            else {
                $vip | add-member -name "mappedport" -membertype NoteProperty -Value $extport
            }
        }

        Invoke-FGTRestMethod -method "POST" -body $vip -uri $uri -connection $connection @invokeParams | Out-Null

        Get-FGTFirewallVip -connection $connection @invokeParams -name $name
    }

    End {
    }
}
function Get-FGTFirewallVip {

    <#
        .SYNOPSIS
        Get list of all (NAT) Virtual IP

        .DESCRIPTION
        Get list of all (NAT) Virtual IP (Ext IP, mapped IP, type...)

        .EXAMPLE
        Get-FGTFirewallVip

        Get list of all nat vip object

        .EXAMPLE
        Get-FGTFirewallVip -name myFGTVip

        Get VIP named myFGTVip

        .EXAMPLE
        Get-FGTFirewallVip -name FGT -filter_type contains

        Get VIP contains *FGT*

        .EXAMPLE
        Get-FGTFirewallVip -uuid 9e73a10e-1772-51ea-a8d7-297686fd7702

        Get VIP with uuid 9e73a10e-1772-51ea-a8d7-297686fd7702

        .EXAMPLE
        Get-FGTFirewallVip -skip

        Get list of all nat vip object (but only relevant attributes)

        .EXAMPLE
        Get-FGTFirewallVip -vdom vdomX

        Get list of all nat vip object on vdomX
    #>


    [CmdletBinding(DefaultParameterSetName = "default")]
    Param(
        [Parameter (Mandatory = $false, Position = 1, ParameterSetName = "name")]
        [string]$name,
        [Parameter (Mandatory = $false, ParameterSetName = "uuid")]
        [string]$uuid,
        [Parameter (Mandatory = $false)]
        [Parameter (ParameterSetName = "filter")]
        [string]$filter_attribute,
        [Parameter (Mandatory = $false)]
        [Parameter (ParameterSetName = "name")]
        [Parameter (ParameterSetName = "uuid")]
        [Parameter (ParameterSetName = "filter")]
        [ValidateSet('equal', 'contains')]
        [string]$filter_type = "equal",
        [Parameter (Mandatory = $false)]
        [Parameter (ParameterSetName = "filter")]
        [psobject]$filter_value,
        [Parameter(Mandatory = $false)]
        [switch]$skip,
        [Parameter(Mandatory = $false)]
        [String[]]$vdom,
        [Parameter(Mandatory = $false)]
        [psobject]$connection = $DefaultFGTConnection
    )

    Begin {
    }

    Process {

        $invokeParams = @{ }
        if ( $PsBoundParameters.ContainsKey('skip') ) {
            $invokeParams.add( 'skip', $skip )
        }
        if ( $PsBoundParameters.ContainsKey('vdom') ) {
            $invokeParams.add( 'vdom', $vdom )
        }

        #Filtering
        switch ( $PSCmdlet.ParameterSetName ) {
            "name" {
                $filter_value = $name
                $filter_attribute = "name"
            }
            "uuid" {
                $filter_value = $uuid
                $filter_attribute = "uuid"
            }
            default { }
        }

        #if filter value and filter_attribute, add filter (by default filter_type is equal)
        if ( $filter_value -and $filter_attribute ) {
            $invokeParams.add( 'filter_value', $filter_value )
            $invokeParams.add( 'filter_attribute', $filter_attribute )
            $invokeParams.add( 'filter_type', $filter_type )
        }

        $response = Invoke-FGTRestMethod -uri 'api/v2/cmdb/firewall/vip' -method 'GET' -connection $connection @invokeParams

        $response.results
    }

    End {
    }
}

function Remove-FGTFirewallVip {

    <#
        .SYNOPSIS
        Remove a FortiGate Virtual IP

        .DESCRIPTION
        Remove a Virtual (VIP) object on the FortiGate

        .EXAMPLE
        $MyFGTVIP = Get-FGTFirewallVip -name MyFGTVIP
        PS C:\>$MyFGTVIP | Remove-FGTFirewallVip

        Remove VIP object $MyFGTVIP

        .EXAMPLE
        $MyFGTVIP = Get-FGTFirewallVip -name MyFGTVIP
        PS C:\>$MyFGTVIP | Remove-FGTFirewallVip -confirm:$false

        Remove VIP object MyFGTVIP with no confirmation

    #>


    [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'high')]
    Param(
        [Parameter (Mandatory = $true, ValueFromPipeline = $true, Position = 1)]
        [ValidateScript( { Confirm-FGTVip $_ })]
        [psobject]$vip,
        [Parameter(Mandatory = $false)]
        [String[]]$vdom,
        [Parameter(Mandatory = $false)]
        [psobject]$connection = $DefaultFGTConnection
    )

    Begin {
    }

    Process {

        $invokeParams = @{ }
        if ( $PsBoundParameters.ContainsKey('vdom') ) {
            $invokeParams.add( 'vdom', $vdom )
        }

        $uri = "api/v2/cmdb/firewall/vip/$($vip.name)"

        if ($PSCmdlet.ShouldProcess($vip.name, 'Remove Firewall VIP')) {
            $null = Invoke-FGTRestMethod -method "DELETE" -uri $uri -connection $connection @invokeParams
        }
    }

    End {
    }
}