Skip to content

Commit 40a1727

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

File tree

4 files changed

+76
-111
lines changed

4 files changed

+76
-111
lines changed

src/accessControlService.ts

Lines changed: 2 additions & 17 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 } from './resourceManager.js';
5+
import { ResourceManager, PolicySetService } from './resourceManager.js';
66
import { RedisClientType } from 'redis';
77
import { AccessController } from './core/accessController.js';
88
import { loadPoliciesFromDoc } from './core/utils.js';
@@ -35,22 +35,7 @@ export class AccessControlService implements AccessControlServiceImplementation
3535
}
3636
async loadPolicies(): Promise<void> {
3737
this.logger.info('Loading policies');
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-
}
38+
this.accessController.loadPolicies(this.resourceManager);
5439
}
5540

5641
clearPolicies(): void {

src/core/accessController.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ 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 } from './utils.js';
23+
import { conditionMatches, loadPoliciesFromDoc } from './utils.js';
24+
import { PolicySetService, ResourceManager } from '../resourceManager.js';
2425

2526
export class AccessController {
2627
policySets: Map<string, PolicySetWithCombinables>;
@@ -75,6 +76,24 @@ export class AccessController {
7576
this.userService = userService;
7677
}
7778

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+
7897
clearPolicies(): void {
7998
this.policySets.clear();
8099
}

src/resourceManager.ts

Lines changed: 52 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,22 @@ 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+
111127
async getRules(ruleIDs?: string[]): Promise<Map<string, Rule>> {
112128
const filters = ruleIDs ? makeFilter(ruleIDs) : {};
113129
const result = await super.read(ReadRequest.fromPartial({ filters }), {});
@@ -141,20 +157,8 @@ export class RuleService extends ServiceBase<RuleListResponse, RuleList> impleme
141157

142158
async superUpsert(request: RuleList, ctx: any): Promise<DeepPartial<RuleListResponse>> {
143159
const result = await super.upsert(request, ctx);
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-
}
160+
// const policySets: Map<string, PolicySetWithCombinables> = await policySetService.load() || new Map();
161+
// this.policySets = policySets;
158162
return result;
159163
}
160164

@@ -185,20 +189,7 @@ export class RuleService extends ServiceBase<RuleListResponse, RuleList> impleme
185189
return { operation_status: acsResponse.operation_status };
186190
}
187191
const result = await super.create(request, ctx);
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-
}
192+
await this.reloadRules(result);
202193
return result;
203194
}
204195

@@ -257,6 +248,7 @@ export class RuleService extends ServiceBase<RuleListResponse, RuleList> impleme
257248
return { operation_status: acsResponse.operation_status };
258249
}
259250
const result = await super.update(request, ctx);
251+
await this.reloadRules(result);
260252
return result;
261253
}
262254

@@ -285,7 +277,7 @@ export class RuleService extends ServiceBase<RuleListResponse, RuleList> impleme
285277
if (acsResponse.decision != Response_Decision.PERMIT) {
286278
return { operation_status: acsResponse.operation_status };
287279
}
288-
const result = await super.upsert(request, ctx);
280+
const result = await this.superUpsert(request, ctx);
289281
return result;
290282
}
291283

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

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+
393411
async superUpsert(request: PolicyList, ctx: any): Promise<DeepPartial<PolicyListResponse>> {
394412
const result = await super.upsert(request, ctx);
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-
}
413+
await _accessController.loadPolicies();
419414
return result;
420415
}
421416

@@ -445,30 +440,7 @@ export class PolicyService extends ServiceBase<PolicyListResponse, PolicyList> i
445440
return { operation_status: acsResponse.operation_status };
446441
}
447442
const result = await super.create(request, ctx);
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-
}
443+
await this.reloadPolicies(result);
472444

473445
return result;
474446
}
@@ -541,6 +513,7 @@ export class PolicyService extends ServiceBase<PolicyListResponse, PolicyList> i
541513
return { operation_status: acsResponse.operation_status };
542514
}
543515
const result = await super.update(request, ctx);
516+
await this.reloadPolicies(result);
544517
return result;
545518
}
546519

@@ -569,7 +542,7 @@ export class PolicyService extends ServiceBase<PolicyListResponse, PolicyList> i
569542
if (acsResponse.decision != Response_Decision.PERMIT) {
570543
return { operation_status: acsResponse.operation_status };
571544
}
572-
const result = await super.upsert(request, ctx);
545+
const result = await this.superUpsert(request, ctx);
573546
return result;
574547
}
575548

@@ -985,7 +958,7 @@ export class PolicySetService extends ServiceBase<PolicySetListResponse, PolicyS
985958
if (acsResponse.decision != Response_Decision.PERMIT) {
986959
return { operation_status: acsResponse.operation_status };
987960
}
988-
const result = await super.upsert(request, ctx);
961+
const result = await this.superUpsert(request, ctx);
989962
return result;
990963
}
991964
}

src/worker.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -106,17 +106,6 @@ 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-
];
120109
const hierarchicalScopesResponse = 'hierarchicalScopesResponse';
121110
const events = new Events(kafkaConfig, this.logger); // Kafka
122111
await events.start();
@@ -236,14 +225,13 @@ export class Worker {
236225

237226
this.logger.info('Access control service started correctly!');
238227
await accessControlService.loadPolicies();
228+
this.logger.info('Access control service policies loaded successfully');
239229

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

0 commit comments

Comments
 (0)