-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdirectory.go
352 lines (316 loc) · 9.8 KB
/
directory.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// IRIS Endpoint-Server (EPS)
// Copyright (C) 2021-2021 The IRIS Endpoint-Server Authors (see AUTHORS.md)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package eps
import (
"fmt"
"time"
)
type DirectoryDefinition struct {
Name string `json:"name"`
Description string `json:"description"`
Maker DirectoryMaker `json:"-"`
SettingsValidator SettingsValidator `json:"-"`
}
func MakeDirectoryEntry() *DirectoryEntry {
return &DirectoryEntry{
Groups: []string{},
Channels: []*OperatorChannel{},
Services: []*OperatorService{},
Certificates: []*OperatorCertificate{},
Settings: []*OperatorSettings{},
Records: []*SignedChangeRecord{},
Properties: &OperatorProperties{},
}
}
type DirectoryEntry struct {
Name string `json:"name"`
Groups []string `json:"groups"`
Channels []*OperatorChannel `json:"channels"`
Services []*OperatorService `json:"services"`
Certificates []*OperatorCertificate `json:"certificates"`
Settings []*OperatorSettings `json:"settings"`
Preferences []*OperatorPreferences `json:"preferences"`
Records []*SignedChangeRecord `json:"records"`
Properties *OperatorProperties `json:"properties"`
}
type OperatorProperties struct {
DisplayName string `json:"displayName"`
}
// preferences may be set by the corresponding operator itself
type OperatorPreferences struct {
Operator string `json:"operator"`
Service string `json:"service"`
Environment string `json:"environment"`
Preferences map[string]interface{} `json:"preferences"`
}
// settings may only be set by a directory admin
type OperatorSettings struct {
Operator string `json:"operator"`
Service string `json:"service"`
Environment string `json:"environment"`
Settings map[string]interface{} `json:"settings"`
}
type OperatorChannel struct {
Type string `json:"type"`
Settings map[string]interface{} `json:"settings"`
}
type OperatorCertificate struct {
Fingerprint string `json:"fingerprint"`
KeyUsage string `json:"key_usage"`
}
type OperatorService struct {
Name string `json:"name"`
Permissions []*Permission `json:"permissions"`
Methods []*ServiceMethod `json:"methods"`
}
type ServiceMethod struct {
Name string `json:"name"`
Permissions []*Permission `json:"permissions"`
Parameters []*ServiceParameter `json:"parameters"`
}
type Permission struct {
Group string `json:"group"`
Rights []string `json:"rights"`
}
type ServiceParameter struct {
Name string `json:"name"`
Validators []*ServiceValidator `json:"validators"`
}
type ServiceValidator struct {
Type string `json:"type"`
Parameters map[string]interface{} `json:"parameters"`
}
type SignedChangeRecord struct {
ParentHash string `json:"parent_hash"`
Hash string `json:"hash"`
Signature *Signature `json:"signature"`
Record *ChangeRecord `json:"record"`
}
type Signature struct {
R string `json:"r"`
S string `json:"s"`
Certificate string `json:"c"`
}
type SignedData struct {
Signature *Signature `json:"signature"`
Data interface{} `json:"data"`
}
// describes a change in a specific section of the service directory
type ChangeRecord struct {
Name string `json:"name"`
Section string `json:"section"`
Data interface{} `json:"data"`
CreatedAt HashableTime `json:"created_at"`
}
type HashableTime struct {
time.Time
}
var NoEntryFound = fmt.Errorf("no directory entry found")
func (h HashableTime) HashValue() interface{} {
return h.Time.Format(time.RFC3339Nano)
}
func (d *DirectoryEntry) Channel(channelType string) *OperatorChannel {
for _, channel := range d.Channels {
if channel.Type == channelType {
return channel
}
}
return nil
}
func (d *DirectoryEntry) SettingsFor(service, operator string) *OperatorSettings {
for _, settings := range d.Settings {
if (settings.Service == service || settings.Service == "") && (settings.Operator == operator || settings.Operator == "") {
return settings
}
}
return nil
}
type DirectoryQuery struct {
Group string `json:"group"`
Operator string `json:"operator"`
Channels []string `json:"channels"`
}
type DirectoryEntries []*DirectoryEntry
type DirectoryDefinitions map[string]DirectoryDefinition
type DirectoryMaker func(name string, settings interface{}) (Directory, error)
// A directory can deliver and accept message
type Directory interface {
Entries(*DirectoryQuery) ([]*DirectoryEntry, error)
EntryFor(string) (*DirectoryEntry, error)
OwnEntry() (*DirectoryEntry, error)
Name() string
}
type WritableDirectory interface {
Directory
// required for submitting change records
Tip() (*SignedChangeRecord, error)
Submit([]*SignedChangeRecord) error
}
type BaseDirectory struct {
Name_ string
}
func (b *BaseDirectory) Name() string {
return b.Name_
}
func ServiceFor(entry *DirectoryEntry, method string) *OperatorService {
for _, service := range entry.Services {
for _, serviceMethod := range service.Methods {
if serviceMethod.Name == method {
return service
}
}
}
return nil
}
func CanCall(caller, callee *DirectoryEntry, method string) bool {
service := ServiceFor(callee, method)
if service == nil {
// can't find this service
return false
}
callerGroups := map[string]bool{}
for _, group := range caller.Groups {
callerGroups[group] = true
}
// we look at the permissions of the overall service and at the permissions
// of the matching method (if such a method exists)
permissions := [][]*Permission{service.Permissions}
for _, serviceMethod := range service.Methods {
if serviceMethod.Name == method {
permissions = append(permissions, serviceMethod.Permissions)
break
}
}
for _, pms := range permissions {
// we go through all permissions
for _, permission := range pms {
// we check if the caller has a matching group entry
if _, ok := callerGroups[permission.Group]; ok || permission.Group == "*" {
// we go through all permission rights
for _, right := range permission.Rights {
// we check if the caller has the 'call' right
if right == "call" {
return true
}
}
}
}
}
return false
}
// get all groups that may call service methods of this entry
func GetPeerGroups(entry *DirectoryEntry) []string {
groups := map[string]bool{}
for _, service := range entry.Services {
for _, servicePermission := range service.Permissions {
groups[servicePermission.Group] = true
}
for _, method := range service.Methods {
for _, methodPermission := range method.Permissions {
groups[methodPermission.Group] = true
}
}
}
groupValues := make([]string, 0)
for name, _ := range groups {
groupValues = append(groupValues, name)
}
return groupValues
}
func intersects(a, b []string) bool {
am := map[string]bool{}
for _, ai := range a {
am[ai] = true
}
for _, bi := range b {
if _, ok := am[bi]; ok {
return true
}
}
return false
}
// Return all entries that are relevant for the given base entry (i.e. that
// can call a service on the base entrys' endpoint or can be called by the
// base entrys' endpoint, respectively)
func GetPeers(baseEntry *DirectoryEntry, entries []*DirectoryEntry, incomingOnly bool) []*DirectoryEntry {
peerEntries := make([]*DirectoryEntry, 0)
basePeerGroups := GetPeerGroups(baseEntry)
for _, entry := range entries {
if entry.Name == baseEntry.Name {
continue // this is the same entry
}
// the peer groups contain all groups that can call services on this entrys' endpoint
peerGroups := GetPeerGroups(entry)
found := false
if !incomingOnly {
if intersects(baseEntry.Groups, peerGroups) {
// the base entrys' endpoint can call a service on the entrys' endpoint
peerEntries = append(peerEntries, entry)
found = true
}
}
if !found && intersects(entry.Groups, basePeerGroups) {
// the entrys' endpoint can call a service on the base entrys' endpoint
peerEntries = append(peerEntries, entry)
}
}
return peerEntries
}
// helper function that can be used by directory implementations that
// have a list of local directory entries
func FilterDirectoryEntriesByQuery(entries []*DirectoryEntry, query *DirectoryQuery) []*DirectoryEntry {
relevantEntries := make([]*DirectoryEntry, 0)
for _, entry := range entries {
// we filter the entries by the specified operator name
if query.Operator != "" && entry.Name != query.Operator {
continue
}
if query.Group != "" {
found := false
for _, group := range entry.Groups {
if group == query.Group {
found = true
break
}
}
if !found {
continue
}
}
// we filter the entries by the specified channel types
found := false
if query.Channels != nil {
for _, queryChannel := range query.Channels {
for _, entryChannel := range entry.Channels {
if entryChannel.Type == queryChannel {
found = true
break
}
}
if found {
break
}
}
} else {
found = true
}
if !found {
continue
}
relevantEntries = append(relevantEntries, entry)
}
return relevantEntries
}