1
0
mirror of https://github.com/go-acme/lego.git synced 2024-12-23 17:21:50 +02:00
lego/providers/dns/scaleway/scaleway.go

184 lines
5.4 KiB
Go
Raw Normal View History

2020-01-23 16:51:47 +02:00
// Package scaleway implements a DNS provider for solving the DNS-01 challenge using Scaleway Domains API.
// Token: https://www.scaleway.com/en/docs/generate-an-api-token/
package scaleway
import (
"errors"
"fmt"
"strconv"
"strings"
2020-01-23 16:51:47 +02:00
"time"
2020-09-02 03:20:01 +02:00
"github.com/go-acme/lego/v4/challenge/dns01"
"github.com/go-acme/lego/v4/platform/config/env"
2021-04-28 16:49:05 +02:00
scwdomain "github.com/scaleway/scaleway-sdk-go/api/domain/v2beta1"
"github.com/scaleway/scaleway-sdk-go/scw"
2020-01-23 16:51:47 +02:00
)
const (
minTTL = 60
defaultPollingInterval = 10 * time.Second
defaultPropagationTimeout = 120 * time.Second
)
// The access key is not used by the Scaleway client.
const dumpAccessKey = "SCWXXXXXXXXXXXXXXXXX"
// Environment variables names.
2020-01-23 16:51:47 +02:00
const (
envNamespace = "SCALEWAY_"
2021-04-28 16:49:05 +02:00
EnvAPIToken = envNamespace + "API_TOKEN"
EnvProjectID = envNamespace + "PROJECT_ID"
altEnvNamespace = "SCW_"
EnvAccessKey = altEnvNamespace + "ACCESS_KEY"
EnvSecretKey = altEnvNamespace + "SECRET_KEY"
EnvTTL = envNamespace + "TTL"
EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
2020-01-23 16:51:47 +02:00
)
// Config is used to configure the creation of the DNSProvider.
type Config struct {
2021-04-28 16:49:05 +02:00
ProjectID string
Token string // TODO(ldez) rename to SecretKey in the next major.
AccessKey string
2020-01-23 16:51:47 +02:00
PropagationTimeout time.Duration
PollingInterval time.Duration
TTL int
}
// NewDefaultConfig returns a default configuration for the DNSProvider.
func NewDefaultConfig() *Config {
return &Config{
AccessKey: dumpAccessKey,
TTL: env.GetOneWithFallback(EnvTTL, minTTL, strconv.Atoi, altEnvName(EnvTTL)),
PropagationTimeout: env.GetOneWithFallback(EnvPropagationTimeout, defaultPropagationTimeout, env.ParseSecond, altEnvName(EnvPropagationTimeout)),
PollingInterval: env.GetOneWithFallback(EnvPollingInterval, defaultPollingInterval, env.ParseSecond, altEnvName(EnvPollingInterval)),
2020-01-23 16:51:47 +02:00
}
}
2020-05-08 19:35:25 +02:00
// DNSProvider implements the challenge.Provider interface.
2020-01-23 16:51:47 +02:00
type DNSProvider struct {
config *Config
2021-04-28 16:49:05 +02:00
client *scwdomain.API
2020-01-23 16:51:47 +02:00
}
// NewDNSProvider returns a DNSProvider instance configured for Scaleway Domains API.
2021-04-28 16:49:05 +02:00
// Credentials must be passed in the environment variables:
// SCALEWAY_API_TOKEN, SCALEWAY_PROJECT_ID.
2020-01-23 16:51:47 +02:00
func NewDNSProvider() (*DNSProvider, error) {
values, err := env.GetWithFallback([]string{EnvSecretKey, EnvAPIToken})
2020-01-23 16:51:47 +02:00
if err != nil {
return nil, fmt.Errorf("scaleway: %w", err)
}
config := NewDefaultConfig()
config.Token = values[EnvSecretKey]
config.AccessKey = env.GetOrDefaultString(EnvAccessKey, dumpAccessKey)
2021-04-28 16:49:05 +02:00
config.ProjectID = env.GetOrFile(EnvProjectID)
2020-01-23 16:51:47 +02:00
return NewDNSProviderConfig(config)
}
// NewDNSProviderConfig return a DNSProvider instance configured for scaleway.
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
if config == nil {
return nil, errors.New("scaleway: the configuration of the DNS provider is nil")
}
if config.Token == "" {
return nil, errors.New("scaleway: credentials missing")
}
if config.TTL < minTTL {
config.TTL = minTTL
}
2021-04-28 16:49:05 +02:00
configuration := []scw.ClientOption{
scw.WithAuth(config.AccessKey, config.Token),
2021-04-28 16:49:05 +02:00
scw.WithUserAgent("Scaleway Lego's provider"),
}
if config.ProjectID != "" {
configuration = append(configuration, scw.WithDefaultProjectID(config.ProjectID))
}
// Create a Scaleway client
clientScw, err := scw.NewClient(configuration...)
if err != nil {
return nil, fmt.Errorf("scaleway: %w", err)
}
2020-01-23 16:51:47 +02:00
2021-04-28 16:49:05 +02:00
return &DNSProvider{config: config, client: scwdomain.NewAPI(clientScw)}, nil
2020-01-23 16:51:47 +02:00
}
// 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
}
// Present creates a TXT record to fulfill DNS-01 challenge.
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
info := dns01.GetChallengeInfo(domain, keyAuth)
2020-01-23 16:51:47 +02:00
2021-04-28 16:49:05 +02:00
records := []*scwdomain.Record{{
Data: fmt.Sprintf(`%q`, info.Value),
Name: info.EffectiveFQDN,
2021-04-28 16:49:05 +02:00
TTL: uint32(d.config.TTL),
Type: scwdomain.RecordTypeTXT,
Comment: scw.StringPtr("used by lego"),
}}
req := &scwdomain.UpdateDNSZoneRecordsRequest{
2024-01-06 14:02:10 +02:00
DNSZone: info.EffectiveFQDN,
2021-04-28 16:49:05 +02:00
Changes: []*scwdomain.RecordChange{{
Add: &scwdomain.RecordChangeAdd{Records: records},
}},
2024-01-06 14:02:10 +02:00
ReturnAllRecords: scw.BoolPtr(false),
DisallowNewZoneCreation: true,
2020-01-23 16:51:47 +02:00
}
2021-04-28 16:49:05 +02:00
_, err := d.client.UpdateDNSZoneRecords(req)
2020-01-23 16:51:47 +02:00
if err != nil {
return fmt.Errorf("scaleway: %w", err)
}
2021-04-28 16:49:05 +02:00
2020-01-23 16:51:47 +02:00
return nil
}
// CleanUp removes a TXT record used for DNS-01 challenge.
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
info := dns01.GetChallengeInfo(domain, keyAuth)
2020-01-23 16:51:47 +02:00
2021-04-28 16:49:05 +02:00
recordIdentifier := &scwdomain.RecordIdentifier{
Name: info.EffectiveFQDN,
2021-04-28 16:49:05 +02:00
Type: scwdomain.RecordTypeTXT,
Data: scw.StringPtr(fmt.Sprintf(`%q`, info.Value)),
2020-01-23 16:51:47 +02:00
}
2021-04-28 16:49:05 +02:00
req := &scwdomain.UpdateDNSZoneRecordsRequest{
2024-01-06 14:02:10 +02:00
DNSZone: info.EffectiveFQDN,
2021-04-28 16:49:05 +02:00
Changes: []*scwdomain.RecordChange{{
Delete: &scwdomain.RecordChangeDelete{IDFields: recordIdentifier},
}},
2024-01-06 14:02:10 +02:00
ReturnAllRecords: scw.BoolPtr(false),
DisallowNewZoneCreation: true,
2021-04-28 16:49:05 +02:00
}
_, err := d.client.UpdateDNSZoneRecords(req)
2020-01-23 16:51:47 +02:00
if err != nil {
return fmt.Errorf("scaleway: %w", err)
}
2021-04-28 16:49:05 +02:00
2020-01-23 16:51:47 +02:00
return nil
}
func altEnvName(v string) string {
return strings.ReplaceAll(v, envNamespace, altEnvNamespace)
}