Skip to content

Commit

Permalink
feat(getstatements): add public function to get statements in policies
Browse files Browse the repository at this point in the history
  • Loading branch information
roggervalf committed May 30, 2020
1 parent e116987 commit b1aa400
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 23 deletions.
8 changes: 8 additions & 0 deletions dist/main.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ declare class IdentityBased extends Statement {
private action?;
private notResource?;
private notAction?;
private statement;
constructor(identity: IdentityBasedType);
getStatement(): IdentityBasedType;
matches({ action, resource, context, conditionResolver }: MatchIdentityBasedInterface): boolean;
private matchActions;
private matchNotActions;
Expand All @@ -138,7 +140,9 @@ declare class ResourceBased extends Statement {
private notPrincipal?;
private notResource?;
private notAction?;
private statement;
constructor(identity: ResourceBasedType);
getStatement(): ResourceBasedType;
matches({ principal, action, resource, principalType, context, conditionResolver }: MatchResourceBasedInterface): boolean;
matchPrincipals(principal: string, principalType?: string, context?: Context): boolean;
matchNotPrincipals(principal: string, principalType?: string, context?: Context): boolean;
Expand All @@ -152,7 +156,9 @@ declare class IdentityBasedPolicy {
private denyStatements;
private allowStatements;
private conditionResolver?;
private statements;
constructor(config: IdentityBasedType[], conditionResolver?: ConditionResolver);
getStatements(): IdentityBasedType[];
evaluate({ action, resource, context }: EvaluateIdentityBasedInterface): boolean;
can({ action, resource, context }: EvaluateIdentityBasedInterface): boolean;
cannot({ action, resource, context }: EvaluateIdentityBasedInterface): boolean;
Expand All @@ -161,7 +167,9 @@ declare class ResourceBasedPolicy {
private denyStatements;
private allowStatements;
private conditionResolver?;
private statements;
constructor(config: ResourceBasedType[], conditionResolver?: ConditionResolver);
getStatements(): ResourceBasedType[];
evaluate({ principal, action, resource, principalType, context }: EvaluateResourceBasedInterface): boolean;
can({ principal, action, resource, principalType, context }: EvaluateResourceBasedInterface): boolean;
cannot({ principal, action, resource, principalType, context }: EvaluateResourceBasedInterface): boolean;
Expand Down
28 changes: 22 additions & 6 deletions dist/main.es.js

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

2 changes: 1 addition & 1 deletion dist/main.es.js.map

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

28 changes: 22 additions & 6 deletions dist/main.js

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

2 changes: 1 addition & 1 deletion dist/main.js.map

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"prettier:format": "./node_modules/.bin/prettier **/*.{ts,js,json,yml} -l --ignore-path ./.eslintignore",
"prettier:fix": "./node_modules/.bin/prettier **/*.{ts,js,json,yml} --write --ignore-path ./.eslintignore",
"eslint:format": "./node_modules/.bin/eslint **/*.{ts,js} --ignore-path ./.eslintignore",
"eslint:fix": "./node_modules/.bin/eslint . --ignore-path ./.eslintignore",
"eslint:fix": "./node_modules/.bin/eslint . --ignore-path ./.eslintignore --fix",
"prepublishOnly": "yarn build",
"test": "jest --config .unit.jest.config.js",
"test:watch": "jest --config .unit.jest.config.js --watch",
Expand Down
17 changes: 15 additions & 2 deletions src/IdentityBasedPolicy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('IdentityBasedPolicy Class', () => {
() =>
new IdentityBasedPolicy([
{
resource: 'some:glob:*:string/wqweqw',
resource: 'some:glob:*:string/word',
action: ['read', 'write']
}
])
Expand All @@ -18,14 +18,27 @@ describe('IdentityBasedPolicy Class', () => {
expect(
new IdentityBasedPolicy([
{
resource: 'some:glob:*:string/wqweqw',
resource: 'some:glob:*:string/word',
action: ['read', 'write']
}
])
).toBeInstanceOf(IdentityBasedPolicy);
});
});

describe('when get statements', () => {
it('returns those statements', () => {
const statements = [
{
resource: ['books:horror:*'],
action: ['read']
}
];
const policy = new IdentityBasedPolicy(statements);
expect(policy.getStatements()).toEqual(statements);
});
});

describe('when match actions', () => {
it('returns true or false', () => {
const policy = new IdentityBasedPolicy([
Expand Down
6 changes: 6 additions & 0 deletions src/IdentityBasedStatement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class IdentityBased extends Statement {
private action?: string[];
private notResource?: string[];
private notAction?: string[];
private statement: IdentityBasedType;

constructor(identity: IdentityBasedType) {
super(identity);
Expand All @@ -40,6 +41,11 @@ class IdentityBased extends Statement {
? [identity.notAction]
: identity.notAction;
}
this.statement = identity;
}

getStatement(): IdentityBasedType {
return this.statement;
}

matches({
Expand Down
22 changes: 16 additions & 6 deletions src/Policies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@ export class IdentityBasedPolicy {
private denyStatements: IdentityBased[];
private allowStatements: IdentityBased[];
private conditionResolver?: ConditionResolver;
private statements: IdentityBasedType[];
constructor(
config: IdentityBasedType[],
conditionResolver?: ConditionResolver
) {
const statements = config.map(s => new IdentityBased(s));
this.allowStatements = statements.filter(s => s.effect === 'allow');
this.denyStatements = statements.filter(s => s.effect === 'deny');
const statementInstances = config.map(s => new IdentityBased(s));
this.allowStatements = statementInstances.filter(s => s.effect === 'allow');
this.denyStatements = statementInstances.filter(s => s.effect === 'deny');
this.conditionResolver = conditionResolver;
this.statements = config;
}
getStatements(): IdentityBasedType[] {
return this.statements;
}
evaluate({
action,
Expand Down Expand Up @@ -59,14 +64,19 @@ export class ResourceBasedPolicy {
private denyStatements: ResourceBased[];
private allowStatements: ResourceBased[];
private conditionResolver?: ConditionResolver;
private statements: ResourceBasedType[];
constructor(
config: ResourceBasedType[],
conditionResolver?: ConditionResolver
) {
const statements = config.map(s => new ResourceBased(s));
this.allowStatements = statements.filter(s => s.effect === 'allow');
this.denyStatements = statements.filter(s => s.effect === 'deny');
const statementInstances = config.map(s => new ResourceBased(s));
this.allowStatements = statementInstances.filter(s => s.effect === 'allow');
this.denyStatements = statementInstances.filter(s => s.effect === 'deny');
this.conditionResolver = conditionResolver;
this.statements = config;
}
getStatements(): ResourceBasedType[] {
return this.statements;
}
evaluate({
principal,
Expand Down
14 changes: 14 additions & 0 deletions src/ResourceBasedPolicy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ describe('ResourceBasedPolicy Class', () => {
});
});

describe('when get statements', () => {
it('returns those statements', () => {
const statements = [
{
principal: 'andre',
resource: ['books:horror:*'],
action: ['read']
}
];
const policy = new ResourceBasedPolicy(statements);
expect(policy.getStatements()).toEqual(statements);
});
});

describe('when match principal', () => {
it('returns true or false', () => {
const policy = new ResourceBasedPolicy([
Expand Down
6 changes: 6 additions & 0 deletions src/ResourceBasedStatement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class ResourceBased extends Statement {
private notPrincipal?: PrincipalMap | string[];
private notResource?: string[];
private notAction?: string[];
private statement: ResourceBasedType;

constructor(identity: ResourceBasedType) {
super(identity);
Expand Down Expand Up @@ -56,6 +57,11 @@ class ResourceBased extends Statement {
? [identity.notPrincipal]
: identity.notPrincipal;
}
this.statement = identity;
}

getStatement(): ResourceBasedType {
return this.statement;
}

matches({
Expand Down

0 comments on commit b1aa400

Please sign in to comment.