From 35829ccefd20149fbb156e079d13bdc668f507b3 Mon Sep 17 00:00:00 2001 From: Jihoon Seo Date: Tue, 25 Jan 2022 15:30:12 +0900 Subject: [PATCH] Enhance registerExistingSSHKey feature --- src/api/rest/docs/docs.go | 18 +++- src/api/rest/docs/swagger.json | 18 +++- src/api/rest/docs/swagger.yaml | 18 +++- src/core/mcir/sshkey.go | 82 +++++++++++++------ .../5.sshKey/spider-test-register-sshKey.sh | 58 +++++++++++++ .../5.sshKey/test-register-csp-sshKey.sh | 56 +++++++++++++ ...hKey.sh => test-register-spider-sshKey.sh} | 4 +- 7 files changed, 222 insertions(+), 32 deletions(-) create mode 100755 src/testclient/scripts/5.sshKey/spider-test-register-sshKey.sh create mode 100755 src/testclient/scripts/5.sshKey/test-register-csp-sshKey.sh rename src/testclient/scripts/5.sshKey/{test-register-sshKey.sh => test-register-spider-sshKey.sh} (93%) diff --git a/src/api/rest/docs/docs.go b/src/api/rest/docs/docs.go index 9cb4dae8e..20f5e2234 100644 --- a/src/api/rest/docs/docs.go +++ b/src/api/rest/docs/docs.go @@ -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" }, @@ -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": { @@ -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" }, @@ -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": { diff --git a/src/api/rest/docs/swagger.json b/src/api/rest/docs/swagger.json index 9325f052b..caf55670b 100644 --- a/src/api/rest/docs/swagger.json +++ b/src/api/rest/docs/swagger.json @@ -5313,6 +5313,10 @@ "connectionName": { "type": "string" }, + "cspSecurityGroupId": { + "description": "CspSecurityGroupId is required to register object from CSP (option=register)", + "type": "string" + }, "description": { "type": "string" }, @@ -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": { @@ -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" }, @@ -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": { diff --git a/src/api/rest/docs/swagger.yaml b/src/api/rest/docs/swagger.yaml index 9597bdad8..4ee08c5a3 100644 --- a/src/api/rest/docs/swagger.yaml +++ b/src/api/rest/docs/swagger.yaml @@ -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: @@ -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 @@ -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: @@ -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 diff --git a/src/core/mcir/sshkey.go b/src/core/mcir/sshkey.go index af6e2af35..de5b80942 100644 --- a/src/core/mcir/sshkey.go +++ b/src/core/mcir/sshkey.go @@ -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} @@ -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"` @@ -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"` @@ -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 @@ -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 { @@ -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) @@ -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() @@ -216,13 +241,6 @@ 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{} @@ -230,6 +248,8 @@ func CreateSshKey(nsId string, u *TbSshKeyReq, option string) (TbSshKeyInfo, err 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 @@ -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) diff --git a/src/testclient/scripts/5.sshKey/spider-test-register-sshKey.sh b/src/testclient/scripts/5.sshKey/spider-test-register-sshKey.sh new file mode 100755 index 000000000..ec5ab9038 --- /dev/null +++ b/src/testclient/scripts/5.sshKey/spider-test-register-sshKey.sh @@ -0,0 +1,58 @@ +#!/bin/bash + +function CallSpider() { + echo "- Get sshKey in ${MCIRRegionName}" + + resp=$( + curl -H "${AUTH}" -sX POST http://$SpiderServer/spider/regkeypair -H 'Content-Type: application/json' -d @- <