forked from pydio/cells
/
handler.go
145 lines (122 loc) · 3.91 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
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
package grpc
import (
"context"
"crypto/md5"
"fmt"
"strings"
"time"
json "github.com/pydio/cells/x/jsonx"
"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, request.Change.Path)
config.Save(common.PydioSystemUsername, "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(request.Change.Path)
config.Save(common.PydioSystemUsername, "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.Get().Bytes()
}
} else if request.Id == "vault" {
value = config.Vault().Val(request.Path).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 {
defer stream.Close()
if request.Id == "config" {
w, err := config.Watch()
if err != nil {
return err
}
for {
next, err := w.Next()
if err != nil {
return err
}
value := next.Bytes()
hasher := md5.New()
hasher.Write(value)
checksum := fmt.Sprintf("%x", hasher.Sum(nil))
stream.Send(&proto.WatchResponse{
Id: "config",
ChangeSet: &go_micro_os_config.ChangeSet{
Data: string(value),
Source: request.Id,
Checksum: checksum,
Timestamp: time.Now().Unix(),
},
})
}
return nil
}
return NotImplemented
}