Repository navigation
Expand file tree
/
Copy pathhandler.go
More file actions
412 lines (357 loc) · 10.2 KB
/
Copy pathhandler.go
File metadata and controls
412 lines (357 loc) · 10.2 KB
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
package caddydns01proxy
import (
"fmt"
"net/http"
"time"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/caddyserver/caddy/v2/modules/caddyhttp/caddyauth"
"github.com/caddyserver/certmagic"
"github.com/libdns/libdns"
"github.com/liujed/caddy-dns01proxy/jsonutil"
"github.com/liujed/goutil/optionals"
"go.uber.org/zap"
)
func init() {
caddy.RegisterModule(Handler{})
httpcaddyfile.RegisterHandlerDirective("dns01proxy", parseHandler)
httpcaddyfile.RegisterDirectiveOrder(
"dns01proxy",
httpcaddyfile.After,
"acme_server",
)
}
// Implements an API for proxying ACME DNS-01 challenges.
//
// This is a Caddy `http.handlers` module.
type Handler struct {
DNS DNSConfig `json:"dns"`
// Configures HTTP basic authentication and the domains for which each user
// can get TLS certificates.
//
// (During provisioning, this is used to fill in [Authentication] and
// [ClientRegistry].)
AccountsRaw []RawAccount `json:"accounts"`
// Specifies how clients should be authenticated. If absent, then clients must
// be authenticated by an `http.handlers.authentication` instance earlier in
// the handler chain. Derived from [AccountsRaw].
//
// XXX This should be an Optional[*caddyauth.Authentication], but Caddy's
// documentation generator doesn't work with generics.
Authentication *caddyauth.Authentication `json:"-"`
// Identifies the domains at which each client is allowed to answer DNS-01
// challenges. Derived from [AccountsRaw].
ClientRegistry ClientRegistry `json:"-"`
logger *zap.Logger
}
var _ caddy.Module = (*Handler)(nil)
var _ caddy.Provisioner = (*Handler)(nil)
var _ caddyhttp.MiddlewareHandler = (*Handler)(nil)
var _ caddyfile.Unmarshaler = (*Handler)(nil)
type RawAccount struct {
ClientPolicy
// The user's password, hashed using `caddy hash-password`. Optional. If
// omitted, then clients must be authenticated by an
// `http.handlers.authentication` instance earlier in the handler chain.
Password *string `json:"password,omitempty"`
}
func (Handler) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "http.handlers.dns01proxy",
New: func() caddy.Module {
return new(Handler)
},
}
}
func (h *Handler) Provision(ctx caddy.Context) error {
h.logger = ctx.Logger()
// Provision DNS.
err := h.DNS.Provision(ctx)
if err != nil {
return err
}
// Provision Authentication from AccountsRaw.
accountList := []caddyauth.Account{}
for _, rawAccount := range h.AccountsRaw {
if rawAccount.Password != nil {
accountList = append(accountList, caddyauth.Account{
Username: rawAccount.UserID,
Password: *rawAccount.Password,
})
}
}
if len(accountList) > 0 {
auth := &caddyauth.Authentication{
ProvidersRaw: caddy.ModuleMap{
"http_basic": caddyconfig.JSON(
caddyauth.HTTPBasicAuth{
AccountList: accountList,
},
nil,
),
},
}
err := auth.Provision(ctx)
if err != nil {
return fmt.Errorf("unable to provision authenticaiton: %w", err)
}
h.Authentication = auth
}
// Normally, we expect either all users or no users to have a password
// configured. Warn if this is not the case.
if len(accountList) > 0 && len(accountList) != len(h.AccountsRaw) {
ctx.Logger().Warn("some users will always fail authentication because they do not have a password configured")
}
// Provision ClientRegistry from AccountsRaw.
err = h.ClientRegistry.Provision(ctx, h.AccountsRaw)
if err != nil {
return fmt.Errorf("unable to provision client registry: %w", err)
}
// Allow AccountsRaw to be GC'd.
h.AccountsRaw = nil
return nil
}
func (h *Handler) ServeHTTP(
w http.ResponseWriter,
req *http.Request,
nextHandler caddyhttp.Handler,
) error {
var mode handlerMode
switch req.URL.Path {
case "/present":
if req.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
return nil
}
mode = hmPresent
case "/cleanup":
if req.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
return nil
}
mode = hmCleanup
default:
return nextHandler.ServeHTTP(w, req)
}
handlerImpl := jsonutil.WrapHandler(h.handleDNSRequest(mode))
if h.Authentication != nil {
return h.Authentication.ServeHTTP(w, req, handlerImpl)
}
return handlerImpl.ServeHTTP(w, req)
}
type handlerMode string
const (
hmPresent handlerMode = "present"
hmCleanup handlerMode = "cleanup"
)
func (h *Handler) handleDNSRequest(
mode handlerMode,
) func(*http.Request, RequestBody) (int, optionals.Optional[ResponseBody], error) {
return func(
req *http.Request,
reqBody RequestBody,
) (httpStatus int, respBody optionals.Optional[ResponseBody], err error) {
// Check that the user gave a valid request body.
if !reqBody.IsValid() {
return http.StatusBadRequest, optionals.None[ResponseBody](), nil
}
// Log the challenge domain that appears in the request.
addLogField(req, zap.String("domain", reqBody.ChallengeFQDN))
// Check that the user is authorized for the challenge domain in the
// request.
denyReasonOpt, err := h.ClientRegistry.AuthorizeUserChallengeDomain(
req,
reqBody.ChallengeFQDN,
)
if err != nil {
return 0, optionals.None[ResponseBody](),
fmt.Errorf("unable to authorize user for requested domain: %w", err)
}
if denyReason, denied := denyReasonOpt.Get(); denied {
addLogField(req, zap.String(logAuthorizationFailure, string(denyReason)))
return http.StatusForbidden, optionals.None[ResponseBody](), nil
}
// Figure out the challenge domain's DNS zone.
zone, err := certmagic.FindZoneByFQDN(
req.Context(),
h.logger,
reqBody.ChallengeFQDN,
certmagic.RecursiveNameservers(h.DNS.Resolvers),
)
if err != nil {
return 0, optionals.None[ResponseBody](),
fmt.Errorf(
"unable to find DNS zone for %q: %w",
reqBody.ChallengeFQDN,
err,
)
}
// Build the DNS record to create/delete.
ttl := time.Duration(0)
if mode != hmCleanup && h.DNS.TTL != nil {
ttl = time.Duration(*h.DNS.TTL)
}
records := []libdns.Record{
libdns.TXT{
Name: libdns.RelativeName(reqBody.ChallengeFQDN, zone),
TTL: ttl,
Text: `"` + reqBody.Value + `"`,
},
}
switch mode {
case hmPresent:
// Create the DNS record.
_, err = h.DNS.Provider.AppendRecords(req.Context(), zone, records)
if err != nil {
return 0, optionals.None[ResponseBody](),
fmt.Errorf("error creating DNS record: %w", err)
}
return http.StatusOK, optionals.Some(reqBody), nil
case hmCleanup:
// Delete the DNS record.
_, err = h.DNS.Provider.DeleteRecords(req.Context(), zone, records)
if err != nil {
return 0, optionals.None[ResponseBody](),
fmt.Errorf("error deleting DNS record: %w", err)
}
return http.StatusOK, optionals.Some(reqBody), nil
}
return 0, optionals.None[ResponseBody](),
fmt.Errorf("unknown handler mode: %q", mode)
}
}
// Parses a dns01proxy directive into a Handler instance.
//
// Syntax:
//
// dns01proxy {
// dns <provider_name> [<params...>]
// dns_ttl <ttl>
// resolvers <resolvers...>
// user <userID> {
// password <hashed_password>
// allow_domains <domains...>
// deny_domains <domains...>
// }
// }
func (h *Handler) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
// Consume directive name.
d.Next()
// No inline arguments allowed.
if d.NextArg() {
return d.ArgErr()
}
for nesting := d.Nesting(); d.NextBlock(nesting); {
switch d.Val() {
case "dns":
// Expect a provider name.
if !d.NextArg() {
return d.ArgErr()
}
provName := d.Val()
unm, err := caddyfile.UnmarshalModule(d, "dns.providers."+provName)
if err != nil {
return err
}
h.DNS.ProviderRaw = caddyconfig.JSONModuleObject(
unm,
"name",
provName,
nil,
)
case "dns_ttl":
var ttl string
if !d.AllArgs(&ttl) {
return d.ArgErr()
}
parsedTTL, err := caddy.ParseDuration(ttl)
if err != nil {
return err
}
caddyTTL := caddy.Duration(parsedTTL)
h.DNS.TTL = &caddyTTL
case "resolvers":
h.DNS.Resolvers = d.RemainingArgs()
if len(h.DNS.Resolvers) == 0 {
return d.Errf("must specify at least one resolver address")
}
case "user":
var userID string
if !d.AllArgs(&userID) {
return d.ArgErr()
}
account := RawAccount{
ClientPolicy: ClientPolicy{
UserID: userID,
},
}
// Parse the client declaration.
for nesting := d.Nesting(); d.NextBlock(nesting); {
var curDomainsRaw *[]string
fieldName := d.Val()
switch fieldName {
case "password":
var password string
if !d.AllArgs((&password)) {
return d.ArgErr()
}
if account.Password != nil {
return fmt.Errorf("cannot specify more than one password per user")
}
account.Password = &password
continue
case "allow_domains":
curDomainsRaw = &account.AllowDomainsRaw
case "deny_domains":
curDomainsRaw = &account.DenyDomainsRaw
default:
return d.Errf("unrecognized user directive: %q", fieldName)
}
if *curDomainsRaw != nil {
return d.Errf(
"cannot specify more than one %q policy per user",
fieldName,
)
}
domainList := d.RemainingArgs()
if len(domainList) == 0 {
return d.Errf("must specify at least one domain")
}
*curDomainsRaw = domainList
}
// Register the account.
h.AccountsRaw = append(h.AccountsRaw, account)
default:
return d.Errf("unrecognized dns01proxy handler directive: %q", d.Val())
}
}
return nil
}
// Unmarshals tokens from h into a new Handler instance that is ready for
// provisioning.
func parseHandler(
h httpcaddyfile.Helper,
) (caddyhttp.MiddlewareHandler, error) {
var result Handler
err := result.UnmarshalCaddyfile(h.Dispenser)
if len(result.DNS.ProviderRaw) == 0 {
// No locally configured DNS provider. Use the global option.
val := h.Option("acme_dns")
if val == nil {
val = h.Option("dns")
if val == nil {
return nil, fmt.Errorf("must configure a DNS provider")
}
}
result.DNS.ProviderRaw = caddyconfig.JSONModuleObject(
val,
"name",
val.(caddy.Module).CaddyModule().ID.Name(),
nil,
)
}
return &result, err
}