forked from wallix/awless
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver_funcs.go
455 lines (398 loc) · 13.1 KB
/
driver_funcs.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
/*
Copyright 2017 WALLIX
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package aws
import (
"errors"
"fmt"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/wallix/awless/config"
"github.com/wallix/awless/console"
"github.com/wallix/awless/graph"
)
func (d *AwsDriver) Check_Instance_DryRun(params map[string]interface{}) (interface{}, error) {
input := &ec2.DescribeInstancesInput{}
input.DryRun = aws.Bool(true)
for _, val := range []string{"state", "id", "timeout"} {
if _, ok := params[val]; !ok {
err := fmt.Errorf("check instance error: missing required param '%s'", val)
d.logger.Errorf("%s", err)
return nil, err
}
}
if _, ok := params["timeout"].(int); !ok {
err := errors.New("check instance error: timeout param is not int")
d.logger.Errorf("%s", err)
return nil, err
}
// Required params
setField(params["id"], input, "InstanceIds")
_, err := d.ec2.DescribeInstances(input)
if awsErr, ok := err.(awserr.Error); ok {
switch code := awsErr.Code(); {
case code == "DryRunOperation", strings.HasSuffix(code, "NotFound"):
id := fakeDryRunId("instance")
d.logger.Verbose("full dry run: check instance ok")
return id, nil
}
}
d.logger.Errorf("dry run: check instance error: %s", err)
return nil, err
}
func (d *AwsDriver) Check_Instance(params map[string]interface{}) (interface{}, error) {
input := &ec2.DescribeInstancesInput{}
// Required params
setField(params["id"], input, "InstanceIds")
timeout := time.Duration(params["timeout"].(int)) * time.Second
timer := time.NewTimer(timeout)
for {
select {
case <-time.After(1 * time.Second):
output, err := d.ec2.DescribeInstances(input)
if err != nil {
d.logger.Errorf("check instance error: %s", err)
return nil, err
}
if res := output.Reservations; len(res) > 0 {
if instances := output.Reservations[0].Instances; len(instances) > 0 {
for _, inst := range instances {
if aws.StringValue(inst.InstanceId) == params["id"] {
if aws.StringValue(inst.State.Name) == params["state"] {
d.logger.Verbose("check instance status '%s' done", params["state"])
return nil, nil
}
}
}
}
}
case <-timer.C:
err := fmt.Errorf("timeout of %s expired", timeout)
d.logger.Errorf("%s", err)
return nil, err
}
}
d.logger.Verbose("check instance done")
return nil, nil
}
func (d *AwsDriver) Create_Tags_DryRun(params map[string]interface{}) (interface{}, error) {
input := &ec2.CreateTagsInput{}
input.DryRun = aws.Bool(true)
input.Resources = append(input.Resources, aws.String(fmt.Sprint(params["resource"])))
for k, v := range params {
if k == "resource" {
continue
}
input.Tags = append(input.Tags, &ec2.Tag{Key: aws.String(k), Value: aws.String(fmt.Sprint(v))})
}
_, err := d.ec2.CreateTags(input)
if awsErr, ok := err.(awserr.Error); ok {
switch code := awsErr.Code(); {
case code == "DryRunOperation", strings.HasSuffix(code, "NotFound"):
d.logger.Verbose("full dry run: create tags ok")
return nil, nil
}
}
d.logger.Errorf("dry run: create tags error: %s", err)
return nil, err
}
func (d *AwsDriver) Create_Tags(params map[string]interface{}) (interface{}, error) {
input := &ec2.CreateTagsInput{}
input.Resources = append(input.Resources, aws.String(fmt.Sprint(params["resource"])))
for k, v := range params {
if k == "resource" {
continue
}
input.Tags = append(input.Tags, &ec2.Tag{Key: aws.String(k), Value: aws.String(fmt.Sprint(v))})
}
_, err := d.ec2.CreateTags(input)
if err != nil {
d.logger.Errorf("create tags error: %s", err)
return nil, err
}
d.logger.Verbose("create tags done")
return nil, nil
}
func (d *AwsDriver) Create_Keypair_DryRun(params map[string]interface{}) (interface{}, error) {
input := &ec2.ImportKeyPairInput{}
input.DryRun = aws.Bool(true)
setField(params["name"], input, "KeyName")
if params["name"] == "" {
err := fmt.Errorf("empty 'name' parameter")
d.logger.Errorf("dry run: saving private key error: %s", err)
return nil, err
}
privKeyPath := filepath.Join(config.KeysDir, fmt.Sprint(params["name"])+".pem")
_, err := os.Stat(privKeyPath)
if err == nil {
fileExist := fmt.Errorf("file already exists at path: %s", privKeyPath)
d.logger.Errorf("dry run: saving private key error: %s", fileExist)
return nil, fileExist
}
return nil, nil
}
func (d *AwsDriver) Create_Keypair(params map[string]interface{}) (interface{}, error) {
input := &ec2.ImportKeyPairInput{}
setField(params["name"], input, "KeyName")
d.logger.Info("Generating locally a RSA 4096 bits keypair...")
pub, priv, err := console.GenerateSSHKeyPair(4096)
if err != nil {
d.logger.Errorf("generating keypair error: %s", err)
return nil, err
}
privKeyPath := filepath.Join(config.KeysDir, fmt.Sprint(params["name"])+".pem")
_, err = os.Stat(privKeyPath)
if err == nil {
fileExist := fmt.Errorf("file already exists at path: %s", privKeyPath)
d.logger.Errorf("saving private key error: %s", fileExist)
return nil, fileExist
}
err = ioutil.WriteFile(privKeyPath, priv, 0400)
if err != nil {
d.logger.Errorf("saving private key error: %s", err)
return nil, err
}
fmt.Printf("4096 RSA keypair generated locally and stored in '%s'\n", privKeyPath)
input.PublicKeyMaterial = pub
output, err := d.ec2.ImportKeyPair(input)
if err != nil {
d.logger.Errorf("create keypair error: %s", err)
return nil, err
}
id := aws.StringValue(output.KeyName)
d.logger.Infof("create keypair '%s' done", id)
return aws.StringValue(output.KeyName), nil
}
func (d *AwsDriver) Update_Securitygroup_DryRun(params map[string]interface{}) (interface{}, error) {
ipPerms, err := buildIpPermissionsFromParams(params)
if err != nil {
return nil, err
}
var input interface{}
if action, ok := params["inbound"].(string); ok {
switch action {
case "authorize":
input = &ec2.AuthorizeSecurityGroupIngressInput{DryRun: aws.Bool(true), IpPermissions: ipPerms}
case "revoke":
input = &ec2.RevokeSecurityGroupIngressInput{DryRun: aws.Bool(true), IpPermissions: ipPerms}
default:
return nil, fmt.Errorf("'inbound' parameter expect 'authorize' or 'revoke', got %s", action)
}
}
if action, ok := params["outbound"].(string); ok {
switch action {
case "authorize":
input = &ec2.AuthorizeSecurityGroupEgressInput{DryRun: aws.Bool(true), IpPermissions: ipPerms}
case "revoke":
input = &ec2.RevokeSecurityGroupEgressInput{DryRun: aws.Bool(true), IpPermissions: ipPerms}
default:
return nil, fmt.Errorf("'outbound' parameter expect 'authorize' or 'revoke', got %s", action)
}
}
if input == nil {
return nil, fmt.Errorf("expect either 'inbound' or 'outbound' parameter")
}
// Required params
setField(params["id"], input, "GroupId")
switch ii := input.(type) {
case *ec2.AuthorizeSecurityGroupIngressInput:
_, err = d.ec2.AuthorizeSecurityGroupIngress(ii)
case *ec2.RevokeSecurityGroupIngressInput:
_, err = d.ec2.RevokeSecurityGroupIngress(ii)
case *ec2.AuthorizeSecurityGroupEgressInput:
_, err = d.ec2.AuthorizeSecurityGroupEgress(ii)
case *ec2.RevokeSecurityGroupEgressInput:
_, err = d.ec2.RevokeSecurityGroupEgress(ii)
}
if awsErr, ok := err.(awserr.Error); ok {
switch code := awsErr.Code(); {
case code == "DryRunOperation", strings.HasSuffix(code, "NotFound"):
d.logger.Verbose("full dry run: update securitygroup ok")
return nil, nil
}
}
d.logger.Errorf("dry run: update securitygroup error: %s", err)
return nil, err
}
func (d *AwsDriver) Update_Securitygroup(params map[string]interface{}) (interface{}, error) {
ipPerms, err := buildIpPermissionsFromParams(params)
if err != nil {
return nil, err
}
var input interface{}
if action, ok := params["inbound"].(string); ok {
switch action {
case "authorize":
input = &ec2.AuthorizeSecurityGroupIngressInput{IpPermissions: ipPerms}
case "revoke":
input = &ec2.RevokeSecurityGroupIngressInput{IpPermissions: ipPerms}
default:
return nil, fmt.Errorf("'inbound' parameter expect 'authorize' or 'revoke', got %s", action)
}
}
if action, ok := params["outbound"].(string); ok {
switch action {
case "authorize":
input = &ec2.AuthorizeSecurityGroupEgressInput{IpPermissions: ipPerms}
case "revoke":
input = &ec2.RevokeSecurityGroupEgressInput{IpPermissions: ipPerms}
default:
return nil, fmt.Errorf("'outbound' parameter expect 'authorize' or 'revoke', got %s", action)
}
}
if input == nil {
return nil, fmt.Errorf("expect either 'inbound' or 'outbound' parameter")
}
// Required params
setField(params["id"], input, "GroupId")
var output interface{}
switch ii := input.(type) {
case *ec2.AuthorizeSecurityGroupIngressInput:
output, err = d.ec2.AuthorizeSecurityGroupIngress(ii)
case *ec2.RevokeSecurityGroupIngressInput:
output, err = d.ec2.RevokeSecurityGroupIngress(ii)
case *ec2.AuthorizeSecurityGroupEgressInput:
output, err = d.ec2.AuthorizeSecurityGroupEgress(ii)
case *ec2.RevokeSecurityGroupEgressInput:
output, err = d.ec2.RevokeSecurityGroupEgress(ii)
}
if err != nil {
d.logger.Errorf("update securitygroup error: %s", err)
return nil, err
}
d.logger.Verbose("update securitygroup done")
return output, nil
}
// This function was auto generated
func (d *AwsDriver) Create_Storageobject_DryRun(params map[string]interface{}) (interface{}, error) {
if _, ok := params["bucket"]; !ok {
return nil, errors.New("create storageobject: missing required params 'bucket'")
}
if _, ok := params["file"].(string); !ok {
return nil, errors.New("create storageobject: missing required string params 'file'")
}
stat, err := os.Stat(params["file"].(string))
if os.IsNotExist(err) {
return nil, fmt.Errorf("cannot find file '%s'", params["file"])
}
if err != nil {
return nil, err
}
if stat.IsDir() {
return nil, fmt.Errorf("'%s' is a directory", params["file"])
}
d.logger.Verbose("params dry run: create storageobject ok")
return nil, nil
}
// This function was auto generated
func (d *AwsDriver) Create_Storageobject(params map[string]interface{}) (interface{}, error) {
input := &s3.PutObjectInput{}
f, err := os.Open(params["file"].(string))
if err != nil {
return nil, err
}
defer f.Close()
input.Body = f
var fileName string
if n, ok := params["name"].(string); ok && n != "" {
fileName = n
} else {
fileName = f.Name()
}
input.Key = aws.String(fileName)
// Required params
setField(params["bucket"], input, "Bucket")
output, err := d.s3.PutObject(input)
if err != nil {
d.logger.Errorf("create storageobject error: %s", err)
return nil, err
}
output = output
d.logger.Verbose("create storageobject done")
return output, nil
}
func buildIpPermissionsFromParams(params map[string]interface{}) ([]*ec2.IpPermission, error) {
if _, ok := params["cidr"].(string); !ok {
return nil, fmt.Errorf("invalid cidr '%v'", params["cidr"])
}
ipPerm := &ec2.IpPermission{
IpRanges: []*ec2.IpRange{{CidrIp: aws.String(params["cidr"].(string))}},
}
if _, ok := params["protocol"].(string); !ok {
return nil, fmt.Errorf("invalid protocol '%v'", params["protocol"])
}
p := params["protocol"].(string)
if strings.Contains("any", p) {
ipPerm.FromPort = aws.Int64(int64(-1))
ipPerm.ToPort = aws.Int64(int64(-1))
ipPerm.IpProtocol = aws.String("-1")
return []*ec2.IpPermission{ipPerm}, nil
}
ipPerm.IpProtocol = aws.String(p)
switch ports := params["portrange"].(type) {
case int:
ipPerm.FromPort = aws.Int64(int64(ports))
ipPerm.ToPort = aws.Int64(int64(ports))
case int64:
ipPerm.FromPort = aws.Int64(ports)
ipPerm.ToPort = aws.Int64(ports)
case string:
switch {
case strings.Contains(ports, "any"):
ipPerm.FromPort = aws.Int64(int64(-1))
ipPerm.ToPort = aws.Int64(int64(-1))
case strings.Contains(ports, "-"):
from, err := strconv.ParseInt(strings.SplitN(ports, "-", 2)[0], 10, 64)
if err != nil {
return nil, err
}
to, err := strconv.ParseInt(strings.SplitN(ports, "-", 2)[1], 10, 64)
if err != nil {
return nil, err
}
ipPerm.FromPort = aws.Int64(from)
ipPerm.ToPort = aws.Int64(to)
default:
port, err := strconv.ParseInt(ports, 10, 64)
if err != nil {
return nil, err
}
ipPerm.FromPort = aws.Int64(port)
ipPerm.ToPort = aws.Int64(port)
}
}
return []*ec2.IpPermission{ipPerm}, nil
}
func fakeDryRunId(entity string) string {
suffix := rand.Intn(1e6)
switch entity {
case "instance":
return fmt.Sprintf("i-%d", suffix)
case "volume":
return fmt.Sprintf("vol-%d", suffix)
case "securitygroup":
return fmt.Sprintf("sg-%d", suffix)
case graph.InternetGateway.String():
return fmt.Sprintf("igw-%d", suffix)
default:
return fmt.Sprintf("dryrunid-%d", suffix)
}
}