Skip to content

Commit bc94bf7

Browse files
committed
fix: revert removed event listeners and move reload to CRMD
1 parent 40a1727 commit bc94bf7

File tree

4 files changed

+111
-76
lines changed

4 files changed

+111
-76
lines changed

src/accessControlService.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import _ from 'lodash-es';
22
import { Server } from '@restorecommerce/chassis-srv';
33
import { Events } from '@restorecommerce/kafka-client';
44
import { CommandInterface } from '@restorecommerce/chassis-srv';
5-
import { ResourceManager, PolicySetService } from './resourceManager.js';
5+
import { ResourceManager } from './resourceManager.js';
66
import { RedisClientType } from 'redis';
77
import { AccessController } from './core/accessController.js';
88
import { loadPoliciesFromDoc } from './core/utils.js';
@@ -35,7 +35,22 @@ export class AccessControlService implements AccessControlServiceImplementation
3535
}
3636
async loadPolicies(): Promise<void> {
3737
this.logger.info('Loading policies');
38-
this.accessController.loadPolicies(this.resourceManager);
38+
39+
const policiesCfg = this.cfg.get('policies');
40+
const loadType = policiesCfg?.type;
41+
switch (loadType) {
42+
case 'local':
43+
const path: string = policiesCfg?.path;
44+
this.accessController = await loadPoliciesFromDoc(this.accessController, path);
45+
this.logger.silly('Policies from local files loaded');
46+
break;
47+
case 'database':
48+
const policySetService = this.resourceManager.getResourceService('policy_set');
49+
const policySets: Map<string, PolicySetWithCombinables> = await policySetService.load() || new Map();
50+
this.accessController.policySets = policySets;
51+
this.logger.silly('Policies from database loaded');
52+
break;
53+
}
3954
}
4055

4156
clearPolicies(): void {

src/core/accessController.ts

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ import { Logger } from 'winston';
2020
import { createClient, RedisClientType } from 'redis';
2121
import { Topic } from '@restorecommerce/kafka-client';
2222
import { verifyACLList } from './verifyACL.js';
23-
import { conditionMatches, loadPoliciesFromDoc } from './utils.js';
24-
import { PolicySetService, ResourceManager } from '../resourceManager.js';
23+
import { conditionMatches } from './utils.js';
2524

2625
export class AccessController {
2726
policySets: Map<string, PolicySetWithCombinables>;
@@ -76,24 +75,6 @@ export class AccessController {
7675
this.userService = userService;
7776
}
7877

79-
async loadPolicies(resourceManager: ResourceManager): Promise<void> {
80-
const policiesCfg = this.cfg.get('policies');
81-
const loadType = policiesCfg?.type;
82-
switch (loadType) {
83-
case 'local':
84-
const path: string = policiesCfg?.path;
85-
await loadPoliciesFromDoc(this, path);
86-
this.logger.silly('Policies from local files loaded');
87-
break;
88-
case 'database':
89-
const policySetService: PolicySetService = resourceManager.getResourceService('policy_set');
90-
const policySets: Map<string, PolicySetWithCombinables> = await policySetService.load() || new Map();
91-
this.policySets = policySets;
92-
this.logger.silly('Policies from database loaded');
93-
break;
94-
}
95-
}
96-
9778
clearPolicies(): void {
9879
this.policySets.clear();
9980
}

src/resourceManager.ts

Lines changed: 79 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -108,22 +108,6 @@ export class RuleService extends ServiceBase<RuleListResponse, RuleList> impleme
108108
return this.getRules();
109109
}
110110

111-
// async reloadRules(result: DeepPartial<RuleListResponse>): Promise<void> {
112-
// const policySets = _.cloneDeep(_accessController.policySets);
113-
// if (result?.items?.length > 0) {
114-
// for (let item of result.items) {
115-
// const rule: Rule = marshallResource(item?.payload, 'rule');
116-
// for (let [, policySet] of policySets) {
117-
// for (let [, policy] of (policySet).combinables) {
118-
// if (!_.isNil(policy) && policy.combinables.has(rule.id)) {
119-
// _accessController.updateRule(policySet.id, policy.id, rule);
120-
// }
121-
// }
122-
// }
123-
// }
124-
// }
125-
// }
126-
127111
async getRules(ruleIDs?: string[]): Promise<Map<string, Rule>> {
128112
const filters = ruleIDs ? makeFilter(ruleIDs) : {};
129113
const result = await super.read(ReadRequest.fromPartial({ filters }), {});
@@ -157,8 +141,20 @@ export class RuleService extends ServiceBase<RuleListResponse, RuleList> impleme
157141

158142
async superUpsert(request: RuleList, ctx: any): Promise<DeepPartial<RuleListResponse>> {
159143
const result = await super.upsert(request, ctx);
160-
// const policySets: Map<string, PolicySetWithCombinables> = await policySetService.load() || new Map();
161-
// this.policySets = policySets;
144+
const policySets = _.cloneDeep(_accessController.policySets);
145+
146+
if (result?.items?.length > 0) {
147+
for (let item of result.items) {
148+
const rule: Rule = marshallResource(item?.payload, 'rule');
149+
for (let [, policySet] of policySets) {
150+
for (let [, policy] of (policySet).combinables) {
151+
if (!_.isNil(policy) && policy.combinables.has(rule.id)) {
152+
_accessController.updateRule(policySet.id, policy.id, rule);
153+
}
154+
}
155+
}
156+
}
157+
}
162158
return result;
163159
}
164160

@@ -189,7 +185,20 @@ export class RuleService extends ServiceBase<RuleListResponse, RuleList> impleme
189185
return { operation_status: acsResponse.operation_status };
190186
}
191187
const result = await super.create(request, ctx);
192-
await this.reloadRules(result);
188+
const policySets = _.cloneDeep(_accessController.policySets);
189+
190+
if (result?.items?.length > 0) {
191+
for (let item of result.items) {
192+
const rule: Rule = marshallResource(item?.payload, 'rule');
193+
for (let [, policySet] of policySets) {
194+
for (let [, policy] of (policySet).combinables) {
195+
if (!_.isNil(policy) && policy.combinables.has(rule.id)) {
196+
_accessController.updateRule(policySet.id, policy.id, rule);
197+
}
198+
}
199+
}
200+
}
201+
}
193202
return result;
194203
}
195204

@@ -248,7 +257,6 @@ export class RuleService extends ServiceBase<RuleListResponse, RuleList> impleme
248257
return { operation_status: acsResponse.operation_status };
249258
}
250259
const result = await super.update(request, ctx);
251-
await this.reloadRules(result);
252260
return result;
253261
}
254262

@@ -277,7 +285,7 @@ export class RuleService extends ServiceBase<RuleListResponse, RuleList> impleme
277285
if (acsResponse.decision != Response_Decision.PERMIT) {
278286
return { operation_status: acsResponse.operation_status };
279287
}
280-
const result = await this.superUpsert(request, ctx);
288+
const result = await super.upsert(request, ctx);
281289
return result;
282290
}
283291

@@ -382,35 +390,32 @@ export class PolicyService extends ServiceBase<PolicyListResponse, PolicyList> i
382390
return this.getPolicies();
383391
}
384392

385-
// async reloadPolicies(result: DeepPartial<PolicyListResponse>): Promise<void> {
386-
// const policySets = _.cloneDeep(_accessController.policySets);
387-
// if (result?.items?.length > 0) {
388-
// for (let item of result.items) {
389-
// for (let [, policySet] of policySets) {
390-
// if (policySet.combinables.has(item.payload?.id)) {
391-
// const policy: PolicyWithCombinables = marshallResource(item.payload, 'policy');
392-
393-
// if (_.has(item.payload, 'rules') && !_.isEmpty(item.payload.rules)) {
394-
// policy.combinables = await ruleService.getRules(item.payload.rules);
395-
396-
// if (policy.combinables.size != item?.payload?.rules?.length) {
397-
// for (let id of item.payload.rules) {
398-
// if (!policy.combinables.has(id)) {
399-
// policy.combinables.set(id, null);
400-
// }
401-
// }
402-
// }
403-
// }
404-
// _accessController.updatePolicy(policySet.id, policy);
405-
// }
406-
// }
407-
// }
408-
// }
409-
// }
410-
411393
async superUpsert(request: PolicyList, ctx: any): Promise<DeepPartial<PolicyListResponse>> {
412394
const result = await super.upsert(request, ctx);
413-
await _accessController.loadPolicies();
395+
const policySets = _.cloneDeep(_accessController.policySets);
396+
397+
if (result?.items?.length > 0) {
398+
for (let item of result.items) {
399+
for (let [, policySet] of policySets) {
400+
if (policySet.combinables.has(item.payload?.id)) {
401+
const policy: PolicyWithCombinables = marshallResource(item.payload, 'policy');
402+
403+
if (_.has(item.payload, 'rules') && !_.isEmpty(item.payload.rules)) {
404+
policy.combinables = await ruleService.getRules(item.payload.rules);
405+
406+
if (policy.combinables.size != item?.payload?.rules?.length) {
407+
for (let id of item.payload.rules) {
408+
if (!policy.combinables.has(id)) {
409+
policy.combinables.set(id, null);
410+
}
411+
}
412+
}
413+
}
414+
_accessController.updatePolicy(policySet.id, policy);
415+
}
416+
}
417+
}
418+
}
414419
return result;
415420
}
416421

@@ -440,7 +445,30 @@ export class PolicyService extends ServiceBase<PolicyListResponse, PolicyList> i
440445
return { operation_status: acsResponse.operation_status };
441446
}
442447
const result = await super.create(request, ctx);
443-
await this.reloadPolicies(result);
448+
const policySets = _.cloneDeep(_accessController.policySets);
449+
450+
if (result?.items?.length > 0) {
451+
for (let item of result.items) {
452+
for (let [, policySet] of policySets) {
453+
if (policySet.combinables.has(item.payload?.id)) {
454+
const policy: PolicyWithCombinables = marshallResource(item.payload, 'policy');
455+
456+
if (_.has(item.payload, 'rules') && !_.isEmpty(item.payload.rules)) {
457+
policy.combinables = await ruleService.getRules(item.payload.rules);
458+
459+
if (policy.combinables.size != item?.payload?.rules?.length) {
460+
for (let id of item.payload.rules) {
461+
if (!policy.combinables.has(id)) {
462+
policy.combinables.set(id, null);
463+
}
464+
}
465+
}
466+
}
467+
_accessController.updatePolicy(policySet.id, policy);
468+
}
469+
}
470+
}
471+
}
444472

445473
return result;
446474
}
@@ -513,7 +541,6 @@ export class PolicyService extends ServiceBase<PolicyListResponse, PolicyList> i
513541
return { operation_status: acsResponse.operation_status };
514542
}
515543
const result = await super.update(request, ctx);
516-
await this.reloadPolicies(result);
517544
return result;
518545
}
519546

@@ -542,7 +569,7 @@ export class PolicyService extends ServiceBase<PolicyListResponse, PolicyList> i
542569
if (acsResponse.decision != Response_Decision.PERMIT) {
543570
return { operation_status: acsResponse.operation_status };
544571
}
545-
const result = await this.superUpsert(request, ctx);
572+
const result = await super.upsert(request, ctx);
546573
return result;
547574
}
548575

@@ -958,7 +985,7 @@ export class PolicySetService extends ServiceBase<PolicySetListResponse, PolicyS
958985
if (acsResponse.decision != Response_Decision.PERMIT) {
959986
return { operation_status: acsResponse.operation_status };
960987
}
961-
const result = await this.superUpsert(request, ctx);
988+
const result = await super.upsert(request, ctx);
962989
return result;
963990
}
964991
}

src/worker.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,17 @@ export class Worker {
106106
_.assign({}, kafkaConfig, policySetConfig, policyConfig, ruleConfig));
107107

108108
kafkaConfig = this.cfg.get('events:kafka');
109+
const acsEvents = [
110+
'policy_setCreated',
111+
'policy_setModified',
112+
'policy_setDeleted',
113+
'policyCreated',
114+
'policyModified',
115+
'policyDeleted',
116+
'ruleCreated',
117+
'ruleModified',
118+
'ruleDeleted',
119+
];
109120
const hierarchicalScopesResponse = 'hierarchicalScopesResponse';
110121
const events = new Events(kafkaConfig, this.logger); // Kafka
111122
await events.start();
@@ -225,13 +236,14 @@ export class Worker {
225236

226237
this.logger.info('Access control service started correctly!');
227238
await accessControlService.loadPolicies();
228-
this.logger.info('Access control service policies loaded successfully');
229239

230240
const that = this;
231241
const commandTopic = await events.topic(this.cfg.get('events:kafka:topics:command:topic'));
232242
const eventListener = async (msg: any,
233243
context: any, config: any, eventName: string): Promise<any> => {
234-
if (eventName === hierarchicalScopesResponse) {
244+
if (acsEvents.indexOf(eventName) > -1) {
245+
await accessControlService.loadPolicies();
246+
} else if (eventName === hierarchicalScopesResponse) {
235247
// Add subject_id to waiting list
236248
const hierarchical_scopes = msg?.hierarchical_scopes ? msg.hierarchical_scopes : [];
237249
const tokenDate = msg?.token;

0 commit comments

Comments
 (0)