2020-01-23 14:51:47 +00: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"
|
|
|
|
"time"
|
|
|
|
|
2020-09-02 01:20:01 +00:00
|
|
|
"github.com/go-acme/lego/v4/challenge/dns01"
|
|
|
|
"github.com/go-acme/lego/v4/platform/config/env"
|
2021-04-28 14:49:05 +00:00
|
|
|
scwdomain "github.com/scaleway/scaleway-sdk-go/api/domain/v2beta1"
|
|
|
|
"github.com/scaleway/scaleway-sdk-go/scw"
|
2020-01-23 14:51:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
minTTL = 60
|
|
|
|
defaultPollingInterval = 10 * time.Second
|
|
|
|
defaultPropagationTimeout = 120 * time.Second
|
|
|
|
)
|
|
|
|
|
2020-03-11 22:51:10 +00:00
|
|
|
// Environment variables names.
|
2020-01-23 14:51:47 +00:00
|
|
|
const (
|
2020-03-11 22:51:10 +00:00
|
|
|
envNamespace = "SCALEWAY_"
|
|
|
|
|
2021-04-28 14:49:05 +00:00
|
|
|
EnvAPIToken = envNamespace + "API_TOKEN"
|
|
|
|
EnvProjectID = envNamespace + "PROJECT_ID"
|
2020-03-11 22:51:10 +00:00
|
|
|
|
|
|
|
EnvTTL = envNamespace + "TTL"
|
|
|
|
EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
|
|
|
|
EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
|
2020-01-23 14:51:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config is used to configure the creation of the DNSProvider.
|
|
|
|
type Config struct {
|
2021-04-28 14:49:05 +00:00
|
|
|
ProjectID string
|
2020-01-23 14:51:47 +00:00
|
|
|
Token string
|
|
|
|
PropagationTimeout time.Duration
|
|
|
|
PollingInterval time.Duration
|
|
|
|
TTL int
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDefaultConfig returns a default configuration for the DNSProvider.
|
|
|
|
func NewDefaultConfig() *Config {
|
|
|
|
return &Config{
|
2020-03-11 22:51:10 +00:00
|
|
|
TTL: env.GetOrDefaultInt(EnvTTL, minTTL),
|
|
|
|
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, defaultPropagationTimeout),
|
|
|
|
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, defaultPollingInterval),
|
2020-01-23 14:51:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// DNSProvider implements the challenge.Provider interface.
|
2020-01-23 14:51:47 +00:00
|
|
|
type DNSProvider struct {
|
|
|
|
config *Config
|
2021-04-28 14:49:05 +00:00
|
|
|
client *scwdomain.API
|
2020-01-23 14:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProvider returns a DNSProvider instance configured for Scaleway Domains API.
|
2021-04-28 14:49:05 +00:00
|
|
|
// Credentials must be passed in the environment variables:
|
|
|
|
// SCALEWAY_API_TOKEN, SCALEWAY_PROJECT_ID.
|
2020-01-23 14:51:47 +00:00
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
2020-03-11 22:51:10 +00:00
|
|
|
values, err := env.Get(EnvAPIToken)
|
2020-01-23 14:51:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("scaleway: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config := NewDefaultConfig()
|
2020-03-11 22:51:10 +00:00
|
|
|
config.Token = values[EnvAPIToken]
|
2021-04-28 14:49:05 +00:00
|
|
|
config.ProjectID = env.GetOrFile(EnvProjectID)
|
2020-01-23 14:51:47 +00: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 14:49:05 +00:00
|
|
|
configuration := []scw.ClientOption{
|
|
|
|
scw.WithAuth("SCWXXXXXXXXXXXXXXXXX", config.Token),
|
|
|
|
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 14:51:47 +00:00
|
|
|
|
2021-04-28 14:49:05 +00:00
|
|
|
return &DNSProvider{config: config, client: scwdomain.NewAPI(clientScw)}, nil
|
2020-01-23 14:51:47 +00: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 {
|
2023-03-07 08:39:05 +00:00
|
|
|
info := dns01.GetChallengeInfo(domain, keyAuth)
|
2020-01-23 14:51:47 +00:00
|
|
|
|
2021-04-28 14:49:05 +00:00
|
|
|
records := []*scwdomain.Record{{
|
2023-03-07 08:39:05 +00:00
|
|
|
Data: fmt.Sprintf(`%q`, info.Value),
|
|
|
|
Name: info.EffectiveFQDN,
|
2021-04-28 14:49:05 +00:00
|
|
|
TTL: uint32(d.config.TTL),
|
|
|
|
Type: scwdomain.RecordTypeTXT,
|
|
|
|
Comment: scw.StringPtr("used by lego"),
|
|
|
|
}}
|
|
|
|
|
2022-11-25 17:12:21 +00:00
|
|
|
// TODO(ldez) replace domain by FQDN to follow CNAME.
|
2021-04-28 14:49:05 +00:00
|
|
|
req := &scwdomain.UpdateDNSZoneRecordsRequest{
|
|
|
|
DNSZone: domain,
|
|
|
|
Changes: []*scwdomain.RecordChange{{
|
|
|
|
Add: &scwdomain.RecordChangeAdd{Records: records},
|
|
|
|
}},
|
|
|
|
ReturnAllRecords: scw.BoolPtr(false),
|
2020-01-23 14:51:47 +00:00
|
|
|
}
|
|
|
|
|
2021-04-28 14:49:05 +00:00
|
|
|
_, err := d.client.UpdateDNSZoneRecords(req)
|
2020-01-23 14:51:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("scaleway: %w", err)
|
|
|
|
}
|
2021-04-28 14:49:05 +00:00
|
|
|
|
2020-01-23 14:51:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CleanUp removes a TXT record used for DNS-01 challenge.
|
|
|
|
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
2023-03-07 08:39:05 +00:00
|
|
|
info := dns01.GetChallengeInfo(domain, keyAuth)
|
2020-01-23 14:51:47 +00:00
|
|
|
|
2021-04-28 14:49:05 +00:00
|
|
|
recordIdentifier := &scwdomain.RecordIdentifier{
|
2023-03-07 08:39:05 +00:00
|
|
|
Name: info.EffectiveFQDN,
|
2021-04-28 14:49:05 +00:00
|
|
|
Type: scwdomain.RecordTypeTXT,
|
2023-03-07 08:39:05 +00:00
|
|
|
Data: scw.StringPtr(fmt.Sprintf(`%q`, info.Value)),
|
2020-01-23 14:51:47 +00:00
|
|
|
}
|
|
|
|
|
2022-11-25 17:12:21 +00:00
|
|
|
// TODO(ldez) replace domain by FQDN to follow CNAME.
|
2021-04-28 14:49:05 +00:00
|
|
|
req := &scwdomain.UpdateDNSZoneRecordsRequest{
|
|
|
|
DNSZone: domain,
|
|
|
|
Changes: []*scwdomain.RecordChange{{
|
|
|
|
Delete: &scwdomain.RecordChangeDelete{IDFields: recordIdentifier},
|
|
|
|
}},
|
|
|
|
ReturnAllRecords: scw.BoolPtr(false),
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := d.client.UpdateDNSZoneRecords(req)
|
2020-01-23 14:51:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("scaleway: %w", err)
|
|
|
|
}
|
2021-04-28 14:49:05 +00:00
|
|
|
|
2020-01-23 14:51:47 +00:00
|
|
|
return nil
|
|
|
|
}
|