This repository has been archived by the owner on Mar 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 102
/
provider.go
84 lines (67 loc) · 1.98 KB
/
provider.go
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
package tls
import (
"strings"
"time"
"github.com/acorn-io/runtime/pkg/dns"
"github.com/go-acme/lego/v4/challenge/dns01"
"github.com/sirupsen/logrus"
)
const (
AuthorizationHeader = "Authorization"
ContentTypeHeader = "Content-Type"
ContentTypeJSON = "application/json"
txtPathPattern = "%s/domain/%s/txt"
)
/*
* DNS01 Challenge Solver (Lego Interface)
*/
type ACMEDNS01ChallengeProvider struct {
client DNSClient
}
func NewACMEDNS01ChallengeProvider(endpoint, domain, token string) *ACMEDNS01ChallengeProvider {
return &ACMEDNS01ChallengeProvider{
client: NewDNSClient(endpoint, domain, token),
}
}
func (d *ACMEDNS01ChallengeProvider) Present(domain, token, keyAuth string) error {
fqdn, value := dns01.GetRecord(domain, keyAuth)
return d.client.SetTXTRecord(fqdn, value)
}
func (d *ACMEDNS01ChallengeProvider) CleanUp(domain, token, keyAuth string) error {
return d.client.DeleteDNSRecord(domain)
}
func (d *ACMEDNS01ChallengeProvider) Timeout() (timeout, interval time.Duration) {
return 3 * time.Minute, 1 * time.Minute
}
/*
* AcornDNS Helper
*/
type DNSClient struct {
dns dns.Client
domain string
token string
endpoint string
}
func NewDNSClient(endpoint, domain, token string) DNSClient {
return DNSClient{
dns: dns.NewClient(),
domain: domain,
token: token,
endpoint: endpoint,
}
}
func (d *DNSClient) SetTXTRecord(domain, text string) error {
prefix := strings.TrimSuffix(strings.TrimSuffix(domain, "."), d.domain)
var requests []dns.RecordRequest
requests = append(requests, dns.RecordRequest{
Name: prefix,
Type: dns.RecordTypeTxt,
Values: []string{text},
})
logrus.Debugf("Setting TXT record %s - %s for domain %s", prefix, text, d.domain)
return d.dns.CreateRecords(d.endpoint, d.domain, d.token, requests)
}
func (d *DNSClient) DeleteDNSRecord(domain string) error {
prefix := strings.TrimSuffix(strings.TrimSuffix(domain, "."), d.domain)
return d.dns.DeleteRecord(d.endpoint, d.domain, prefix, d.token)
}