@@ -30,17 +30,23 @@ export class AccessController {
30
30
resourceAdapter : ResourceAdapter ;
31
31
redisClient : RedisClientType < any , any > ;
32
32
userTopic : Topic ;
33
- waiting : any [ ] ;
33
+ waiting : any ;
34
34
cfg : any ;
35
35
userService : UserServiceClient ;
36
- constructor ( private logger : Logger , opts : AccessControlConfiguration ,
37
- userTopic : Topic , cfg : any , userService : UserServiceClient ) {
36
+
37
+ constructor (
38
+ private logger : Logger ,
39
+ opts : AccessControlConfiguration ,
40
+ userTopic : Topic ,
41
+ cfg : any ,
42
+ userService : UserServiceClient
43
+ ) {
38
44
this . policySets = new Map < string , PolicySetWithCombinables > ( ) ;
39
45
this . combiningAlgorithms = new Map < string , any > ( ) ;
40
46
41
47
logger . info ( 'Parsing combining algorithms from access control configuration...' ) ;
42
48
// parsing URNs and mapping them to functions
43
- const combiningAlgorithms : CombiningAlgorithm [ ] = opts ?. combiningAlgorithms || [ ] ;
49
+ const combiningAlgorithms : CombiningAlgorithm [ ] = opts ?. combiningAlgorithms ?? [ ] ;
44
50
for ( let ca of combiningAlgorithms ) {
45
51
const urn = ca . urn ;
46
52
const method = ca . method ;
@@ -115,22 +121,29 @@ export class AccessController {
115
121
116
122
// policyEffect needed to evalute if the properties should be PERMIT / DENY
117
123
let policyEffect : Effect ;
118
- if ( ( ! ! policySet . target && await this . targetMatches ( policySet . target , request , 'isAllowed' , obligations ) )
119
- || ! policySet . target ) {
124
+ if (
125
+ ! policySet . target
126
+ || await this . targetMatches ( policySet . target , request , 'isAllowed' , obligations )
127
+ ) {
120
128
let exactMatch = false ;
121
129
for ( let [ , policyValue ] of policySet . combinables ) {
122
130
const policy : Policy = policyValue ;
123
131
if ( policy . effect ) {
124
132
policyEffect = policy . effect ;
125
- } else if ( policy . combining_algorithm ) {
133
+ }
134
+ else if ( policy . combining_algorithm ) {
126
135
const method = this . combiningAlgorithms . get ( policy . combining_algorithm ) ;
127
136
if ( method === 'permitOverrides' ) {
128
137
policyEffect = Effect . PERMIT ;
129
138
} else if ( method === 'denyOverrides' ) {
130
139
policyEffect = Effect . DENY ;
131
140
}
132
141
}
133
- if ( ! ! policy . target && await this . targetMatches ( policy . target , request , 'isAllowed' , obligations , policyEffect ) ) {
142
+
143
+ if (
144
+ policy . target
145
+ && await this . targetMatches ( policy . target , request , 'isAllowed' , obligations , policyEffect )
146
+ ) {
134
147
exactMatch = true ;
135
148
break ;
136
149
}
@@ -151,11 +164,18 @@ export class AccessController {
151
164
continue ;
152
165
}
153
166
const ruleEffects : EffectEvaluation [ ] = [ ] ;
154
- if ( ( ! ! policy . target && exactMatch && await this . targetMatches ( policy . target , request , 'isAllowed' , obligations , policyEffect ) )
167
+ if (
168
+ ! policy . target
169
+ || (
170
+ exactMatch
171
+ && await this . targetMatches ( policy . target , request , 'isAllowed' , obligations , policyEffect )
172
+ )
155
173
// regex match
156
- || ( ! ! policy . target && ! exactMatch && await this . targetMatches ( policy . target , request , 'isAllowed' , obligations , policyEffect , true ) )
157
- || ! policy . target ) {
158
-
174
+ || (
175
+ ! exactMatch
176
+ && await this . targetMatches ( policy . target , request , 'isAllowed' , obligations , policyEffect , true )
177
+ )
178
+ ) {
159
179
const rules : Map < string , Rule > = policy . combinables ;
160
180
this . logger . verbose ( `Checking policy ${ policy . name } ` ) ;
161
181
// only apply a policy effect if there are no rules
@@ -184,19 +204,26 @@ export class AccessController {
184
204
}
185
205
186
206
if ( matches ) {
187
- this . logger . verbose ( `Checking rule ${ rule . name } ` ) ;
207
+ this . logger . verbose ( `Checking rule HR Scope for ${ rule . name } ` ) ;
188
208
if ( matches && rule . target ) {
189
209
matches = await checkHierarchicalScope ( rule . target , request , this . urns , this , this . logger ) ;
190
210
}
191
211
192
212
try {
193
- if ( matches && ! _ . isEmpty ( rule . condition ) ) {
213
+ if ( matches && rule . condition ?. length ) {
194
214
// context query is only checked when a rule exists
195
215
let context : any ;
196
- if ( ! _ . isEmpty ( rule . context_query ) && this . resourceAdapter ) {
216
+ if (
217
+ this . resourceAdapter
218
+ && (
219
+ rule . context_query ?. filters ?. length
220
+ || rule . context_query ?. query ?. length
221
+ )
222
+ ) {
197
223
context = await this . pullContextResources ( rule . context_query , request ) ;
198
224
199
225
if ( _ . isNil ( context ) ) {
226
+ this . logger . debug ( 'Context query response is empty!' ) ;
200
227
return { // deny by default
201
228
decision : Response_Decision . DENY ,
202
229
obligations,
@@ -209,12 +236,12 @@ export class AccessController {
209
236
}
210
237
}
211
238
212
- request . context = context || request . context ;
239
+ request . context = context ?? request . context ;
213
240
this . logger . debug ( 'Validating rule condition' , { name : rule . name , condition : rule . condition } ) ;
214
241
matches = conditionMatches ( rule . condition , request ) ;
215
242
this . logger . debug ( 'condition validation response' , { matches } ) ;
216
243
}
217
- } catch ( err ) {
244
+ } catch ( err : any ) {
218
245
this . logger . error ( 'Caught an exception while applying rule condition to request' , { code : err . code , message : err . message , stack : err . stack } ) ;
219
246
return { // if an exception is caught deny by default
220
247
decision : Response_Decision . DENY ,
@@ -296,7 +323,10 @@ export class AccessController {
296
323
let obligations : Attribute [ ] = [ ] ;
297
324
for ( let [ , value ] of this . policySets ) {
298
325
let pSet : PolicySetRQ ;
299
- if ( _ . isEmpty ( value . target ) || await this . targetMatches ( value . target , request , 'whatIsAllowed' , obligations ) ) {
326
+ if (
327
+ _ . isEmpty ( value . target )
328
+ || await this . targetMatches ( value . target , request , 'whatIsAllowed' , obligations )
329
+ ) {
300
330
pSet = _ . merge ( { } , { combining_algorithm : value . combining_algorithm } , _ . pick ( value , [ 'id' , 'target' , 'effect' ] ) ) as any ;
301
331
pSet . policies = [ ] ;
302
332
0 commit comments