diff --git a/src/enforcer.ts b/src/enforcer.ts index 1ad7db3..91ad530 100644 --- a/src/enforcer.ts +++ b/src/enforcer.ts @@ -132,19 +132,15 @@ export class Enforcer { this.model = m; this.model.printModel(); this.fm = FunctionMap.loadFunctionMap(); - - this.initialize(); - - if (this.adapter && (this.adapter instanceof FileAdapter)) { - // error intentionally ignored - this.loadPolicy(); - } } - private initialize(): void { + public async initialize(): Promise { this.enabled = true; this.autoSave = true; this.autoBuildRoleLinks = true; + if (this.adapter) { + await this.loadPolicy(); + } } /** @@ -222,7 +218,7 @@ export class Enforcer { */ public setWatcher(watcher: Watcher): void { this.watcher = watcher; - watcher.setUpdateCallback(() => this.loadPolicy()); + watcher.setUpdateCallback(async () => await this.loadPolicy()); } /** @@ -253,9 +249,9 @@ export class Enforcer { /** * loadPolicy reloads the policy from file/database. */ - public loadPolicy(): void { + public async loadPolicy(): Promise { this.model.clearPolicy(); - this.adapter.loadPolicy(this.model); + await this.adapter.loadPolicy(this.model); this.model.printPolicy(); if (this.autoBuildRoleLinks) { @@ -300,11 +296,12 @@ export class Enforcer { * savePolicy saves the current policy (usually after changed with * Casbin API) back to file/database. */ - public savePolicy(): boolean { + public async savePolicy(): Promise { if (this.isFiltered()) { throw new Error('cannot save a filtered policy'); } - if (!this.adapter.savePolicy(this.model)) { + const flag = await this.adapter.savePolicy(this.model); + if (!flag) { return false; } if (this.watcher) { @@ -510,7 +507,7 @@ export class Enforcer { /** * addPolicy adds a rule to the current policy. */ - public addPolicy(sec: string | any[], key?: string, rule?: string[]): boolean { + public async addPolicy(sec: string | any[], key?: string, rule?: string[]): Promise { if (typeof sec === 'string' && key && rule) { const ruleAdded = this.model.addPolicy(sec, key, rule); if (!ruleAdded) { @@ -518,7 +515,7 @@ export class Enforcer { } if (this.adapter !== null && this.autoSave) { - this.adapter.addPolicy(sec, key, rule); + await this.adapter.addPolicy(sec, key, rule); if (this.watcher !== null) { // error intentionally ignored this.watcher.update(); @@ -527,7 +524,7 @@ export class Enforcer { return ruleAdded; } else if (sec instanceof Array) { - return this.addNamedPolicy('p', sec); + return await this.addNamedPolicy('p', sec); } else { return false; } @@ -536,7 +533,7 @@ export class Enforcer { /** * removePolicy removes a rule from the current policy. */ - public removePolicy(sec: string | any[], key?: string, rule?: string[]): boolean { + public async removePolicy(sec: string | any[], key?: string, rule?: string[]): Promise { if (typeof sec === 'string' && key && rule) { const ruleRemoved = this.model.removePolicy(sec, key, rule); if (!ruleRemoved) { @@ -544,7 +541,7 @@ export class Enforcer { } if (this.adapter !== null && this.autoSave) { - this.adapter.removePolicy(sec, key, rule); + await this.adapter.removePolicy(sec, key, rule); if (this.watcher !== null) { // error intentionally ignored this.watcher.update(); @@ -553,7 +550,7 @@ export class Enforcer { return ruleRemoved; } else if (sec instanceof Array) { - return this.removeNamedPolicy('p', sec); + return await this.removeNamedPolicy('p', sec); } else { return false; } @@ -562,7 +559,7 @@ export class Enforcer { /** * removeFilteredPolicy removes rules based on field filters from the current policy. */ - public removeFilteredPolicy(sec: string | number, key: string | string[], fieldIndex?: number, fieldValues?: string[]): boolean { + public async removeFilteredPolicy(sec: string | number, key: string | string[], fieldIndex?: number, fieldValues?: string[]): Promise { if (typeof sec === 'string' && typeof key === 'string' && fieldIndex && fieldValues instanceof Array) { const ruleRemoved = this.model.removeFilteredPolicy(sec, key, fieldIndex, ...fieldValues); if (!ruleRemoved) { @@ -570,7 +567,7 @@ export class Enforcer { } if (this.adapter !== null && this.autoSave) { - this.adapter.removeFilteredPolicy(sec, key, fieldIndex, ...fieldValues); + await this.adapter.removeFilteredPolicy(sec, key, fieldIndex, ...fieldValues); if (this.watcher !== null) { // error intentionally ignored this.watcher.update(); @@ -579,7 +576,7 @@ export class Enforcer { return ruleRemoved; } else if (typeof sec === 'number' && key instanceof Array) { - return this.removeFilteredNamedPolicy('p', sec, key); + return await this.removeFilteredNamedPolicy('p', sec, key); } else { return false; } @@ -806,12 +803,12 @@ export class Enforcer { * @param params the "p" policy rule. * @return succeeds or not. */ - public addNamedPolicy(ptype: string, params: any[]): boolean { + public async addNamedPolicy(ptype: string, params: any[]): Promise { let ruleAdded = false; if (params.length === 1 && params[0] instanceof Array) { - ruleAdded = this.addPolicy('p', ptype, params[0]); + ruleAdded = await this.addPolicy('p', ptype, params[0]); } else { - ruleAdded = this.addPolicy('p', ptype, params); + ruleAdded = await this.addPolicy('p', ptype, params); } return ruleAdded; @@ -824,12 +821,12 @@ export class Enforcer { * @param params the "p" policy rule. * @return succeeds or not. */ - public removeNamedPolicy(ptype: string, params: any[]): boolean { + public async removeNamedPolicy(ptype: string, params: any[]): Promise { let ruleRemoved = false; if (params.length === 1 && params[0] instanceof Array) { - ruleRemoved = this.removePolicy('p', ptype, params[0]); + ruleRemoved = await this.removePolicy('p', ptype, params[0]); } else { - ruleRemoved = this.removePolicy('p', ptype, params); + ruleRemoved = await this.removePolicy('p', ptype, params); } return ruleRemoved; @@ -844,8 +841,8 @@ export class Enforcer { * means not to match this field. * @return succeeds or not. */ - public removeFilteredNamedPolicy(ptype: string, fieldIndex: number, fieldValues: string[]): boolean { - return this.removeFilteredPolicy('p', ptype, fieldIndex, fieldValues); + public async removeFilteredNamedPolicy(ptype: string, fieldIndex: number, fieldValues: string[]): Promise { + return await this.removeFilteredPolicy('p', ptype, fieldIndex, fieldValues); } /** @@ -880,8 +877,8 @@ export class Enforcer { * @param params the "g" policy rule, ptype "g" is implicitly used. * @return succeeds or not. */ - public addGroupingPolicy(...params: any[]): boolean { - return this.addNamedGroupingPolicy('g', params); + public async addGroupingPolicy(...params: any[]): Promise { + return await this.addNamedGroupingPolicy('g', params); } /** @@ -893,12 +890,12 @@ export class Enforcer { * @param params the "g" policy rule. * @return succeeds or not. */ - public addNamedGroupingPolicy(ptype: string, ...params: any[]): boolean { + public async addNamedGroupingPolicy(ptype: string, ...params: any[]): Promise { let ruleadded = false; if (params.length === 1 && params[0] instanceof Array) { - ruleadded = this.addPolicy('g', ptype, params[0]); + ruleadded = await this.addPolicy('g', ptype, params[0]); } else { - ruleadded = this.addPolicy('g', ptype, params); + ruleadded = await this.addPolicy('g', ptype, params); } if (this.autoBuildRoleLinks) { @@ -914,8 +911,8 @@ export class Enforcer { * @param params the "g" policy rule, ptype "g" is implicitly used. * @return succeeds or not. */ - public removeGroupingPolicy(...params: any[]): boolean { - return this.removeNamedGroupingPolicy('g', params); + public async removeGroupingPolicy(...params: any[]): Promise { + return await this.removeNamedGroupingPolicy('g', params); } /** @@ -926,8 +923,8 @@ export class Enforcer { * means not to match this field. * @return succeeds or not. */ - public removeFilteredGroupingPolicy(fieldIndex: number, ...fieldValues: string[]): boolean { - return this.removeFilteredNamedGroupingPolicy('g', fieldIndex, ...fieldValues); + public async removeFilteredGroupingPolicy(fieldIndex: number, ...fieldValues: string[]): Promise { + return await this.removeFilteredNamedGroupingPolicy('g', fieldIndex, ...fieldValues); } /** @@ -937,12 +934,12 @@ export class Enforcer { * @param params the "g" policy rule. * @return succeeds or not. */ - public removeNamedGroupingPolicy(ptype: string, ...params: any[]): boolean { + public async removeNamedGroupingPolicy(ptype: string, ...params: any[]): Promise { let ruleRemoved = false; if (params.length === 1 && params[0] instanceof Array) { - ruleRemoved = this.removePolicy('g', ptype, params[0]); + ruleRemoved = await this.removePolicy('g', ptype, params[0]); } else { - ruleRemoved = this.removePolicy('g', ptype, params); + ruleRemoved = await this.removePolicy('g', ptype, params); } if (this.autoBuildRoleLinks) { @@ -960,8 +957,8 @@ export class Enforcer { * means not to match this field. * @return succeeds or not. */ - public removeFilteredNamedGroupingPolicy(ptype: string, fieldIndex: number, ...fieldValues: string[]): boolean { - const ruleRemoved = this.removeFilteredPolicy('g', ptype, fieldIndex, fieldValues); + public async removeFilteredNamedGroupingPolicy(ptype: string, fieldIndex: number, ...fieldValues: string[]): Promise { + const ruleRemoved = await this.removeFilteredPolicy('g', ptype, fieldIndex, fieldValues); if (this.autoBuildRoleLinks) { this.buildRoleLinks(); } @@ -1032,8 +1029,8 @@ export class Enforcer { * @param role the role. * @return succeeds or not. */ - public addRoleForUser(user: string, role: string): boolean { - return this.addGroupingPolicy(user, role); + public async addRoleForUser(user: string, role: string): Promise { + return await this.addGroupingPolicy(user, role); } /** @@ -1044,8 +1041,8 @@ export class Enforcer { * @param role the role. * @return succeeds or not. */ - public deleteRoleForUser(user: string, role: string): boolean { - return this.removeGroupingPolicy(user, role); + public async deleteRoleForUser(user: string, role: string): Promise { + return await this.removeGroupingPolicy(user, role); } /** @@ -1055,8 +1052,8 @@ export class Enforcer { * @param user the user. * @return succeeds or not. */ - public deleteRolesForUser(user: string): boolean { - return this.removeFilteredGroupingPolicy(0, user); + public async deleteRolesForUser(user: string): Promise { + return await this.removeFilteredGroupingPolicy(0, user); } /** @@ -1066,8 +1063,8 @@ export class Enforcer { * @param user the user. * @return succeeds or not. */ - public deleteUser(user: string): boolean { - return this.removeFilteredGroupingPolicy(0, user); + public async deleteUser(user: string): Promise { + return await this.removeFilteredGroupingPolicy(0, user); } /** @@ -1075,9 +1072,9 @@ export class Enforcer { * * @param role the role. */ - public deleteRole(role: string) { - this.removeFilteredGroupingPolicy(1, role); - this.removeFilteredPolicy(0, role); + public async deleteRole(role: string): Promise { + await this.removeFilteredGroupingPolicy(1, role); + await this.removeFilteredPolicy(0, role); } /** @@ -1087,8 +1084,8 @@ export class Enforcer { * @param permission the permission, usually be (obj, act). It is actually the rule without the subject. * @return succeeds or not. */ - public deletePermission(...permission: string[]): boolean { - return this.removeFilteredPolicy(1, permission); + public async deletePermission(...permission: string[]): Promise { + return await this.removeFilteredPolicy(1, permission); } /** @@ -1099,9 +1096,9 @@ export class Enforcer { * @param permission the permission, usually be (obj, act). It is actually the rule without the subject. * @return succeeds or not. */ - public addPermissionForUser(user: string, ...permission: string[]): boolean { + public async addPermissionForUser(user: string, ...permission: string[]): Promise { permission.unshift(user); - return this.addPolicy(permission); + return await this.addPolicy(permission); } /** @@ -1112,9 +1109,9 @@ export class Enforcer { * @param permission the permission, usually be (obj, act). It is actually the rule without the subject. * @return succeeds or not. */ - public deletePermissionForUser(user: string, ...permission: string[]): boolean { + public async deletePermissionForUser(user: string, ...permission: string[]): Promise { permission.unshift(user); - return this.removePolicy(permission); + return await this.removePolicy(permission); } /** @@ -1124,8 +1121,8 @@ export class Enforcer { * @param user the user. * @return succeeds or not. */ - public deletePermissionsForUser(user: string): boolean { - return this.removeFilteredPolicy(0, user); + public async deletePermissionsForUser(user: string): Promise { + return await this.removeFilteredPolicy(0, user); } /** diff --git a/src/persist/adapter.ts b/src/persist/adapter.ts index f5de9ea..7326d9c 100644 --- a/src/persist/adapter.ts +++ b/src/persist/adapter.ts @@ -15,14 +15,14 @@ import { Model } from '../model'; export interface Adapter { - loadPolicy(model: Model): void; + loadPolicy(model: Model): Promise; - savePolicy(model: Model): void; + savePolicy(model: Model): Promise; - addPolicy(sec: string, ptype: string, rule: string[]): void; + addPolicy(sec: string, ptype: string, rule: string[]): Promise; - removePolicy(sec: string, ptype: string, rule: string[]): void; + removePolicy(sec: string, ptype: string, rule: string[]): Promise; removeFilteredPolicy(sec: string, ptype: string, fieldIndex: number, ...fieldValues: string[] - ): void; + ): Promise; } diff --git a/src/persist/defaultFilteredAdapter.ts b/src/persist/defaultFilteredAdapter.ts index 6da3783..0f5e0e1 100644 --- a/src/persist/defaultFilteredAdapter.ts +++ b/src/persist/defaultFilteredAdapter.ts @@ -3,7 +3,7 @@ import { Model } from '../model'; import { FileAdapter } from './fileAdapter'; import { Adapter } from './adapter'; import { Helper } from './helper'; -import { readFileSync } from 'fs'; +import { readFile } from '../util'; export class Filter { public g: string[] = []; @@ -20,14 +20,14 @@ export class DefaultFilteredAdapter extends FileAdapter } // loadPolicy loads all policy rules from the storage. - public loadPolicy(model: Model): void { + public async loadPolicy(model: Model): Promise { this.filtered = false; - super.loadPolicy(model); + await super.loadPolicy(model); } - public loadFilteredPolicy(model: Model, filter: Filter): void { + public async loadFilteredPolicy(model: Model, filter: Filter): Promise { if (!filter) { - this.loadPolicy(model); + await this.loadPolicy(model); return; } @@ -35,15 +35,15 @@ export class DefaultFilteredAdapter extends FileAdapter throw new Error('invalid file path, file path cannot be empty'); } - this.loadFilteredPolicyFile(model, filter, Helper.loadPolicyLine); + await this.loadFilteredPolicyFile(model, filter, Helper.loadPolicyLine); this.filtered = true; } - private loadFilteredPolicyFile(model: Model, filter: Filter, handler: (line: string, model: Model) => void - ) { - const bodyBuf = readFileSync(this.filePath); + private async loadFilteredPolicyFile(model: Model, filter: Filter, handler: (line: string, model: Model) => void + ): Promise { + const bodyBuf = await readFile(this.filePath); const lines = bodyBuf.toString().split('\n'); - lines.forEach((n, index) => { + lines.forEach((n: string, index: number) => { const line = n.trim(); if (!line || DefaultFilteredAdapter.filterLine(line, filter)) { return; @@ -56,11 +56,12 @@ export class DefaultFilteredAdapter extends FileAdapter return this.filtered; } - public savePolicy(model: Model): void { + public async savePolicy(model: Model): Promise { if (this.filtered) { throw new Error('cannot save a filtered policy'); } - super.savePolicy(model); + await super.savePolicy(model); + return true; } private static filterLine(line: string, filter: Filter): boolean { diff --git a/src/persist/fileAdapter.ts b/src/persist/fileAdapter.ts index aa46938..0427a5a 100644 --- a/src/persist/fileAdapter.ts +++ b/src/persist/fileAdapter.ts @@ -1,8 +1,7 @@ import { Adapter } from './adapter'; import { Model } from '../model'; import { Helper } from './helper'; -import { readFileSync, writeFileSync } from 'fs'; -import { arrayToString } from '../util'; +import { readFile, writeFile, arrayToString } from '../util'; /** * FileAdapter is the file adapter for Casbin. @@ -19,18 +18,18 @@ export class FileAdapter implements Adapter { this.filePath = filePath; } - public loadPolicy(model: Model): void { + public async loadPolicy(model: Model): Promise { if (!this.filePath) { // throw new Error('invalid file path, file path cannot be empty'); return; } - this.loadPolicyFile(model, Helper.loadPolicyLine); + await this.loadPolicyFile(model, Helper.loadPolicyLine); } - private loadPolicyFile(model: Model, handler: (line: string, model: Model) => void) { - const bodyBuf = readFileSync(this.filePath); + private async loadPolicyFile(model: Model, handler: (line: string, model: Model) => void): Promise { + const bodyBuf = await readFile(this.filePath); const lines = bodyBuf.toString().split('\n'); - lines.forEach((n, index) => { + lines.forEach((n: string, index: number) => { const line = n.trim(); if (!line) { return; @@ -42,23 +41,23 @@ export class FileAdapter implements Adapter { /** * addPolicy adds a policy rule to the storage. */ - public addPolicy(sec: string, ptype: string, rule: string[]): void { + public async addPolicy(sec: string, ptype: string, rule: string[]): Promise { throw new Error('not implemented'); } /** * savePolicy saves all policy rules to the storage. */ - public savePolicy(model: Model): void { + public async savePolicy(model: Model): Promise { if (!this.filePath) { // throw new Error('invalid file path, file path cannot be empty'); - return; + return false; } let result = ''; const pList = model.model.get('p'); if (!pList) { - return; + return false; } pList.forEach(n => { n.policy.forEach(m => { @@ -70,7 +69,7 @@ export class FileAdapter implements Adapter { const gList = model.model.get('g'); if (!gList) { - return; + return false; } gList.forEach(n => { n.policy.forEach(m => { @@ -80,24 +79,25 @@ export class FileAdapter implements Adapter { }); }); - this.savePolicyFile(result.trim()); + await this.savePolicyFile(result.trim()); + return true; } - private savePolicyFile(text: string) { - writeFileSync(this.filePath, text); + private async savePolicyFile(text: string): Promise { + await writeFile(this.filePath, text); } /** * removePolicy removes a policy rule from the storage. */ - public removePolicy(sec: string, ptype: string, rule: string[]): void { + public async removePolicy(sec: string, ptype: string, rule: string[]): Promise { throw new Error('not implemented'); } /** * removeFilteredPolicy removes policy rules that match the filter from the storage. */ - public removeFilteredPolicy(sec: string, ptype: string, fieldIndex: number, ...fieldValues: string[]): void { + public async removeFilteredPolicy(sec: string, ptype: string, fieldIndex: number, ...fieldValues: string[]): Promise { throw new Error('not implemented'); } } diff --git a/src/persist/filteredAdapter.ts b/src/persist/filteredAdapter.ts index 9555cd1..924f0e6 100644 --- a/src/persist/filteredAdapter.ts +++ b/src/persist/filteredAdapter.ts @@ -3,7 +3,7 @@ import { Adapter } from './adapter'; export interface FilteredAdapter extends Adapter { // loadFilteredPolicy loads only policy rules that match the filter. - loadFilteredPolicy(model: Model, filter: any): void; + loadFilteredPolicy(model: Model, filter: any): Promise; // isFiltered returns true if the loaded policy has been filtered. isFiltered(): boolean; } diff --git a/src/util/util.ts b/src/util/util.ts index e06969c..482e535 100644 --- a/src/util/util.ts +++ b/src/util/util.ts @@ -13,6 +13,7 @@ // limitations under the License. import * as _ from 'lodash'; +import * as fs from 'fs'; // escapeAssertion escapes the dots in the assertion, // because the expression evaluation doesn't support such variable names. @@ -58,6 +59,30 @@ function setEquals(a: string[], b: string[]): boolean { return _.isEqual(_.sortedUniq(a), _.sortedUniq(b)); } +// readFile return a promise for readFile. +function readFile(path: string, encoding?: string): any { + return new Promise((resolve, reject) => { + fs.readFile(path, encoding || 'utf8', (error, data) => { + if (error) { + reject(error); + } + resolve(data); + }); + }); +} + +// writeFile return a promise for writeFile. +function writeFile(path: string, file: string, encoding?: string): any { + return new Promise((resolve, reject) => { + fs.writeFile(path, file, encoding || 'utf8', (error) => { + if (error) { + reject(error); + } + resolve(true); + }); + }); +} + export { escapeAssertion, removeComments, @@ -66,5 +91,7 @@ export { arrayRemoveDuplicates, arrayToString, paramsToString, - setEquals + setEquals, + readFile, + writeFile }; diff --git a/test/enforcer.test.ts b/test/enforcer.test.ts index 3fe45bf..c18c04b 100644 --- a/test/enforcer.test.ts +++ b/test/enforcer.test.ts @@ -19,7 +19,7 @@ function testEnforce(e: Enforcer, sub: string, obj: string, act: string, res: bo expect(e.enforce(sub, obj, act)).toBe(res); } -test('TestKeyMatchModelInMemory', () => { +test('TestKeyMatchModelInMemory', async () => { const m = Enforcer.newModel(); m.addDef('r', 'r', 'sub, obj, act'); m.addDef('p', 'p', 'sub, obj, act'); @@ -29,6 +29,7 @@ test('TestKeyMatchModelInMemory', () => { const a = new FileAdapter('examples/keymatch_policy.csv'); let e = new Enforcer(m, a); + await e.initialize(); testEnforce(e, 'alice', '/alice_data/resource1', 'GET', true); testEnforce(e, 'alice', '/alice_data/resource1', 'POST', true); @@ -53,7 +54,7 @@ test('TestKeyMatchModelInMemory', () => { testEnforce(e, 'cathy', '/cathy_data', 'DELETE', false); e = new Enforcer(m); - a.loadPolicy(e.getModel()); + await e.initialize(); testEnforce(e, 'alice', '/alice_data/resource1', 'GET', true); testEnforce(e, 'alice', '/alice_data/resource1', 'POST', true); @@ -78,7 +79,7 @@ test('TestKeyMatchModelInMemory', () => { testEnforce(e, 'cathy', '/cathy_data', 'DELETE', false); }); -test('TestKeyMatchModelInMemoryDeny', () => { +test('TestKeyMatchModelInMemoryDeny', async () => { const m = Enforcer.newModel(); m.addDef('r', 'r', 'sub, obj, act'); m.addDef('p', 'p', 'sub, obj, act'); @@ -88,6 +89,7 @@ test('TestKeyMatchModelInMemoryDeny', () => { const a = new FileAdapter('examples/keymatch_policy.csv'); const e = new Enforcer(m, a); + await e.initialize(); testEnforce(e, 'alice', '/alice_data/resource2', 'POST', true); }); diff --git a/test/model.test.ts b/test/model.test.ts index 2764b40..0aa9643 100644 --- a/test/model.test.ts +++ b/test/model.test.ts @@ -27,8 +27,9 @@ function testDomainEnforce(e: Enforcer, sub: string, dom: string, obj: string, a expect(e.enforce(sub, dom, obj, act)).toBe(res); } -test('TestBasicModel', () => { +test('TestBasicModel', async () => { const e = new Enforcer('examples/basic_model.conf', 'examples/basic_policy.csv'); + await e.initialize(); testEnforce(e, 'alice', 'data1', 'read', true); testEnforce(e, 'alice', 'data1', 'write', false); @@ -40,8 +41,9 @@ test('TestBasicModel', () => { testEnforce(e, 'bob', 'data2', 'write', true); }); -test('TestBasicModelNoPolicy', () => { +test('TestBasicModelNoPolicy', async () => { const e = new Enforcer('examples/basic_model.conf'); + await e.initialize(); testEnforce(e, 'alice', 'data1', 'read', false); testEnforce(e, 'alice', 'data1', 'write', false); @@ -53,8 +55,9 @@ test('TestBasicModelNoPolicy', () => { testEnforce(e, 'bob', 'data2', 'write', false); }); -test('TestBasicModelWithRoot', () => { +test('TestBasicModelWithRoot', async () => { const e = new Enforcer('examples/basic_with_root_model.conf', 'examples/basic_policy.csv'); + await e.initialize(); testEnforce(e, 'alice', 'data1', 'read', true); testEnforce(e, 'alice', 'data1', 'write', false); @@ -70,8 +73,9 @@ test('TestBasicModelWithRoot', () => { testEnforce(e, 'root', 'data2', 'write', true); }); -test('TestBasicModelWithRootNoPolicy', () => { +test('TestBasicModelWithRootNoPolicy', async () => { const e = new Enforcer('examples/basic_with_root_model.conf'); + await e.initialize(); testEnforce(e, 'alice', 'data1', 'read', false); testEnforce(e, 'alice', 'data1', 'write', false); @@ -87,8 +91,9 @@ test('TestBasicModelWithRootNoPolicy', () => { testEnforce(e, 'root', 'data2', 'write', true); }); -test('TestBasicModelWithoutUsers', () => { +test('TestBasicModelWithoutUsers', async () => { const e = new Enforcer('examples/basic_without_users_model.conf', 'examples/basic_without_users_policy.csv'); + await e.initialize(); testEnforceWithoutUsers(e, 'data1', 'read', true); testEnforceWithoutUsers(e, 'data1', 'write', false); @@ -96,8 +101,9 @@ test('TestBasicModelWithoutUsers', () => { testEnforceWithoutUsers(e, 'data2', 'write', true); }); -test('TestBasicModelWithoutResources', () => { +test('TestBasicModelWithoutResources', async () => { const e = new Enforcer('examples/basic_without_resources_model.conf', 'examples/basic_without_resources_policy.csv'); + await e.initialize(); testEnforceWithoutUsers(e, 'alice', 'read', true); testEnforceWithoutUsers(e, 'alice', 'write', false); @@ -105,8 +111,9 @@ test('TestBasicModelWithoutResources', () => { testEnforceWithoutUsers(e, 'bob', 'write', true); }); -test('TestRBACModel', () => { +test('TestRBACModel', async () => { const e = new Enforcer('examples/rbac_model.conf', 'examples/rbac_policy.csv'); + await e.initialize(); testEnforce(e, 'alice', 'data1', 'read', true); testEnforce(e, 'alice', 'data1', 'write', false); @@ -118,8 +125,9 @@ test('TestRBACModel', () => { testEnforce(e, 'bob', 'data2', 'write', true); }); -test('TestRBACModelWithResourceRoles', () => { +test('TestRBACModelWithResourceRoles', async () => { const e = new Enforcer('examples/rbac_with_resource_roles_model.conf', 'examples/rbac_with_resource_roles_policy.csv'); + await e.initialize(); testEnforce(e, 'alice', 'data1', 'read', true); testEnforce(e, 'alice', 'data1', 'write', true); @@ -131,8 +139,9 @@ test('TestRBACModelWithResourceRoles', () => { testEnforce(e, 'bob', 'data2', 'write', true); }); -test('TestRBACModelWithDomains', () => { +test('TestRBACModelWithDomains', async () => { const e = new Enforcer('examples/rbac_with_domains_model.conf', 'examples/rbac_with_domains_policy.csv'); + await e.initialize(); testDomainEnforce(e, 'alice', 'domain1', 'data1', 'read', true); testDomainEnforce(e, 'alice', 'domain1', 'data1', 'write', true); @@ -154,8 +163,9 @@ class TestResource { } } -test('TestABACModel', () => { +test('TestABACModel', async () => { const e = new Enforcer('examples/abac_model.conf'); + await e.initialize(); const data1 = new TestResource('data1', 'alice'); const data2 = new TestResource('data2', 'bob'); @@ -170,8 +180,9 @@ test('TestABACModel', () => { testEnforce(e, 'bob', data2, 'write', true); }); -test('TestKeyMatchModel', () => { +test('TestKeyMatchModel', async () => { const e = new Enforcer('examples/keymatch_model.conf', 'examples/keymatch_policy.csv'); + await e.initialize(); testEnforce(e, 'alice', '/alice_data/resource1', 'GET', true); testEnforce(e, 'alice', '/alice_data/resource1', 'POST', true); @@ -196,8 +207,9 @@ test('TestKeyMatchModel', () => { testEnforce(e, 'cathy', '/cathy_data', 'DELETE', false); }); -test('TestKeyMatch2Model', () => { +test('TestKeyMatch2Model', async () => { const e = new Enforcer('examples/keymatch2_model.conf', 'examples/keymatch2_policy.csv'); + await e.initialize(); testEnforce(e, 'alice', '/alice_data', 'GET', false); testEnforce(e, 'alice', '/alice_data/resource1', 'GET', true); @@ -222,8 +234,9 @@ function customFunctionWrapper(...args: any[]): boolean { return customFunction(name1, name2); } -test('TestKeyMatchCustomModel', () => { +test('TestKeyMatchCustomModel', async () => { const e = new Enforcer('examples/keymatch_custom_model.conf', 'examples/keymatch2_policy.csv'); + await e.initialize(); e.addFunction('keyMatchCustom', customFunctionWrapper); @@ -231,8 +244,9 @@ test('TestKeyMatchCustomModel', () => { testEnforce(e, 'alice', '/alice_data2/myid/using/res_id', 'GET', true); }); -test('TestIPMatchModel', () => { +test('TestIPMatchModel', async () => { const e = new Enforcer('examples/ipmatch_model.conf', 'examples/ipmatch_policy.csv'); + await e.initialize(); testEnforce(e, '192.168.2.123', 'data1', 'read', true); testEnforce(e, '192.168.2.123', 'data1', 'write', false); @@ -255,8 +269,9 @@ test('TestIPMatchModel', () => { testEnforce(e, '192.168.0.1', 'data2', 'write', false); }); -test('TestPriorityModel', () => { +test('TestPriorityModel', async () => { const e = new Enforcer('examples/priority_model.conf', 'examples/priority_policy.csv'); + await e.initialize(); testEnforce(e, 'alice', 'data1', 'read', true); testEnforce(e, 'alice', 'data1', 'write', false); @@ -268,8 +283,9 @@ test('TestPriorityModel', () => { testEnforce(e, 'bob', 'data2', 'write', false); }); -test('TestPriorityModelIndeterminate', () => { +test('TestPriorityModelIndeterminate', async () => { const e = new Enforcer('examples/priority_model.conf', 'examples/priority_indeterminate_policy.csv'); + await e.initialize(); testEnforce(e, 'alice', 'data1', 'read', false); }); diff --git a/yarn.lock b/yarn.lock index 605b03b..f502a88 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2412,11 +2412,7 @@ lodash.sortby@^4.7.0: version "4.7.0" resolved "http://registry.npm.taobao.org/lodash.sortby/download/lodash.sortby-4.7.0.tgz#edd14c824e2cc9c1e0b0a1b42bb5210516a42438" -lodash@^4.13.1, lodash@^4.17.4, lodash@^4.17.5: - version "4.17.10" - resolved "http://registry.npm.taobao.org/lodash/download/lodash-4.17.10.tgz#1b7793cf7259ea38fb3661d4d38b3260af8ae4e7" - -lodash@^4.17.10: +lodash@^4.13.1, lodash@^4.17.10, lodash@^4.17.4, lodash@^4.17.5: version "4.17.10" resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.10.tgz#1b7793cf7259ea38fb3661d4d38b3260af8ae4e7"