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
|
// Package exoscale implements a DNS provider for solving the DNS-01 challenge using Exoscale DNS.
package exoscale
import (
"context"
"errors"
"fmt"
"time"
egoscale "github.com/exoscale/egoscale/v2"
"github.com/go-acme/lego/v4/challenge/dns01"
"github.com/go-acme/lego/v4/platform/config/env"
)
// Default Exoscale API endpoint.
const defaultBaseURL = "https://api.exoscale.com/v2"
// Default Exosacle API zone.
// Each data center location hosts the API and API zone determines which one to connect to.
const defaultAPIZone = "ch-gva-2"
// Environment variables names.
const (
envNamespace = "EXOSCALE_"
EnvAPISecret = envNamespace + "API_SECRET"
EnvAPIKey = envNamespace + "API_KEY"
EnvEndpoint = envNamespace + "ENDPOINT"
EnvAPIZone = envNamespace + "API_ZONE"
EnvTTL = envNamespace + "TTL"
EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT"
)
// Config is used to configure the creation of the DNSProvider.
type Config struct {
APIKey string
APISecret string
Endpoint string
HTTPTimeout time.Duration
PropagationTimeout time.Duration
PollingInterval time.Duration
TTL int64
}
// NewDefaultConfig returns a default configuration for the DNSProvider.
func NewDefaultConfig() *Config {
return &Config{
TTL: int64(env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL)),
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
HTTPTimeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 60*time.Second),
}
}
// DNSProvider implements the challenge.Provider interface.
type DNSProvider struct {
config *Config
client *egoscale.Client
apiZone string
}
// NewDNSProvider Credentials must be passed in the environment variables:
// EXOSCALE_API_KEY, EXOSCALE_API_SECRET, EXOSCALE_ENDPOINT.
func NewDNSProvider() (*DNSProvider, error) {
values, err := env.Get(EnvAPIKey, EnvAPISecret)
if err != nil {
return nil, fmt.Errorf("exoscale: %w", err)
}
config := NewDefaultConfig()
config.APIKey = values[EnvAPIKey]
config.APISecret = values[EnvAPISecret]
config.Endpoint = env.GetOrFile(EnvEndpoint)
return NewDNSProviderConfig(config)
}
// NewDNSProviderConfig return a DNSProvider instance configured for Exoscale.
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
if config == nil {
return nil, errors.New("exoscale: the configuration of the DNS provider is nil")
}
if config.APIKey == "" || config.APISecret == "" {
return nil, errors.New("exoscale: credentials missing")
}
if config.Endpoint == "" {
config.Endpoint = defaultBaseURL
}
client, err := egoscale.NewClient(
config.APIKey,
config.APISecret,
egoscale.ClientOptWithAPIEndpoint(config.Endpoint),
egoscale.ClientOptWithTimeout(config.HTTPTimeout),
)
if err != nil {
return nil, fmt.Errorf("exoscale: initializing client: %w", err)
}
return &DNSProvider{
client: client,
config: config,
apiZone: env.GetOrDefaultString(EnvAPIZone, defaultAPIZone),
}, nil
}
// Present creates a TXT record to fulfill the dns-01 challenge.
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
ctx := context.Background()
fqdn, value := dns01.GetRecord(domain, keyAuth)
zoneName, recordName, err := d.findZoneAndRecordName(fqdn)
if err != nil {
return err
}
zone, err := d.findExistingZone(zoneName)
if err != nil {
return fmt.Errorf("exoscale: %w", err)
}
if zone == nil {
return fmt.Errorf("exoscale: zone %q not found", zoneName)
}
recordID, err := d.findExistingRecordID(*zone.ID, recordName)
if err != nil {
return fmt.Errorf("exoscale: %w", err)
}
recordType := "TXT"
if recordID == "" {
record := egoscale.DNSDomainRecord{
Name: &recordName,
TTL: &d.config.TTL,
Content: &value,
Type: &recordType,
}
_, err = d.client.CreateDNSDomainRecord(ctx, d.apiZone, *zone.ID, &record)
if err != nil {
return fmt.Errorf("exoscale: error while creating DNS record: %w", err)
}
return nil
}
record := egoscale.DNSDomainRecord{
ID: &recordID,
Name: &recordName,
TTL: &d.config.TTL,
Content: &value,
Type: &recordType,
}
err = d.client.UpdateDNSDomainRecord(ctx, d.apiZone, *zone.ID, &record)
if err != nil {
return fmt.Errorf("exoscale: error while updating DNS record: %w", err)
}
return nil
}
// CleanUp removes the record matching the specified parameters.
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
ctx := context.Background()
fqdn, _ := dns01.GetRecord(domain, keyAuth)
zoneName, recordName, err := d.findZoneAndRecordName(fqdn)
if err != nil {
return err
}
zone, err := d.findExistingZone(zoneName)
if err != nil {
return fmt.Errorf("exoscale: %w", err)
}
if zone == nil {
return fmt.Errorf("exoscale: zone %q not found", zoneName)
}
recordID, err := d.findExistingRecordID(*zone.ID, recordName)
if err != nil {
return err
}
if recordID != "" {
err = d.client.DeleteDNSDomainRecord(ctx, d.apiZone, *zone.ID, &egoscale.DNSDomainRecord{ID: &recordID})
if err != nil {
return fmt.Errorf("exoscale: error while deleting DNS record: %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
}
// findExistingZone Query Exoscale to find an existing zone for this name.
// Returns nil result if no zone could be found.
func (d *DNSProvider) findExistingZone(zoneName string) (*egoscale.DNSDomain, error) {
ctx := context.Background()
zones, err := d.client.ListDNSDomains(ctx, d.apiZone)
if err != nil {
return nil, fmt.Errorf("error while retrieving DNS zones: %w", err)
}
for _, zone := range zones {
if zone.UnicodeName != nil && *zone.UnicodeName == zoneName {
return &zone, nil
}
}
return nil, nil
}
// findExistingRecordID Query Exoscale to find an existing record for this name.
// Returns empty result if no record could be found.
func (d *DNSProvider) findExistingRecordID(zoneID, recordName string) (string, error) {
ctx := context.Background()
records, err := d.client.ListDNSDomainRecords(ctx, d.apiZone, zoneID)
if err != nil {
return "", fmt.Errorf("error while retrieving DNS records: %w", err)
}
recordType := "TXT"
for _, record := range records {
if record.Name != nil && *record.Name == recordName &&
record.Type != nil && *record.Type == recordType {
return *record.ID, nil
}
}
return "", nil
}
// findZoneAndRecordName Extract DNS zone and DNS entry name.
func (d *DNSProvider) findZoneAndRecordName(fqdn string) (string, string, error) {
zone, err := dns01.FindZoneByFqdn(fqdn)
if err != nil {
return "", "", err
}
zone = dns01.UnFqdn(zone)
name := dns01.UnFqdn(fqdn)
name = name[:len(name)-len("."+zone)]
return zone, name, nil
}
|