vercel.go raw
1 // Package vercel implements a DNS provider for solving the DNS-01 challenge using Vercel DNS.
2 package vercel
3
4 import (
5 "context"
6 "errors"
7 "fmt"
8 "net/http"
9 "sync"
10 "time"
11
12 "github.com/go-acme/lego/v4/challenge"
13 "github.com/go-acme/lego/v4/challenge/dns01"
14 "github.com/go-acme/lego/v4/platform/config/env"
15 "github.com/go-acme/lego/v4/providers/dns/internal/clientdebug"
16 "github.com/go-acme/lego/v4/providers/dns/vercel/internal"
17 )
18
19 // Environment variables names.
20 const (
21 envNamespace = "VERCEL_"
22
23 EnvAuthToken = envNamespace + "API_TOKEN"
24 EnvTeamID = envNamespace + "TEAM_ID"
25
26 EnvTTL = envNamespace + "TTL"
27 EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
28 EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
29 EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT"
30 )
31
32 var _ challenge.ProviderTimeout = (*DNSProvider)(nil)
33
34 // Config is used to configure the creation of the DNSProvider.
35 type Config struct {
36 AuthToken string
37 TeamID string
38 TTL int
39 PropagationTimeout time.Duration
40 PollingInterval time.Duration
41 HTTPClient *http.Client
42 }
43
44 // NewDefaultConfig returns a default configuration for the DNSProvider.
45 func NewDefaultConfig() *Config {
46 return &Config{
47 TTL: env.GetOrDefaultInt(EnvTTL, 60),
48 PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, time.Minute),
49 PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 5*time.Second),
50 HTTPClient: &http.Client{
51 Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
52 },
53 }
54 }
55
56 // DNSProvider implements the challenge.Provider interface.
57 type DNSProvider struct {
58 config *Config
59 client *internal.Client
60
61 recordIDs map[string]string
62 recordIDsMu sync.Mutex
63 }
64
65 // NewDNSProvider returns a DNSProvider instance configured for Vercel.
66 // Credentials must be passed in the environment variables: VERCEL_API_TOKEN, VERCEL_TEAM_ID.
67 func NewDNSProvider() (*DNSProvider, error) {
68 values, err := env.Get(EnvAuthToken)
69 if err != nil {
70 return nil, fmt.Errorf("vercel: %w", err)
71 }
72
73 config := NewDefaultConfig()
74 config.AuthToken = values[EnvAuthToken]
75 config.TeamID = env.GetOrDefaultString(EnvTeamID, "")
76
77 return NewDNSProviderConfig(config)
78 }
79
80 // NewDNSProviderConfig return a DNSProvider instance configured for Digital Ocean.
81 func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
82 if config == nil {
83 return nil, errors.New("vercel: the configuration of the DNS provider is nil")
84 }
85
86 if config.AuthToken == "" {
87 return nil, errors.New("vercel: credentials missing")
88 }
89
90 client := internal.NewClient(
91 clientdebug.Wrap(
92 internal.OAuthStaticAccessToken(config.HTTPClient, config.AuthToken),
93 ),
94 config.TeamID,
95 )
96
97 return &DNSProvider{
98 config: config,
99 client: client,
100 recordIDs: make(map[string]string),
101 }, nil
102 }
103
104 // Timeout returns the timeout and interval to use when checking for DNS propagation.
105 // Adjusting here to cope with spikes in propagation times.
106 func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
107 return d.config.PropagationTimeout, d.config.PollingInterval
108 }
109
110 // Present creates a TXT record using the specified parameters.
111 func (d *DNSProvider) Present(domain, token, keyAuth string) error {
112 info := dns01.GetChallengeInfo(domain, keyAuth)
113
114 authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
115 if err != nil {
116 return fmt.Errorf("vercel: could not find zone for domain %q: %w", domain, err)
117 }
118
119 record := internal.Record{
120 Name: info.EffectiveFQDN,
121 Type: "TXT",
122 Value: info.Value,
123 TTL: d.config.TTL,
124 }
125
126 respData, err := d.client.CreateRecord(context.Background(), authZone, record)
127 if err != nil {
128 return fmt.Errorf("vercel: %w", err)
129 }
130
131 d.recordIDsMu.Lock()
132 d.recordIDs[token] = respData.UID
133 d.recordIDsMu.Unlock()
134
135 return nil
136 }
137
138 // CleanUp removes the TXT record matching the specified parameters.
139 func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
140 info := dns01.GetChallengeInfo(domain, keyAuth)
141
142 authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
143 if err != nil {
144 return fmt.Errorf("vercel: could not find zone for domain %q: %w", domain, err)
145 }
146
147 // get the record's unique ID from when we created it
148 d.recordIDsMu.Lock()
149 recordID, ok := d.recordIDs[token]
150 d.recordIDsMu.Unlock()
151
152 if !ok {
153 return fmt.Errorf("vercel: unknown record ID for '%s'", info.EffectiveFQDN)
154 }
155
156 err = d.client.DeleteRecord(context.Background(), authZone, recordID)
157 if err != nil {
158 return fmt.Errorf("vercel: %w", err)
159 }
160
161 // Delete record ID from map
162 d.recordIDsMu.Lock()
163 delete(d.recordIDs, token)
164 d.recordIDsMu.Unlock()
165
166 return nil
167 }
168