-
Notifications
You must be signed in to change notification settings - Fork 5
/
secrets.go
200 lines (186 loc) · 4.48 KB
/
secrets.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package main
import (
"encoding/base64"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"regexp"
"strings"
"time"
"github.com/mijia/sweb/log"
"golang.org/x/net/context"
)
var (
slugPattern = regexp.MustCompile(`^[A-Za-z][A-Za-z0-9_]*(.[a-zA-Z0-9]+)*$`)
coder = base64.StdEncoding
)
const (
APPNAME string = "app"
PROCNAME = "proc"
PATHNAME = "path"
DATAPATHPREFIX = "secret/lvault/"
)
func ValidateSlug(slug string) error {
if slug == "" {
return errors.New("Empty slug")
}
if len(slug) > 256 {
return errors.New("Slug too long")
}
if !slugPattern.MatchString(slug) {
log.Debug("don't match")
return errors.New("Invalid slug")
}
return nil
}
func parseErr(err error) (int, []byte) {
if strings.Contains(err.Error(), "Vault is sealed") {
return http.StatusServiceUnavailable, []byte(err.Error())
} else {
return http.StatusInternalServerError, []byte(err.Error())
}
}
func (l *Lvault) SecretsEndpoint(ctx context.Context, w http.ResponseWriter, req *http.Request) context.Context {
log.Infof("Receive a request: %+v, now: %v.", req, time.Now())
body, _ := ioutil.ReadAll(req.Body)
req.ParseForm()
// Auth
if ok := checkMaintainerAuth(req); !ok {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte(`{"error":"authentication failed"}`))
log.Debug(time.Now().UnixNano())
return ctx
}
log.Debug(time.Now().UnixNano())
app, proc, path, err := getCheckedParams(req)
log.Debug(app)
log.Debug(proc)
log.Debug(err)
log.Debug(path)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
log.Debug(time.Now().UnixNano())
return ctx
}
switch req.Method {
case "GET":
var totalPath string
if proc == "" {
totalPath = DATAPATHPREFIX + app
} else {
totalPath = DATAPATHPREFIX + app + "/" + proc
}
secrets, err := l.Vault.ListSecrets(l.RootToken, totalPath)
if err != nil {
log.Error(err)
code, retD := parseErr(err)
w.WriteHeader(code)
w.Write(retD)
log.Debug(time.Now().UnixNano())
return ctx
}
var data []Data
data = []Data{}
for i := 0; i < len(secrets); i++ {
var singleData Data
plainData, _ := coder.DecodeString(secrets[i])
err = json.Unmarshal(plainData, &singleData)
if err != nil {
log.Error(err)
panic(err)
}
if !strings.EqualFold(singleData.Path, "") {
data = append(data, singleData)
}
}
retData, err := json.Marshal(data)
if err != nil {
log.Error(err)
panic(err)
}
if len(data) == 0 {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("Secrets Not Found"))
} else {
w.WriteHeader(http.StatusOK)
w.Write(retData)
}
case "PUT":
if path == "" || proc == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("require parameters"))
log.Debug(time.Now().UnixNano())
return ctx
}
totalPath := DATAPATHPREFIX + app + "/" + proc + path
putData := ParseInput((app + "/" + proc + path), string(body))
byteData, _ := json.Marshal(putData)
byteDataBase64 := coder.EncodeToString(byteData)
log.Debug(string(byteData))
err = l.Vault.PutSecret(l.RootToken, totalPath, byteDataBase64)
if err != nil {
log.Error(err)
code, retD := parseErr(err)
w.WriteHeader(code)
w.Write(retD)
log.Debug(time.Now().UnixNano())
return ctx
}
case "DELETE":
if path == "" || proc == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("require parameters"))
log.Debug(time.Now().UnixNano())
return ctx
}
totalPath := app + "/" + proc + path
err = l.Vault.DeleteSecret(l.RootToken, DATAPATHPREFIX+totalPath)
if err != nil {
log.Error(err)
code, retD := parseErr(err)
w.WriteHeader(code)
w.Write(retD)
log.Debug(time.Now().UnixNano())
return ctx
}
}
log.Debug(time.Now().UnixNano())
return ctx
}
func getCheckedParams(req *http.Request) (app string, proc string, path string, err error) {
log.Debug(req.Form)
if len(req.Form[APPNAME]) > 0 {
app = req.Form[APPNAME][0]
if err = ValidateSlug(app); err != nil {
return
}
} else {
app = ""
err = errors.New("can't find appname")
return
}
if len(req.Form[PROCNAME]) > 0 {
proc = req.Form[PROCNAME][0]
if err = ValidateSlug(proc); err != nil {
return
}
} else {
proc = ""
}
if len(req.Form[PATHNAME]) > 0 {
path = req.Form[PATHNAME][0]
if !strings.HasPrefix(path, "/") {
err = errors.New("path should begin with /")
return
}
if strings.Index(path, "..") != -1 {
err = errors.New("path should not contain ..")
return
}
} else {
path = ""
}
return
}