-
Notifications
You must be signed in to change notification settings - Fork 47
/
SecurityHandler.go
563 lines (496 loc) · 18.2 KB
/
SecurityHandler.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
// Cloud Driver Interface of CB-Spider.
// The CB-Spider is a sub-Framework of the Cloud-Barista Multi-Cloud Project.
// The CB-Spider Mission is to connect all the clouds with a single interface.
//
// * Cloud-Barista: https://github.com/cloud-barista
//
// This is Resouces interfaces of Cloud Driver.
//
// by CB-Spider Team, 2019.06.
package resources
import (
"errors"
"reflect"
"strconv"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/ec2"
call "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/call-log"
idrv "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces"
irs "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces/resources"
"github.com/davecgh/go-spew/spew"
)
type AwsSecurityHandler struct {
Region idrv.RegionInfo
Client *ec2.EC2
}
//2019-11-16부로 CB-Driver 전체 로직이 NameId 기반으로 변경됨. (보안 그룹은 그룹명으로 처리 가능하기 때문에 Name 태깅시 에러는 무시함)
//@TODO : 존재하는 보안 그룹에 정책 추가하는 기능 필요
//VPC 생략 시 활성화된 세션의 기본 VPC를 이용 함.
func (securityHandler *AwsSecurityHandler) CreateSecurity(securityReqInfo irs.SecurityReqInfo) (irs.SecurityInfo, error) {
cblogger.Infof("securityReqInfo : ", securityReqInfo)
spew.Dump(securityReqInfo)
/*
//VPC & Subnet을 자동으로 찾아서 처리
VPCHandler := AwsVPCHandler{Client: securityHandler.Client}
awsCBNetworkInfo, errAutoCBNetInfo := VPCHandler.GetAutoCBNetworkInfo()
if errAutoCBNetInfo != nil || awsCBNetworkInfo.VpcId == "" {
cblogger.Error("VPC 정보 획득 실패")
return irs.SecurityInfo{}, errors.New("mcloud-barista의 기본 네트워크 정보를 찾을 수 없습니다.")
}
cblogger.Infof("==> [%s] CB Default VPC 정보 찾음", awsCBNetworkInfo.VpcId)
vpcId := awsCBNetworkInfo.VpcId
*/
vpcId := securityReqInfo.VpcIID.SystemId
// Create the security group with the VPC, name and description.
//createRes, err := securityHandler.Client.CreateSecurityGroup(&ec2.CreateSecurityGroupInput{
input := ec2.CreateSecurityGroupInput{
//GroupName: aws.String(securityReqInfo.Name),
GroupName: aws.String(securityReqInfo.IId.NameId),
//Description: aws.String(securityReqInfo.Name),
Description: aws.String(securityReqInfo.IId.NameId),
// VpcId: aws.String(securityReqInfo.VpcId),awsCBNetworkInfo
VpcId: aws.String(vpcId),
}
cblogger.Debugf("보안 그룹 생성 요청 정보", input)
// logger for HisCall
callogger := call.GetLogger("HISCALL")
callLogInfo := call.CLOUDLOGSCHEMA{
CloudOS: call.AWS,
RegionZone: securityHandler.Region.Zone,
ResourceType: call.SECURITYGROUP,
ResourceName: securityReqInfo.IId.NameId,
CloudOSAPI: "CreateSecurityGroup()",
ElapsedTime: "",
ErrorMSG: "",
}
callLogStart := call.Start()
createRes, err := securityHandler.Client.CreateSecurityGroup(&input)
callLogInfo.ElapsedTime = call.Elapsed(callLogStart)
if err != nil {
callLogInfo.ErrorMSG = err.Error()
callogger.Info(call.String(callLogInfo))
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case "InvalidVpcID.NotFound":
cblogger.Errorf("Unable to find VPC with ID %q.", vpcId)
return irs.SecurityInfo{}, err
case "InvalidGroup.Duplicate":
cblogger.Errorf("Security group %q already exists.", securityReqInfo.IId.NameId)
return irs.SecurityInfo{}, err
}
}
cblogger.Errorf("Unable to create security group %q, %v", securityReqInfo.IId.NameId, err)
return irs.SecurityInfo{}, err
}
callogger.Info(call.String(callLogInfo))
cblogger.Infof("[%s] 보안 그룹 생성완료", aws.StringValue(createRes.GroupId))
spew.Dump(createRes)
//newGroupId = *createRes.GroupId
cblogger.Infof("인바운드 보안 정책 처리")
//Ingress 처리
var ipPermissions []*ec2.IpPermission
for _, ip := range *securityReqInfo.SecurityRules {
//for _, ip := range securityReqInfo.IPPermissions {
if ip.Direction != "inbound" {
cblogger.Debug("==> inbound가 아닌 보안 그룹 Skip : ", ip.Direction)
continue
}
ipPermission := new(ec2.IpPermission)
ipPermission.SetIpProtocol(ip.IPProtocol)
if ip.FromPort != "" {
if n, err := strconv.ParseInt(ip.FromPort, 10, 64); err == nil {
ipPermission.SetFromPort(n)
} else {
cblogger.Error(ip.FromPort, "은 숫자가 아님!!")
return irs.SecurityInfo{}, err
}
} else {
//ipPermission.SetFromPort(0)
}
if ip.ToPort != "" {
if n, err := strconv.ParseInt(ip.ToPort, 10, 64); err == nil {
ipPermission.SetToPort(n)
} else {
cblogger.Error(ip.ToPort, "은 숫자가 아님!!")
return irs.SecurityInfo{}, err
}
} else {
//ipPermission.SetToPort(0)
}
ipPermission.SetIpRanges([]*ec2.IpRange{
(&ec2.IpRange{}).
//SetCidrIp(ip.Cidr),
SetCidrIp("0.0.0.0/0"),
})
ipPermissions = append(ipPermissions, ipPermission)
}
//인바운드 정책이 있는 경우에만 처리
if len(ipPermissions) > 0 {
// Add permissions to the security group
_, err = securityHandler.Client.AuthorizeSecurityGroupIngress(&ec2.AuthorizeSecurityGroupIngressInput{
//GroupName: aws.String(securityReqInfo.Name),
GroupId: createRes.GroupId,
IpPermissions: ipPermissions,
})
if err != nil {
cblogger.Errorf("Unable to set security group %q ingress, %v", securityReqInfo.IId.NameId, err)
return irs.SecurityInfo{}, err
}
cblogger.Info("Successfully set security group ingress")
}
cblogger.Infof("아웃바운드 보안 정책 처리")
//Egress 처리
var ipPermissionsEgress []*ec2.IpPermission
//for _, ip := range securityReqInfo.IPPermissionsEgress {
for _, ip := range *securityReqInfo.SecurityRules {
if ip.Direction != "outbound" {
cblogger.Debug("==> outbound가 아닌 보안 그룹 Skip : ", ip.Direction)
continue
}
ipPermission := new(ec2.IpPermission)
ipPermission.SetIpProtocol(ip.IPProtocol)
//ipPermission.SetFromPort(ip.FromPort)
//ipPermission.SetToPort(ip.ToPort)
if ip.FromPort != "" {
if n, err := strconv.ParseInt(ip.FromPort, 10, 64); err == nil {
ipPermission.SetFromPort(n)
} else {
cblogger.Error(ip.FromPort, "은 숫자가 아님!!")
return irs.SecurityInfo{}, err
}
} else {
//ipPermission.SetFromPort(0)
}
if ip.ToPort != "" {
if n, err := strconv.ParseInt(ip.ToPort, 10, 64); err == nil {
ipPermission.SetToPort(n)
} else {
cblogger.Error(ip.ToPort, "은 숫자가 아님!!")
return irs.SecurityInfo{}, err
}
} else {
//ipPermission.SetToPort(0)
}
ipPermission.SetIpRanges([]*ec2.IpRange{
(&ec2.IpRange{}).
//SetCidrIp(ip.Cidr),
SetCidrIp("0.0.0.0/0"),
})
//ipPermissions = append(ipPermissions, ipPermission)
ipPermissionsEgress = append(ipPermissionsEgress, ipPermission)
}
//아웃바운드 정책이 있는 경우에만 처리
if len(ipPermissionsEgress) > 0 {
// Add permissions to the security group
_, err = securityHandler.Client.AuthorizeSecurityGroupEgress(&ec2.AuthorizeSecurityGroupEgressInput{
GroupId: createRes.GroupId,
IpPermissions: ipPermissionsEgress,
})
if err != nil {
cblogger.Errorf("Unable to set security group %q egress, %v", securityReqInfo.IId.NameId, err)
return irs.SecurityInfo{}, err
}
cblogger.Info("Successfully set security group egress")
}
cblogger.Info("Name Tag 처리")
//======================
// Name 태그 처리
//======================
//VPC Name 태깅
tagInput := &ec2.CreateTagsInput{
Resources: []*string{
aws.String(*createRes.GroupId),
},
Tags: []*ec2.Tag{
{
Key: aws.String("Name"),
Value: aws.String(securityReqInfo.IId.NameId),
},
},
}
//spew.Dump(tagInput)
_, errTag := securityHandler.Client.CreateTags(tagInput)
//Tag 실패 시 별도의 처리 없이 에러 로그만 남겨 놓음.
if errTag != nil {
cblogger.Error(errTag)
}
//securityInfo, _ := securityHandler.GetSecurity(*createRes.GroupId)
//securityInfo, _ := securityHandler.GetSecurity(securityReqInfo.IId) //2019-11-16 NameId 기반으로 변경됨
securityInfo, _ := securityHandler.GetSecurity(irs.IID{SystemId: *createRes.GroupId}) //2020-04-09 SystemId기반으로 변경
securityInfo.IId.NameId = securityReqInfo.IId.NameId // Name이 필수가 아니므로 혹시 모르니 사용자가 요청한 NameId로 재설정 함.
securityInfo.VpcIID.NameId = securityReqInfo.VpcIID.NameId // Name이 필수가 아니므로 객체에 저장되지 않기 때문에 시스템에서 활용 가능하도록 사용자가 요청한 NameId 값을 그대로 돌려 줌.
return securityInfo, nil
}
func (securityHandler *AwsSecurityHandler) ListSecurity() ([]*irs.SecurityInfo, error) {
//VPC ID 조회
/* 2020-04-13 : 전체 영역에서 조회하도록 변경
VPCHandler := AwsVPCHandler{Client: securityHandler.Client}
vpcId := VPCHandler.GetMcloudBaristaDefaultVpcId()
if vpcId == "" {
return nil, nil
}
*/
input := &ec2.DescribeSecurityGroupsInput{
GroupIds: []*string{
nil,
},
/*
Filters: []*ec2.Filter{
{
Name: aws.String("vpc-id"),
Values: aws.StringSlice([]string{vpcId}),
},
},
*/
}
// logger for HisCall
callogger := call.GetLogger("HISCALL")
callLogInfo := call.CLOUDLOGSCHEMA{
CloudOS: call.AWS,
RegionZone: securityHandler.Region.Zone,
ResourceType: call.SECURITYGROUP,
ResourceName: "List()",
CloudOSAPI: "DescribeSecurityGroups()",
ElapsedTime: "",
ErrorMSG: "",
}
callLogStart := call.Start()
result, err := securityHandler.Client.DescribeSecurityGroups(input)
callLogInfo.ElapsedTime = call.Elapsed(callLogStart)
cblogger.Info("result : ", result)
if err != nil {
callLogInfo.ErrorMSG = err.Error()
callogger.Info(call.String(callLogInfo))
cblogger.Info("err : ", err)
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
default:
cblogger.Error(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
cblogger.Error(err.Error())
}
return nil, err
}
callogger.Info(call.String(callLogInfo))
var results []*irs.SecurityInfo
for _, securityGroup := range result.SecurityGroups {
securityInfo := ExtractSecurityInfo(securityGroup)
results = append(results, &securityInfo)
}
return results, nil
}
//2019-11-16부로 CB-Driver 전체 로직이 NameId 기반으로 변경됨.
//func (securityHandler *AwsSecurityHandler) GetSecurity(securityNameId string) (irs.SecurityInfo, error) {
func (securityHandler *AwsSecurityHandler) GetSecurity(securityIID irs.IID) (irs.SecurityInfo, error) {
cblogger.Infof("securityNameId : [%s]", securityIID.SystemId)
//2020-04-09 Filter 대신 SystemId 기반으로 변경
input := &ec2.DescribeSecurityGroupsInput{
GroupIds: []*string{
aws.String(securityIID.SystemId),
},
}
/* 2020-04-09 Name 기반으로 조회 하지 않기때문에 미사용
input.Filters = ([]*ec2.Filter{
&ec2.Filter{
//Name: aws.String("tag:Name"), // subnet-id
Name: aws.String("group-name"), // subnet-id
Values: []*string{
aws.String(securityIID.SystemId),
},
},
})
*/
cblogger.Info(input)
// logger for HisCall
callogger := call.GetLogger("HISCALL")
callLogInfo := call.CLOUDLOGSCHEMA{
CloudOS: call.AWS,
RegionZone: securityHandler.Region.Zone,
ResourceType: call.SECURITYGROUP,
ResourceName: securityIID.SystemId,
CloudOSAPI: "DescribeSecurityGroups()",
ElapsedTime: "",
ErrorMSG: "",
}
callLogStart := call.Start()
result, err := securityHandler.Client.DescribeSecurityGroups(input)
callLogInfo.ElapsedTime = call.Elapsed(callLogStart)
cblogger.Info("result : ", result)
cblogger.Info("err : ", err)
if err != nil {
callLogInfo.ErrorMSG = err.Error()
callogger.Info(call.String(callLogInfo))
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
default:
cblogger.Error(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
cblogger.Error(err.Error())
}
return irs.SecurityInfo{}, err
}
callogger.Info(call.String(callLogInfo))
if len(result.SecurityGroups) > 0 {
securityInfo := ExtractSecurityInfo(result.SecurityGroups[0])
return securityInfo, nil
} else {
//return irs.SecurityInfo{}, errors.New("[" + securityNameId + "] 정보를 찾을 수 없습니다.")
return irs.SecurityInfo{}, errors.New("InvalidSecurityGroup.NotFound: The security group '" + securityIID.SystemId + "' does not exist")
}
}
func ExtractSecurityInfo(securityGroupResult *ec2.SecurityGroup) irs.SecurityInfo {
var ipPermissions []irs.SecurityRuleInfo
var ipPermissionsEgress []irs.SecurityRuleInfo
var securityRules []irs.SecurityRuleInfo
cblogger.Debugf("===[그룹아이디:%s]===", *securityGroupResult.GroupId)
ipPermissions = ExtractIpPermissions(securityGroupResult.IpPermissions, "inbound")
cblogger.Debug("InBouds : ", ipPermissions)
ipPermissionsEgress = ExtractIpPermissions(securityGroupResult.IpPermissionsEgress, "outbound")
cblogger.Debug("OutBounds : ", ipPermissionsEgress)
//spew.Dump(ipPermissionsEgress)
securityRules = append(ipPermissions, ipPermissionsEgress...)
securityInfo := irs.SecurityInfo{
//Id: *securityGroupResult.GroupId,
IId: irs.IID{"", *securityGroupResult.GroupId},
//SecurityRules: &[]irs.SecurityRuleInfo{},
SecurityRules: &securityRules,
VpcIID: irs.IID{"", *securityGroupResult.VpcId},
KeyValueList: []irs.KeyValue{
{Key: "GroupName", Value: *securityGroupResult.GroupName},
{Key: "VpcID", Value: *securityGroupResult.VpcId},
{Key: "OwnerID", Value: *securityGroupResult.OwnerId},
{Key: "Description", Value: *securityGroupResult.Description},
},
}
//Name은 Tag의 "Name" 속성에만 저장됨
cblogger.Debug("Name Tag 찾기")
for _, t := range securityGroupResult.Tags {
if *t.Key == "Name" {
//securityInfo.Name = *t.Value
securityInfo.IId.NameId = *t.Value
cblogger.Debug("Name : ", securityInfo.IId.NameId)
break
}
}
return securityInfo
}
// IpPermission에서 공통정보 추출
func ExtractIpPermissionCommon(ip *ec2.IpPermission, securityRuleInfo *irs.SecurityRuleInfo) {
//공통 정보
if !reflect.ValueOf(ip.FromPort).IsNil() {
//securityRuleInfo.FromPort = *ip.FromPort
securityRuleInfo.FromPort = strconv.FormatInt(*ip.FromPort, 10)
}
if !reflect.ValueOf(ip.ToPort).IsNil() {
//securityRuleInfo.ToPort = *ip.ToPort
securityRuleInfo.ToPort = strconv.FormatInt(*ip.ToPort, 10)
}
securityRuleInfo.IPProtocol = *ip.IpProtocol
}
func ExtractIpPermissions(ipPermissions []*ec2.IpPermission, direction string) []irs.SecurityRuleInfo {
var results []irs.SecurityRuleInfo
for _, ip := range ipPermissions {
//ipv4 처리
for _, ipv4 := range ip.IpRanges {
cblogger.Debug("Inbound/Outbound 정보 조회 : ", *ip.IpProtocol)
securityRuleInfo := irs.SecurityRuleInfo{
Direction: direction, // "inbound | outbound"
//Cidr: *ipv4.CidrIp,
}
cblogger.Debug(*ipv4.CidrIp)
ExtractIpPermissionCommon(ip, &securityRuleInfo) //IP & Port & Protocol 추출
results = append(results, securityRuleInfo)
}
//ipv6 처리
for _, ipv6 := range ip.Ipv6Ranges {
securityRuleInfo := irs.SecurityRuleInfo{
Direction: direction, // "inbound | outbound"
//Cidr: *ipv6.CidrIpv6,
}
cblogger.Debug(*ipv6.CidrIpv6)
ExtractIpPermissionCommon(ip, &securityRuleInfo) //IP & Port & Protocol 추출
results = append(results, securityRuleInfo)
}
//ELB나 보안그룹 참조 방식 처리
for _, userIdGroup := range ip.UserIdGroupPairs {
securityRuleInfo := irs.SecurityRuleInfo{
Direction: direction, // "inbound | outbound"
//Cidr: *userIdGroup.GroupId,
}
cblogger.Debug(*userIdGroup.UserId)
ExtractIpPermissionCommon(ip, &securityRuleInfo) //IP & Port & Protocol 추출
results = append(results, securityRuleInfo)
}
/* @TODO : 미지원 방식 체크 로직 추가 여부 결정해야 함.
if !reflect.ValueOf(ip.IpRanges).IsNil() {
securityRuleInfo.Cidr = *ip.IpRanges[0].CidrIp
} else {
//ELB나 다른 보안그룹 참조처럼 IpRanges가 없고 UserIdGroupPairs가 있는 경우 처리
//https://docs.aws.amazon.com/ko_kr/elasticloadbalancing/latest/classic/elb-security-groups.html
if !reflect.ValueOf(ip.UserIdGroupPairs).IsNil() {
securityRuleInfo.Cidr = *ip.UserIdGroupPairs[0].GroupId
} else {
cblogger.Error("미지원 보안 그룹 형태 발견 - 구조 파악 필요 ", ip)
}
}
*/
}
return results
}
//2019-11-16부로 CB-Driver 전체 로직이 NameId 기반으로 변경됨.
//func (securityHandler *AwsSecurityHandler) DeleteSecurity(securityNameId string) (bool, error) {
func (securityHandler *AwsSecurityHandler) DeleteSecurity(securityIID irs.IID) (bool, error) {
cblogger.Infof("securityNameId : [%s]", securityIID.SystemId)
/* //2020-04-09 SystemId 기반으로 변경되어서 필요 없음.
securityInfo, errsecurityInfo := securityHandler.GetSecurity(securityIID)
if errsecurityInfo != nil {
return false, errsecurityInfo
}
cblogger.Info(securityInfo)
*/
//securityID := securityInfo.Id
//securityID := securityInfo.IId.SystemId
securityID := securityIID.SystemId
// logger for HisCall
callogger := call.GetLogger("HISCALL")
callLogInfo := call.CLOUDLOGSCHEMA{
CloudOS: call.AWS,
RegionZone: securityHandler.Region.Zone,
ResourceType: call.SECURITYGROUP,
ResourceName: securityIID.SystemId,
CloudOSAPI: "DeleteSecurityGroup()",
ElapsedTime: "",
ErrorMSG: "",
}
callLogStart := call.Start()
// Delete the security group.
_, err := securityHandler.Client.DeleteSecurityGroup(&ec2.DeleteSecurityGroupInput{
GroupId: aws.String(securityID),
})
callLogInfo.ElapsedTime = call.Elapsed(callLogStart)
if err != nil {
callLogInfo.ErrorMSG = err.Error()
callogger.Info(call.String(callLogInfo))
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case "InvalidGroupId.Malformed":
fallthrough
case "InvalidGroup.NotFound":
cblogger.Errorf("%s.", aerr.Message())
return false, err
}
}
cblogger.Errorf("Unable to get descriptions for security groups, %v.", err)
return false, err
}
callogger.Info(call.String(callLogInfo))
cblogger.Infof("Successfully delete security group %q.", securityID)
return true, nil
}