forked from go-acme/lego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scaleway.go
166 lines (136 loc) · 4.72 KB
/
scaleway.go
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
// 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"
"github.com/LukasDeco/lego/v4/challenge/dns01"
"github.com/LukasDeco/lego/v4/platform/config/env"
scwdomain "github.com/scaleway/scaleway-sdk-go/api/domain/v2beta1"
"github.com/scaleway/scaleway-sdk-go/scw"
)
const (
minTTL = 60
defaultPollingInterval = 10 * time.Second
defaultPropagationTimeout = 120 * time.Second
)
// Environment variables names.
const (
envNamespace = "SCALEWAY_"
EnvAPIToken = envNamespace + "API_TOKEN"
EnvProjectID = envNamespace + "PROJECT_ID"
EnvTTL = envNamespace + "TTL"
EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
)
// Config is used to configure the creation of the DNSProvider.
type Config struct {
ProjectID string
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),
}
}
// DNSProvider implements the challenge.Provider interface.
type DNSProvider struct {
config *Config
client *scwdomain.API
}
// NewDNSProvider returns a DNSProvider instance configured for Scaleway Domains API.
// Credentials must be passed in the environment variables:
// SCALEWAY_API_TOKEN, SCALEWAY_PROJECT_ID.
func NewDNSProvider() (*DNSProvider, error) {
values, err := env.Get(EnvAPIToken)
if err != nil {
return nil, fmt.Errorf("scaleway: %w", err)
}
config := NewDefaultConfig()
config.Token = values[EnvAPIToken]
config.ProjectID = env.GetOrFile(EnvProjectID)
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
}
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)
}
return &DNSProvider{config: config, client: scwdomain.NewAPI(clientScw)}, 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
}
// Present creates a TXT record to fulfill DNS-01 challenge.
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
info := dns01.GetChallengeInfo(domain, keyAuth)
records := []*scwdomain.Record{{
Data: fmt.Sprintf(`%q`, info.Value),
Name: info.EffectiveFQDN,
TTL: uint32(d.config.TTL),
Type: scwdomain.RecordTypeTXT,
Comment: scw.StringPtr("used by lego"),
}}
// TODO(ldez) replace domain by FQDN to follow CNAME.
req := &scwdomain.UpdateDNSZoneRecordsRequest{
DNSZone: domain,
Changes: []*scwdomain.RecordChange{{
Add: &scwdomain.RecordChangeAdd{Records: records},
}},
ReturnAllRecords: scw.BoolPtr(false),
}
_, err := d.client.UpdateDNSZoneRecords(req)
if err != nil {
return fmt.Errorf("scaleway: %w", err)
}
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)
recordIdentifier := &scwdomain.RecordIdentifier{
Name: info.EffectiveFQDN,
Type: scwdomain.RecordTypeTXT,
Data: scw.StringPtr(fmt.Sprintf(`%q`, info.Value)),
}
// TODO(ldez) replace domain by FQDN to follow CNAME.
req := &scwdomain.UpdateDNSZoneRecordsRequest{
DNSZone: domain,
Changes: []*scwdomain.RecordChange{{
Delete: &scwdomain.RecordChangeDelete{IDFields: recordIdentifier},
}},
ReturnAllRecords: scw.BoolPtr(false),
}
_, err := d.client.UpdateDNSZoneRecords(req)
if err != nil {
return fmt.Errorf("scaleway: %w", err)
}
return nil
}