/
entity.go
269 lines (227 loc) · 7.79 KB
/
entity.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package rpc
import (
"context"
"strings"
"github.com/NetAuth/NetAuth/internal/token"
"github.com/golang/protobuf/proto"
"github.com/spf13/viper"
pb "github.com/NetAuth/Protocol"
)
// NewEntity creates a new entity. This action must be authorized by
// the presentation of a valid token containing appropriate
// capabilities.
func (s *NetAuthServer) NewEntity(ctx context.Context, r *pb.ModEntityRequest) (*pb.SimpleResult, error) {
client := r.GetInfo()
e := r.GetEntity()
t := r.GetAuthToken()
if viper.GetBool("server.readonly") {
s.Log.Warn("Denied NewEntity request (read-only mode is enabled)",
"service", client.GetService(),
"client", client.GetID())
return &pb.SimpleResult{
Msg: proto.String("This server is in read-only mode"),
Success: proto.Bool(false),
}, toWireError(ErrReadOnly)
}
c, err := s.Token.Validate(t)
if err != nil {
return nil, toWireError(err)
}
// Verify the correct capability is present in the token.
if !c.HasCapability("CREATE_ENTITY") {
return nil, toWireError(ErrRequestorUnqualified)
}
if err := s.Tree.CreateEntity(e.GetID(), e.GetNumber(), e.GetSecret()); err != nil {
return nil, toWireError(err)
}
s.Log.Info("New entity created",
"entity", e.GetID(),
"authority", c.EntityID,
"service", client.GetService(),
"client", client.GetID())
return &pb.SimpleResult{
Msg: proto.String("New entity created successfully"),
Success: proto.Bool(true),
}, toWireError(nil)
}
// RemoveEntity removes an entity. This action must be authorized by
// the presentation of a valid token containing appropriate
// capabilities.
func (s *NetAuthServer) RemoveEntity(ctx context.Context, r *pb.ModEntityRequest) (*pb.SimpleResult, error) {
client := r.GetInfo()
e := r.GetEntity()
t := r.GetAuthToken()
if viper.GetBool("server.readonly") {
s.Log.Warn("Denied RemoveEntity request (read-only mode is enabled)",
"service", client.GetService(),
"client", client.GetID())
return &pb.SimpleResult{
Msg: proto.String("This server is in read-only mode"),
Success: proto.Bool(false),
}, toWireError(ErrReadOnly)
}
c, err := s.Token.Validate(t)
if err != nil {
return nil, toWireError(err)
}
// Verify the correct capability is present in the token.
if !c.HasCapability("DESTROY_ENTITY") {
return nil, toWireError(ErrRequestorUnqualified)
}
if err := s.Tree.DestroyEntity(e.GetID()); err != nil {
return nil, toWireError(err)
}
s.Log.Info("Entity removed",
"entity", e.GetID(),
"authority", c.EntityID,
"service", client.GetService(),
"client", client.GetID())
return &pb.SimpleResult{
Msg: proto.String("Entity removed successfully"),
Success: proto.Bool(true),
}, toWireError(nil)
}
// EntityInfo returns as much information about an entity is as known.
// This response will not include information about the entity's
// memberships in groups within the tree, but will include all fields
// in the EntityMeta section.
func (s *NetAuthServer) EntityInfo(ctx context.Context, r *pb.NetAuthRequest) (*pb.Entity, error) {
client := r.GetInfo()
e := r.GetEntity()
s.Log.Info("Entity information request",
"entity", e.GetID(),
"service", client.GetService(),
"client", client.GetID())
e, err := s.Tree.FetchEntity(e.GetID())
return e, toWireError(err)
}
// ModifyEntityMeta can be used to modify the EntityMeta section of an
// Entity. This request must be authorized by a token that contains
// the correct capabilities to modify others. Some fields cannot be
// changed by this mechanism and must be changed via other calls which
// perform more authorization and validation checks.
func (s *NetAuthServer) ModifyEntityMeta(ctx context.Context, r *pb.ModEntityRequest) (*pb.SimpleResult, error) {
client := r.GetInfo()
e := r.GetEntity()
t := r.GetAuthToken()
if viper.GetBool("server.readonly") {
s.Log.Warn("Denied ModifyEntityMeta request (read-only mode is enabled)",
"service", client.GetService(),
"client", client.GetID())
return &pb.SimpleResult{
Msg: proto.String("This server is in read-only mode"),
Success: proto.Bool(false),
}, toWireError(ErrReadOnly)
}
c, err := s.Token.Validate(t)
if err != nil {
return nil, toWireError(err)
}
// Verify the correct capability is present in the token.
if !c.HasCapability("MODIFY_ENTITY_META") {
return nil, toWireError(ErrRequestorUnqualified)
}
if err := s.Tree.UpdateEntityMeta(e.GetID(), e.GetMeta()); err != nil {
s.Log.Error("Metadata update error", "error", err)
return nil, toWireError(err)
}
s.Log.Info("Entity metadata update",
"entity", e.GetID(),
"authority", c.EntityID,
"service", client.GetService(),
"client", client.GetID())
return &pb.SimpleResult{
Msg: proto.String("Metadata Updated"),
Success: proto.Bool(true),
}, toWireError(nil)
}
// ModifyEntityKeys can be used to add, remove, or retrieve the keys
// associated with an entity. This action must be authorized by the
// presentation of a token with appropriate capabilities.
func (s *NetAuthServer) ModifyEntityKeys(ctx context.Context, r *pb.ModEntityKeyRequest) (*pb.KeyList, error) {
client := r.GetInfo()
e := r.GetEntity()
t := r.GetAuthToken()
mode := strings.ToUpper(r.GetMode())
if viper.GetBool("server.readonly") && mode != "LIST" {
s.Log.Warn("Denied ModifyEntityKeys request (read-only mode is enabled)",
"service", client.GetService(),
"client", client.GetID())
return &pb.KeyList{}, toWireError(ErrReadOnly)
}
// If we aren't doing a read only operation then we need a
// token for this
var c token.Claims
if mode != "LIST" {
c, err := s.Token.Validate(t)
if err != nil {
return nil, toWireError(err)
}
// Verify the correct capability is present in the token or
// that this is not a read only query.
if !c.HasCapability("MODIFY_ENTITY_KEYS") && c.EntityID != e.GetID() {
return nil, toWireError(ErrRequestorUnqualified)
}
}
// Get run the transaction on the key database.
keys, err := s.Tree.UpdateEntityKeys(e.GetID(), r.GetMode(), r.GetType(), r.GetKey())
if err != nil {
return nil, toWireError(err)
}
verb := "updated"
if mode == "LIST" {
verb = "requested"
}
s.Log.Info("Entity Key Handling Event",
"entity", e.GetID(),
"authority", c.EntityID,
"action", verb,
"service", client.GetService(),
"client", client.GetID())
return &pb.KeyList{
Keys: keys,
}, toWireError(nil)
}
// ModifyUntypedEntityMeta alters the data stored in the untyped K/V
// segment of an entity's metadata. This action must be authorized by
// the presentation of a token with appropriate capabilities.
func (s *NetAuthServer) ModifyUntypedEntityMeta(ctx context.Context, r *pb.ModEntityMetaRequest) (*pb.UntypedMetaResult, error) {
client := r.GetInfo()
e := r.GetEntity()
t := r.GetAuthToken()
mode := strings.ToUpper(r.GetMode())
if viper.GetBool("server.readonly") && mode != "READ" {
s.Log.Warn("Denied ModifyUntypedEntityMeta request (read-only mode is enabled)",
"service", client.GetService(),
"client", client.GetID())
return &pb.UntypedMetaResult{}, toWireError(ErrReadOnly)
}
// If we aren't doing a read only operation then we need a
// token for this
var c token.Claims
var err error
if mode != "READ" {
c, err = s.Token.Validate(t)
if err != nil {
return nil, toWireError(err)
}
// Verify the correct capability is present in the token or
// that this is not a read only query.
if !c.HasCapability("MODIFY_ENTITY_KEYS") {
return nil, toWireError(ErrRequestorUnqualified)
}
}
meta, err := s.Tree.ManageUntypedEntityMeta(e.GetID(), r.GetMode(), r.GetKey(), r.GetValue())
if err != nil {
return nil, toWireError(err)
}
if mode != "READ" {
s.Log.Info("Entity UntypedMeta updated",
"entity", e.GetID(),
"authority", c.EntityID,
"service", client.GetService(),
"client", client.GetID(),
)
}
return &pb.UntypedMetaResult{UntypedMeta: meta}, toWireError(nil)
}