forked from gaia-pipeline/gaia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vault.go
95 lines (88 loc) · 2.28 KB
/
vault.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package handlers
import (
"net/http"
"github.com/gaia-pipeline/gaia/services"
"github.com/labstack/echo"
)
type addSecret struct {
Key string `json:"key"`
Value string `json:"value"`
}
type updateSecret struct {
Key string `json:"key"`
Value string `json:"newvalue"`
}
// SetSecret creates or updates a given secret
func SetSecret(c echo.Context) error {
var key, value string
if c.Request().Method == "POST" {
s := new(addSecret)
err := c.Bind(s)
if err != nil {
return c.String(http.StatusBadRequest, err.Error())
}
key = s.Key
value = s.Value
} else if c.Request().Method == "PUT" {
s := new(updateSecret)
err := c.Bind(s)
if err != nil {
return c.String(http.StatusBadRequest, err.Error())
}
key = s.Key
value = s.Value
}
v, err := services.VaultService(nil)
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
err = v.LoadSecrets()
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
v.Add(key, []byte(value))
err = v.SaveSecrets()
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
return c.String(http.StatusCreated, "secret successfully set")
}
// ListSecrets retrieves all secrets from the vault.
func ListSecrets(c echo.Context) error {
secrets := make([]addSecret, 0)
v, err := services.VaultService(nil)
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
err = v.LoadSecrets()
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
kvs := v.GetAll()
for _, k := range kvs {
s := addSecret{Key: k, Value: "**********"}
secrets = append(secrets, s)
}
return c.JSON(http.StatusOK, secrets)
}
// RemoveSecret removes a secret from the vault.
func RemoveSecret(c echo.Context) error {
key := c.Param("key")
if key == "" {
return c.String(http.StatusBadRequest, "invalid key given")
}
v, err := services.VaultService(nil)
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
err = v.LoadSecrets()
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
v.Remove(key)
err = v.SaveSecrets()
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
return c.String(http.StatusOK, "secret successfully deleted")
}