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

167 lines
4.7 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"
"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
)
// 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"
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
2020-01-23 16:51:47 +02:00
Token string
PropagationTimeout time.Duration
PollingInterval time.Duration
TTL int
}
// NewDefaultConfig returns a default configuration for the DNSProvider.
func NewDefaultConfig() *Config {
return &Config{
TTL: env.GetOrDefaultInt(EnvTTL, minTTL),
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, defaultPropagationTimeout),
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, defaultPollingInterval),
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.Get(EnvAPIToken)
2020-01-23 16:51:47 +02:00
if err != nil {
return nil, fmt.Errorf("scaleway: %w", err)
}
config := NewDefaultConfig()
config.Token = values[EnvAPIToken]
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("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 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"),
}}
2022-11-25 19:12:21 +02:00
// TODO(ldez) replace domain by FQDN to follow CNAME.
2021-04-28 16:49:05 +02:00
req := &scwdomain.UpdateDNSZoneRecordsRequest{
DNSZone: domain,
Changes: []*scwdomain.RecordChange{{
Add: &scwdomain.RecordChangeAdd{Records: records},
}},
ReturnAllRecords: scw.BoolPtr(false),
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
}
2022-11-25 19:12:21 +02:00
// TODO(ldez) replace domain by FQDN to follow CNAME.
2021-04-28 16:49:05 +02: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 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
}