This repository has been archived by the owner on Feb 5, 2020. It is now read-only.
/
aws_handlers.go
624 lines (546 loc) · 18.9 KB
/
aws_handlers.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
package server
import (
"crypto/rand"
"encoding/json"
"fmt"
"math/big"
"net"
"net/http"
"regexp"
"sort"
"strings"
"sync"
log "github.com/Sirupsen/logrus"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/kms"
"github.com/aws/aws-sdk-go/service/route53"
"github.com/aws/aws-sdk-go/service/route53domains"
"golang.org/x/net/context"
"github.com/aws/aws-sdk-go/service/sts"
"github.com/coreos/tectonic-installer/installer/server/aws/cloudforms"
"github.com/coreos/tectonic-installer/installer/server/ctxh"
)
type listItem struct {
Label string `json:"label"`
Value string `json:"value"`
}
// ListItems are a slice of listItems
type ListItems []listItem
func (slice ListItems) Len() int {
return len(slice)
}
func (slice ListItems) Less(i, j int) bool {
return slice[i].Label < slice[j].Label
}
func (slice ListItems) Swap(i, j int) {
slice[i], slice[j] = slice[j], slice[i]
}
type awsCredentials struct {
AccessKeyID string `json:"accessKeyId"`
SecretAccessKey string `json:"secretAccessKey"`
SessionToken string `json:"sessionToken"`
Region string `json:"region"`
}
// Characters that aren't allowed in AWS KMS aliases
var coerceKMSAlias = regexp.MustCompile("[^a-zA-Z0-9/_-]+")
// toAwsAppError returns an AWS-specific error along with AWS's error message
func toAwsAppError(err error) *ctxh.AppError {
// Generic AWS Error with Code, Message, and original error (if any)
if awsErr, ok := err.(awserr.Error); ok {
if reqErr, ok := err.(awserr.RequestFailure); ok {
return ctxh.NewAppError(err, awsErr.Message(), reqErr.StatusCode())
}
return ctxh.NewAppError(err, awsErr.Message(), http.StatusInternalServerError)
}
return ctxh.NewAppError(err, fmt.Sprintf("AWS API Error: %v", err), http.StatusInternalServerError)
}
// ec2FromRequest takes an http request and returns an ec2 session
func ec2FromRequest(req *http.Request) (*ec2.EC2, *ctxh.AppError) {
sess, err := awsSessionFromRequest(req)
if err != nil {
return nil, ctxh.NewAppError(err, "could not create AWS session", http.StatusInternalServerError)
}
return ec2.New(sess), nil
}
// awsGetDomainInfoHandler returns the SOA record for a given zone.
func awsGetDomainInfoHandler() ctxh.ContextHandler {
fn := func(ctx context.Context, w http.ResponseWriter, req *http.Request) *ctxh.AppError {
input := struct {
ID string `json:"id"`
Name string `json:"name"`
}{}
if err := json.NewDecoder(req.Body).Decode(&input); err != nil {
return ctxh.NewAppError(err, "could not unmarshall json", http.StatusBadRequest)
}
sess, err := awsSessionFromRequest(req)
if err != nil {
return ctxh.NewAppError(err, "could not create AWS session", http.StatusInternalServerError)
}
AccessKeyID := req.Header.Get("Tectonic-AccessKeyID")
SecretAccessKey := req.Header.Get("Tectonic-SecretAccessKey")
SessionToken := req.Header.Get("Tectonic-SessionToken")
// route53domains doesn't work in all regions. use us-east-1
// ignore error as usEast1Sess is only used by route53DomainsSvc
usEast1Sess, _ := getAWSSession(AccessKeyID, SecretAccessKey, SessionToken, "us-east-1")
domainInfo := struct {
Errors struct {
SOA string `json:"soa"`
Registered string `json:"registered"`
AWSNS string `json:"awsNS"`
PublicNS string `json:"publicNS"`
} `json:"errors"`
SoaTTL int64 `json:"soaTTL"`
SoaValue string `json:"soaValue"`
Registered string `json:"registered"`
AWSNS []string `json:"awsNS"`
PublicNS []string `json:"publicNS"`
}{}
route53Svc := route53.New(sess)
var wg sync.WaitGroup
wg.Add(4)
go func() {
defer wg.Done()
r53input := route53.ListResourceRecordSetsInput{
HostedZoneId: aws.String(input.ID),
StartRecordName: aws.String(input.Name),
StartRecordType: aws.String("SOA"),
MaxItems: aws.String("1"),
}
resp, err := route53Svc.ListResourceRecordSets(&r53input)
if err != nil {
domainInfo.Errors.SOA = err.Error()
return
}
if len(resp.ResourceRecordSets) == 1 {
domainInfo.SoaTTL = aws.Int64Value(resp.ResourceRecordSets[0].TTL)
domainInfo.SoaValue = aws.StringValue(resp.ResourceRecordSets[0].ResourceRecords[0].Value)
}
}()
go func() {
defer wg.Done()
r53input := route53.ListResourceRecordSetsInput{
HostedZoneId: aws.String(input.ID),
StartRecordName: aws.String(input.Name),
StartRecordType: aws.String("NS"),
MaxItems: aws.String("10"),
}
resp, err := route53Svc.ListResourceRecordSets(&r53input)
if err != nil {
domainInfo.Errors.AWSNS = err.Error()
return
}
if len(resp.ResourceRecordSets) > 0 {
domains := make([]string, len(resp.ResourceRecordSets[0].ResourceRecords))
for i, record := range resp.ResourceRecordSets[0].ResourceRecords {
domains[i] = aws.StringValue(record.Value)
}
sort.Strings(domains)
domainInfo.AWSNS = domains
}
}()
go func() {
defer wg.Done()
split := strings.Split(input.Name, ".")
if usEast1Sess == nil || len(split) < 2 {
domainInfo.Registered = route53domains.DomainAvailabilityDontKnow
return
}
domainName := strings.Join(split[len(split)-2:], ".")
route53DomainsSvc := route53domains.New(usEast1Sess)
availableResp, err := route53DomainsSvc.CheckDomainAvailability(&route53domains.CheckDomainAvailabilityInput{
DomainName: aws.String(domainName),
})
if err != nil {
domainInfo.Errors.Registered = err.Error()
return
}
domainInfo.Registered = aws.StringValue(availableResp.Availability)
}()
go func() {
defer wg.Done()
nsRecords, err := net.LookupNS(input.Name)
if err != nil {
domainInfo.Errors.PublicNS = err.Error()
return
}
domains := make([]string, len(nsRecords))
for i, ns := range nsRecords {
domains[i] = ns.Host
}
sort.Strings(domains)
domainInfo.PublicNS = domains
}()
wg.Wait()
writeJSONData(w, domainInfo)
return nil
}
return requireHTTPMethod("POST", ctxh.ContextHandlerFuncWithError(fn))
}
func awsGetZonesHandler() ctxh.ContextHandler {
fn := func(ctx context.Context, w http.ResponseWriter, req *http.Request) *ctxh.AppError {
sess, err := awsSessionFromRequest(req)
if err != nil {
return ctxh.NewAppError(err, "could not create AWS session", http.StatusInternalServerError)
}
route53Svc := route53.New(sess)
resp, err := route53Svc.ListHostedZones(&route53.ListHostedZonesInput{})
if err != nil {
return toAwsAppError(err)
}
keys := []listItem{}
for _, key := range resp.HostedZones {
// Strip trailing dot off domain names & add "(private)" to private zones
label := strings.TrimSuffix(aws.StringValue(key.Name), ".")
if aws.BoolValue(key.Config.PrivateZone) {
label = fmt.Sprintf("%s (private)", label)
}
keys = append(keys, listItem{
Label: label,
Value: aws.StringValue(key.Id),
})
}
writeJSONData(w, keys)
return nil
}
return requireHTTPMethod("POST", ctxh.ContextHandlerFuncWithError(fn))
}
// awsGetKeyPairsHandler responds with the list of AWS keypairs. An AWS Session
// is read from the context.
func awsGetKeyPairsHandler() ctxh.ContextHandler {
fn := func(ctx context.Context, w http.ResponseWriter, req *http.Request) *ctxh.AppError {
ec2Svc, appErr := ec2FromRequest(req)
if appErr != nil {
return appErr
}
resp, err := ec2Svc.DescribeKeyPairs(&ec2.DescribeKeyPairsInput{})
if err != nil {
return toAwsAppError(err)
}
keys := make(ListItems, len(resp.KeyPairs))
for i, keypair := range resp.KeyPairs {
keyName := aws.StringValue(keypair.KeyName)
keys[i] = listItem{
Label: keyName,
Value: keyName,
}
}
sort.Sort(keys)
writeJSONData(w, keys)
return nil
}
return requireHTTPMethod("POST", ctxh.ContextHandlerFuncWithError(fn))
}
// awsDescribeRegionsHandler responds with the list of AWS regions.
func awsDescribeRegionsHandler() ctxh.ContextHandler {
fn := func(ctx context.Context, w http.ResponseWriter, req *http.Request) *ctxh.AppError {
ec2Svc, appErr := ec2FromRequest(req)
if appErr != nil {
return appErr
}
resp, err := ec2Svc.DescribeRegions(&ec2.DescribeRegionsInput{})
if err != nil {
return toAwsAppError(err)
}
regions := make([]string, len(resp.Regions))
for i, region := range resp.Regions {
regions[i] = aws.StringValue(region.RegionName)
}
writeJSONData(w, regions)
return nil
}
return requireHTTPMethod("POST", ctxh.ContextHandlerFuncWithError(fn))
}
func formatKeyLabel(alias string, keyID string) string {
alias = strings.TrimPrefix(alias, "alias/")
if alias == "" {
return keyID
}
return fmt.Sprintf("%s - %s", alias, keyID)
}
// awsCreateKMSHandler creates a new KMS key.
func awsCreateKMSHandler() ctxh.ContextHandler {
fn := func(ctx context.Context, w http.ResponseWriter, req *http.Request) *ctxh.AppError {
sess, err := awsSessionFromRequest(req)
if err != nil {
return ctxh.NewAppError(err, "could not create AWS session", http.StatusInternalServerError)
}
kmsSvc := kms.New(sess)
// Create a new key.
keyOutput, err := kmsSvc.CreateKey(&kms.CreateKeyInput{
Description: aws.String("Tectonic installer"),
})
if err != nil {
return toAwsAppError(err)
}
// Generate intelligible name for the key.
// The name's format is <username>-tectonic/<random-number>.
// The random-number is used so multiple keys can be created by the same
// user.
num, err := rand.Int(rand.Reader, new(big.Int).SetInt64(10000))
if err != nil {
return ctxh.NewAppError(err, "could not create bigInt", http.StatusInternalServerError)
}
username, err := awsGetUsername(sess)
if err != nil {
return ctxh.NewAppError(err, "could not retrieve username", http.StatusInternalServerError)
}
username = coerceKMSAlias.ReplaceAllString(username, "")
alias := fmt.Sprintf("%v-tectonic/%v", username, num)
// Create an alias for the key using the name we just generated.
_, err = kmsSvc.CreateAlias(&kms.CreateAliasInput{
AliasName: aws.String("alias/" + alias),
TargetKeyId: keyOutput.KeyMetadata.KeyId,
})
if err != nil {
// Failed to create the alias, print the error and set the alias name to
// an empty string so it doesn't appear in the key label.
log.Errorf("failed to create alias: %v", err)
alias = ""
}
writeJSONData(w, listItem{
Label: formatKeyLabel(alias, *keyOutput.KeyMetadata.KeyId),
Value: *keyOutput.KeyMetadata.Arn,
})
return nil
}
return requireHTTPMethod("POST", ctxh.ContextHandlerFuncWithError(fn))
}
// awsGetKMSHandler responds with the list of AWS KMS keys.
func awsGetKMSHandler() ctxh.ContextHandler {
toLabel := func(aliases []*kms.AliasListEntry, keyID string) string {
for _, alias := range aliases {
if alias.TargetKeyId == nil {
continue
}
if *alias.TargetKeyId == keyID {
return formatKeyLabel(*alias.AliasName, keyID)
}
}
return keyID
}
fn := func(ctx context.Context, w http.ResponseWriter, req *http.Request) *ctxh.AppError {
sess, err := awsSessionFromRequest(req)
if err != nil {
return ctxh.NewAppError(err, "could not create AWS session", http.StatusInternalServerError)
}
kmsSvc := kms.New(sess)
output := struct {
keys *kms.ListKeysOutput
aliases *kms.ListAliasesOutput
keysErr error
aliasesErr error
}{}
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
output.keys, output.keysErr = kmsSvc.ListKeys(&kms.ListKeysInput{})
}()
go func() {
defer wg.Done()
output.aliases, output.aliasesErr = kmsSvc.ListAliases(&kms.ListAliasesInput{})
}()
// Wait for health checks to get responses or timeout
wg.Wait()
if output.aliasesErr != nil {
return toAwsAppError(output.aliasesErr)
}
if output.keysErr != nil {
return toAwsAppError(output.keysErr)
}
keys := make(ListItems, len(output.keys.Keys))
for i, key := range output.keys.Keys {
keys[i] = listItem{
Label: toLabel(output.aliases.Aliases, aws.StringValue(key.KeyId)),
Value: aws.StringValue(key.KeyArn),
}
}
sort.Sort(keys)
writeJSONData(w, keys)
return nil
}
return requireHTTPMethod("POST", ctxh.ContextHandlerFuncWithError(fn))
}
// awsGetVPCsHandler responds with the list of AWS VPC instances. An AWS
// Session is read from the context.
func awsGetVPCsHandler() ctxh.ContextHandler {
fn := func(ctx context.Context, w http.ResponseWriter, req *http.Request) *ctxh.AppError {
sess, err := awsSessionFromRequest(req)
if err != nil {
return ctxh.NewAppError(err, "could not create AWS session", http.StatusInternalServerError)
}
ec2Svc := ec2.New(sess)
resp, err := ec2Svc.DescribeVpcs(&ec2.DescribeVpcsInput{})
if err != nil {
return ctxh.NewAppError(err, "could not describe VPCs", http.StatusInternalServerError)
}
vpcs := make(ListItems, 0)
for _, vpc := range resp.Vpcs {
if vpc.VpcId == nil {
continue
}
name := ""
for _, tag := range vpc.Tags {
if *tag.Key == "Name" {
name = aws.StringValue(tag.Value)
break
}
}
vpcID := aws.StringValue(vpc.VpcId)
var label string
if name == "" {
label = vpcID
} else {
label = fmt.Sprintf("%s - %s", name, vpcID)
}
vpcs = append(vpcs, listItem{
Label: label,
Value: vpcID,
})
}
sort.Sort(vpcs)
writeJSONData(w, vpcs)
return nil
}
return requireHTTPMethod("POST", ctxh.ContextHandlerFuncWithError(fn))
}
func awsGetVPCsSubnetsHandler() ctxh.ContextHandler {
fn := func(ctx context.Context, w http.ResponseWriter, req *http.Request) *ctxh.AppError {
sess, err := awsSessionFromRequest(req)
if err != nil {
return ctxh.NewAppError(err, "could not create AWS session", http.StatusInternalServerError)
}
input := struct {
VpcID string `json:"vpcID"`
}{}
if err := json.NewDecoder(req.Body).Decode(&input); err != nil {
return ctxh.NewAppError(err, "could not unmarshal VPC ID", http.StatusBadRequest)
}
if len(input.VpcID) == 0 {
return ctxh.NewAppError(err, "need VPC ID", http.StatusBadRequest)
}
publicSubnets, privateSubnets, err := cloudforms.GetVPCSubnets(sess, input.VpcID)
if err != nil {
return ctxh.NewAppError(err, "could not get net slices", http.StatusBadRequest)
}
response := struct {
Public []cloudforms.VPCSubnet `json:"public"`
Private []cloudforms.VPCSubnet `json:"private"`
}{publicSubnets, privateSubnets}
writeJSONData(w, response)
return nil
}
return requireHTTPMethod("POST", ctxh.ContextHandlerFuncWithError(fn))
}
// awsDefaultSubnetsHandler responds with the default public/private subnets.
func awsDefaultSubnetsHandler() ctxh.ContextHandler {
fn := func(ctx context.Context, w http.ResponseWriter, req *http.Request) *ctxh.AppError {
sess, err := awsSessionFromRequest(req)
if err != nil {
return ctxh.NewAppError(err, "could not create AWS session", http.StatusInternalServerError)
}
vpcCIDR := struct {
VpcCIDR string `json:"vpcCIDR"`
}{}
if err := json.NewDecoder(req.Body).Decode(&vpcCIDR); err != nil {
return ctxh.NewAppError(err, "could not unmarshal VPC CIDR", http.StatusBadRequest)
}
publicSubnets, privateSubnets, err := cloudforms.GetDefaultSubnets(sess, vpcCIDR.VpcCIDR)
if err != nil {
return ctxh.NewAppError(err, "could not get net slices", http.StatusBadRequest)
}
response := struct {
Public []cloudforms.VPCSubnet `json:"public"`
Private []cloudforms.VPCSubnet `json:"private"`
}{publicSubnets, privateSubnets}
writeJSONData(w, response)
return nil
}
return requireHTTPMethod("POST", ctxh.ContextHandlerFuncWithError(fn))
}
// awsValidateSubnets checks VPC and Subnet choices.
func awsValidateSubnetsHandler() ctxh.ContextHandler {
fn := func(ctx context.Context, w http.ResponseWriter, req *http.Request) *ctxh.AppError {
input := struct {
VpcCIDR string `json:"vpcCIDR"`
PodCIDR string `json:"podCIDR"`
ServiceCIDR string `json:"serviceCIDR"`
PublicSubnets []cloudforms.VPCSubnet `json:"publicSubnets"`
PrivateSubnets []cloudforms.VPCSubnet `json:"privateSubnets"`
ExistingVPCID string `json:"awsVpcId"`
}{}
if err := json.NewDecoder(req.Body).Decode(&input); err != nil {
return ctxh.NewAppError(err, "could not unmarshal subnets", http.StatusBadRequest)
}
type Validation struct {
Message string `json:"message"`
Valid bool `json:"valid"`
}
if input.ExistingVPCID == "" {
// new VPC will be created
if err := cloudforms.ValidateSubnets(input.VpcCIDR, input.PublicSubnets); err != nil {
writeJSONData(w, Validation{err.Error(), false})
return nil
}
if err := cloudforms.ValidateSubnets(input.VpcCIDR, input.PrivateSubnets); err != nil {
writeJSONData(w, Validation{err.Error(), false})
return nil
}
if err := cloudforms.ValidateKubernetesCIDRs(input.VpcCIDR, input.PodCIDR, input.ServiceCIDR); err != nil {
writeJSONData(w, Validation{err.Error(), false})
return nil
}
} else {
// existing VPC will be used, check against it
sess, err := awsSessionFromRequest(req)
if err != nil {
return ctxh.NewAppError(err, "could not create AWS session", http.StatusInternalServerError)
}
err = cloudforms.CheckSubnetsAgainstExistingVPC(sess, input.ExistingVPCID, input.PublicSubnets, input.PrivateSubnets)
if err != nil {
writeJSONData(w, Validation{err.Error(), false})
return nil
}
err = cloudforms.CheckKubernetesCIDRs(sess, input.ExistingVPCID, input.PodCIDR, input.ServiceCIDR)
if err != nil {
writeJSONData(w, Validation{err.Error(), false})
return nil
}
}
writeJSONData(w, Validation{"", true})
return nil
}
return requireHTTPMethod("POST", ctxh.ContextHandlerFuncWithError(fn))
}
// awsSessionFromRequest creates an AWS Session from credentials in the POST Body.
func awsSessionFromRequest(req *http.Request) (*session.Session, error) {
AccessKeyID := req.Header.Get("Tectonic-AccessKeyID")
SecretAccessKey := req.Header.Get("Tectonic-SecretAccessKey")
SessionToken := req.Header.Get("Tectonic-SessionToken")
Region := req.Header.Get("Tectonic-Region")
return getAWSSession(AccessKeyID, SecretAccessKey, SessionToken, Region)
}
// awsGetUsername returns the username of the session.
func awsGetUsername(session *session.Session) (string, error) {
// Regardless of the credentials used by the session being a static
// AccessKeyID / SecretAccessKey or a temporary set using an STS Token,
// GetCallerIdentity always works, requires no permission and returns the ARN.
//
// The last part of the ARN contains the username, the role session name
// when STS is used, or the role name when STS is used and there was no role
// session name provided.
stsOutput, err := sts.New(session).GetCallerIdentity(&sts.GetCallerIdentityInput{})
if err != nil {
// Typically happens if the credentials are expired.
return "", err
}
arnS := strings.Split(*stsOutput.Arn, "/")
if len(arnS) == 0 {
// Should never happen, the ARN should always contain at least one '/'.
return "", fmt.Errorf("misformated ARN: %q", *stsOutput.Arn)
}
return arnS[len(arnS)-1], nil
}