/
route53.go
305 lines (262 loc) · 9.69 KB
/
route53.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
300
301
302
303
304
305
// +skip_license_check
/*
This file contains portions of code directly taken from the 'xenolf/lego' project.
A copy of the license for this code can be found in the file named LICENSE in
this directory.
*/
// Package route53 implements a DNS provider for solving the DNS-01 challenge
// using AWS Route 53 DNS.
package route53
import (
"fmt"
"strings"
"time"
logf "github.com/jetstack/cert-manager/pkg/logs"
"github.com/go-logr/logr"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/route53"
"github.com/aws/aws-sdk-go/service/sts"
"github.com/aws/aws-sdk-go/service/sts/stsiface"
"github.com/jetstack/cert-manager/pkg/issuer/acme/dns/util"
pkgutil "github.com/jetstack/cert-manager/pkg/util"
)
const (
maxRetries = 5
route53TTL = 10
)
// DNSProvider implements the util.ChallengeProvider interface
type DNSProvider struct {
dns01Nameservers []string
client *route53.Route53
hostedZoneID string
log logr.Logger
}
type sessionProvider struct {
AccessKeyID string
SecretAccessKey string
Ambient bool
Region string
Role string
StsProvider func(*session.Session) stsiface.STSAPI
log logr.Logger
}
func (d *sessionProvider) GetSession() (*session.Session, error) {
if d.AccessKeyID == "" && d.SecretAccessKey == "" {
if !d.Ambient {
return nil, fmt.Errorf("unable to construct route53 provider: empty credentials; perhaps you meant to enable ambient credentials?")
}
} else if d.AccessKeyID == "" || d.SecretAccessKey == "" {
// It's always an error to set one of those but not the other
return nil, fmt.Errorf("unable to construct route53 provider: only one of access and secret key was provided")
}
useAmbientCredentials := d.Ambient && (d.AccessKeyID == "" && d.SecretAccessKey == "")
config := aws.NewConfig()
sessionOpts := session.Options{
Config: *config,
}
if useAmbientCredentials {
d.log.V(logf.DebugLevel).Info("using ambient credentials")
// Leaving credentials unset results in a default credential chain being
// used; this chain is a reasonable default for getting ambient creds.
// https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/configuring-sdk.html#specifying-credentials
} else {
d.log.V(logf.DebugLevel).Info("not using ambient credentials")
sessionOpts.Config.Credentials = credentials.NewStaticCredentials(d.AccessKeyID, d.SecretAccessKey, "")
// also disable 'ambient' region sources
sessionOpts.SharedConfigState = session.SharedConfigDisable
}
sess, err := session.NewSessionWithOptions(sessionOpts)
if err != nil {
return nil, fmt.Errorf("unable to create aws session: %s", err)
}
if d.Role != "" {
d.log.V(logf.DebugLevel).WithValues("role", d.Role).Info("assuming role")
stsSvc := d.StsProvider(sess)
result, err := stsSvc.AssumeRole(&sts.AssumeRoleInput{
RoleArn: aws.String(d.Role),
RoleSessionName: aws.String("cert-manager"),
})
if err != nil {
return nil, fmt.Errorf("unable to assume role: %s", err)
}
creds := credentials.Value{
AccessKeyID: *result.Credentials.AccessKeyId,
SecretAccessKey: *result.Credentials.SecretAccessKey,
SessionToken: *result.Credentials.SessionToken,
}
sessionOpts.Config.Credentials = credentials.NewStaticCredentialsFromCreds(creds)
sess, err = session.NewSessionWithOptions(sessionOpts)
if err != nil {
return nil, fmt.Errorf("unable to create aws session: %s", err)
}
}
// If ambient credentials aren't permitted, always set the region, even if to
// empty string, to avoid it falling back on the environment.
// this has to be set after session is constructed
if d.Region != "" || !useAmbientCredentials {
sess.Config.WithRegion(d.Region)
}
sess.Handlers.Build.PushBack(request.WithAppendUserAgent(pkgutil.CertManagerUserAgent))
return sess, nil
}
func newSessionProvider(accessKeyID, secretAccessKey, region, role string, ambient bool) (*sessionProvider, error) {
return &sessionProvider{
AccessKeyID: accessKeyID,
SecretAccessKey: secretAccessKey,
Ambient: ambient,
Region: region,
Role: role,
StsProvider: defaultSTSProvider,
log: logf.Log.WithName("route53-session-provider"),
}, nil
}
func defaultSTSProvider(sess *session.Session) stsiface.STSAPI {
return sts.New(sess)
}
// NewDNSProvider returns a DNSProvider instance configured for the AWS
// Route 53 service using static credentials from its parameters or, if they're
// unset and the 'ambient' option is set, credentials from the environment.
func NewDNSProvider(accessKeyID, secretAccessKey, hostedZoneID, region, role string, ambient bool, dns01Nameservers []string) (*DNSProvider, error) {
provider, err := newSessionProvider(accessKeyID, secretAccessKey, region, role, ambient)
if err != nil {
return nil, err
}
sess, err := provider.GetSession()
if err != nil {
return nil, err
}
client := route53.New(sess)
return &DNSProvider{
client: client,
hostedZoneID: hostedZoneID,
dns01Nameservers: dns01Nameservers,
log: logf.Log.WithName("route53"),
}, nil
}
// Present creates a TXT record using the specified parameters
func (r *DNSProvider) Present(domain, fqdn, value string) error {
value = `"` + value + `"`
return r.changeRecord(route53.ChangeActionUpsert, fqdn, value, route53TTL)
}
// CleanUp removes the TXT record matching the specified parameters
func (r *DNSProvider) CleanUp(domain, fqdn, value string) error {
value = `"` + value + `"`
return r.changeRecord(route53.ChangeActionDelete, fqdn, value, route53TTL)
}
func (r *DNSProvider) changeRecord(action, fqdn, value string, ttl int) error {
hostedZoneID, err := r.getHostedZoneID(fqdn)
if err != nil {
return fmt.Errorf("Failed to determine Route 53 hosted zone ID: %v", err)
}
recordSet := newTXTRecordSet(fqdn, value, ttl)
reqParams := &route53.ChangeResourceRecordSetsInput{
HostedZoneId: aws.String(hostedZoneID),
ChangeBatch: &route53.ChangeBatch{
Comment: aws.String("Managed by cert-manager"),
Changes: []*route53.Change{
{
Action: &action,
ResourceRecordSet: recordSet,
},
},
},
}
resp, err := r.client.ChangeResourceRecordSets(reqParams)
if err != nil {
if awserr, ok := err.(awserr.Error); ok {
if action == route53.ChangeActionDelete && awserr.Code() == route53.ErrCodeInvalidChangeBatch {
r.log.V(logf.DebugLevel).WithValues("error", err).Info("ignoring InvalidChangeBatch error")
// If we try to delete something and get a 'InvalidChangeBatch' that
// means it's already deleted, no need to consider it an error.
return nil
}
}
return fmt.Errorf("Failed to change Route 53 record set: %v", removeReqID(err))
}
statusID := resp.ChangeInfo.Id
return util.WaitFor(120*time.Second, 4*time.Second, func() (bool, error) {
reqParams := &route53.GetChangeInput{
Id: statusID,
}
resp, err := r.client.GetChange(reqParams)
if err != nil {
return false, fmt.Errorf("Failed to query Route 53 change status: %v", removeReqID(err))
}
if *resp.ChangeInfo.Status == route53.ChangeStatusInsync {
return true, nil
}
return false, nil
})
}
func (r *DNSProvider) getHostedZoneID(fqdn string) (string, error) {
if r.hostedZoneID != "" {
return r.hostedZoneID, nil
}
authZone, err := util.FindZoneByFqdn(fqdn, r.dns01Nameservers)
if err != nil {
return "", fmt.Errorf("error finding zone from fqdn: %v", err)
}
// .DNSName should not have a trailing dot
reqParams := &route53.ListHostedZonesByNameInput{
DNSName: aws.String(util.UnFqdn(authZone)),
}
resp, err := r.client.ListHostedZonesByName(reqParams)
if err != nil {
return "", removeReqID(err)
}
zoneToID := make(map[string]string)
var hostedZones []string
for _, hostedZone := range resp.HostedZones {
// .Name has a trailing dot
if !*hostedZone.Config.PrivateZone {
zoneToID[*hostedZone.Name] = *hostedZone.Id
hostedZones = append(hostedZones, *hostedZone.Name)
}
}
authZone, err = util.FindBestMatch(fqdn, hostedZones...)
if err != nil {
return "", fmt.Errorf("Zone %s not found in Route 53 for domain %s", authZone, fqdn)
}
hostedZoneID, ok := zoneToID[authZone]
if len(hostedZoneID) == 0 || !ok {
return "", fmt.Errorf("Zone %s not found in Route 53 for domain %s", authZone, fqdn)
}
if strings.HasPrefix(hostedZoneID, "/hostedzone/") {
hostedZoneID = strings.TrimPrefix(hostedZoneID, "/hostedzone/")
}
return hostedZoneID, nil
}
func newTXTRecordSet(fqdn, value string, ttl int) *route53.ResourceRecordSet {
return &route53.ResourceRecordSet{
Name: aws.String(fqdn),
Type: aws.String(route53.RRTypeTxt),
TTL: aws.Int64(int64(ttl)),
ResourceRecords: []*route53.ResourceRecord{
{Value: aws.String(value)},
},
}
}
// The aws-sdk-go library appends a request id to its error messages. We
// want our error messages to be the same when the cause is the same to
// avoid spurious challenge updates.
//
// The given error must not be nil. This function must be called everywhere
// we have a non-nil error coming from an aws-sdk-go func.
func removeReqID(err error) error {
// NOTE(mael): I first tried to unwrap the RequestFailure to get rid of
// this request id. But the concrete type requestFailure is private, so
// I can't unwrap it. Instead, I recreate a new awserr.baseError. It's
// also a awserr.Error except it doesn't have the request id.
//
// Also note that we do not give the origErr to awserr.New. If we did,
// err.Error() would show the origErr, which we don't want since it
// contains a request id.
if e, ok := err.(awserr.RequestFailure); ok {
return awserr.New(e.Code(), e.Message(), nil)
}
return err
}