Skip to content

Commit

Permalink
refactor(createhrscope): some small refactoring of createHRScope
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerald Baulig committed May 6, 2024
1 parent f731f1c commit bf97d86
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 24 deletions.
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"@types/nock": "^11.1.0",
"@types/node": "^20.12.7",
"@typescript-eslint/eslint-plugin": "^7.7.0",
"@types/traverse": "^0.6.36",
"@typescript-eslint/eslint-plugin-tslint": "^7.0.2",
"@typescript-eslint/parser": "^7.7.0",
"c8": "^9.1.0",
Expand Down
32 changes: 15 additions & 17 deletions src/core/accessController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -730,23 +730,16 @@ export class AccessController {
let redisHRScopesKey;
if (tokenFound?.interactive) {
redisHRScopesKey = `cache:${subjectID}:hrScopes`;
} else if (tokenFound && !tokenFound.interactive) {
redisHRScopesKey = `cache:${subjectID}:${token}:hrScopes`;
}
let timeout = this.cfg.get('authorization:hrReqTimeout');
if (!timeout) {
timeout = 300000;
}
let hrScopes: any;
try {
hrScopes = await this.getRedisKey(redisHRScopesKey);
} catch (err) {
this.logger.info(`Subject or HR Scope not persisted in redis in acs`);
else if (tokenFound && !tokenFound.interactive) {
redisHRScopesKey = `cache:${subjectID}:${token}:hrScopes`;
}
let keyExist;
if (redisHRScopesKey) {
keyExist = await this.redisClient.exists(redisHRScopesKey);
else {
return context;
}
const timeout = this.cfg.get('authorization:hrReqTimeout') ?? 300000;
const keyExist = await this.redisClient.exists(redisHRScopesKey);

if (!keyExist) {
const date = new Date().toISOString();
const tokenDate = token + ':' + date;
Expand All @@ -759,14 +752,19 @@ export class AccessController {
}, timeout);
this.waiting[tokenDate].push({ resolve, reject, timeoutId });
});
const subjectHRScopes = await this.getRedisKey(redisHRScopesKey);
Object.assign(context.subject, { hierarchical_scopes: subjectHRScopes });
} catch (err) {
// unhandled promise rejection for timeout
this.logger.error(`Error creating Hierarchical scope for subject ${tokenDate}`);
}
const subjectHRScopes = await this.getRedisKey(redisHRScopesKey);
Object.assign(context.subject, { hierarchical_scopes: subjectHRScopes });
} else {
Object.assign(context.subject, { hierarchical_scopes: hrScopes });
try {
const subjectHRScopes = await this.getRedisKey(redisHRScopesKey);
Object.assign(context.subject, { hierarchical_scopes: subjectHRScopes });
} catch (err) {
this.logger.info(`Subject or HR Scope not persisted in redis in acs`);
}
}
return context;
}
Expand Down
28 changes: 21 additions & 7 deletions src/core/hierarchicalScope.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import _ from 'lodash-es';
import traverse from 'traverse';
import { Logger } from 'winston';
import { AccessController } from '.';
import { AccessController } from './index.js';
import { Request } from '@restorecommerce/rc-grpc-clients/dist/generated-server/io/restorecommerce/access_control.js';
import { Target } from '@restorecommerce/rc-grpc-clients/dist/generated-server/io/restorecommerce/rule.js';
import { Attribute } from '@restorecommerce/rc-grpc-clients/dist/generated-server/io/restorecommerce/attribute.js';
import { Resource, ContextWithSubResolved } from './interfaces.js';

export const checkHierarchicalScope = async (ruleTarget: Target,
request: Request, urns: Map<string, string>, accessController: AccessController, logger?: Logger): Promise<boolean> => {
export const checkHierarchicalScope = async (
ruleTarget: Target,
request: Request,
urns: Map<string, string>,
accessController: AccessController,
logger?: Logger
): Promise<boolean> => {
// 1) create a Map of resourceID with Owners for resource IDs which have the rule entity matching
// 2) In HR scope match validate the Owner indicatory entity with vale from matching users Rule's role for
// matching role scoping enitty with instance
Expand Down Expand Up @@ -162,10 +167,18 @@ export const checkHierarchicalScope = async (ruleTarget: Target,
return reducedUserRoleAssocs?.some((roleObj) => {
// check if Rule's roleScoping Entity matches the Owner's role scoping entity and RoleAssociation RoleScoping entity (ex: Organization / User / Klasse etc)
// and check if roleScoping Instance matches with owner instance
const match = roleObj?.attributes?.some((roleAttributeObject) => roleAttributeObject?.id === urns.get('roleScopingEntity')
&& ownerObj?.id === urns.get('ownerEntity') && ownerObj.value === ruleRoleScopingEntity && ownerObj.value === roleAttributeObject?.value
&& roleAttributeObject?.attributes?.some((roleInstObj) =>
roleInstObj?.id === urns.get('roleScopingInstance') && ownerObj?.attributes?.find((ownerInstObj) => ownerInstObj?.value === roleInstObj?.value)));
const match = roleObj?.attributes?.some(
(roleAttributeObject) => roleAttributeObject?.id === urns.get('roleScopingEntity')
&& ownerObj?.id === urns.get('ownerEntity')
&& ownerObj.value === ruleRoleScopingEntity
&& ownerObj.value === roleAttributeObject?.value
&& roleAttributeObject?.attributes?.some(
(roleInstObj) => roleInstObj?.id === urns.get('roleScopingInstance')
&& ownerObj?.attributes?.find(
(ownerInstObj) => ownerInstObj?.value === roleInstObj?.value
)
)
);
logger.debug('Match result for comparing owner indicatory entity and instance with role scoping entity and instance', { match });
return match;
});
Expand All @@ -190,6 +203,7 @@ export const checkHierarchicalScope = async (ruleTarget: Target,
if (context?.subject?.token && _.isEmpty(context.subject.hierarchical_scopes)) {
context = await accessController.createHRScope(context);
}

const reducedHRScopes = context?.subject?.hierarchical_scopes?.filter((hrObj) => hrObj?.role === ruleRole);
for (let [resourceId, owners] of resourceIdOwnersMap) {
const ownerInstances: string[] = owners.filter(
Expand Down

0 comments on commit bf97d86

Please sign in to comment.