Skip to content

Commit

Permalink
Merge pull request #2659 from LiilyZhang/zhangl/issue2657
Browse files Browse the repository at this point in the history
Issue 2657 - Format change of GET /secrets/secretName response
  • Loading branch information
dabooz committed Jul 22, 2021
2 parents dc1cc78 + 79858b4 commit 2e76384
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
21 changes: 10 additions & 11 deletions resource/secret_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package resource

import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/boltdb/bolt"
Expand All @@ -42,11 +43,11 @@ type SecretAPI struct {
authenticator *SecretsAPIAuthenticate
}

// secretObject includes the secret details.
// The details are a JSON document containing {"key":"<key>", "value": "<value>"} that has been marshalled into a string and then base64 encoded.
// secretObject includes the secret key and secret value.
// swagger:model
type secretObject struct {
Details string `json:"details"`
Key string `json:"key"`
Value string `json:"value"`
}

func NewSecretAPI(db *bolt.DB, am *AuthenticationManager) *SecretAPI {
Expand Down Expand Up @@ -321,14 +322,12 @@ func (api *SecretAPI) handleSecrets(writer http.ResponseWriter, request *http.Re
glog.V(3).Infof(secAPILogString(fmt.Sprintf("SecretStatus saved for MSSInstance: %v", savedMSSInst.String())))
}

// return secret name and secret details
m := make(map[string]*secretObject, 0)

secretName := psecret.SvcSecretName
secretObj := &secretObject{Details: psecret.SvcSecretValue}
m[secretName] = secretObj

if data, err := json.MarshalIndent(m, "", " "); err != nil {
var sobj secretObject
if dbyte, err := base64.StdEncoding.DecodeString(psecret.SvcSecretValue); err != nil {
returnErrorResponse(writer, err, "Failed to decode the secret details.", http.StatusInternalServerError)
} else if err := json.Unmarshal(dbyte, &sobj); err != nil {
returnErrorResponse(writer, err, "Failed to unmarshal the secret byte to object.", http.StatusInternalServerError)
} else if data, err := json.MarshalIndent(sobj, "", " "); err != nil {
returnErrorResponse(writer, err, "Failed to marshal the secret.", http.StatusInternalServerError)
} else {
writer.Header().Add(contentType, applicationJSON)
Expand Down
11 changes: 7 additions & 4 deletions resource/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,16 @@
},
"definitions": {
"secretObject": {
"description": "The details are a JSON document containing {\"key\":\"\u003ckey\u003e\", \"value\": \"\u003cvalue\u003e\"} that has been marshalled into a string and then base64 encoded.",
"type": "object",
"title": "secretObject includes the secret details.",
"title": "secretObject includes the secret key and secret value.",
"properties": {
"details": {
"key": {
"type": "string",
"x-go-name": "Details"
"x-go-name": "Key"
},
"value": {
"type": "string",
"x-go-name": "Value"
}
},
"x-go-package": "github.com/open-horizon/anax/resource"
Expand Down

0 comments on commit 2e76384

Please sign in to comment.