-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
115 lines (100 loc) · 3.48 KB
/
handler.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
package grpc
import (
"context"
"crypto/md5"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/micro/go-micro/errors"
"github.com/pydio/cells/common"
"github.com/pydio/cells/common/config"
"github.com/pydio/cells/common/log"
proto "github.com/pydio/config-srv/proto/config"
go_micro_os_config "github.com/pydio/go-os/config/proto"
)
var (
notImplemented = errors.New("notimplemented", "service not implemented", 501)
)
type Handler struct{}
// Create just forwards to Update
func (h *Handler) Create(ctx context.Context, request *proto.CreateRequest, response *proto.CreateResponse) error {
return h.Update(ctx, &proto.UpdateRequest{
Change: request.Change,
}, &proto.UpdateResponse{})
}
// Update will write to local config or vault
func (h *Handler) Update(ctx context.Context, request *proto.UpdateRequest, response *proto.UpdateResponse) error {
var data interface{}
if e := json.Unmarshal([]byte(request.Change.ChangeSet.Data), &data); e != nil {
return e
}
if request.Change.Id == "config" {
log.Logger(ctx).Info("Updating config remotely")
config.Set(data, strings.Split(request.Change.Path, "/")...)
config.Save(common.PYDIO_SYSTEM_USERNAME, "Updated config remotely")
} else if request.Change.Id == "vault" {
log.Logger(ctx).Info("Updating vault remotely")
config.SetSecret(request.Change.Path, data.(string))
} else {
return errors.BadRequest("config.update", "config ID not supported, please use config or vault")
}
return nil
}
// Delete will write to local config or vault
func (h *Handler) Delete(ctx context.Context, request *proto.DeleteRequest, response *proto.DeleteResponse) error {
if request.Change.Id == "config" {
log.Logger(ctx).Info("Updating config remotely")
config.Del(strings.Split(request.Change.Path, "/")...)
config.Save(common.PYDIO_SYSTEM_USERNAME, "Updated config remotely")
} else if request.Change.Id == "vault" {
log.Logger(ctx).Info("Updating vault remotely")
config.DelSecret(request.Change.Path)
} else {
return errors.BadRequest("config.delete", "config ID not supported, please use config or vault")
}
return nil
}
func (h *Handler) Search(ctx context.Context, request *proto.SearchRequest, response *proto.SearchResponse) error {
return notImplemented
}
// Read will grab info from local config or vault
func (h *Handler) Read(ctx context.Context, request *proto.ReadRequest, response *proto.ReadResponse) error {
var value []byte
if request.Id == "config" {
if request.Path != "" {
value = config.Get(strings.Split(request.Path, "/")...).Bytes()
} else {
value = config.Default().Bytes()
}
} else if request.Id == "vault" {
if request.Path != "" {
value = config.Vault().Get(strings.Split(request.Path, "/")...).Bytes()
} else {
value = config.Vault().Bytes()
}
} else {
return errors.BadRequest("config.read", "config ID not supported, please use config or vault")
}
hasher := md5.New()
hasher.Write(value)
checksum := fmt.Sprintf("%x", hasher.Sum(nil))
response.Change = &proto.Change{
Timestamp: time.Now().Unix(),
Path: request.Path,
ChangeSet: &go_micro_os_config.ChangeSet{
Data: string(value),
Source: request.Id,
Checksum: checksum,
Timestamp: time.Now().Unix(),
},
}
return nil
}
func (h *Handler) AuditLog(ctx context.Context, request *proto.AuditLogRequest, response *proto.AuditLogResponse) error {
return notImplemented
}
func (h *Handler) Watch(ctx context.Context, request *proto.WatchRequest, stream proto.Config_WatchStream) error {
// TODO
return nil
}