summaryrefslogtreecommitdiffstats
path: root/sms-service
diff options
context:
space:
mode:
authorvamshi.nemalikonda <vn00480215@techmahindra.com>2018-02-22 10:44:25 +0000
committerKiran <kiran.k.kamineni@intel.com>2018-02-22 12:55:46 -0800
commitcfc939f3cb2042c220d38ad730e993d133d926e9 (patch)
tree825feeab2a3446c19305b1742d5ad944d3036782 /sms-service
parent735839b4257e0341ab2225e763247e848bb31696 (diff)
CreateSecret implementaion
Implementaion code added for createsecret method Updated handler.go to check return value from createsecret Added checktoken before write secret Issue-ID: AAF-126 Change-Id: Ia46a0f1be5904072da74e04d123f88b09b46ed69 Signed-off-by: vamshi.nemalikonda <vn00480215@techmahindra.com>
Diffstat (limited to 'sms-service')
-rw-r--r--sms-service/src/sms/backend/backend.go6
-rw-r--r--sms-service/src/sms/backend/vault.go17
-rw-r--r--sms-service/src/sms/handler/handler.go20
-rw-r--r--sms-service/src/sms/handler/handler_test.go4
4 files changed, 34 insertions, 13 deletions
diff --git a/sms-service/src/sms/backend/backend.go b/sms-service/src/sms/backend/backend.go
index 2536fe1..3ea651a 100644
--- a/sms-service/src/sms/backend/backend.go
+++ b/sms-service/src/sms/backend/backend.go
@@ -36,8 +36,8 @@ type SecretKeyValue struct {
// Secret is the struct that defines the structure of a secret
// A single Secret can have any number of SecretKeyValue pairs
type Secret struct {
- Name string `json:"name"`
- Values map[string]string `json:"values"`
+ Name string `json:"name"`
+ Values map[string]interface{} `json:"values"`
}
// SecretBackend interface that will be implemented for various secret backends
@@ -49,7 +49,7 @@ type SecretBackend interface {
GetSecret(dom string, sec string) (Secret, error)
CreateSecretDomain(name string) (SecretDomain, error)
- CreateSecret(dom string, sec Secret) (Secret, error)
+ CreateSecret(dom string, sec Secret) error
DeleteSecretDomain(name string) error
DeleteSecret(dom string, name string) error
diff --git a/sms-service/src/sms/backend/vault.go b/sms-service/src/sms/backend/vault.go
index c912dae..2a0b3cd 100644
--- a/sms-service/src/sms/backend/vault.go
+++ b/sms-service/src/sms/backend/vault.go
@@ -20,6 +20,7 @@ import (
uuid "github.com/hashicorp/go-uuid"
vaultapi "github.com/hashicorp/vault/api"
+ "errors"
"fmt"
"log"
"strings"
@@ -128,9 +129,21 @@ func (v *Vault) CreateSecretDomain(name string) (SecretDomain, error) {
// CreateSecret creates a secret mounted on a particular domain name
// The secret itself is mounted on a path specified by name
-func (v *Vault) CreateSecret(dom string, sec Secret) (Secret, error) {
+func (v *Vault) CreateSecret(dom string, sec Secret) error {
+ err := v.checkToken()
+ if err != nil {
+ return errors.New("Token checking returned an error" + err.Error())
+ }
- return Secret{}, nil
+ dom = v.vaultMount + "/" + dom
+
+ // Vault write return is empty on successful write
+ _, err = v.vaultClient.Logical().Write(dom+"/"+sec.Name, sec.Values)
+ if err != nil {
+ return errors.New("Unable to create Secret at provided path")
+ }
+
+ return nil
}
// DeleteSecretDomain deletes a secret domain which translates to
diff --git a/sms-service/src/sms/handler/handler.go b/sms-service/src/sms/handler/handler.go
index f287263..ed68c81 100644
--- a/sms-service/src/sms/handler/handler.go
+++ b/sms-service/src/sms/handler/handler.go
@@ -18,6 +18,7 @@ package handler
import (
"encoding/json"
+ "fmt"
"github.com/gorilla/mux"
"net/http"
@@ -43,7 +44,7 @@ func (h handler) createSecretDomainHandler(w http.ResponseWriter, r *http.Reques
dom, err := h.secretBackend.CreateSecretDomain(d.Name)
if err != nil {
- http.Error(w, err.Error(), 400)
+ http.Error(w, err.Error(), 500)
return
}
@@ -59,7 +60,8 @@ func (h handler) getSecretDomainHandler(w http.ResponseWriter, r *http.Request)
vars := mux.Vars(r)
domName := vars["domName"]
- h.secretBackend.GetSecretDomain(domName)
+ fmt.Fprintf(w, "Got req %s", domName)
+ //h.secretBackend.GetSecretDomain(domName)
//encode data into json and return
}
@@ -85,7 +87,13 @@ func (h handler) createSecretHandler(w http.ResponseWriter, r *http.Request) {
return
}
- h.secretBackend.CreateSecret(domName, b)
+ err = h.secretBackend.CreateSecret(domName, b)
+ if err != nil {
+ http.Error(w, err.Error(), 500)
+ return
+ }
+
+ w.WriteHeader(http.StatusCreated)
}
// getSecretHandler handles reading a secret by given domain name and secret name
@@ -163,9 +171,9 @@ func CreateRouter(b smsbackend.SecretBackend) http.Handler {
router.HandleFunc("/v1/sms/domain/{domName}", h.getSecretDomainHandler).Methods("GET")
router.HandleFunc("/v1/sms/domain/{domName}", h.deleteSecretDomainHandler).Methods("DELETE")
- router.HandleFunc("v1/sms/domain/{domainName}/secret", h.createSecretHandler).Methods("POST")
- router.HandleFunc("v1/sms/domain/{domainName}/secret/{secretName}", h.getSecretHandler).Methods("GET")
- router.HandleFunc("v1/sms/domain/{domainName}/secret/{secretName}", h.deleteSecretHandler).Methods("DELETE")
+ router.HandleFunc("/v1/sms/domain/{domName}/secret", h.createSecretHandler).Methods("POST")
+ router.HandleFunc("/v1/sms/domain/{domName}/secret/{secretName}", h.getSecretHandler).Methods("GET")
+ router.HandleFunc("/v1/sms/domain/{domName}/secret/{secretName}", h.deleteSecretHandler).Methods("DELETE")
return router
}
diff --git a/sms-service/src/sms/handler/handler_test.go b/sms-service/src/sms/handler/handler_test.go
index 3ca2ae6..affa6f7 100644
--- a/sms-service/src/sms/handler/handler_test.go
+++ b/sms-service/src/sms/handler/handler_test.go
@@ -54,8 +54,8 @@ func (b *TestBackend) CreateSecretDomain(name string) (smsbackend.SecretDomain,
return smsbackend.SecretDomain{}, nil
}
-func (b *TestBackend) CreateSecret(dom string, sec smsbackend.Secret) (smsbackend.Secret, error) {
- return smsbackend.Secret{}, nil
+func (b *TestBackend) CreateSecret(dom string, sec smsbackend.Secret) error {
+ return nil
}
func (b *TestBackend) DeleteSecretDomain(name string) error {