-
Notifications
You must be signed in to change notification settings - Fork 74
/
certificate_challenges.go
299 lines (252 loc) · 8.37 KB
/
certificate_challenges.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package acme
import (
"fmt"
"strconv"
"time"
"github.com/go-acme/lego/v4/challenge"
"github.com/go-acme/lego/v4/challenge/dns01"
"github.com/go-acme/lego/v4/challenge/http01"
"github.com/go-acme/lego/v4/challenge/tlsalpn01"
"github.com/go-acme/lego/v4/lego"
"github.com/go-acme/lego/v4/providers/http/memcached"
"github.com/go-acme/lego/v4/providers/http/s3"
"github.com/go-acme/lego/v4/providers/http/webroot"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/vancluever/terraform-provider-acme/v2/acme/dnsplugin"
)
// setCertificateChallengeProviders sets all of the challenge providers in the
// client that are needed for obtaining the certificate.
//
// The returned func() is a closer for all of the configured DNS providers that
// should be called when they are no longer needed (i.e. in a defer after one of
// the CRUD functions are complete).
func setCertificateChallengeProviders(client *lego.Client, d *schema.ResourceData) (func(), error) {
// DNS
var dnsClosers []func()
dnsCloser := func() {
for _, f := range dnsClosers {
f()
}
}
if providers, ok := d.GetOk("dns_challenge"); ok {
var providerWrapper challenge.Provider
var err error
providerWrapper, dnsClosers, err = expandDNSChallengeWrapperProvider(d, providers.([]interface{}))
if err != nil {
return dnsCloser, err
}
if err := client.Challenge.SetDNS01Provider(
providerWrapper,
expandDNSChallengeOptions(d)...,
); err != nil {
return dnsCloser, err
}
}
// HTTP (server)
if provider, ok := d.GetOk("http_challenge"); ok {
opts := provider.([]interface{})[0].(map[string]interface{})
httpServerProvider := http01.NewProviderServer("", strconv.Itoa(opts["port"].(int)))
if proxyHeader, ok := opts["proxy_header"]; ok {
httpServerProvider.SetProxyHeader(proxyHeader.(string))
}
if err := client.Challenge.SetHTTP01Provider(httpServerProvider); err != nil {
return dnsCloser, err
}
}
// HTTP (webroot)
if provider, ok := d.GetOk("http_webroot_challenge"); ok {
httpWebrootProvider, err := webroot.NewHTTPProvider(
provider.([]interface{})[0].(map[string]interface{})["directory"].(string))
if err != nil {
return dnsCloser, err
}
if err := client.Challenge.SetHTTP01Provider(httpWebrootProvider); err != nil {
return dnsCloser, err
}
}
// HTTP (memcached)
if provider, ok := d.GetOk("http_memcached_challenge"); ok {
httpMemcachedProvider, err := memcached.NewMemcachedProvider(
stringSlice(provider.([]interface{})[0].(map[string]interface{})["hosts"].(*schema.Set).List()))
if err != nil {
return dnsCloser, err
}
if err := client.Challenge.SetHTTP01Provider(httpMemcachedProvider); err != nil {
return dnsCloser, err
}
}
// HTTP (s3)
if provider, ok := d.GetOk("http_s3_challenge"); ok {
httpS3Provider, err := s3.NewHTTPProvider(
provider.([]interface{})[0].(map[string]interface{})["s3_bucket"].(string))
if err != nil {
return dnsCloser, err
}
if err := client.Challenge.SetHTTP01Provider(httpS3Provider); err != nil {
return dnsCloser, err
}
}
// TLS
if provider, ok := d.GetOk("tls_challenge"); ok {
tlsProvider := tlsalpn01.NewProviderServer(
"", strconv.Itoa(provider.([]interface{})[0].(map[string]interface{})["port"].(int)))
if err := client.Challenge.SetTLSALPN01Provider(tlsProvider); err != nil {
return dnsCloser, err
}
}
return dnsCloser, nil
}
func expandDNSChallengeWrapperProvider(
d *schema.ResourceData,
providers []interface{},
) (challenge.Provider, []func(), error) {
dnsClosers := make([]func(), 0)
dnsProvider, err := NewDNSProviderWrapper()
if err != nil {
return nil, nil, err
}
var isSequential bool
var sequentialInterval time.Duration
for _, providerRaw := range providers {
if result, err := expandDNSChallenge(
providerRaw.(map[string]interface{}),
expandRecursiveNameservers(d),
); err == nil {
dnsProvider.providers = append(dnsProvider.providers, result.Provider)
dnsClosers = append(dnsClosers, result.Closer)
if result.IsSequential {
isSequential = true
}
if result.SequentialInterval > sequentialInterval {
sequentialInterval = result.SequentialInterval
}
} else {
return nil, nil, err
}
}
if isSequential {
// Is our provider set sequential? If so, convert this to a sequential wrapper
return dnsProvider.ToSequential(sequentialInterval), dnsClosers, nil
}
// Otherwise, return as the regular wrapper
return dnsProvider, dnsClosers, nil
}
func expandDNSChallenge(m map[string]interface{}, nameServers []string) (dnsplugin.NewClientResult, error) {
var providerName string
if v, ok := m["provider"]; ok && v.(string) != "" {
providerName = v.(string)
} else {
return dnsplugin.NewClientResult{}, fmt.Errorf("DNS challenge provider not defined")
}
// Config only needs to be set if it's defined, otherwise existing env/SDK
// defaults are fine.
config := make(map[string]string)
if v, ok := m["config"]; ok {
for k, v := range v.(map[string]interface{}) {
config[k] = v.(string)
}
}
return dnsplugin.NewClient(providerName, config, nameServers)
}
func expandDNSChallengeOptions(d *schema.ResourceData) []dns01.ChallengeOption {
var opts []dns01.ChallengeOption
if nameservers := expandRecursiveNameservers(d); len(nameservers) > 0 {
opts = append(opts, dns01.AddRecursiveNameservers(nameservers))
}
if d.Get("disable_complete_propagation").(bool) {
opts = append(opts, dns01.DisableCompletePropagationRequirement())
}
if preCheckDelay := d.Get("pre_check_delay").(int); preCheckDelay > 0 {
opts = append(opts, dns01.WrapPreCheck(resourceACMECertificatePreCheckDelay(preCheckDelay)))
}
return opts
}
func expandRecursiveNameservers(d *schema.ResourceData) []string {
s := make([]string, 0)
if nameservers := d.Get("recursive_nameservers").([]interface{}); len(nameservers) > 0 {
for _, ns := range nameservers {
s = append(s, ns.(string))
}
}
return s
}
// DNSProviderWrapper is a multi-provider wrapper to support multiple
// DNS challenges.
type DNSProviderWrapper struct {
providers []challenge.ProviderTimeout
}
// NewDNSProviderWrapper returns an freshly initialized
// DNSProviderWrapper.
func NewDNSProviderWrapper() (*DNSProviderWrapper, error) {
return &DNSProviderWrapper{}, nil
}
// Present implements challenge.Provider for DNSProviderWrapper.
func (d *DNSProviderWrapper) Present(domain, token, keyAuth string) error {
var err error
for _, p := range d.providers {
err = p.Present(domain, token, keyAuth)
if err != nil {
err = multierror.Append(err, fmt.Errorf("error encountered while presenting token for DNS challenge: %s", err.Error()))
}
}
return err
}
// CleanUp implements challenge.Provider for DNSProviderWrapper.
func (d *DNSProviderWrapper) CleanUp(domain, token, keyAuth string) error {
var err error
for _, p := range d.providers {
err = p.CleanUp(domain, token, keyAuth)
if err != nil {
err = multierror.Append(err, fmt.Errorf("error encountered while cleaning token for DNS challenge: %s", err.Error()))
}
}
return err
}
// Timeout implements challenge.ProviderTimeout for
// DNSProviderWrapper.
//
// The highest polling interval and timeout values defined across all
// providers is used.
func (d *DNSProviderWrapper) Timeout() (time.Duration, time.Duration) {
var timeout, interval time.Duration
for _, p := range d.providers {
t, i := p.Timeout()
if t > timeout {
timeout = t
}
if i > interval {
interval = i
}
}
if timeout < 1 {
timeout = dns01.DefaultPropagationTimeout
}
if interval < 1 {
interval = dns01.DefaultPollingInterval
}
return timeout, interval
}
// DNSProviderWrapperSequential is a multi-provider wrapper to support multiple
// DNS challenges.
//
// This wrapper is used whenever there is a sequential provider in the provider
// set.
type DNSProviderWrapperSequential struct {
*DNSProviderWrapper
interval time.Duration
}
// ToSequential converts the DNS provider wrapper to a sequential one with the
// interval passed in.
func (d *DNSProviderWrapper) ToSequential(interval time.Duration) *DNSProviderWrapperSequential {
return &DNSProviderWrapperSequential{
DNSProviderWrapper: d,
interval: interval,
}
}
// Sequential implements the internal sequential interfaces that lego needs to
// be able to probe for a sequential provider. This returns the pre-probed
// duration.
func (d *DNSProviderWrapperSequential) Sequential() time.Duration {
return d.interval
}