-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_data.go
86 lines (73 loc) · 1.88 KB
/
server_data.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
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"github.com/tehmaze-labs/secrets/router"
"github.com/tehmaze-labs/secrets/secret"
)
func getGroupData(cfg *Config) router.HandlerFunc {
return func(req *router.Request) (interface{}, error) {
name := req.Params["name"]
key := fmt.Sprintf(req.Params["key"])
group := cfg.Group[name]
if group == nil {
return nil, nil
}
log.Printf("group %s: get %q (%s)", group.Name, key, req.RemoteAddr)
if !cfg.ACL.GroupPermitted(group, req.RemoteAddr) {
return nil, errors.New("access denied by ACL")
}
if !group.Data.Has(key) {
return nil, nil
}
var s secret.Secret
if err := group.Data.Get(key, &s); err != nil {
return nil, err
}
return s, nil
}
}
func getGroupDatas(cfg *Config) router.HandlerFunc {
return func(req *router.Request) (interface{}, error) {
name := req.Params["name"]
group := cfg.Group[name]
if group == nil {
return nil, nil
}
log.Printf("group %s: list (%s)", group.Name, req.RemoteAddr)
var keys = []string{}
for key := range group.Data.Scan("") {
keys = append(keys, key)
}
return map[string]interface{}{"keys": keys}, nil
}
}
func putGroupData(cfg *Config) router.HandlerFunc {
return func(req *router.Request) (interface{}, error) {
name := req.Params["name"]
key := fmt.Sprintf(req.Params["key"])
group := cfg.Group[name]
if group == nil {
return nil, nil
}
log.Printf("group %s: put %q (%s)", group.Name, key, req.RemoteAddr)
if !cfg.ACL.GroupPermitted(group, req.RemoteAddr) {
return nil, errors.New("access denied by ACL")
}
data, err := ioutil.ReadAll(req.Body)
if err != nil {
return nil, err
}
var s secret.Secret
if err := json.Unmarshal(data, &s); err != nil {
return nil, err
}
if err := group.Data.Set(key, &s); err != nil {
return nil, err
}
return map[string]string{"updated": key}, nil
}
}