Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add registerExistingSSHKey feature #1016

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/api/grpc/server/mcir/sshkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (s *MCIRService) CreateSshKey(ctx context.Context, req *pb.TbSshKeyCreateRe
return nil, gc.ConvGrpcStatusErr(err, "", "MCIRService.CreateSshKey()")
}

content, err := mcir.CreateSshKey(req.NsId, &mcirObj)
content, err := mcir.CreateSshKey(req.NsId, &mcirObj, "")
if err != nil {
return nil, gc.ConvGrpcStatusErr(err, "", "MCIRService.CreateSshKey()")
}
Expand Down
19 changes: 19 additions & 0 deletions src/api/rest/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5541,11 +5541,30 @@ var doc = `{
"connectionName": {
"type": "string"
},
"cspSshKeyName": {
"description": "Fields for \"Register existing SSH keys\" feature",
"type": "string"
},
"description": {
"type": "string"
},
"fingerprint": {
"type": "string"
},
"name": {
"type": "string"
},
"privateKey": {
"type": "string"
},
"publicKey": {
"type": "string"
},
"username": {
"type": "string"
},
"verifiedUsername": {
"type": "string"
}
}
},
Expand Down
19 changes: 19 additions & 0 deletions src/api/rest/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -5527,11 +5527,30 @@
"connectionName": {
"type": "string"
},
"cspSshKeyName": {
"description": "Fields for \"Register existing SSH keys\" feature",
"type": "string"
},
"description": {
"type": "string"
},
"fingerprint": {
"type": "string"
},
"name": {
"type": "string"
},
"privateKey": {
"type": "string"
},
"publicKey": {
"type": "string"
},
"username": {
"type": "string"
},
"verifiedUsername": {
"type": "string"
}
}
},
Expand Down
13 changes: 13 additions & 0 deletions src/api/rest/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -623,10 +623,23 @@ definitions:
properties:
connectionName:
type: string
cspSshKeyName:
description: Fields for "Register existing SSH keys" feature
type: string
description:
type: string
fingerprint:
type: string
name:
type: string
privateKey:
type: string
publicKey:
type: string
username:
type: string
verifiedUsername:
type: string
required:
- connectionName
- name
Expand Down
8 changes: 4 additions & 4 deletions src/api/rest/server/mcir/sshkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@ import (
// @Failure 500 {object} common.SimpleMsg
// @Router /ns/{nsId}/resources/sshKey [post]
func RestPostSshKey(c echo.Context) error {
fmt.Println("[POST SshKey]")

nsId := c.Param("nsId")

optionFlag := c.QueryParam("option")

u := &mcir.TbSshKeyReq{}
if err := c.Bind(u); err != nil {
return err
}

fmt.Println("[POST SshKey")
//fmt.Println("[Creating SshKey]")
//content, responseCode, _, err := CreateSshKey(nsId, u)
content, err := mcir.CreateSshKey(nsId, u)
content, err := mcir.CreateSshKey(nsId, u, optionFlag)
if err != nil {
common.CBLog.Error(err)
mapA := map[string]string{"message": err.Error()}
Expand Down
2 changes: 1 addition & 1 deletion src/core/mcir/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -1990,7 +1990,7 @@ func LoadDefaultResource(nsId string, resType string, connectionName string) err

common.PrintJsonPretty(reqTmp)

resultInfo, err := CreateSshKey(nsId, &reqTmp)
resultInfo, err := CreateSshKey(nsId, &reqTmp, "")
if err != nil {
common.CBLog.Error(err)
// If already exist, error will occur
Expand Down
58 changes: 35 additions & 23 deletions src/core/mcir/sshkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ type TbSshKeyReq struct {
Name string `json:"name" validate:"required"`
ConnectionName string `json:"connectionName" validate:"required"`
Description string `json:"description"`

// Fields for "Register existing SSH keys" feature
CspSshKeyName string `json:"cspSshKeyName"`
Fingerprint string `json:"fingerprint"`
Username string `json:"username"`
VerifiedUsername string `json:"verifiedUsername"`
PublicKey string `json:"publicKey"`
PrivateKey string `json:"privateKey"`
}

// TbSshKeyReqStructLevelValidation is a function to validate 'TbSshKeyReq' object.
Expand Down Expand Up @@ -83,7 +91,7 @@ type TbSshKeyInfo struct {
}

// CreateSshKey accepts SSH key creation request, creates and returns an TB sshKey object
func CreateSshKey(nsId string, u *TbSshKeyReq) (TbSshKeyInfo, error) {
func CreateSshKey(nsId string, u *TbSshKeyReq, option string) (TbSshKeyInfo, error) {

resourceType := common.StrSSHKey

Expand All @@ -94,34 +102,31 @@ func CreateSshKey(nsId string, u *TbSshKeyReq) (TbSshKeyInfo, error) {
return temp, err
}

// returns InvalidValidationError for bad validation input, nil or ValidationErrors ( []FieldError )
if option == "register" {
errs := []error{}
errs = append(errs, validate.Var(u.Username, "required"))
errs = append(errs, validate.Var(u.PrivateKey, "required"))

for _, err := range errs {
if err != nil {
temp := TbSshKeyInfo{}
if _, ok := err.(*validator.InvalidValidationError); ok {
fmt.Println(err)
return temp, err
}
return temp, err
}
}
}

err = validate.Struct(u)
if err != nil {

// this check is only needed when your code could produce
// an invalid value for validation such as interface with nil
// value most including myself do not usually have code like this.
if _, ok := err.(*validator.InvalidValidationError); ok {
fmt.Println(err)
temp := TbSshKeyInfo{}
return temp, err
}

// for _, err := range err.(validator.ValidationErrors) {

// fmt.Println(err.Namespace()) // can differ when a custom TagNameFunc is registered or
// fmt.Println(err.Field()) // by passing alt name to ReportError like below
// fmt.Println(err.StructNamespace())
// fmt.Println(err.StructField())
// fmt.Println(err.Tag())
// fmt.Println(err.ActualTag())
// fmt.Println(err.Kind())
// fmt.Println(err.Type())
// fmt.Println(err.Value())
// fmt.Println(err.Param())
// fmt.Println()
// }

temp := TbSshKeyInfo{}
return temp, err
}
Expand All @@ -147,7 +152,7 @@ func CreateSshKey(nsId string, u *TbSshKeyReq) (TbSshKeyInfo, error) {

var tempSpiderKeyPairInfo *SpiderKeyPairInfo

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

url := common.SpiderRestUrl + "/keypair"

Expand Down Expand Up @@ -179,7 +184,7 @@ func CreateSshKey(nsId string, u *TbSshKeyReq) (TbSshKeyInfo, error) {

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

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

// Set CCM gRPC API
ccm := api.NewCloudResourceHandler()
Expand Down Expand Up @@ -211,6 +216,13 @@ func CreateSshKey(nsId string, u *TbSshKeyReq) (TbSshKeyInfo, error) {
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{}
Expand Down
2 changes: 1 addition & 1 deletion src/testclient/scripts/5.sshKey/spider-get-sshKey.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function CallSpider() {
echo "- Get sshKey in ${MCIRRegionName}"

resp=$(
curl -H "${AUTH}" -sX GET http://$SpiderServer/spider/keypair/${CONN_CONFIG[$INDEX,$REGION]}-${POSTFIX}?force=true -H 'Content-Type: application/json' -d @- <<EOF
curl -H "${AUTH}" -sX GET http://$SpiderServer/spider/keypair/${NSID}-${CONN_CONFIG[$INDEX,$REGION]}-${POSTFIX}?force=true -H 'Content-Type: application/json' -d @- <<EOF
{
"ConnectionName": "${CONN_CONFIG[$INDEX,$REGION]}"
}
Expand Down
56 changes: 56 additions & 0 deletions src/testclient/scripts/5.sshKey/test-register-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}'",
"cspSshKeyName": "'${NSID}-${CONN_CONFIG[$INDEX,$REGION]}-${POSTFIX}'",
"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'
}

#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