forked from EmbeddedEnterprises/autobahnkreuz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
featureauthorizer.go
288 lines (210 loc) · 7.73 KB
/
featureauthorizer.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
package auth
import (
"context"
"errors"
"github.com/EmbeddedEnterprises/autobahnkreuz/util"
mapset "github.com/deckarep/golang-set"
"github.com/gammazero/nexus/client"
"github.com/gammazero/nexus/wamp"
)
type FeatureAuthorizer struct {
PermitDefault bool
MatrixURI string
MappingURI string
TrustedAuthRoles mapset.Set
FeatureMatrix *FeatureMatrix
FeatureMapping *FeatureMapping
CallCounter int
}
type FeatureMatrix map[wamp.URI]map[string]bool
type FeatureMapping map[wamp.URI]wamp.URI
func NewFeatureAuthorizer(permitDefault bool, matrixURI string, mappingURI string, trustedAuthRoles mapset.Set) *FeatureAuthorizer {
featureAuthorizer := FeatureAuthorizer{}
util.Logger.Infof("permitDefault: %v", permitDefault)
featureAuthorizer.PermitDefault = permitDefault
featureAuthorizer.MatrixURI = matrixURI
featureAuthorizer.MappingURI = mappingURI
featureAuthorizer.TrustedAuthRoles = trustedAuthRoles
featureAuthorizer.FeatureMatrix = nil
featureAuthorizer.FeatureMapping = nil
featureAuthorizer.CallCounter = 0
return &featureAuthorizer
}
func (this *FeatureAuthorizer) Initialize() {
util.Logger.Infof("Initializing Feature Authroizer..")
util.Logger.Infof("Registering wamp.featureauth.update")
// TBD: We can't use wamp.* prefix here, it's restricted to the router-internal meta client. - Martin
// I just changed it to ee.*, which should be the fitting namespace at this point. - Johann
err := util.LocalClient.Register("ee.featureauth.update", this.Update, wamp.Dict{})
if err != nil {
util.Logger.Warningf("%v", err)
}
}
func (this *FeatureAuthorizer) Update(_ context.Context, args wamp.List, _, _ wamp.Dict) *client.InvokeResult {
util.Logger.Infof("Updating Matrix and Mapping.")
err := this.UpdateMatrix()
if err != nil {
return &client.InvokeResult{
Err: wamp.URI("wamp.error.internal-error"),
}
}
err = this.UpdateMapping()
if err != nil {
return &client.InvokeResult{
Err: wamp.URI("wamp.error.internal-error"),
}
}
return &client.InvokeResult{}
}
func (this *FeatureAuthorizer) UpdateMapping() error {
ctx := context.Background()
emptyDict := wamp.Dict{}
// callArguments is empty right now, but maybe not forever.
callArguments := wamp.List{}
callRes, callErr := util.LocalClient.Call(ctx, this.MappingURI, emptyDict, callArguments, emptyDict, "")
if callErr != nil {
util.Logger.Warningf("%s was not callable.", this.MappingURI)
util.Logger.Warningf("%v", callErr)
return callErr
}
util.Logger.Infof("Got callRes: %v", callRes)
if len(callRes.Arguments) < 1 {
// First Element cannot be accessed -> Segfault
util.Logger.Warningf("Invalid Reply from MappingURI")
return errors.New("Invalid Reply from MappingURI")
}
util.Logger.Infof("%v", callRes.Arguments[0])
mappingRaw, castOkay := callRes.Arguments[0].(map[string]interface{})
if !castOkay {
util.Logger.Warningf("Invalid Reply from MappingURI, Cast to map[string][]interface{} was not successful.")
return errors.New("Invalid Reply from MappingURI")
}
newFeatureMapping := make(FeatureMapping)
for featureItem, endpointURIs := range mappingRaw {
featureItemURI := wamp.URI(featureItem)
endpointURIs, castOkay := wamp.AsList(endpointURIs)
if !castOkay {
util.Logger.Warningf("Invalid Reply from MappingURI, Cast with wamp.AsList was not successful.")
return errors.New("Invalid Reply from MappingURI")
}
for _, endpointInterface := range endpointURIs {
endpointString, castOkay := wamp.AsString(endpointInterface)
if !castOkay {
util.Logger.Warningf("Invalid Reply from MappingURI, Cast with wamp.AsString was not successful.")
return errors.New("Invalid Reply from MappingURI")
}
endpointURI := wamp.URI(endpointString)
newFeatureMapping[endpointURI] = featureItemURI
}
}
this.FeatureMapping = &newFeatureMapping
util.Logger.Infof("Assigned new Feature Mapping: %v", this.FeatureMapping)
return nil
}
func (this *FeatureAuthorizer) UpdateMatrix() error {
ctx := context.Background()
emptyDict := wamp.Dict{}
// callArguments is empty right now, but maybe not forever.
callArguments := wamp.List{}
callRes, callErr := util.LocalClient.Call(ctx, this.MatrixURI, emptyDict, callArguments, emptyDict, "")
if callErr != nil {
util.Logger.Warningf("%s was not callable.", this.MatrixURI)
util.Logger.Warningf("%v", callErr)
return callErr
}
util.Logger.Infof("Got callRes: %v", callRes)
if len(callRes.Arguments) < 1 {
// First Element cannot be accessed -> Segfault
util.Logger.Warningf("Invalid Reply from MatrixURI")
return errors.New("Invalid Reply from MatrixURI")
}
featureMatrixRaw, ok := callRes.Arguments[0].(map[string]interface{})
newFeatureMatrix := make(FeatureMatrix)
if !ok {
util.Logger.Warningf("Invalid Reply from MatrixURI")
return errors.New("Invalid Reply from MatrixURI")
}
for featureItem, authRoleList := range featureMatrixRaw {
featureItemUri := wamp.URI(featureItem)
newFeatureMatrix[featureItemUri] = make(map[string]bool)
authRoleList, ok := wamp.AsDict(authRoleList)
if !ok {
util.Logger.Warningf("Invalid Reply from MatrixURI")
return errors.New("Invalid Reply from MatrixURI")
}
for authRole, isAllowed := range authRoleList {
isAllowed, castOkay := isAllowed.(bool)
if !castOkay {
isAllowed = false
}
newFeatureMatrix[featureItemUri][authRole] = isAllowed
}
}
// Assign newFeatureMatrix to existing matrix
this.FeatureMatrix = &newFeatureMatrix
util.Logger.Infof("Assigned new featureMatrix: %v", this.FeatureMatrix)
return nil
}
func (this *FeatureAuthorizer) Authorize(sess *wamp.Session, msg wamp.Message) (bool, error) {
util.Logger.Debugf("Pointer Address from FeatureAuthorizer: %p", &this)
this.CallCounter++
util.Logger.Debugf("Call Counter from FeatureAuthorizer: %v", this.CallCounter)
roles, err := extractAuthRoles(sess.Details["authrole"])
util.Logger.Debugf("Request: %v Session: %v", msg, sess)
if err != nil {
return this.PermitDefault, nil
}
util.Logger.Infof("Check for trustedAuthRoles")
isTrustedAuthRole := roles.checkTrustedAuthRoles(this.TrustedAuthRoles)
if isTrustedAuthRole {
util.Logger.Infof("Call was from trusted auth role. Access granted.")
return true, nil
}
if this.FeatureMatrix == nil || this.FeatureMapping == nil {
util.Logger.Warningf("FeatureMatrix or FeatureMapping is not defined.")
util.Logger.Warningf("FeatureMapping: %v", this.FeatureMapping)
util.Logger.Warningf("FeatureMatrix: %v", this.FeatureMatrix)
return this.PermitDefault, nil
}
// Transform endpointURI to featureItem
var messageURI wamp.URI
switch msg.MessageType() {
case wamp.CALL:
messageURI = msg.(*wamp.Call).Procedure
case wamp.REGISTER:
messageURI = msg.(*wamp.Register).Procedure
case wamp.SUBSCRIBE:
messageURI = msg.(*wamp.Subscribe).Topic
case wamp.PUBLISH:
messageURI = msg.(*wamp.Publish).Topic
default:
// I am pretty sure, I changed this earlier to true.
// It is important to allow every other call than
// CALL, REGISTER, SUBSCRIBE and PUBLISH to allow nexus to function correctly.
return true, nil
}
featureMapping := *this.FeatureMapping
featureURI, isOkay := featureMapping[messageURI]
if !isOkay {
for potentialMessageURI, tFeatureURI := range featureMapping {
isMatchingWildcard := messageURI.WildcardMatch(potentialMessageURI)
util.Logger.Debugf("messsageURI: %v, potentialMessageURI: %v, isMatchingWildcard: %v", messageURI, potentialMessageURI, isMatchingWildcard)
if isMatchingWildcard {
featureURI = tFeatureURI
isOkay = true
break
}
}
}
if !isOkay {
return this.PermitDefault, nil
}
featureMatrix := *this.FeatureMatrix
for _, authRole := range *roles {
hasPermission := featureMatrix[featureURI][authRole]
if hasPermission {
return true, nil
}
}
return this.PermitDefault, nil
}