DnsPlugins/AcmeDns.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
function Add-DnsTxtAcmeDns {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory,Position=0)]
        [string]$RecordName,
        [Parameter(Mandatory,Position=1)]
        [string]$TxtValue,
        [Parameter(Mandatory,Position=2)]
        [string]$ACMEServer,
        [string[]]$ACMEAllowFrom,
        [Parameter(ValueFromRemainingArguments)]
        $ExtraParams
    )

    # Each name used with AcmeDns must be dynamically registered with the acme-dns
    # server and a CNAME record created by the user based on the registration before
    # proceeding. Each registration needs to be saved so it can be re-used later without
    # the user needing to explicitly pass it in.

    # So we're going to use the new Import/Export-PluginVar functions but we also need to
    # allow for importing the legacy ACMEReg value from plugindata.xml that will no longer
    # be passed in.
    if (-not ($ACMEReg = Import-PluginVar ACMEReg)) {
        # check for legacy registration data by getting all saved pluginargs on this account
        # which should include AcmeReg if it still exists
        $pargs = Import-PluginArgs
        if ($pargs.ACMEReg) {
            # Convert the hashtable to JSON and back to basically make it PSCustomObject
            # which the rest of the plugin is now expecting
            Write-Verbose "Migrating legacy acme-dns registrations"
            $ACMEReg = $pargs.ACMEReg | ConvertTo-Json | ConvertFrom-Json
        } else {
            # no existing data, so create an empty object to use
            Write-Verbose "No existing acme-dns registrations found"
            $ACMEReg = [pscustomobject]@{}
        }
    }

    # create a new subdomain registration if necessary
    if ($RecordName -notin $ACMEReg.PSObject.Properties.Name) {
        $reg = New-AcmeDnsRegistration $ACMEServer $ACMEAllowFrom

        $ACMEReg | Add-Member $RecordName @($reg.subdomain,$reg.username,$reg.password,$reg.fulldomain)

        # we need to notify the user to create a CNAME for this registration
        # so save it to memory to display later during Save-DnsTxtAcmeDns
        if (!$script:ACMECNAMES) { $script:ACMECNAMES = @() }
        $script:ACMECNAMES += [pscustomobject]@{Record=$RecordName;CNAME=$reg.fulldomain}

        # save the new registration
        Export-PluginVar ACMEReg $ACMEReg
    }

    # grab a reference to this record's registration values
    $regVals = $ACMEReg.$RecordName

    # create the auth header object
    $authHead = @{'X-Api-User'=$regVals[1];'X-Api-Key'=$regVals[2]}

    # create the update body
    $updateBody = @{subdomain=$regVals[0];txt=$TxtValue} | ConvertTo-Json -Compress

    # send the update
    try {
        Write-Verbose "Updating $($regVals[3]) with $TxtValue"
        $response = Invoke-RestMethod "https://$ACMEServer/update" -Method Post `
            -Headers $authHead -Body $updateBody @script:UseBasic
        Write-Debug ($response | ConvertTo-Json)
    } catch { throw }

    <#
    .SYNOPSIS
        Add a DNS TXT record to acme-dns.
 
    .DESCRIPTION
        This plugin requires using the -DnsAlias option. The value for DnsAlias is the "fulldomain" returned by the acme-dns register endpoint.
 
    .PARAMETER RecordName
        The fully qualified name of the TXT record.
 
    .PARAMETER TxtValue
        The value of the TXT record.
 
    .PARAMETER ACMEServer
        The FQDN of the acme-dns server instance.
 
    .PARAMETER ACMEAllowFrom
        A list of networks in CIDR notation that the acme-dns server should allow updates from. If not specified, the acme-dns server will not block any updates based on IP address.
 
    .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-DnsTxtAcmeDns '_acme-challenge.site1.example.com' 'xxxxxxxxxxXXXXXXXXXXxxxxxxxxxxXXXXXXXXXX001' 'auth.acme-dns.io'
 
        Adds a TXT record for the specified site with the specified value and no IP filtering.
 
    .EXAMPLE
        Add-DnsTxtAcmeDns '_acme-challenge.site1.example.com' 'xxxxxxxxxxXXXXXXXXXXxxxxxxxxxxXXXXXXXXXX001' 'auth.acme-dns.io' @('192.168.100.1/24","2002:c0a8:2a00::0/40")
 
        Adds a TXT record for the specified site with the specified value and only allowed from the specified networks.
    #>

}

function Remove-DnsTxtAcmeDns {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory,Position=0)]
        [string]$RecordName,
        [Parameter(Mandatory,Position=1)]
        [string]$TxtValue,
        [Parameter(ValueFromRemainingArguments)]
        $ExtraParams
    )
    <#
    .SYNOPSIS
        Not required.
 
    .DESCRIPTION
        This provider does not require calling this function to remove DNS TXT records.
 
    .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.
    #>

}

function Save-DnsTxtAcmeDns {
    [CmdletBinding()]
    param(
        [Parameter(ValueFromRemainingArguments)]
        $ExtraParams
    )

    if ($script:ACMECNAMES -and $script:ACMECNAMES.Count -gt 0) {

        Write-Host
        Write-Host "Please create the following CNAME records:"
        Write-Host "------------------------------------------"
        $script:ACMECNAMES | ForEach-Object {
            Write-Host "$($_.Record) -> $($_.CNAME)"
        }
        Write-Host "------------------------------------------"
        Write-Host

        # clear out the variable so we don't notify twice
        Remove-Variable ACMECNAMES -Scope Script

        Read-Host -Prompt "Press any key to continue." | Out-Null
    }

    <#
    .SYNOPSIS
        Returns CNAME records that must be created by the user.
 
    .DESCRIPTION
        If new acme-dns registrations have previously been made with Add-DnsTxtAcmeDns, the CNAMEs need to be created by the user before challenge validation will succeed. This function outputs any pending CNAMEs to be created and then waits for user confirmation to continue.
 
    .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-DnsTxtAcmeDns
 
        Displays pending CNAME records to create.
    #>

}

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

function New-AcmeDnsRegistration {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory,Position=0)]
        [string]$ACMEServer,
        [Parameter(Position=1)]
        [string[]]$ACMEAllowFrom
    )

    # build the registration body
    if ($ACMEAllowFrom) {
        $regBody = @{allowfrom=$ACMEAllowFrom} | ConvertTo-Json -Compress
    } else {
        $regBody = '{}'
    }

    # do the registration
    try {
        Write-Verbose "Registering new subdomain on $ACMEServer"
        $reg = Invoke-RestMethod "https://$ACMEServer/register" -Method POST -Body $regBody `
            -ContentType 'application/json' @script:UseBasic
        Write-Debug ($reg | ConvertTo-Json)
    } catch { throw }

    # return the results
    return $reg
}