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
|
// Package vinyldns implements a DNS provider for solving the DNS-01 challenge using VinylDNS.
package vinyldns
import (
"errors"
"fmt"
"strings"
"time"
"github.com/go-acme/lego/v4/challenge/dns01"
"github.com/go-acme/lego/v4/platform/config/env"
"github.com/go-acme/lego/v4/platform/wait"
"github.com/vinyldns/go-vinyldns/vinyldns"
)
// Environment variables names.
const (
envNamespace = "VINYLDNS_"
EnvAccessKey = envNamespace + "ACCESS_KEY"
EnvSecretKey = envNamespace + "SECRET_KEY"
EnvHost = envNamespace + "HOST"
EnvTTL = envNamespace + "TTL"
EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
)
// Config is used to configure the creation of the DNSProvider.
type Config struct {
AccessKey string
SecretKey string
Host string
TTL int
PropagationTimeout time.Duration
PollingInterval time.Duration
}
// NewDefaultConfig returns a default configuration for the DNSProvider.
func NewDefaultConfig() *Config {
return &Config{
TTL: env.GetOrDefaultInt(EnvTTL, 30),
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 2*time.Minute),
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 4*time.Second),
}
}
// DNSProvider implements the challenge.Provider interface.
type DNSProvider struct {
client *vinyldns.Client
config *Config
}
// NewDNSProvider returns a DNSProvider instance configured for VinylDNS.
// Credentials must be passed in the environment variables:
// VINYLDNS_ACCESS_KEY, VINYLDNS_SECRET_KEY, VINYLDNS_HOST.
func NewDNSProvider() (*DNSProvider, error) {
values, err := env.Get(EnvAccessKey, EnvSecretKey, EnvHost)
if err != nil {
return nil, fmt.Errorf("vinyldns: %w", err)
}
config := NewDefaultConfig()
config.AccessKey = values[EnvAccessKey]
config.SecretKey = values[EnvSecretKey]
config.Host = values[EnvHost]
return NewDNSProviderConfig(config)
}
// NewDNSProviderConfig return a DNSProvider instance configured for VinylDNS.
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
if config == nil {
return nil, errors.New("vinyldns: the configuration of the VinylDNS DNS provider is nil")
}
if config.AccessKey == "" || config.SecretKey == "" {
return nil, errors.New("vinyldns: credentials are missing")
}
if config.Host == "" {
return nil, errors.New("vinyldns: host is missing")
}
client := vinyldns.NewClient(vinyldns.ClientConfiguration{
AccessKey: config.AccessKey,
SecretKey: config.SecretKey,
Host: config.Host,
UserAgent: "go-acme/lego",
})
client.HTTPClient.Timeout = 30 * time.Second
return &DNSProvider{client: client, config: config}, nil
}
// Present creates a TXT record to fulfill the dns-01 challenge.
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
fqdn, value := dns01.GetRecord(domain, keyAuth)
existingRecord, err := d.getRecordSet(fqdn)
if err != nil {
return fmt.Errorf("vinyldns: %w", err)
}
record := vinyldns.Record{Text: value}
if existingRecord == nil || existingRecord.ID == "" {
err = d.createRecordSet(fqdn, []vinyldns.Record{record})
if err != nil {
return fmt.Errorf("vinyldns: %w", err)
}
return nil
}
for _, i := range existingRecord.Records {
if i.Text == value {
return nil
}
}
records := existingRecord.Records
records = append(records, record)
err = d.updateRecordSet(existingRecord, records)
if err != nil {
return fmt.Errorf("vinyldns: %w", err)
}
return nil
}
// CleanUp removes the TXT record matching the specified parameters.
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
fqdn, value := dns01.GetRecord(domain, keyAuth)
existingRecord, err := d.getRecordSet(fqdn)
if err != nil {
return fmt.Errorf("vinyldns: %w", err)
}
if existingRecord == nil || existingRecord.ID == "" || len(existingRecord.Records) == 0 {
return nil
}
var records []vinyldns.Record
for _, i := range existingRecord.Records {
if i.Text != value {
records = append(records, i)
}
}
if len(records) == 0 {
err = d.deleteRecordSet(existingRecord)
if err != nil {
return fmt.Errorf("vinyldns: %w", err)
}
return nil
}
err = d.updateRecordSet(existingRecord, records)
if err != nil {
return fmt.Errorf("vinyldns: %w", err)
}
return nil
}
// Timeout returns the timeout and interval to use when checking for DNS propagation.
// Adjusting here to cope with spikes in propagation times.
func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
return d.config.PropagationTimeout, d.config.PollingInterval
}
func (d *DNSProvider) getRecordSet(fqdn string) (*vinyldns.RecordSet, error) {
zoneName, hostName, err := splitDomain(fqdn)
if err != nil {
return nil, err
}
zone, err := d.client.ZoneByName(zoneName)
if err != nil {
return nil, err
}
allRecordSets, err := d.client.RecordSetsListAll(zone.ID, vinyldns.ListFilter{NameFilter: hostName})
if err != nil {
return nil, err
}
var recordSets []vinyldns.RecordSet
for _, i := range allRecordSets {
if i.Type == "TXT" {
recordSets = append(recordSets, i)
}
}
switch {
case len(recordSets) > 1:
return nil, fmt.Errorf("ambiguous recordset definition of %s", fqdn)
case len(recordSets) == 1:
return &recordSets[0], nil
default:
return nil, nil
}
}
func (d *DNSProvider) createRecordSet(fqdn string, records []vinyldns.Record) error {
zoneName, hostName, err := splitDomain(fqdn)
if err != nil {
return err
}
zone, err := d.client.ZoneByName(zoneName)
if err != nil {
return err
}
recordSet := vinyldns.RecordSet{
Name: hostName,
ZoneID: zone.ID,
Type: "TXT",
TTL: d.config.TTL,
Records: records,
}
resp, err := d.client.RecordSetCreate(&recordSet)
if err != nil {
return err
}
return d.waitForChanges("CreateRS", resp)
}
func (d *DNSProvider) updateRecordSet(recordSet *vinyldns.RecordSet, newRecords []vinyldns.Record) error {
operation := "delete"
if len(recordSet.Records) < len(newRecords) {
operation = "add"
}
recordSet.Records = newRecords
recordSet.TTL = d.config.TTL
resp, err := d.client.RecordSetUpdate(recordSet)
if err != nil {
return err
}
return d.waitForChanges("UpdateRS - "+operation, resp)
}
func (d *DNSProvider) deleteRecordSet(existingRecord *vinyldns.RecordSet) error {
resp, err := d.client.RecordSetDelete(existingRecord.ZoneID, existingRecord.ID)
if err != nil {
return err
}
return d.waitForChanges("DeleteRS", resp)
}
func (d *DNSProvider) waitForChanges(operation string, resp *vinyldns.RecordSetUpdateResponse) error {
return wait.For("vinyldns", d.config.PropagationTimeout, d.config.PollingInterval,
func() (bool, error) {
change, err := d.client.RecordSetChange(resp.Zone.ID, resp.RecordSet.ID, resp.ChangeID)
if err != nil {
return false, fmt.Errorf("failed to query change status: %w", err)
}
if change.Status == "Complete" {
return true, nil
}
return false, fmt.Errorf("waiting operation: %s, zoneID: %s, recordsetID: %s, changeID: %s",
operation, resp.Zone.ID, resp.RecordSet.ID, resp.ChangeID)
},
)
}
// splitDomain splits the hostname from the authoritative zone, and returns both parts.
func splitDomain(fqdn string) (string, string, error) {
zone, err := dns01.FindZoneByFqdn(fqdn)
if err != nil {
return "", "", err
}
host := dns01.UnFqdn(strings.TrimSuffix(fqdn, zone))
return zone, host, nil
}
|