Skip to content

Commit

Permalink
GCP 보안그룹의 요청 Port에 -1(All or Nothing) 기능 적용
Browse files Browse the repository at this point in the history
- FromPort & ToPort 둘 다 -1 또는 둘 중 하나라도 -1 이면서 다른 포트가 공백이면 All로 처리
- FromPort & ToPort 둘 중 하나에 -1이 사용되면 -1이 사용되지 않은 Port의 값을 이용
- FromPort & ToPort 모두 공백이면 All로 처리
  • Loading branch information
jaesung-yoo committed May 12, 2020
1 parent 9b6d377 commit 40232af
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,9 @@ func handleSecurity() {
//config := readConfigFile()
//VmID := config.Aws.VmID

securityName := "cb-securitytest1"
securityId := "sg-6weeb9xaodr65g7bq10c"
securityName := "cb-securitytest-all"
securityId := "cb-securitytest-all"
//securityId := "cb-secu-all"
vpcId := "cb-vpc"

for {
Expand Down Expand Up @@ -168,6 +169,12 @@ func handleSecurity() {
IId: irs.IID{NameId: securityName},
VpcIID: irs.IID{SystemId: vpcId},
SecurityRules: &[]irs.SecurityRuleInfo{ //보안 정책 설정
{
FromPort: "",
ToPort: "",
IPProtocol: "icmp", //icmp는 포트 정보가 없음
Direction: "inbound",
},
{
FromPort: "20",
ToPort: "22",
Expand All @@ -183,30 +190,50 @@ func handleSecurity() {
},
{
FromPort: "8080",
ToPort: "8080",
ToPort: "-1", //FromPort나 ToPort중 하나에 -1이 입력될 경우 -1이 입력된 경우 -1을 공백으로 처리
IPProtocol: "tcp",
Direction: "inbound",
},
{
FromPort: "443",
ToPort: "443",
FromPort: "-1", //FromPort나 ToPort중 하나에 -1이 입력될 경우 -1이 입력된 경우 -1을 공백으로 처리
ToPort: "1323",
IPProtocol: "tcp",
Direction: "outbound",
Direction: "inbound",
},
{
FromPort: "8443",
ToPort: "9999",
FromPort: "",
ToPort: "1024",
IPProtocol: "tcp",
Direction: "outbound",
Direction: "inbound",
},
{
FromPort: "1234",
ToPort: "",
IPProtocol: "tcp",
Direction: "inbound",
},
/*
{
//FromPort: "8443",
//ToPort: "9999",
IPProtocol: "-1", // 모두 허용 (포트 정보 없음)
//FromPort: "",
//ToPort: "",
IPProtocol: "all", // 모두 허용 (포트 정보 없음)
Direction: "inbound",
},
*/
/*
{
FromPort: "443",
ToPort: "443",
IPProtocol: "tcp",
Direction: "outbound",
},
{
FromPort: "8443",
ToPort: "9999",
IPProtocol: "tcp",
Direction: "outbound",
},
*/
},
}

Expand Down Expand Up @@ -860,8 +887,8 @@ func main() {
//handleVPC()
//handleVMSpec()
//handleImage() //AMI
handleKeyPair()
//handleSecurity()
//handleKeyPair()
handleSecurity()

//handleVM()
//cblogger.Info(filepath.Join("a/b", "\\cloud-driver-libs\\.ssh-gcp\\"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,29 +54,56 @@ func (securityHandler *GCPSecurityHandler) CreateSecurity(securityReqInfo irs.Se
ports := *securityReqInfo.SecurityRules
var firewallAllowed []*compute.FirewallAllowed

//다른 드라이버와의 통일을 위해 All은 -1로 처리함.
//GCP는 포트 번호를 적지 않으면 All임.
//GCP 방화벽 정책
//https://cloud.google.com/vpc/docs/firewalls?hl=ko&_ga=2.238147008.-1577666838.1589162755#protocols_and_ports
for _, item := range ports {
var port string
fp := item.FromPort
tp := item.ToPort

if tp != "" && fp != "" {
port = fp + "-" + tp
}
if tp != "" && fp == "" {
port = tp
}
if tp == "" && fp != "" {
port = fp
// CB Rule에 의해 Port 번호에 -1이 기입된 경우 GCP Rule에 맞게 치환함.
if fp == "-1" || tp == "-1" {
if (fp == "-1" && tp == "-1") || (fp == "-1" && tp == "") || (fp == "" && tp == "-1") {
port = ""
} else if fp == "-1" {
port = tp
} else {
port = fp
}
} else {
//둘 다 있는 경우
if tp != "" && fp != "" {
port = fp + "-" + tp
//From Port가 없는 경우
} else if tp != "" && fp == "" {
port = tp
//To Port가 없는 경우
} else if tp == "" && fp != "" {
port = fp
} else {
port = ""
}
}

firewallAllowed = append(firewallAllowed, &compute.FirewallAllowed{
IPProtocol: item.IPProtocol,
Ports: []string{
port,
},
})
if port == "" {
firewallAllowed = append(firewallAllowed, &compute.FirewallAllowed{
IPProtocol: item.IPProtocol,
})
} else {
firewallAllowed = append(firewallAllowed, &compute.FirewallAllowed{
IPProtocol: item.IPProtocol,
Ports: []string{
port,
},
})
}
}

cblogger.Info("생성할 방화벽 정책")
spew.Dump(firewallAllowed)

var sgDirection string
if strings.EqualFold(securityReqInfo.Direction, "inbound") {
sgDirection = "INGRESS"
Expand Down

0 comments on commit 40232af

Please sign in to comment.