-
Notifications
You must be signed in to change notification settings - Fork 82
/
key-pair.go
175 lines (158 loc) · 4.88 KB
/
key-pair.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
package eks
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/s3"
humanize "github.com/dustin/go-humanize"
"go.uber.org/zap"
)
// SECURITY NOTE: MAKE SURE PRIVATE KEY NEVER GETS UPLOADED TO CLOUD STORAGE AND DLETE AFTER USE!!!
func (ts *Tester) createKeyPair() (err error) {
if !ts.cfg.RemoteAccessKeyCreate {
ts.lg.Info("skipping creating EC2 key pair")
return nil
}
if ts.cfg.RemoteAccessKeyName == "" {
return errors.New("cannot create EC2 key pair without key name")
}
if ts.cfg.RemoteAccessPrivateKeyPath == "" {
return errors.New("cannot create EC2 key pair without private key path")
}
now := time.Now()
ts.lg.Info("creating EC2 key pair", zap.String("key-pair-name", ts.cfg.RemoteAccessKeyName))
var output *ec2.CreateKeyPairOutput
output, err = ts.ec2API.CreateKeyPair(&ec2.CreateKeyPairInput{
KeyName: aws.String(ts.cfg.RemoteAccessKeyName),
})
if err != nil {
return err
}
if aws.StringValue(output.KeyName) != ts.cfg.RemoteAccessKeyName {
return fmt.Errorf("unexpected key name %q, expected %q", aws.StringValue(output.KeyName), ts.cfg.RemoteAccessKeyName)
}
ts.lg.Info(
"created EC2 key pair",
zap.String("key-name", ts.cfg.RemoteAccessKeyName),
zap.String("started", humanize.RelTime(now, time.Now(), "ago", "from now")),
)
if err = os.MkdirAll(filepath.Dir(ts.cfg.RemoteAccessPrivateKeyPath), 0700); err != nil {
return err
}
if err = ioutil.WriteFile(
ts.cfg.RemoteAccessPrivateKeyPath,
[]byte(*output.KeyMaterial),
0400,
); err != nil {
return err
}
ts.lg.Info(
"wrote EC2 private key on disk",
zap.String("key-path", ts.cfg.RemoteAccessPrivateKeyPath),
)
if ts.cfg.S3BucketName != "" {
s3Key := path.Join(ts.cfg.Name, ts.cfg.RemoteAccessKeyName+".private.pem")
_, err = ts.s3API.PutObject(&s3.PutObjectInput{
Bucket: aws.String(ts.cfg.S3BucketName),
Key: aws.String(s3Key),
Body: strings.NewReader(aws.StringValue(output.KeyMaterial)),
// https://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html#canned-acl
// vs. "public-read"
ACL: aws.String("private"),
Metadata: map[string]*string{
"Kind": aws.String("aws-k8s-tester"),
},
})
if err == nil {
ts.lg.Info("uploaded EC2 private key",
zap.String("bucket", ts.cfg.S3BucketName),
zap.String("remote-path", s3Key),
)
} else {
ts.lg.Warn("failed to upload EC2 private key",
zap.String("bucket", ts.cfg.S3BucketName),
zap.String("remote-path", s3Key),
zap.Error(err),
)
}
} else {
ts.lg.Info("skipping EC2 private key uploads")
}
return err
}
func (ts *Tester) deleteKeyPair() error {
if !ts.cfg.RemoteAccessKeyCreate {
ts.lg.Info("skipping deleting EC2 key pair")
return nil
}
if ts.cfg.RemoteAccessKeyName == "" {
return errors.New("cannot delete EC2 key pair without key name")
}
err := os.RemoveAll(ts.cfg.RemoteAccessPrivateKeyPath)
ts.lg.Info("deleted EC2 private key on disk", zap.String("key-path", ts.cfg.RemoteAccessPrivateKeyPath))
ts.lg.Info("deleting EC2 key pair", zap.String("key-pair-name", ts.cfg.RemoteAccessKeyName))
_, err = ts.ec2API.DeleteKeyPair(&ec2.DeleteKeyPairInput{
KeyName: aws.String(ts.cfg.RemoteAccessKeyName),
})
if err != nil {
return err
}
time.Sleep(time.Second)
deleted := false
for i := 0; i < 10; i++ {
_, err = ts.ec2API.DescribeKeyPairs(&ec2.DescribeKeyPairsInput{
KeyNames: aws.StringSlice([]string{ts.cfg.RemoteAccessKeyName}),
})
if err == nil {
time.Sleep(3 * time.Second)
continue
}
if request.IsErrorRetryable(err) || request.IsErrorThrottle(err) {
ts.lg.Warn("failed to describe key pair, retrying...", zap.Error(err))
time.Sleep(5 * time.Second)
continue
}
// https://docs.aws.amazon.com/AWSEC2/latest/APIReference/errors-overview.html
awsErr, ok := err.(awserr.Error)
if ok && awsErr.Code() == "InvalidKeyPair.NotFound" {
deleted = true
break
}
}
if !deleted {
return fmt.Errorf("deleted key pair but %q still exists", ts.cfg.RemoteAccessKeyName)
}
ts.lg.Info("deleted EC2 key pair", zap.String("key-pair-name", ts.cfg.RemoteAccessKeyName))
if ts.cfg.S3BucketName != "" {
s3Key := path.Join(ts.cfg.Name, ts.cfg.RemoteAccessKeyName+".private.pem")
_, err = ts.s3API.DeleteObject(&s3.DeleteObjectInput{
Bucket: aws.String(ts.cfg.S3BucketName),
Key: aws.String(s3Key),
})
if err == nil {
ts.lg.Info("deleted EC2 private key in S3",
zap.String("bucket", ts.cfg.S3BucketName),
zap.String("remote-path", s3Key),
)
} else {
ts.lg.Warn("failed to delete EC2 private key in S3",
zap.String("bucket", ts.cfg.S3BucketName),
zap.String("remote-path", s3Key),
zap.Error(err),
)
return err
}
} else {
ts.lg.Info("skipping S3 EC2 private key clean-up")
}
return nil
}