Skip to content

Commit

Permalink
Merge pull request #1019 from jihoon-seo/220124_Enhance_registerExist…
Browse files Browse the repository at this point in the history
…ingSG-SSHKey_feature

Enhance `registerExisting SG/SSHKey` feature
  • Loading branch information
seokho-son authored Jan 25, 2022
2 parents 6c872c6 + 35829cc commit 4be0655
Show file tree
Hide file tree
Showing 10 changed files with 292 additions and 35 deletions.
18 changes: 16 additions & 2 deletions src/api/rest/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5327,6 +5327,10 @@ var doc = `{
"connectionName": {
"type": "string"
},
"cspSecurityGroupId": {
"description": "CspSecurityGroupId is required to register object from CSP (option=register)",
"type": "string"
},
"description": {
"type": "string"
},
Expand Down Expand Up @@ -5493,7 +5497,12 @@ var doc = `{
"connectionName": {
"type": "string"
},
"cspSshKeyId": {
"description": "CspSshKeyId used for CSP-native identifier (either Name or ID)",
"type": "string"
},
"cspSshKeyName": {
"description": "CspSshKeyName used for CB-Spider identifier",
"type": "string"
},
"description": {
Expand Down Expand Up @@ -5523,6 +5532,11 @@ var doc = `{
"publicKey": {
"type": "string"
},
"systemLabel": {
"description": "SystemLabel is for describing the MCIR in a keyword (any string can be used) for special System purpose",
"type": "string",
"example": "Managed by CB-Tumblebug"
},
"username": {
"type": "string"
},
Expand All @@ -5541,8 +5555,8 @@ var doc = `{
"connectionName": {
"type": "string"
},
"cspSshKeyName": {
"description": "Fields for \"Register existing SSH keys\" feature",
"cspSshKeyId": {
"description": "Fields for \"Register existing SSH keys\" feature\nCspSshKeyId is required to register object from CSP (option=register)",
"type": "string"
},
"description": {
Expand Down
18 changes: 16 additions & 2 deletions src/api/rest/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -5313,6 +5313,10 @@
"connectionName": {
"type": "string"
},
"cspSecurityGroupId": {
"description": "CspSecurityGroupId is required to register object from CSP (option=register)",
"type": "string"
},
"description": {
"type": "string"
},
Expand Down Expand Up @@ -5479,7 +5483,12 @@
"connectionName": {
"type": "string"
},
"cspSshKeyId": {
"description": "CspSshKeyId used for CSP-native identifier (either Name or ID)",
"type": "string"
},
"cspSshKeyName": {
"description": "CspSshKeyName used for CB-Spider identifier",
"type": "string"
},
"description": {
Expand Down Expand Up @@ -5509,6 +5518,11 @@
"publicKey": {
"type": "string"
},
"systemLabel": {
"description": "SystemLabel is for describing the MCIR in a keyword (any string can be used) for special System purpose",
"type": "string",
"example": "Managed by CB-Tumblebug"
},
"username": {
"type": "string"
},
Expand All @@ -5527,8 +5541,8 @@
"connectionName": {
"type": "string"
},
"cspSshKeyName": {
"description": "Fields for \"Register existing SSH keys\" feature",
"cspSshKeyId": {
"description": "Fields for \"Register existing SSH keys\" feature\nCspSshKeyId is required to register object from CSP (option=register)",
"type": "string"
},
"description": {
Expand Down
18 changes: 16 additions & 2 deletions src/api/rest/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,9 @@ definitions:
properties:
connectionName:
type: string
cspSecurityGroupId:
description: CspSecurityGroupId is required to register object from CSP (option=register)
type: string
description:
type: string
firewallRules:
Expand Down Expand Up @@ -594,7 +597,11 @@ definitions:
type: array
connectionName:
type: string
cspSshKeyId:
description: CspSshKeyId used for CSP-native identifier (either Name or ID)
type: string
cspSshKeyName:
description: CspSshKeyName used for CB-Spider identifier
type: string
description:
type: string
Expand All @@ -614,6 +621,11 @@ definitions:
type: string
publicKey:
type: string
systemLabel:
description: SystemLabel is for describing the MCIR in a keyword (any string
can be used) for special System purpose
example: Managed by CB-Tumblebug
type: string
username:
type: string
verifiedUsername:
Expand All @@ -623,8 +635,10 @@ definitions:
properties:
connectionName:
type: string
cspSshKeyName:
description: Fields for "Register existing SSH keys" feature
cspSshKeyId:
description: |-
Fields for "Register existing SSH keys" feature
CspSshKeyId is required to register object from CSP (option=register)
type: string
description:
type: string
Expand Down
16 changes: 13 additions & 3 deletions src/core/mcir/securitygroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type SpiderSecurityInfo struct { // Spider
// Fields for request
Name string
VPCName string
CSPId string

// Fields for both request and response
SecurityRules *[]SpiderSecurityRuleInfo
Expand All @@ -64,6 +65,9 @@ type TbSecurityGroupReq struct { // Tumblebug
VNetId string `json:"vNetId" validate:"required"`
Description string `json:"description"`
FirewallRules *[]SpiderSecurityRuleInfo `json:"firewallRules"` // validate:"required"`

// CspSecurityGroupId is required to register object from CSP (option=register)
CspSecurityGroupId string `json:"cspSecurityGroupId"`
}

// TbSecurityGroupReqStructLevelValidation is a function to validate 'TbSecurityGroupReq' object.
Expand Down Expand Up @@ -178,6 +182,7 @@ func CreateSecurityGroup(nsId string, u *TbSecurityGroupReq, option string) (TbS
tempReq.ReqInfo.Name = u.Name
tempReq.ReqInfo.VPCName = vNetInfo.CspVNetName
tempReq.ReqInfo.SecurityRules = u.FirewallRules
tempReq.ReqInfo.CSPId = u.CspSecurityGroupId

var tempSpiderSecurityInfo *SpiderSecurityInfo

Expand All @@ -196,10 +201,13 @@ func CreateSecurityGroup(nsId string, u *TbSecurityGroupReq, option string) (TbS
var err error

var url string
if option == "register" {
if option == "register" && u.CspSecurityGroupId == "" {
url = fmt.Sprintf("%s/securitygroup/%s", common.SpiderRestUrl, u.Name)
resp, err = req.Get(url)
} else {
} else if option == "register" && u.CspSecurityGroupId != "" {
url = fmt.Sprintf("%s/regsecuritygroup", common.SpiderRestUrl)
resp, err = req.Post(url)
} else { // option != "register"
url = fmt.Sprintf("%s/securitygroup", common.SpiderRestUrl)
resp, err = req.Post(url)
}
Expand Down Expand Up @@ -275,7 +283,9 @@ func CreateSecurityGroup(nsId string, u *TbSecurityGroupReq, option string) (TbS
content.KeyValueList = tempSpiderSecurityInfo.KeyValueList
content.AssociatedObjectList = []string{}

if option == "register" {
if option == "register" && u.CspSecurityGroupId == "" {
content.SystemLabel = "Registered from CB-Spider resource"
} else if option == "register" && u.CspSecurityGroupId != "" {
content.SystemLabel = "Registered from CSP resource"
}

Expand Down
82 changes: 58 additions & 24 deletions src/core/mcir/sshkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ type SpiderKeyPairReqInfoWrapper struct { // Spider
// SpiderKeyPairInfo is a struct to create JSON body of 'Create keypair request'
type SpiderKeyPairInfo struct { // Spider
// Fields for request
Name string
Name string
CSPId string

// Fields for response
IId common.IID // {NameId, SystemId}
Expand All @@ -53,7 +54,8 @@ type TbSshKeyReq struct {
Description string `json:"description"`

// Fields for "Register existing SSH keys" feature
CspSshKeyName string `json:"cspSshKeyName"`
// CspSshKeyId is required to register object from CSP (option=register)
CspSshKeyId string `json:"cspSshKeyId"`
Fingerprint string `json:"fingerprint"`
Username string `json:"username"`
VerifiedUsername string `json:"verifiedUsername"`
Expand All @@ -75,11 +77,17 @@ func TbSshKeyReqStructLevelValidation(sl validator.StructLevel) {

// TbSshKeyInfo is a struct that represents TB SSH key object.
type TbSshKeyInfo struct {
Id string `json:"id"`
Name string `json:"name"`
ConnectionName string `json:"connectionName"`
Description string `json:"description"`
CspSshKeyName string `json:"cspSshKeyName"`
Id string `json:"id"`
Name string `json:"name"`
ConnectionName string `json:"connectionName"`
Description string `json:"description"`

// CspSshKeyId used for CSP-native identifier (either Name or ID)
CspSshKeyId string `json:"cspSshKeyId"`

// CspSshKeyName used for CB-Spider identifier
CspSshKeyName string `json:"cspSshKeyName"`

Fingerprint string `json:"fingerprint"`
Username string `json:"username"`
VerifiedUsername string `json:"verifiedUsername"`
Expand All @@ -88,6 +96,9 @@ type TbSshKeyInfo struct {
KeyValueList []common.KeyValue `json:"keyValueList"`
AssociatedObjectList []string `json:"associatedObjectList"`
IsAutoGenerated bool `json:"isAutoGenerated"`

// SystemLabel is for describing the MCIR in a keyword (any string can be used) for special System purpose
SystemLabel string `json:"systemLabel" example:"Managed by CB-Tumblebug" default:""`
}

// CreateSshKey accepts SSH key creation request, creates and returns an TB sshKey object
Expand All @@ -102,10 +113,10 @@ func CreateSshKey(nsId string, u *TbSshKeyReq, option string) (TbSshKeyInfo, err
return temp, err
}

if option == "register" {
if option == "register" { // fields validation
errs := []error{}
errs = append(errs, validate.Var(u.Username, "required"))
errs = append(errs, validate.Var(u.PrivateKey, "required"))
// errs = append(errs, validate.Var(u.Username, "required"))
// errs = append(errs, validate.Var(u.PrivateKey, "required"))

for _, err := range errs {
if err != nil {
Expand Down Expand Up @@ -149,21 +160,35 @@ func CreateSshKey(nsId string, u *TbSshKeyReq, option string) (TbSshKeyInfo, err
tempReq := SpiderKeyPairReqInfoWrapper{}
tempReq.ConnectionName = u.ConnectionName
tempReq.ReqInfo.Name = nsId + "-" + u.Name
tempReq.ReqInfo.CSPId = u.CspSshKeyId

var tempSpiderKeyPairInfo *SpiderKeyPairInfo

if os.Getenv("SPIDER_CALL_METHOD") == "REST" && option != "register" {

url := common.SpiderRestUrl + "/keypair"
if os.Getenv("SPIDER_CALL_METHOD") == "REST" {

client := resty.New().SetCloseConnection(true)
client.SetAllowGetMethodPayload(true)

resp, err := client.R().
req := client.R().
SetHeader("Content-Type", "application/json").
SetBody(tempReq).
SetResult(&SpiderKeyPairInfo{}). // or SetResult(AuthSuccess{}).
SetResult(&SpiderKeyPairInfo{}) // or SetResult(AuthSuccess{}).
//SetError(&AuthError{}). // or SetError(AuthError{}).
Post(url)

var resp *resty.Response
var err error

var url string
if option == "register" && u.CspSshKeyId == "" {
url = fmt.Sprintf("%s/keypair/%s", common.SpiderRestUrl, u.Name)
resp, err = req.Get(url)
} else if option == "register" && u.CspSshKeyId != "" {
url = fmt.Sprintf("%s/regkeypair", common.SpiderRestUrl)
resp, err = req.Post(url)
} else { // option != "register"
url = fmt.Sprintf("%s/keypair", common.SpiderRestUrl)
resp, err = req.Post(url)
}

if err != nil {
common.CBLog.Error(err)
Expand All @@ -184,7 +209,7 @@ func CreateSshKey(nsId string, u *TbSshKeyReq, option string) (TbSshKeyInfo, err

tempSpiderKeyPairInfo = resp.Result().(*SpiderKeyPairInfo)

} else if os.Getenv("SPIDER_CALL_METHOD") != "REST" && option != "register" {
} else { // gRPC

// Set CCM gRPC API
ccm := api.NewCloudResourceHandler()
Expand Down Expand Up @@ -216,20 +241,15 @@ func CreateSshKey(nsId string, u *TbSshKeyReq, option string) (TbSshKeyInfo, err
return TbSshKeyInfo{}, err
}

} else { // option == "register"
tempSpiderKeyPairInfo = &SpiderKeyPairInfo{}
tempSpiderKeyPairInfo.IId.NameId = u.CspSshKeyName
tempSpiderKeyPairInfo.Fingerprint = u.Fingerprint
tempSpiderKeyPairInfo.VMUserID = u.Username
tempSpiderKeyPairInfo.PublicKey = u.PublicKey
tempSpiderKeyPairInfo.PrivateKey = u.PrivateKey
}

content := TbSshKeyInfo{}
//content.Id = common.GenUid()
content.Id = u.Name
content.Name = u.Name
content.ConnectionName = u.ConnectionName
fmt.Printf("tempSpiderKeyPairInfo.IId.SystemId: %s \n", tempSpiderKeyPairInfo.IId.SystemId)
content.CspSshKeyId = tempSpiderKeyPairInfo.IId.SystemId
content.CspSshKeyName = tempSpiderKeyPairInfo.IId.NameId
content.Fingerprint = tempSpiderKeyPairInfo.Fingerprint
content.Username = tempSpiderKeyPairInfo.VMUserID
Expand All @@ -239,6 +259,20 @@ func CreateSshKey(nsId string, u *TbSshKeyReq, option string) (TbSshKeyInfo, err
content.KeyValueList = tempSpiderKeyPairInfo.KeyValueList
content.AssociatedObjectList = []string{}

if option == "register" {
if u.CspSshKeyId == "" {
content.SystemLabel = "Registered from CB-Spider resource"
} else if u.CspSshKeyId != "" {
content.SystemLabel = "Registered from CSP resource"
}

// Rewrite fields again
// content.Fingerprint = u.Fingerprint
content.Username = u.Username
content.PublicKey = u.PublicKey
content.PrivateKey = u.PrivateKey
}

// cb-store
fmt.Println("=========================== PUT CreateSshKey")
Key := common.GenResourceKey(nsId, resourceType, content.Id)
Expand Down
Loading

0 comments on commit 4be0655

Please sign in to comment.