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 c458ede commit bf2ebef
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 24 deletions.
5 changes: 4 additions & 1 deletion src/api/rest/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5496,6 +5496,9 @@ var doc = `{
"connectionName": {
"type": "string"
},
"cspSshKeyId": {
"type": "string"
},
"cspSshKeyName": {
"type": "string"
},
Expand Down Expand Up @@ -5549,7 +5552,7 @@ var doc = `{
"connectionName": {
"type": "string"
},
"cspSshKeyName": {
"cspSshKeyId": {
"description": "Fields for \"Register existing SSH keys\" feature",
"type": "string"
},
Expand Down
5 changes: 4 additions & 1 deletion src/api/rest/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -5482,6 +5482,9 @@
"connectionName": {
"type": "string"
},
"cspSshKeyId": {
"type": "string"
},
"cspSshKeyName": {
"type": "string"
},
Expand Down Expand Up @@ -5535,7 +5538,7 @@
"connectionName": {
"type": "string"
},
"cspSshKeyName": {
"cspSshKeyId": {
"description": "Fields for \"Register existing SSH keys\" feature",
"type": "string"
},
Expand Down
4 changes: 3 additions & 1 deletion src/api/rest/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,8 @@ definitions:
type: array
connectionName:
type: string
cspSshKeyId:
type: string
cspSshKeyName:
type: string
description:
Expand Down Expand Up @@ -630,7 +632,7 @@ definitions:
properties:
connectionName:
type: string
cspSshKeyName:
cspSshKeyId:
description: Fields for "Register existing SSH keys" feature
type: string
description:
Expand Down
66 changes: 47 additions & 19 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,7 @@ type TbSshKeyReq struct {
Description string `json:"description"`

// Fields for "Register existing SSH keys" feature
CspSshKeyName string `json:"cspSshKeyName"`
CspSshKeyId string `json:"cspSshKeyId"`
Fingerprint string `json:"fingerprint"`
Username string `json:"username"`
VerifiedUsername string `json:"verifiedUsername"`
Expand All @@ -79,6 +80,7 @@ type TbSshKeyInfo struct {
Name string `json:"name"`
ConnectionName string `json:"connectionName"`
Description string `json:"description"`
CspSshKeyId string `json:"cspSshKeyId"`
CspSshKeyName string `json:"cspSshKeyName"`
Fingerprint string `json:"fingerprint"`
Username string `json:"username"`
Expand All @@ -88,6 +90,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 +107,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 +154,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 +203,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 +235,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 +253,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
56 changes: 56 additions & 0 deletions src/testclient/scripts/5.sshKey/test-register-csp-sshKey.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/bin/bash

function CallTB() {
echo "- Register sshKey in ${MCIRRegionName}"

curl -H "${AUTH}" -sX POST http://$TumblebugServer/tumblebug/ns/$NSID/resources/sshKey?option=register -H 'Content-Type: application/json' -d \
'{
"connectionName": "'${CONN_CONFIG[$INDEX,$REGION]}'",
"name": "'${CONN_CONFIG[$INDEX,$REGION]}'-'${POSTFIX}'",
"cspSshKeyId": "jhseo-test",
"fingerprint": "test-fingerprint",
"username": "test-username",
"publicKey": "test-public-key",
"privateKey": "test-private-key"
}' | jq ''
}

#function register_sshKey() {

echo "####################################################################"
echo "## 5. sshKey: Register"
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]}

CallTB

done

done
wait

else
echo ""

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

CallTB

fi

#}

#register_sshKey
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ function CallTB() {
'{
"connectionName": "'${CONN_CONFIG[$INDEX,$REGION]}'",
"name": "'${CONN_CONFIG[$INDEX,$REGION]}'-'${POSTFIX}'",
"cspSshKeyName": "'${NSID}-${CONN_CONFIG[$INDEX,$REGION]}-${POSTFIX}'",
"cspSshKeyId": "",
"fingerprint": "xx:c4:5a:ea:7f:c4:db:d5:80:80:92:47:7e:43:c9:2c:01:d3:ee:xx",
"username": "cb-user",
"publicKey": "",
"privateKey": "-----BEGIN RSA PRIVATE KEY-----\nMIIE....Kplg==\n-----END RSA PRIVATE KEY-----"
}' | jq '.message'
}' | jq ''
}

#function register_sshKey() {
Expand Down

0 comments on commit bf2ebef

Please sign in to comment.