Plugins/Beget.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
function Get-CurrentPluginType { 'dns-01' }

function Add-DnsTxt {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory,Position=0)]
        [string]$RecordName,
        [Parameter(Mandatory,Position=1)]
        [string]$TxtValue,
        [Parameter(Mandatory)]
        [pscredential]$BegetCredential,
        [Parameter(ValueFromRemainingArguments)]
        $ExtraParams
    )

    # Beget has a weird issue with their API where you can't retrieve the existing records
    # for a DNS sub-zone unless it also has a sub-domain defined for it. But you can't create
    # sub-domains for things that start with _acme-challenge (presumably they don't like the
    # first char underscore). So if the initial call to dns/getData fails, we're just going to
    # blindly replace the existing records for that sub-zone (which should still work) and
    # hope for the best.

    # Because the dns/changeRecords call is more of a "replace all records for this name" type
    # of call, we're going to queue the changes for each unique name and then wait to
    # do the replace when Save-DnsTxt is called.

    # setup a module variable to queue the changes in
    if (!$script:BegetRecordChanges) { $script:BegetRecordChanges = @{} }

    # check for the record in the cache
    if ($script:BegetRecordChanges.ContainsKey($RecordName)) {
        $changes = $script:BegetRecordChanges.$RecordName
    }

    if (-not $changes) {
        # try to get the existing record data for this FQDN
        $body = @{
            login = $BegetCredential.UserName
            passwd = $BegetCredential.GetNetworkCredential().Password
            input_format = 'json'
            input_data = @{fqdn=$RecordName} | ConvertTo-Json -Compress
        }
        $queryParams = @{
            Uri = 'https://api.beget.com/api/dns/getData'
            Method = 'POST'
            Body = $body
            Verbose = $false
            ErrorAction = 'Stop'
        }
        try {
            Write-Debug "POST $($queryParams.Uri)`n$($body.input_data)"
            $response = Invoke-RestMethod @queryParams @script:UseBasic
        } catch { throw }

        if ($response.answer.status -eq 'error') {
            Write-Verbose "No existing records found for $RecordName. This is expected for sub-zones that don't have a corresponding sub-domain."
        } else {
            # save the existing values for all records
            $changes = $response.answer.result.records
        }
    }

    if ($changes -and $changes.'TXT' -and
        ($TxtValue -in $changes.'TXT'.value -or $TxtValue -in $changes.'TXT'.txtdata))
    {
        Write-Debug "Record $RecordName already contains $TxtValue. Nothing to do."
        return
    }

    if (-not $changes) {
        # start a new record set
        $changes = @{
            'TXT' = @(
                @{ value = $TxtValue }
            )
        }
    } elseif (-not $changes.'TXT') {
        # add a new TXT record
        $changes.'TXT' = @(
            @{ value = $TxtValue }
        )
    } elseif ($TxtValue -notin $changes.'TXT'.value -and $TxtValue -notin $changes.'TXT'.txtdata) {
        # add a new value
        $changes.'TXT' += @{ value = $TxtValue }
    }

    Write-Verbose "Adding $RecordName with value $TxtValue."
    Write-Debug "Queued $($changes | ConvertTo-Json -Compress -Dep 10)"
    $script:BegetRecordChanges.$RecordName = $changes


    <#
    .SYNOPSIS
        Add a DNS TXT record to Beget.com
 
    .DESCRIPTION
        Description for Beget.com
 
    .PARAMETER RecordName
        The fully qualified name of the TXT record.
 
    .PARAMETER TxtValue
        The value of the TXT record.
 
    .PARAMETER ExtraParams
        This parameter can be ignored and is only used to prevent errors when splatting with more parameters than this function supports.
 
    .EXAMPLE
        Add-DnsTxt '_acme-challenge.example.com' 'txt-value' -BegetCredential (Get-Credential)
 
        Adds a TXT record for the specified site with the specified value.
    #>

}

function Remove-DnsTxt {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory,Position=0)]
        [string]$RecordName,
        [Parameter(Mandatory,Position=1)]
        [string]$TxtValue,
        [Parameter(Mandatory)]
        [pscredential]$BegetCredential,
        [Parameter(ValueFromRemainingArguments)]
        $ExtraParams
    )

    # Beget has a weird issue with their API where you can't retrieve the existing records
    # for a DNS sub-zone unless it also has a sub-domain defined for it. But you can't create
    # sub-domains for things that start with _acme-challenge (presumably they don't like the
    # first char underscore). So if the initial call to dns/getData fails, we're just going to
    # blindly replace the existing records for that sub-zone (which should still work) and
    # hope for the best.

    # Because the dns/changeRecords call is more of a "replace all records for this name" type
    # of call, we're going to queue the changes for each unique name and then wait to
    # do the replace when Save-DnsTxt is called.

    # setup a module variable to queue the changes in
    if (!$script:BegetRecordChanges) { $script:BegetRecordChanges = @{} }

    # check for the record in the cache
    if ($script:BegetRecordChanges.ContainsKey($RecordName)) {
        $changes = $script:BegetRecordChanges.$RecordName
    }

    if (-not $changes) {
        # try to get the existing record data for this FQDN
        $body = @{
            login = $BegetCredential.UserName
            passwd = $BegetCredential.GetNetworkCredential().Password
            input_format = 'json'
            input_data = @{fqdn=$RecordName} | ConvertTo-Json -Compress
        }
        $queryParams = @{
            Uri = 'https://api.beget.com/api/dns/getData'
            Method = 'POST'
            Body = $body
            Verbose = $false
            ErrorAction = 'Stop'
        }
        try {
            Write-Debug "POST $($queryParams.Uri)`n$($body.input_data)"
            $response = Invoke-RestMethod @queryParams @script:UseBasic
        } catch { throw }

        if ($response.answer.status -eq 'error') {
            Write-Verbose "No existing records found for $RecordName. This is expected for sub-zones that don't have a corresponding sub-domain."
        } else {
            # save the existing values for all records
            $changes = $response.answer.result.records
        }
    }

    if ($changes -and
        (-not $changes.'TXT' -or
        ($TxtValue -notin $changes.'TXT'.value -and $TxtValue -notin $changes.'TXT'.txtdata)))
    {
        # records exist for this name, but our TXT value isn't in them, so just return
        Write-Debug "Record $RecordName with value $TxtValue doesn't exist. Nothing to do."
        return
    }

    if (-not $changes) {
        # no records could mean that we simply can't get the records because of the whole
        # API weirdness thing with sub-zones. But we can still submit a request to wipe
        # the TXT records for this name.
        Write-Verbose "Deleting all TXT records for $RecordName"
        $changes = @{ 'TXT' = @() }
    }
    else {
        # remove the current value from the set
        Write-Verbose "Removing $RecordName with value $TxtValue."
        $changes.'TXT' = @($changes.'TXT' | Where-Object {
            $_.value -ne $TxtValue -and $_.txtdata -ne $TxtValue
        })
    }

    Write-Debug "Queued $($changes | ConvertTo-Json -Compress -Dep 10)"
    $script:BegetRecordChanges.$RecordName = $changes



    <#
    .SYNOPSIS
        Remove a DNS TXT record from Beget.com
 
    .DESCRIPTION
        Description for Beget.com
 
    .PARAMETER RecordName
        The fully qualified name of the TXT record.
 
    .PARAMETER TxtValue
        The value of the TXT record.
 
    .PARAMETER ExtraParams
        This parameter can be ignored and is only used to prevent errors when splatting with more parameters than this function supports.
 
    .EXAMPLE
        Remove-DnsTxt '_acme-challenge.example.com' 'txt-value' -BegetCredential (Get-Credential)
 
        Removes a TXT record for the specified site with the specified value.
    #>

}

function Save-DnsTxt {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [pscredential]$BegetCredential,
        [Parameter(ValueFromRemainingArguments)]
        $ExtraParams
    )

    $recNames = @()

    # check for queued changes
    if ($script:BegetRecordChanges -and $script:BegetRecordChanges.Keys.Count -gt 0) {
        $recNames = @($script:BegetRecordChanges.Keys)
    }

    $recNames | ForEach-Object {

        $recName = $_
        $recs = $script:BegetRecordChanges.$_
        Write-Verbose "Committing changes for $recName"

        $changeParams = @{
            Uri = ' https://api.beget.com/api/dns/changeRecords'
            Method = 'POST'
            Body = @{
                login = $BegetCredential.UserName
                passwd = $BegetCredential.GetNetworkCredential().Password
                input_format = 'json'
                input_data = @{
                    fqdn = $recName
                    records = $recs
                } | ConvertTo-Json -Compress -Depth 10
            }
            Verbose = $false
            ErrorAction = 'Stop'
        }

        try {
            Write-Debug "POST $($changeParams.Uri)`n$($changeParams.Body.input_data)"
            $response = Invoke-RestMethod @changeParams @script:UseBasic
        } catch { throw }


        if ($response.answer.status -eq 'error') {

            if ($response.answer.errors[0].error_text.type -eq 'INVALID_DATA_SINGLE') {
                # If we're updating a shared domain that has a forced A record, our change
                # request generates an error because the API doesn't want us overwriting it.
                # But we can just take it out of the change set and re-submit.
                Write-Debug "Retrying with no A records in change set."
                $recs.PSObject.Properties.Remove('A')
                $changeParams.Body.input_data = @{
                    fqdn = $recName
                    records = $recs
                } | ConvertTo-Json -Compress -Depth 10
                try {
                    Write-Debug "POST $($changeParams.Uri)`n$($changeParams.Body.input_data)"
                    $response = Invoke-RestMethod @changeParams @script:UseBasic
                } catch { throw }

                if ($response.answer.status -eq 'error') {
                    Write-Debug ($response.answer | ConvertTo-Json -Depth 10)
                    $script:BegetRecordChanges.Remove($recName)
                    throw "Error updating $($recName): $($response.answer.errors | ConvertTo-Json -Dep 10 -Compress)"
                }

            } else {
                Write-Debug ($response.answer | ConvertTo-Json -Depth 10)
                $script:BegetRecordChanges.Remove($recName)
                throw "Error updating $($recName): $($response.answer.errors | ConvertTo-Json -Dep 10 -Compress)"
            }
        }

        # wipe the changes from the queue
        $script:BegetRecordChanges.Remove($recName)

    }



    <#
    .SYNOPSIS
        Commits changes for pending DNS TXT record modifications to Beget.com
 
    .DESCRIPTION
        Description for Beget.com
 
    .PARAMETER ExtraParams
        This parameter can be ignored and is only used to prevent errors when splatting with more parameters than this function supports.
 
    .EXAMPLE
        Save-DnsTxt -BegetCredential (Get-Credential)
 
        Commits changes for pending DNS TXT record modifications.
    #>

}

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

# API Docs:
# https://beget.com/en/kb/api/beget-api