DnsPlugins/Infoblox.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
function Add-DnsTxtInfoblox {
    [CmdletBinding(DefaultParameterSetName='Secure')]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingPlainTextForPassword','')]
    param(
        [Parameter(Mandatory,Position=0)]
        [string]$RecordName,
        [Parameter(Mandatory,Position=1)]
        [string]$TxtValue,
        [Parameter(Mandatory)]
        [string]$IBServer,
        [Parameter(ParameterSetName='Secure',Mandatory)]
        [pscredential]$IBCred,
        [Parameter(ParameterSetName='Insecure',Mandatory)]
        [string]$IBUsername,
        [Parameter(ParameterSetName='Insecure',Mandatory)]
        [string]$IBPassword,
        [string]$IBView='default',
        [switch]$IBIgnoreCert,
        [Parameter(ValueFromRemainingArguments)]
        $ExtraParams
    )

    # create a pscredential from insecure args if necessary
    if ('Insecure' -eq $PSCmdlet.ParameterSetName) {
        $secpass = ConvertTo-SecureString $IBPassword -AsPlainText -Force
        $IBCred = New-Object PSCredential ($IBUsername,$secpass)
    }

    $recUrl = "https://$IBServer/wapi/v1.0/record:txt?name=$RecordName&text=$TxtValue&view=$IBView"

    try {
        # ignore cert validation for the duration of the call
        if ($IBIgnoreCert) { Set-IBCertIgnoreOn }

        # check if the record already exists
        $response = Invoke-RestMethod -Uri $recUrl -Method Get -Credential $IBCred @script:UseBasic

        if ($response -and $response.'_ref') {
            Write-Debug "Record $RecordName with value $TxtValue already exists. Nothing to do."
        } else {
            # add the record
            Write-Verbose "Adding $RecordName with value $TxtValue"
            Invoke-RestMethod -Uri "$recUrl&ttl=0" -Method Post -Credential $IBCred @script:UseBasic | Out-Null
        }

    } finally {
        # return cert validation back to normal
        if ($IBIgnoreCert) { Set-IBCertIgnoreOff }
    }

    <#
    .SYNOPSIS
        Add a DNS TXT record to Infoblox.
 
    .DESCRIPTION
        Add a DNS TXT record to Infoblox.
 
    .PARAMETER RecordName
        The fully qualified name of the TXT record.
 
    .PARAMETER TxtValue
        The value of the TXT record.
 
    .PARAMETER IBServer
        The IP or hostname of the Infoblox server.
 
    .PARAMETER IBCred
        Credentials for Infoblox that have permission to write TXT records to the specified zone. This PSCredential option should only be used from Windows.
 
    .PARAMETER IBUsername
        The Infoblox username that has permission to write TXT records to the specified zone. This should be used from non-Windows.
 
    .PARAMETER IBPassword
        The password for the IBUsername parameter. This should be used from non-Windows.
 
    .PARAMETER IBView
        The name of the DNS View for the specified zone. Defaults to 'default'.
 
    .PARAMETER IBIgnoreCert
        Use this switch to prevent certificate errors when your Infoblox server is using a self-signed or other untrusted SSL certificate. When passing parameters via hashtable, set it as a boolean such as @{IBIgnoreCert=$true}.
 
    .PARAMETER ExtraParams
        This parameter can be ignored and is only used to prevent errors when splatting with more parameters than this function supports.
 
    .EXAMPLE
        $cred = Get-Credential
        PS C:\>$pluginArgs = @{IBServer='gridmaster.example.com'; IBCred=$cred; IBView='External'; IBIgnoreCert=$true}
        PS C:\>Add-DnsTxtInfoblox '_acme-challenge.site1.example.com' 'asdfqwer12345678' @pluginArgs
 
        Adds a TXT record for the specified site/value on Windows.
 
    .EXAMPLE
        $pluginArgs = @{IBServer='gridmaster.example.com'; IBUsername='myuser'; IBPassword='xxxxxxxx'; IBView='External'; IBIgnoreCert=$true}
        PS C:\>Add-DnsTxtInfoblox '_acme-challenge.site1.example.com' 'asdfqwer12345678' @pluginArgs
 
        Adds a TXT record for the specified site/value on non-Windows.
    #>

}

function Remove-DnsTxtInfoblox {
    [CmdletBinding(DefaultParameterSetName='Secure')]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingPlainTextForPassword','')]
    param(
        [Parameter(Mandatory,Position=0)]
        [string]$RecordName,
        [Parameter(Mandatory,Position=1)]
        [string]$TxtValue,
        [Parameter(Mandatory)]
        [string]$IBServer,
        [Parameter(ParameterSetName='Secure',Mandatory)]
        [pscredential]$IBCred,
        [Parameter(ParameterSetName='Insecure',Mandatory)]
        [string]$IBUsername,
        [Parameter(ParameterSetName='Insecure',Mandatory)]
        [string]$IBPassword,
        [string]$IBView='default',
        [switch]$IBIgnoreCert,
        [Parameter(ValueFromRemainingArguments)]
        $ExtraParams
    )

    # create a pscredential from insecure args if necessary
    if ('Insecure' -eq $PSCmdlet.ParameterSetName) {
        $secpass = ConvertTo-SecureString $IBPassword -AsPlainText -Force
        $IBCred = New-Object PSCredential ($IBUsername,$secpass)
    }

    try {
        # ignore cert validation for the duration of the call
        if ($IBIgnoreCert) { Set-IBCertIgnoreOn }

        # query the _ref for the txt record object we want to delete
        $recUrl = "https://$IBServer/wapi/v1.0/record:txt?name=$RecordName&text=$TxtValue&view=$IBView"
        $response = Invoke-RestMethod -Uri $recUrl -Method Get -Credential $IBCred @script:UseBasic

        if ($response -and $response.'_ref') {
            # delete the record
            $delUrl = "https://$IBServer/wapi/v1.0/$($response.'_ref')"
            Write-Verbose "Removing $RecordName with value $TxtValue"
            Invoke-RestMethod -Uri $delUrl -Method Delete -Credential $IBCred @script:UseBasic | Out-Null
        } else {
            Write-Debug "Record $RecordName with value $TxtValue doesn't exist. Nothing to do."
        }

    } finally {
        # return cert validation back to normal
        if ($IBIgnoreCert) { Set-IBCertIgnoreOff }
    }

    <#
    .SYNOPSIS
        Remove a DNS TXT record from Infoblox.
 
    .DESCRIPTION
        Remove a DNS TXT record from Infoblox.
 
    .PARAMETER RecordName
        The fully qualified name of the TXT record.
 
    .PARAMETER TxtValue
        The value of the TXT record.
 
    .PARAMETER IBServer
        The IP or hostname of the Infoblox server.
 
    .PARAMETER IBCred
        Credentials for Infoblox that have permission to write TXT records to the specified zone. This PSCredential option should only be used from Windows.
 
    .PARAMETER IBUsername
        The Infoblox username that has permission to write TXT records to the specified zone. This should be used from non-Windows.
 
    .PARAMETER IBPassword
        The password for the IBUsername parameter. This should be used from non-Windows.
 
    .PARAMETER IBView
        The name of the DNS View for the specified zone. Defaults to 'default'.
 
    .PARAMETER IBIgnoreCert
        Use this switch to prevent certificate errors when your Infoblox server is using a self-signed or other untrusted SSL certificate. When passing parameters via hashtable, set it as a boolean such as @{IBIgnoreCert=$true}.
 
    .PARAMETER ExtraParams
        This parameter can be ignored and is only used to prevent errors when splatting with more parameters than this function supports.
 
    .EXAMPLE
        $cred = Get-Credential
        PS C:\>$pluginArgs = @{IBServer='gridmaster.example.com'; IBCred=$cred; IBView='External'; IBIgnoreCert=$true}
        PS C:\>Remove-DnsTxtInfoblox '_acme-challenge.site1.example.com' 'asdfqwer12345678' @pluginArgs
 
        Removes a TXT record for the specified site/value on Windows.
 
    .EXAMPLE
        $pluginArgs = @{IBServer='gridmaster.example.com'; IBUsername='myuser'; IBPassword='xxxxxxxx'; IBView='External'; IBIgnoreCert=$true}
        PS C:\>Remove-DnsTxtInfoblox '_acme-challenge.site1.example.com' 'asdfqwer12345678' @pluginArgs
 
        Removes a TXT record for the specified site/value on non-Windows.
    #>

}

function Save-DnsTxtInfoblox {
    [CmdletBinding()]
    param(
        [Parameter(ValueFromRemainingArguments)]
        $ExtraParams
    )
    <#
    .SYNOPSIS
        Not required.
 
    .DESCRIPTION
        This provider does not require calling this function to commit changes to DNS records.
 
    .PARAMETER ExtraParams
        This parameter can be ignored and is only used to prevent errors when splatting with more parameters than this function supports.
    #>

}

############################
# Helper Functions
############################

function Set-IBCertIgnoreOn {
    [CmdletBinding()]
    param()

    if ($script:SkipCertSupported) {
        # Core edition
        if (-not $script:UseBasic.SkipCertificateCheck) {
            # temporarily set skip to true
            $script:UseBasic.SkipCertificateCheck = $true
            # remember that we did
            $script:IBUnsetIgnoreAfter = $true
        }

    } else {
        # Desktop edition
        [CertValidation]::Ignore()
    }
}

function Set-IBCertIgnoreOff {
    [CmdletBinding()]
    param()

    if ($script:SkipCertSupported) {
        # Core edition
        if ($script:IBUnsetIgnoreAfter) {
            $script:UseBasic.SkipCertificateCheck = $false
            Remove-Variable IBUnsetIgnoreAfter -Scope Script
        }

    } else {
        # Desktop edition
        [CertValidation]::Restore()
    }
}