Skip to content

Commit

Permalink
Add inherit parent permissions to crawl (#2950)
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-codaio committed Apr 11, 2024
1 parent 2645533 commit 5177082
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 9 deletions.
3 changes: 2 additions & 1 deletion api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -512,11 +512,12 @@ export function makeParameter<T extends ParameterType, O extends ParameterOption
let crawlStrategy: CrawlStrategy | undefined;
if (crawlStrategyDef) {
if (crawlStrategyDef.parentTable) {
const {tableName, propertyKey} = crawlStrategyDef.parentTable;
const {tableName, propertyKey, inheritPermissions} = crawlStrategyDef.parentTable;
crawlStrategy = {
parentTable: {
tableName,
propertyKey: normalizeSchemaKey(propertyKey),
inheritPermissions,
},
};
} else {
Expand Down
4 changes: 4 additions & 0 deletions api_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,10 @@ export interface CrawlStrategy {
interface SyncTableRelation {
tableName: string;
propertyKey: string;
/**
* Indiciates that permissions should be inherited from this relation using the propertyKey as the item id
*/
inheritPermissions?: boolean;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion dist/api.js

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

4 changes: 4 additions & 0 deletions dist/api_types.d.ts

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

4 changes: 4 additions & 0 deletions dist/bundle.d.ts

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

2 changes: 1 addition & 1 deletion dist/bundle.js

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion dist/testing/upload_validation.js

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
@@ -1,6 +1,6 @@
{
"name": "@codahq/packs-sdk",
"version": "1.7.6-prerelease.5",
"version": "1.7.6-prerelease.6",
"license": "MIT",
"workspaces": [
"dev/eslint"
Expand Down
4 changes: 2 additions & 2 deletions test/api_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ describe('API test', () => {
type: ParameterType.String,
name: 'parent',
description: '',
crawlStrategy: {parentTable: {tableName: 'Parent', propertyKey: 'foo'}},
crawlStrategy: {parentTable: {tableName: 'Parent', propertyKey: 'foo', inheritPermissions: false}},
}),
],
async execute() {
Expand All @@ -615,7 +615,7 @@ describe('API test', () => {
});
// Property key should be normalized.
assert.deepEqual(childTable.getter.parameters[0].crawlStrategy, {
parentTable: {tableName: 'Parent', propertyKey: 'Foo'},
parentTable: {tableName: 'Parent', propertyKey: 'Foo', inheritPermissions: false},
});
});
});
Expand Down
65 changes: 64 additions & 1 deletion test/upload_validation_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2222,7 +2222,7 @@ describe('Pack metadata Validation', async () => {
type: ParameterType.String,
name: 'parentParam',
description: '',
crawlStrategy: {parentTable: {tableName: 'Parent', propertyKey: 'account'}},
crawlStrategy: {parentTable: {tableName: 'Parent', propertyKey: 'account', inheritPermissions: true}},
}),
],
async execute() {
Expand Down Expand Up @@ -2502,6 +2502,69 @@ describe('Pack metadata Validation', async () => {
const hierarchy = validateCrawlHierarchy(metadata.syncTables || []);
assert.isUndefined(hierarchy);
});

it('ensures that inheritPermissions column is the id of the parent sync table', async () => {
const parentTable = makeSyncTable({
name: 'Parent',
identityName: 'ParentIdentity',
schema: makeObjectSchema({
type: ValueType.Object,
id: 'account',
primary: 'account',
properties: {
account: {type: ValueType.String},
group: {type: ValueType.String},
},
}),
formula: {
name: 'Whatever',
description: '',
parameters: [],
async execute() {
return {result: []};
},
},
});
const childTable = makeSyncTable({
name: 'Child',
identityName: 'ChildIdentity',
schema: makeObjectSchema({
type: ValueType.Object,
id: 'bar',
primary: 'bar',
properties: {bar: {type: ValueType.String}},
}),
formula: {
name: 'AnotherWhatever',
description: '',
parameters: [
makeParameter({
type: ParameterType.String,
name: 'parentParam',
description: '',
crawlStrategy: {parentTable: {tableName: 'Parent', propertyKey: 'group', inheritPermissions: true}},
}),
],
async execute() {
return {result: []};
},
},
});

const metadata = createFakePack({
id: 1013,
syncTables: [childTable, parentTable],
});
const err = await validateJsonAndAssertFails(metadata);
assert.deepEqual(err.validationErrors, [
{
message: `Sync table Child expects parent table Parent's schema to have inheritPermissions on the id property.`,
path: 'syncTables[0].parameters[0].crawlStrategy.parentTable',
},
]);
const hierarchy = validateCrawlHierarchy(metadata.syncTables || []);
assert.isUndefined(hierarchy);
});
});
});

Expand Down
11 changes: 10 additions & 1 deletion testing/upload_validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ export function validateCrawlHierarchy(
continue;
}
if (param.crawlStrategy.parentTable) {
const {tableName: parentTableName, propertyKey} = param.crawlStrategy.parentTable;
const {tableName: parentTableName, propertyKey, inheritPermissions} = param.crawlStrategy.parentTable;

const tableSchema = syncTableSchemasByName[parentTableName];
if (!tableSchema) {
Expand All @@ -274,6 +274,15 @@ export function validateCrawlHierarchy(
return undefined;
}

if (inheritPermissions && !(tableSchema.id === propertyKey || tableSchema.idProperty === propertyKey)) {
context?.addIssue({
code: z.ZodIssueCode.custom,
path: ['syncTables', tableIndex, 'parameters', paramIndex, 'crawlStrategy', 'parentTable'],
message: `Sync table ${syncTable.name} expects parent table ${parentTableName}'s schema to have inheritPermissions on the id property.`,
});
return undefined;
}

// TODO(patrick): Validate the types match

// We only allow one parent per table.
Expand Down

0 comments on commit 5177082

Please sign in to comment.