Skip to content

Commit

Permalink
Enhance registerExistingSSHKey feature
Browse files Browse the repository at this point in the history
  • Loading branch information
jihoon-seo committed Jan 25, 2022
1 parent 3a94dda commit 35829cc
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 32 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
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
58 changes: 58 additions & 0 deletions src/testclient/scripts/5.sshKey/spider-test-register-sshKey.sh
Original file line number Diff line number Diff line change
@@ -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 @- <<EOF
{
"ConnectionName": "${CONN_CONFIG[$INDEX,$REGION]}",
"ReqInfo": {
"Name": "${CONN_CONFIG[$INDEX,$REGION]}-${POSTFIX}",
"CSPId": "jhseo-test"
}
}
EOF
); echo ${resp} | jq ''
echo ""
}

#function spider_get_sshKey() {

echo "####################################################################"
echo "## 5. sshKey: Get"
echo "####################################################################"

source ../init.sh

if [ "${INDEX}" == "0" ]; then
echo "[Parallel execution for all CSP regions]"
INDEXX=${NumCSP}
for ((cspi = 1; cspi <= INDEXX; cspi++)); do
INDEXY=${NumRegion[$cspi]}
CSP=${CSPType[$cspi]}
echo "[$cspi] $CSP details"
for ((cspj = 1; cspj <= INDEXY; cspj++)); do
echo "[$cspi,$cspj] ${RegionName[$cspi,$cspj]}"

MCIRRegionName=${RegionName[$cspi,$cspj]}

CallSpider

done

done
wait

else
echo ""

MCIRRegionName=${CONN_CONFIG[$INDEX,$REGION]}

CallSpider

fi

#}

#spider_get_sshKey
Loading

0 comments on commit 35829cc

Please sign in to comment.