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

function Add-DnsTxt {
    [CmdletBinding(DefaultParameterSetName='Secure')]
    param(
        [Parameter(Mandatory,Position=0)]
        [string]$RecordName,
        [Parameter(Mandatory,Position=1)]
        [string]$TxtValue,
        [Parameter(ParameterSetName='Secure',Mandatory,Position=2)]
        [securestring]$NameSiloKey,
        [Parameter(ParameterSetName='DeprecatedInsecure',Mandatory,Position=2)]
        [string]$NameSiloKeyInsecure,
        [Parameter(ValueFromRemainingArguments)]
        $ExtraParams
    )

    if ('Secure' -eq $PSCmdlet.ParameterSetName) {
        $NameSiloKeyInsecure = [pscredential]::new('a',$NameSiloKey).GetNetworkCredential().Password
    }

    # query the zone and record ID if it exists
    $zone, $recID = Get-NameSiloTXTRecordID $RecordName $TxtValue $NameSiloKeyInsecure

    if ($recID) {
        Write-Debug "Record $RecordName already contains $TxtValue. Nothing to do."
    } else {

        $recShort = ($RecordName -ireplace [regex]::Escape($zone), [string]::Empty).TrimEnd('.')

        Write-Verbose "Adding a TXT record for $RecordName with value $TxtValue"
        try {
            $query = "https://www.namesilo.com/api/dnsAddRecord?version=1&type=xml&key=$($NameSiloKeyInsecure)&domain=$($zone)&rrtype=TXT&rrhost=$($recShort)&rrvalue=$($TxtValue)&rrttl=3600"
            $response = Invoke-RestMethod $query @script:UseBasic -EA Stop
        } catch { throw }

        if ($response -and $response.namesilo.reply.code -ne 300) {
            throw "Failed to add TXT record: $($response.namesilo.reply.detail)"
        }

    }


    <#
    .SYNOPSIS
        Add a DNS TXT record to NameSilo

    .DESCRIPTION
        Adds the TXT record to the NameSilo Zone

    .PARAMETER RecordName
        The fully qualified name of the TXT record.

    .PARAMETER TxtValue
        The value of the TXT record.

    .PARAMETER NameSiloKey
        The API key for the NameSilo account. Created at https://www.namesilo.com/account/api-manager.

    .PARAMETER NameSiloKeyInsecure
        (DEPRECATED) The API key for the NameSilo account. Created at https://www.namesilo.com/account/api-manager.

    .PARAMETER ExtraParams
        This parameter can be ignored and is only used to prevent errors when splatting with more parameters than this function supports.

    .EXAMPLE
        $key = Read-Host 'API Key' -AsSecureString
        Add-DnsTxt -RecordName '_acme-challenge.example.com' 'txt-value' -NameSiloKey $key

        Adds a TXT record for the specified site with the specified value.
    #>

}

function Remove-DnsTxt {
    [CmdletBinding(DefaultParameterSetName='Secure')]
    param(
        [Parameter(Mandatory,Position=0)]
        [string]$RecordName,
        [Parameter(Mandatory,Position=1)]
        [string]$TxtValue,
        [Parameter(ParameterSetName='Secure',Mandatory,Position=2)]
        [securestring]$NameSiloKey,
        [Parameter(ParameterSetName='DeprecatedInsecure',Mandatory,Position=2)]
        [string]$NameSiloKeyInsecure,
        [Parameter(ValueFromRemainingArguments)]
        $ExtraParams
    )

    if ('Secure' -eq $PSCmdlet.ParameterSetName) {
        $NameSiloKeyInsecure = [pscredential]::new('a',$NameSiloKey).GetNetworkCredential().Password
    }

    # query the zone and record ID if it exists
    $zone, $recID = Get-NameSiloTXTRecordID $RecordName $TxtValue $NameSiloKeyInsecure

    if ($recID) {

        Write-Verbose "Removing TXT record for $RecordName with value $TxtValue and id $recID"

        $query = "https://www.namesilo.com/api/dnsDeleteRecord?version=1&type=xml&key=$($NameSiloKeyInsecure)&domain=$($zone)&rrid=$($recID)"
        $response = Invoke-RestMethod $query @script:UseBasic -EA Stop
        if ($response -and $response.namesilo.reply.code -ne 300) {
            throw "Failed to delete record: $($response.namesilo.reply.detail)"
        }

    } else {
        Write-Debug "Record $RecordName with value $TxtValue doesn't exist. Nothing to do."
    }


    <#
    .SYNOPSIS
        Remove a DNS TXT record from NameSilo

    .DESCRIPTION
        Removes the TXT record from the NameSilo zone

    .PARAMETER RecordName
        The fully qualified name of the TXT record.

    .PARAMETER TxtValue
        The value of the TXT record.

    .PARAMETER NameSiloKey
        The API key for the NameSilo account. Created at https://www.namesilo.com/account/api-manager.

    .PARAMETER NameSiloKeyInsecure
        (DEPRECATED) The API key for the NameSilo account. Created at https://www.namesilo.com/account/api-manager.

    .PARAMETER ExtraParams
        This parameter can be ignored and is only used to prevent errors when splatting with more parameters than this function supports.

    .EXAMPLE
        $key = Read-Host 'API Key' -AsSecureString
        Remove-DnsTxt -RecordName '_acme-challenge.example.com' 'txt-value' -NameSiloKey $key

        Removes a TXT record for the specified site with the specified value.
    #>

}

function Save-DnsTxt {
    [CmdletBinding()]
    param(
        [Parameter(ValueFromRemainingArguments)]
        $ExtraParams
    )
    <#
    .SYNOPSIS
        Not required

    .DESCRIPTION
        This provider does not require calling this function to save 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
##################################

# https://www.namesilo.com/api-reference

function Get-NameSiloTXTRecordID {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory,Position=0)]
        [string]$RecordName,
        [Parameter(Mandatory,Position=1)]
        [string]$TxtValue,
        [Parameter(Mandatory,Position=2)]
        [string]$NameSiloKeyInsecure
    )

    # Get the zone from the record name
    $zone = Find-NameSiloZone $RecordName $NameSiloKeyInsecure
    if ($null -eq $zone) {
        throw "Cannot find NameSilo domain for $RecordName"
    }

    # query all records for the zone
    try {
        $query = "https://www.namesilo.com/api/dnsListRecords?version=1&type=xml&key=$($NameSiloKeyInsecure)&domain=$($zone)"
        $response = Invoke-RestMethod $query @script:UseBasic -EA Stop
    } catch { throw }
    if ($response -and $response.namesilo.reply.code -ne 300) {
        throw "Failed to list domain records: $($response.namesilo.reply.detail)"
    }

    # grab the matching record if it exists
    $record = $response.namesilo.reply.resource_record | Where-Object {
        $_.type -eq 'TXT' -and
        $_.host -eq $RecordName -and
        $_.value -eq $TxtValue
    }

    # return the record ID and zone name
    if ($record) {
        return @($zone,$record.record_id)
    } else {
        return @($zone,$null)
    }

}

function Find-NameSiloZone {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory,Position=0)]
        [string]$RecordName,
        [Parameter(Mandatory,Position=1)]
        [string]$NameSiloKeyInsecure
    )

    if (!$script:NameSiloZones) { $script:NameSiloZones = @{} }

    if ($script:NameSiloZones.ContainsKey($RecordName)) {
        return $script:NameSiloZones.$RecordName
    }

    # grab the list of registered domains for this account
    $query = "https://www.namesilo.com/api/listDomains?version=1&type=xml&key=$($NameSiloKeyInsecure)"
    $response = Invoke-RestMethod $query @script:UseBasic -EA Stop
    if ($response -and $response.namesilo.reply.code -ne 300) {
        throw "Unexpected response from NameSilo API: $($response.namesilo.reply.detail)"
    }
    $domains = @($response.namesilo.reply.domains.domain)

    # find the closest match based on the record name
    $pieces = $RecordName.Split('.')
    for ($i=0; $i -lt ($pieces.Count-1); $i++) {
        $zone = $pieces[$i..($pieces.Count-1)] -join '.'

        if ($zone -in $domains) {
            $script:NameSiloZones.$RecordName = $zone
            return $zone
        }
    }

    return $null
}