DnsPlugins/DMEasy.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
function Add-DnsTxtDMEasy {
    [CmdletBinding(DefaultParameterSetName='Secure')]
    param(
        [Parameter(Mandatory,Position=0)]
        [string]$RecordName,
        [Parameter(Mandatory,Position=1)]
        [string]$TxtValue,
        [Parameter(Mandatory,Position=2)]
        [string]$DMEKey,
        [Parameter(ParameterSetName='Secure',Mandatory,Position=3)]
        [securestring]$DMESecret,
        [Parameter(ParameterSetName='Insecure',Mandatory,Position=3)]
        [string]$DMESecretInsecure,
        [switch]$DMEUseSandbox,
        [Parameter(ValueFromRemainingArguments)]
        $ExtraParams
    )

    # convert the secure secret to a normal string
    if ('Secure' -eq $PSCmdlet.ParameterSetName) {
        $DMESecretInsecure = (New-Object PSCredential ("user", $DMESecret)).GetNetworkCredential().Password
    }

    $apiBase = 'https://api.dnsmadeeasy.com/V2.0/dns/managed'
    if ($DMEUseSandbox) {
        $apiBase = 'https://api.sandbox.dnsmadeeasy.com/V2.0/dns/managed'
    }

    Write-Verbose "Attempting to find hosted zone for $RecordName"
    if (!($zoneID,$zoneName = Find-DMEZone $RecordName $DMEKey $DMESecretInsecure $apiBase)) {
        throw "Unable to find DME hosted zone for $RecordName"
    }

    # separate the portion of the name that doesn't contain the zone name
    $recShort = $RecordName -ireplace [regex]::Escape(".$zoneName"), [string]::Empty


    $recRoot = "$apiBase/$zoneID/records"

    # query the existing record(s)
    try {
        $auth = Get-DMEAuthHeader $DMEKey $DMESecretInsecure
        $response = Invoke-RestMethod "$($recRoot)?recordName=$recShort&type=TXT" `
            -Headers $auth -ContentType 'application/json' @script:UseBasic
    } catch { throw }

    # check if our value is already in there
    if ($response.totalRecords -gt 0) {
        if ("`"$TxtValue`"" -in $response.data.value) {
            Write-Debug "Record $RecordName already contains $TxtValue. Nothing to do."
            return
        }
    }

    # create a new record
    try {
        $auth = Get-DMEAuthHeader $DMEKey $DMESecretInsecure
        $bodyJson = @{name=$recShort;value="`"$TxtValue`"";type='TXT';ttl=10} | ConvertTo-Json -Compress
        Write-Verbose "Creating $RecordName with value $TxtValue"
        Invoke-RestMethod $recRoot -Method Post -Body $bodyJson -Headers $auth `
            -ContentType 'application/json' @script:UseBasic | Out-Null
    } catch { throw }

    <#
    .SYNOPSIS
        Add a DNS TXT record to DNS Made Easy.
 
    .DESCRIPTION
        Add a DNS TXT record to DNS Made Easy.
 
    .PARAMETER RecordName
        The fully qualified name of the TXT record.
 
    .PARAMETER TxtValue
        The value of the TXT record.
 
    .PARAMETER DMEKey
        The DNS Made Easy API key for your account.
 
    .PARAMETER DMESecret
        The DNS Made Easy API secret key for your account. This SecureString version should only be used on Windows.
 
    .PARAMETER DMESecretInsecure
        The DNS Made Easy API secret key for your account. This standard String version should be used on non-Windows OSes.
 
    .PARAMETER DMEUseSandbox
        If specified, all commands will run against the DNS Made Easy sandbox API endpoint. This is generally only used for testing the plugin.
 
    .PARAMETER ExtraParams
        This parameter can be ignored and is only used to prevent errors when splatting with more parameters than this function supports.
 
    .EXAMPLE
        $dmeSecret = Read-Host "DME Secret" -AsSecureString
        PS C:\>Add-DnsTxtDMEasy '_acme-challenge.site1.example.com' 'asdfqwer12345678' 'xxxxxxxxxxxx' $dmeSecret
 
        Adds a TXT record for the specified site with the specified value.
    #>

}

function Remove-DnsTxtDMEasy {
    [CmdletBinding(DefaultParameterSetName='Secure')]
    param(
        [Parameter(Mandatory,Position=0)]
        [string]$RecordName,
        [Parameter(Mandatory,Position=1)]
        [string]$TxtValue,
        [Parameter(Mandatory,Position=2)]
        [string]$DMEKey,
        [Parameter(ParameterSetName='Secure',Mandatory,Position=3)]
        [securestring]$DMESecret,
        [Parameter(ParameterSetName='Insecure',Mandatory,Position=3)]
        [string]$DMESecretInsecure,
        [switch]$DMEUseSandbox,
        [Parameter(ValueFromRemainingArguments)]
        $ExtraParams
    )

    # convert the secure secret to a normal string
    if ('Secure' -eq $PSCmdlet.ParameterSetName) {
        $DMESecretInsecure = (New-Object PSCredential ("user", $DMESecret)).GetNetworkCredential().Password
    }

    $apiBase = 'https://api.dnsmadeeasy.com/V2.0/dns/managed'
    if ($DMEUseSandbox) {
        $apiBase = 'https://api.sandbox.dnsmadeeasy.com/V2.0/dns/managed'
    }

    Write-Verbose "Attempting to find hosted zone for $RecordName"
    if (!($zoneID,$zoneName = Find-DMEZone $RecordName $DMEKey $DMESecretInsecure $apiBase)) {
        throw "Unable to find DME hosted zone for $RecordName"
    }

    # separate the portion of the name that doesn't contain the zone name
    $recShort = $RecordName -ireplace [regex]::Escape(".$zoneName"), [string]::Empty

    $recRoot = "$apiBase/$zoneID/records"

    # query the existing record(s)
    try {
        $auth = Get-DMEAuthHeader $DMEKey $DMESecretInsecure
        $response = Invoke-RestMethod "$($recRoot)?recordName=$recShort&type=TXT" `
            -Headers $auth -ContentType 'application/json' @script:UseBasic
    } catch { throw }

    # check for the value to delete
    if ($response.totalRecords -eq 0) {
        Write-Debug "Record $RecordName doesn't exist. Nothing to do."
        return
    } else {
        if ("`"$TxtValue`"" -notin $response.data.value) {
            Write-Debug "Record $RecordName does not contain $TxtValue. Nothing to do."
            return
        }
        # grab the ID and delete the record
        $recID = ($response.data | Where-Object { $_.value -eq "`"$TxtValue`"" }).id
        try {
            $auth = $auth = Get-DMEAuthHeader $DMEKey $DMESecretInsecure
            Write-Verbose "Deleting record $RecordName with value $TxtValue."
            Invoke-RestMethod "$recRoot/$recID" -Method Delete -Headers $auth `
                -ContentType 'application/json' @script:UseBasic | Out-Null
        } catch { throw }
    }

    <#
    .SYNOPSIS
        Remove a DNS TXT record from DNS Made Easy.
 
    .DESCRIPTION
        Remove a DNS TXT record from DNS Made Easy.
 
    .PARAMETER RecordName
        The fully qualified name of the TXT record.
 
    .PARAMETER TxtValue
        The value of the TXT record.
 
    .PARAMETER DMEKey
        The DNS Made Easy API key for your account.
 
    .PARAMETER DMESecret
        The DNS Made Easy API secret key for your account. This SecureString version should only be used on Windows.
 
    .PARAMETER DMESecretInsecure
        The DNS Made Easy API secret key for your account. This standard String version should be used on non-Windows OSes.
 
    .PARAMETER DMEUseSandbox
        If specified, all commands will run against the DNS Made Easy sandbox API endpoint. This is generally only used for testing the plugin.
 
    .PARAMETER ExtraParams
        This parameter can be ignored and is only used to prevent errors when splatting with more parameters than this function supports.
 
    .EXAMPLE
        $dmeSecret = Read-Host "DME Secret" -AsSecureString
        PS C:\>Remove-DnsTxtDMEasy '_acme-challenge.site1.example.com' 'asdfqwer12345678' 'xxxxxxxxxxxx' $dmeSecret
 
        Removes a TXT record for the specified site with the specified value.
    #>

}

function Save-DnsTxtDMEasy {
    [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 Get-DMEAuthHeader {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory,Position=0)]
        [string]$DMEKey,
        [Parameter(Mandatory,Position=1)]
        [string]$DMESecretInsecure
    )

    # We need to initialize an HMACSHA1 instance with the secret key as a byte array.
    # I know there's probably a safer way to do this that doesn't leave the plaintext
    # secret around in memory for as long, but it's beyond me at the moment.
    $secBytes = [Text.Encoding]::UTF8.GetBytes($DMESecretInsecure)
    $hmac = New-Object Security.Cryptography.HMACSHA1($secBytes,$true)

    # We need to hash a timestamp in "HTTP format", aka RFC 1123
    # https://api-docs.dnsmadeeasy.com/#1bf6d47c-61b1-0cf3-4f04-0ed4772561fe
    # https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings#RFC1123
    $reqDate = (Get-DateTimeOffsetNow).ToString('r')
    $dateBytes = [Text.Encoding]::UTF8.GetBytes($reqDate)
    $dateHash = [BitConverter]::ToString($hmac.ComputeHash($dateBytes)).Replace('-','').ToLower()

    # now build the header hashtable
    $header = @{
       'x-dnsme-apiKey'      = $DMEKey;
       'x-dnsme-requestDate' = $reqDate;
       'x-dnsme-hmac'        = $dateHash;
    }

    return $header
}

function Find-DMEZone {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory,Position=0)]
        [string]$RecordName,
        [Parameter(Mandatory,Position=1)]
        [string]$DMEKey,
        [Parameter(Mandatory,Position=2)]
        [string]$DMESecretInsecure,
        [Parameter(Mandatory,Position=3)]
        [string]$ApiBase
    )

    # setup a module variable to cache the record to zone mapping
    # so it's quicker to find later
    if (!$script:DMERecordZones) { $script:DMERecordZones = @{} }

    # check for the record in the cache
    if ($script:DMERecordZones.ContainsKey($RecordName)) {
        return $script:DMERecordZones.$RecordName
    }

    # The response object for managed zones makes it seem like it supports paging with
    # fields like totalPages/page. But the docs don't really make it clear how to
    # request subsequent pages. They also don't say what the max results per page is.
    # So for now, we'll just assume all results get returned in one page. If any large
    # customers find differently, feel free to submit an issue.
    try {
        $auth = Get-DMEAuthHeader $DMEKey $DMESecretInsecure
        $response = Invoke-RestMethod $ApiBase -Headers $auth -ContentType 'application/json' @script:UseBasic
        $zones = $response.data
    } catch { throw }

    # Since DME could be hosting both apex and sub-zones, we need to find the closest/deepest
    # sub-zone that would hold the record rather than just adding it to the apex. So for something
    # like _acme-challenge.site1.sub1.sub2.example.com, we'd look for zone matches in the following
    # order:
    # - site1.sub1.sub2.example.com
    # - sub1.sub2.example.com
    # - sub2.example.com
    # - example.com
    $pieces = $RecordName.Split('.')
    for ($i=1; $i -lt ($pieces.Count-1); $i++) {
        $zoneTest = "$( $pieces[$i..($pieces.Count-1)] -join '.' )"
        Write-Debug "Checking $zoneTest"

        if ($zoneTest -in $zones.name) {
            $zone = $zones | Where-Object { $_.name -eq $zoneTest }
            $script:DMERecordZones.$RecordName = $zone.id,$zone.name
            return $zone.id,$zone.name
        }
    }

    return $null
}