ensure that match and target are not both used

Co-authored-by: Zeky Abubaker <zeky.abubaker@aa.com>
Signed-off-by: Lucas De Souza <lucas.desouza@aa.com>
This commit is contained in:
Lucas De Souza
2022-11-28 14:43:55 -06:00
parent 5cb15b7370
commit cdb670ce52
2 changed files with 35 additions and 5 deletions
@@ -54,6 +54,28 @@ const location: Record<string, LocationSpec> = {
};
describe('DefaultCatalogRulesEnforcer', () => {
it('should throw an error if both match and target are used', () => {
expect(() =>
DefaultCatalogRulesEnforcer.fromConfig(
new ConfigReader({
catalog: {
rules: [
{
allow: ['Component'],
locations: [
{
type: 'url',
match: 'https://github.com/b/**',
target: 'https://github.com/a/b/blob/master/w.yaml',
},
],
},
],
},
}),
),
).toThrow(/cannot have both target and match values/i);
});
it('should deny by default', () => {
const enforcer = new DefaultCatalogRulesEnforcer([]);
expect(enforcer.isAllowed(entity.user, location.x)).toBe(false);
@@ -100,11 +100,19 @@ export class DefaultCatalogRulesEnforcer implements CatalogRulesEnforcer {
allow: ruleConf.getStringArray('allow').map(kind => ({ kind })),
locations: ruleConf
.getOptionalConfigArray('locations')
?.map(locationConfig => ({
match: locationConfig.getOptionalString('match'),
type: locationConfig.getString('type'),
target: locationConfig.getOptionalString('target'),
})),
?.map(locationConfig => {
const location = {
match: locationConfig.getOptionalString('match'),
type: locationConfig.getString('type'),
target: locationConfig.getOptionalString('target'),
};
if (location.match && location.target) {
throw new Error(
'A catalog rule location cannot have both target and match values',
);
}
return location;
}),
}));
rules.push(...globalRules);
} else {