-
Notifications
You must be signed in to change notification settings - Fork 0
/
vkcloud.go
240 lines (191 loc) · 6.34 KB
/
vkcloud.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Package vkcloud implements a DNS provider for solving the DNS-01 challenge using VK Cloud.
package vkcloud
import (
"errors"
"fmt"
"time"
"github.com/entrustcorporation/dv/dns01"
"github.com/go-acme/lego/v4/platform/config/env"
"github.com/entrustcorporation/dv/providers/vkcloud/internal"
"github.com/gophercloud/gophercloud"
)
const (
defaultIdentityEndpoint = "https://infra.mail.ru/identity/v3/"
defaultDNSEndpoint = "https://mcs.mail.ru/public-dns/v2/dns"
)
const defaultDomainName = "users"
// Environment variables names.
const (
envNamespace = "VK_CLOUD_"
EnvDNSEndpoint = envNamespace + "DNS_ENDPOINT"
EnvIdentityEndpoint = envNamespace + "IDENTITY_ENDPOINT"
EnvDomainName = envNamespace + "DOMAIN_NAME"
EnvProjectID = envNamespace + "PROJECT_ID"
EnvUsername = envNamespace + "USERNAME"
EnvPassword = envNamespace + "PASSWORD"
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
Username string
Password string
DNSEndpoint string
IdentityEndpoint string
DomainName 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, 60),
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
}
}
// DNSProvider implements the challenge.Provider interface.
type DNSProvider struct {
client *internal.Client
config *Config
}
// NewDNSProvider returns a DNSProvider instance configured for VK Cloud.
func NewDNSProvider() (*DNSProvider, error) {
values, err := env.Get(EnvProjectID, EnvUsername, EnvPassword)
if err != nil {
return nil, fmt.Errorf("vkcloud: %w", err)
}
config := NewDefaultConfig()
config.ProjectID = values[EnvProjectID]
config.Username = values[EnvUsername]
config.Password = values[EnvPassword]
config.IdentityEndpoint = env.GetOrDefaultString(EnvIdentityEndpoint, defaultIdentityEndpoint)
config.DomainName = env.GetOrDefaultString(EnvDomainName, defaultDomainName)
config.DNSEndpoint = env.GetOrDefaultString(EnvDNSEndpoint, defaultDNSEndpoint)
return NewDNSProviderConfig(config)
}
// NewDNSProviderConfig return a DNSProvider instance configured for VK Cloud.
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
if config == nil {
return nil, errors.New("vkcloud: the configuration of the DNS provider is nil")
}
if config.DNSEndpoint == "" {
return nil, fmt.Errorf("vkcloud: DNS endpoint is missing in config")
}
authOpts := gophercloud.AuthOptions{
IdentityEndpoint: config.IdentityEndpoint,
Username: config.Username,
Password: config.Password,
DomainName: config.DomainName,
TenantID: config.ProjectID,
}
client, err := internal.NewClient(config.DNSEndpoint, authOpts)
if err != nil {
return nil, fmt.Errorf("vkcloud: unable to build VK Cloud client: %w", err)
}
return &DNSProvider{
client: client,
config: config,
}, nil
}
// Present creates a TXT record to fulfill the dns-01 challenge.
func (r *DNSProvider) Present(domain, _, keyAuth string) error {
info := dns01.GetChallengeInfo(domain, keyAuth)
authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
if err != nil {
return fmt.Errorf("vkcloud: could not find zone for domain %q (%s): %w", domain, info.EffectiveFQDN, err)
}
authZone = dns01.UnFqdn(authZone)
zones, err := r.client.ListZones()
if err != nil {
return fmt.Errorf("vkcloud: unable to fetch dns zones: %w", err)
}
var zoneUUID string
for _, zone := range zones {
if zone.Zone == authZone {
zoneUUID = zone.UUID
}
}
if zoneUUID == "" {
return fmt.Errorf("vkcloud: cant find dns zone %s in VK Cloud", authZone)
}
subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, authZone)
if err != nil {
return fmt.Errorf("vkcloud: %w", err)
}
err = r.upsertTXTRecord(zoneUUID, subDomain, info.Value)
if err != nil {
return fmt.Errorf("vkcloud: %w", err)
}
return nil
}
// CleanUp removes the TXT record matching the specified parameters.
func (r *DNSProvider) CleanUp(domain, _, keyAuth string) error {
info := dns01.GetChallengeInfo(domain, keyAuth)
authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
if err != nil {
return fmt.Errorf("vkcloud: could not find zone for domain %q (%s): %w", domain, info.EffectiveFQDN, err)
}
authZone = dns01.UnFqdn(authZone)
zones, err := r.client.ListZones()
if err != nil {
return fmt.Errorf("vkcloud: unable to fetch dns zones: %w", err)
}
var zoneUUID string
for _, zone := range zones {
if zone.Zone == authZone {
zoneUUID = zone.UUID
}
}
if zoneUUID == "" {
return nil
}
subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, authZone)
if err != nil {
return fmt.Errorf("vkcloud: %w", err)
}
err = r.removeTXTRecord(zoneUUID, subDomain, info.Value)
if err != nil {
return fmt.Errorf("vkcloud: %w", err)
}
return nil
}
// Timeout returns the timeout and interval to use when checking for DNS propagation.
// Adjusting here to cope with spikes in propagation times.
func (r *DNSProvider) Timeout() (timeout, interval time.Duration) {
return r.config.PropagationTimeout, r.config.PollingInterval
}
func (r *DNSProvider) upsertTXTRecord(zoneUUID, name, value string) error {
records, err := r.client.ListTXTRecords(zoneUUID)
if err != nil {
return err
}
for _, record := range records {
if record.Name == name && record.Content == value {
// The DNSRecord is already present, nothing to do
return nil
}
}
return r.client.CreateTXTRecord(zoneUUID, &internal.DNSTXTRecord{
Name: name,
Content: value,
TTL: r.config.TTL,
})
}
func (r *DNSProvider) removeTXTRecord(zoneUUID, name, value string) error {
records, err := r.client.ListTXTRecords(zoneUUID)
if err != nil {
return err
}
name = dns01.UnFqdn(name)
for _, record := range records {
if record.Name == name && record.Content == value {
return r.client.DeleteTXTRecord(zoneUUID, record.UUID)
}
}
// The DNSRecord is not present, nothing to do
return nil
}