Skip to content

Commit

Permalink
[KT Classic/VPC] Apply CreateTime to VPC/SG info
Browse files Browse the repository at this point in the history
  • Loading branch information
innodreamer committed Sep 30, 2024
1 parent 32ef8ff commit 8e1a48b
Show file tree
Hide file tree
Showing 5 changed files with 245 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ import (
"sync"
"time"
"os"
"strings"
// "github.com/davecgh/go-spew/spew"
"github.com/sirupsen/logrus"

cblog "github.com/cloud-barista/cb-log"
ktsdk "github.com/cloud-barista/ktcloud-sdk-go"

cblog "github.com/cloud-barista/cb-log"
idrv "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces"
call "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/call-log"
)

Expand Down Expand Up @@ -129,3 +133,48 @@ func convertTimeFormat(inputTime string) (time.Time, error) {
}
return parsedTime, nil
}

func createClient(connectionInfo idrv.ConnectionInfo) (*ktsdk.KtCloudClient, error) {
cblogger.Info("KT Cloud Driver: called createClient()")
// cblogger.Infof("### connectionInfo.RegionInfo.Zone : [%d]", connectionInfo.RegionInfo.Zone)

// $$$ Caution!!
var apiurl string
if strings.EqualFold(connectionInfo.RegionInfo.Zone, KOR_Seoul_M2_ZoneID) { // When Zone is "KOR-Seoul M2"
apiurl = "https://api.ucloudbiz.olleh.com/server/v2/client/api"
} else {
apiurl = "https://api.ucloudbiz.olleh.com/server/v1/client/api"
}

if len(apiurl) == 0 {
newErr := fmt.Errorf("KT Cloud API URL Not Found!!")
cblogger.Error(newErr.Error())
return nil, newErr
}

apikey := connectionInfo.CredentialInfo.ClientId
if len(apikey) == 0 {
newErr := fmt.Errorf("KT Cloud API Key Not Found!!")
cblogger.Error(newErr.Error())
return nil, newErr
}

secretkey := connectionInfo.CredentialInfo.ClientSecret
if len(secretkey) == 0 {
newErr := fmt.Errorf("KT Cloud Secret Key Not Found!!")
cblogger.Error(newErr.Error())
return nil, newErr
}

// Always validate any SSL certificates in the chain
insecureskipverify := false
client := ktsdk.KtCloudClient{}.New(apiurl, apikey, secretkey, insecureskipverify)

return client, nil
}

func getSeoulCurrentTime() string {
loc, _ := time.LoadLocation("Asia/Seoul")
currentTime := time.Now().In(loc)
return currentTime.Format("2006-01-02 15:04:05")
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// KT Cloud Security Group Handler
//
// by ETRI, 2021.05.
// Updated by ETRI, 2024.09.

package resources

Expand All @@ -18,14 +19,13 @@ import (
// "crypto/aes"
// "crypto/cipher"
"encoding/base64"

// "github.com/davecgh/go-spew/spew"
ktsdk "github.com/cloud-barista/ktcloud-sdk-go"

"encoding/json"
"errors"
// "strconv"

ktsdk "github.com/cloud-barista/ktcloud-sdk-go"

cblog "github.com/cloud-barista/cb-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"
Expand All @@ -48,10 +48,11 @@ func init() {
}

type SecurityGroup struct {
IID IId `json:"IId"`
VpcIID VpcIId `json:"VpcIID"`
Direc string `json:"Direction"`
Secu_Rules []Security_Rule `json:"SecurityRules"`
IID IId `json:"IId"`
VpcIID VpcIId `json:"VpcIID"`
Direc string `json:"Direction"`
Secu_Rules []Security_Rule `json:"SecurityRules"`
KeyValue_List []KeyValue `json:"KeyValueList"`
}

type IId struct {
Expand All @@ -74,6 +75,7 @@ type Security_Rule struct {

func (securityHandler *KtCloudSecurityHandler) CreateSecurity(securityReqInfo irs.SecurityReqInfo) (irs.SecurityInfo, error) {
cblogger.Info("KT Cloud cloud driver: called CreateSecurity()!")

zoneId := securityHandler.RegionInfo.Zone
if zoneId == "" {
cblogger.Error("Failed to Get Zone info. from the connection info.")
Expand Down Expand Up @@ -106,15 +108,33 @@ func (securityHandler *KtCloudSecurityHandler) CreateSecurity(securityReqInfo ir
for _, sg := range sgList {
if sg.IId.NameId == securityReqInfo.IId.NameId {
createErr := errors.New("Security Group with name " + securityReqInfo.IId.NameId + " already exists", )
cblogger.Error(createErr.Error())
return irs.SecurityInfo{}, createErr
}
}

currentTime := getSeoulCurrentTime()

newSGInfo := irs.SecurityInfo{
IId: irs.IID{
NameId: securityReqInfo.IId.NameId,
// Caution!! : securityReqInfo.IId.NameId -> SystemId
SystemId: securityReqInfo.IId.NameId,
},
VpcIID: securityReqInfo.VpcIID,
SecurityRules: securityReqInfo.SecurityRules,
KeyValueList: []irs.KeyValue{
{Key: "KTCloud-SecuriyGroup-info.", Value: "This SecuriyGroup info. is temporary."},
{Key: "CreateTime", Value: currentTime},
},
}
// spew.Dump(newSGInfo)

hashFileName := base64.StdEncoding.EncodeToString([]byte(securityReqInfo.IId.NameId))
cblogger.Infof("# S/G NameId : "+ securityReqInfo.IId.NameId)
cblogger.Infof("# Hashed FileName : "+ hashFileName + ".json")
// cblogger.Infof("# Hashed FileName : "+ hashFileName + ".json")

file, _ := json.MarshalIndent(securityReqInfo, "", " ")
file, _ := json.MarshalIndent(newSGInfo, "", " ")
writeErr := os.WriteFile(sgFilePath + hashFileName + ".json", file, 0644)
if writeErr != nil {
cblogger.Error("Failed to write the file: "+ sgFilePath + hashFileName + ".json", writeErr)
Expand All @@ -139,7 +159,7 @@ func (securityHandler *KtCloudSecurityHandler) GetSecurity(securityIID irs.IID)
hashFileName := base64.StdEncoding.EncodeToString([]byte(securityIID.NameId))

cblogger.Infof("# securityIID.NameId : "+ securityIID.NameId)
cblogger.Infof("# hashFileName : "+ hashFileName + ".json")
// cblogger.Infof("# hashFileName : "+ hashFileName + ".json")

zoneId := securityHandler.RegionInfo.Zone
if zoneId == "" {
Expand Down Expand Up @@ -177,15 +197,15 @@ func (securityHandler *KtCloudSecurityHandler) GetSecurity(securityIID irs.IID)
byteValue, readErr := io.ReadAll(jsonFile)
if readErr != nil {
cblogger.Error("Failed to Read the S/G file : "+ sgFileName, readErr)
return irs.SecurityInfo{}, readErr
}
json.Unmarshal(byteValue, &sg)
// spew.Dump(sg)

// Caution : ~~~ := mappingSecurityInfo( ) => ~~~ := securityHandler.mappingSecurityInfo( )
securityGroupInfo, securityInfoError := securityHandler.mappingSecurityInfo(sg)
if securityInfoError != nil {
cblogger.Error(securityInfoError)
return irs.SecurityInfo{}, securityInfoError
securityGroupInfo, mapError := securityHandler.mappingSecurityInfo(sg)
if mapError != nil {
cblogger.Error(mapError)
return irs.SecurityInfo{}, mapError
}
return securityGroupInfo, nil
}
Expand Down Expand Up @@ -298,34 +318,36 @@ func (securityHandler *KtCloudSecurityHandler) DeleteSecurity(securityIID irs.II
return true, nil
}

func (securityHandler *KtCloudSecurityHandler) mappingSecurityInfo(secuGroup SecurityGroup) (irs.SecurityInfo, error) {
func (securityHandler *KtCloudSecurityHandler) mappingSecurityInfo(sg SecurityGroup) (irs.SecurityInfo, error) {
cblogger.Info("KT Cloud cloud driver: called mappingSecurityInfo()!")
var securityRuleList []irs.SecurityRuleInfo
var securityRuleInfo irs.SecurityRuleInfo

for i := 0; i < len(secuGroup.Secu_Rules); i++ {
securityRuleInfo.FromPort = secuGroup.Secu_Rules[i].FromPort
securityRuleInfo.ToPort = secuGroup.Secu_Rules[i].ToPort
securityRuleInfo.IPProtocol = secuGroup.Secu_Rules[i].Protocol //KT Cloud S/G의 경우, TCP, UDP, ICMP 가능
securityRuleInfo.Direction = secuGroup.Secu_Rules[i].Direc //KT Cloud S/G의 경우 inbound rule만 지원
securityRuleInfo.CIDR = secuGroup.Secu_Rules[i].Cidr
var sgRuleList []irs.SecurityRuleInfo
var sgRuleInfo irs.SecurityRuleInfo
var sgKeyValue irs.KeyValue
var sgKeyValueList []irs.KeyValue

for i := 0; i < len(sg.Secu_Rules); i++ {
sgRuleInfo.FromPort = sg.Secu_Rules[i].FromPort
sgRuleInfo.ToPort = sg.Secu_Rules[i].ToPort
sgRuleInfo.IPProtocol = sg.Secu_Rules[i].Protocol // For KT Cloud Classic S/G, TCP/UDP/ICMP is available
sgRuleInfo.Direction = sg.Secu_Rules[i].Direc // For KT Cloud Classic S/G, supports only inbound rule.
sgRuleInfo.CIDR = sg.Secu_Rules[i].Cidr

securityRuleList = append(securityRuleList, securityRuleInfo)
sgRuleList = append(sgRuleList, sgRuleInfo)
}

for k := 0; k < len(sg.KeyValue_List); k++ {
sgKeyValue.Key = sg.KeyValue_List[k].Key
sgKeyValue.Value = sg.KeyValue_List[k].Value
sgKeyValueList = append(sgKeyValueList, sgKeyValue)
}

securityInfo := irs.SecurityInfo{
IId: irs.IID{NameId: secuGroup.IID.NameID, SystemId: secuGroup.IID.NameID},
IId: irs.IID{NameId: sg.IID.NameID, SystemId: sg.IID.NameID},
//KT Cloud의 CB에서 파일로 관리되므로 SystemId는 NameId와 동일하게
VpcIID: irs.IID{NameId: secuGroup.VpcIID.NameID, SystemId: secuGroup.VpcIID.SystemID},
SecurityRules: &securityRuleList,

// KeyValueList: []irs.KeyValue{
// {Key: "IpAddress", Value: KtCloudFirewallRule.IpAddress},
// {Key: "IpAddressID", Value: KtCloudFirewallRule.IpAddressId},
// {Key: "State", Value: KtCloudFirewallRule.State},
// },
VpcIID: irs.IID{NameId: sg.VpcIID.NameID, SystemId: sg.VpcIID.SystemID},
SecurityRules: &sgRuleList,
KeyValueList: sgKeyValueList,
}

return securityInfo, nil
}

Expand Down
Loading

0 comments on commit 8e1a48b

Please sign in to comment.