Skip to content

Commit 05278a0

Browse files
feat: Adding recursion option while adding Policies from Path (#545)
Adding recursion in addPoliciesFromPath, default to false for backward compatibility. Bumping CDK Version. Fixes #526 --------- Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 392720d commit 05278a0

File tree

8 files changed

+171
-134
lines changed

8 files changed

+171
-134
lines changed

.projen/deps.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.projenrc.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const project = new CdklabsConstructLibrary({
44
authorAddress: 'aws-avp-cdk-dev@amazon.com',
55
description: 'L2 AWS CDK Constructs for Amazon Verified Permissions',
66
keywords: ['cdk', 'aws-cdk', 'awscdk', 'aws', 'verified-permissions', 'authorization', 'verifiedpermissions'],
7-
cdkVersion: '2.198.0',
7+
cdkVersion: '2.234.1',
88
defaultReleaseBranch: 'main',
99
devDeps: ['cdklabs-projen-project-types'],
1010
bundledDeps: ['@cedar-policy/cedar-wasm@4.5.0'],
@@ -13,7 +13,7 @@ const project = new CdklabsConstructLibrary({
1313
majorVersion: 0,
1414
stability: 'experimental',
1515
releaseToNpm: true,
16-
jsiiVersion: '5.7',
16+
jsiiVersion: '5.9',
1717
private: false,
1818
enablePRAutoMerge: true,
1919
repositoryUrl: 'https://github.com/cdklabs/cdk-verified-permissions.git',

API.md

Lines changed: 83 additions & 67 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/policy-store.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -443,21 +443,33 @@ export class PolicyStore extends PolicyStoreBase {
443443

444444
/**
445445
* Takes in an absolute path to a directory containing .cedar files and adds the contents of each
446-
* .cedar file as policies to this policy store. Parses the policies with cedar-wasm and, if the policy store has a schema,
446+
* .cedar file as policies to this policy store (searching recursively if needed).
447+
* Parses the policies with cedar-wasm and, if the policy store has a schema,
447448
* performs semantic validation of the policies as well.
448449
* @param absolutePath a string representing an absolute path to the directory containing your policies
450+
* @param recursive a boolean representing whether or not to search the directory recursively for .cedar files
449451
* @returns An array of created Policy constructs.
450452
*/
451-
public addPoliciesFromPath(absolutePath: string): Policy[] {
453+
public addPoliciesFromPath(absolutePath: string, recursive: boolean = false): Policy[] {
452454
if (!fs.statSync(absolutePath).isDirectory()) {
453455
throw new Error(
454456
`The path ${absolutePath} does not appear to be a directory`,
455457
);
456458
}
457-
const policyFileNames = fs
458-
.readdirSync(absolutePath)
459-
.map((f) => path.join(absolutePath, f))
460-
.filter((f) => !fs.statSync(f).isDirectory() && f.endsWith('.cedar'));
459+
460+
const policyFileNames: string[] = [];
461+
const processDir = (dirPath: string) => {
462+
const entries = fs.readdirSync(dirPath, { withFileTypes: true });
463+
for (const entry of entries) {
464+
const fullPath = path.join(dirPath, entry.name);
465+
if (entry.isFile() && entry.name.endsWith('.cedar')) {
466+
policyFileNames.push(fullPath);
467+
} else if (entry.isDirectory() && recursive) {
468+
processDir(fullPath);
469+
}
470+
}
471+
};
472+
processDir(absolutePath);
461473

462474
if (this.validationSettings.mode === ValidationSettingsMode.STRICT) {
463475
if (!this.schema) {

test/policy-store.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,43 @@ describe('Policy store with policies from a path', () => {
603603
expect(Object.keys(policyDefns)).toHaveLength(8);
604604
});
605605

606+
test('Creating Policy Store and adding policies to it from a path with recursion', () => {
607+
// GIVEN
608+
const stack = new Stack(undefined, 'Stack');
609+
610+
// WHEN
611+
const policyStore = new PolicyStore(stack, 'PolicyStore', {
612+
validationSettings: {
613+
mode: ValidationSettingsMode.STRICT,
614+
},
615+
schema: {
616+
cedarJson: JSON.stringify(exampleSchema),
617+
},
618+
description: 'PhotoApp',
619+
});
620+
621+
policyStore.addPoliciesFromPath(path.join(__dirname, 'test-policies', 'all-valid'), true);
622+
623+
// THEN
624+
Template.fromStack(stack).hasResourceProperties(
625+
'AWS::VerifiedPermissions::PolicyStore',
626+
{
627+
ValidationSettings: {
628+
Mode: ValidationSettingsMode.STRICT,
629+
},
630+
DeletionProtection: {
631+
Mode: DeletionProtectionMode.DISABLED,
632+
},
633+
Schema: {
634+
CedarJson: JSON.stringify(exampleSchema),
635+
},
636+
},
637+
);
638+
639+
const policyDefns = Template.fromStack(stack).findResources('AWS::VerifiedPermissions::Policy');
640+
expect(Object.keys(policyDefns)).toHaveLength(9);
641+
});
642+
606643
test('fails if the path is not a directory', () => {
607644
// GIVEN
608645
const stack = new Stack(undefined, 'Stack');
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@cdkDescription("I am a description of a policy")
2+
@cdkId("IamAnIDButImNumberTwo")
3+
permit(principal, action, resource);

yarn.lock

Lines changed: 23 additions & 54 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)