updating to use location instead of sources

Co-authored-by: Zeky Abubaker <zekyabu@users.noreply.github.com>
Signed-off-by: Lucas De Souza <lucas.desouza@aa.com>
This commit is contained in:
Lucas De Souza
2022-11-21 15:01:30 -06:00
parent f75ec75d3e
commit e8303e99ff
3 changed files with 25 additions and 32 deletions
+1 -2
View File
@@ -218,8 +218,7 @@ catalog:
- System
- Domain
- Location
sources:
- github.com/backstage
processors:
ldapOrg:
@@ -37,7 +37,7 @@ const entity = {
const location: Record<string, LocationSpec> = {
w: {
type: 'url',
target: 'https://github.com/backstage/blob/master/w.yaml',
target: 'https://github.com/b/c/blob/master/w.yaml',
},
x: {
type: 'url',
@@ -209,7 +209,7 @@ describe('DefaultCatalogRulesEnforcer', () => {
const enforcer = DefaultCatalogRulesEnforcer.fromConfig(
new ConfigReader({
catalog: {
rules: [{ allow: ['Group'], locations: [{ type: 'url' }] }],
rules: [{ allow: ['Group'] }],
},
}),
);
@@ -221,11 +221,11 @@ describe('DefaultCatalogRulesEnforcer', () => {
expect(enforcer.isAllowed(entity.location, location.z)).toBe(false);
});
it('should only allow sources that are specified in sources', () => {
it('should only allow locations that match a given regex', () => {
const enforcer = DefaultCatalogRulesEnforcer.fromConfig(
new ConfigReader({
catalog: {
rules: [{ allow: ['Component'], sources: ['github.com/backstage'] }],
rules: [{ allow: ['Component'], locations: [{type: 'url', match: 'https://github.com/b/*'}] }],
},
}),
);
@@ -31,9 +31,7 @@ export type CatalogRule = {
locations?: Array<{
target?: string;
type: string;
}>;
sources?: Array<{
source: string;
match?: string;
}>;
};
@@ -58,7 +56,6 @@ export class DefaultCatalogRulesEnforcer implements CatalogRulesEnforcer {
static readonly defaultRules: CatalogRule[] = [
{
allow: ['Component', 'API', 'Location'].map(kind => ({ kind })),
sources: [],
},
];
@@ -96,10 +93,21 @@ export class DefaultCatalogRulesEnforcer implements CatalogRulesEnforcer {
const rules = new Array<CatalogRule>();
if (config.has('catalog.rules')) {
const globalRules = config.getConfigArray('catalog.rules').map(sub => ({
allow: sub.getStringArray('allow').map(kind => ({ kind })),
sources: (sub.getOptionalStringArray('sources') || []).map(source => ({ source })),
}));
const globalRules = config.getConfigArray('catalog.rules').map(ruleConfig => {
const rule: CatalogRule = {
allow: ruleConfig.getStringArray('allow').map(kind => ({ kind })),
};
const locConf = ruleConfig.getOptionalConfigArray('locations');
if (locConf)
rule.locations = locConf.map( locationConfig => ({
match: locationConfig.getOptionalString('match'),
type: locationConfig.getString('type'),
target: locationConfig.getOptionalString('target')
}))
return rule;
});
rules.push(...globalRules);
} else {
rules.push(...DefaultCatalogRulesEnforcer.defaultRules);
@@ -139,10 +147,6 @@ export class DefaultCatalogRulesEnforcer implements CatalogRulesEnforcer {
continue;
}
if (!this.matchSources(location, rule.sources)) {
return false;
}
if (this.matchEntity(entity, rule.allow)) {
return true;
}
@@ -153,7 +157,7 @@ export class DefaultCatalogRulesEnforcer implements CatalogRulesEnforcer {
private matchLocation(
location: LocationSpec,
matchers?: { target?: string; type: string }[],
matchers?: { target?: string; type: string, match?: string }[],
): boolean {
if (!matchers) {
return true;
@@ -166,6 +170,9 @@ export class DefaultCatalogRulesEnforcer implements CatalogRulesEnforcer {
if (matcher.target && matcher.target !== location?.target) {
continue;
}
if (matcher.match && !location?.target.match(matcher.match)) {
continue;
}
return true;
}
@@ -187,19 +194,6 @@ export class DefaultCatalogRulesEnforcer implements CatalogRulesEnforcer {
return false;
}
private matchSources(location: LocationSpec, matchers?: { source: string }[]): boolean {
if (!matchers || matchers.length === 0) {
return true;
}
const filteredRegex = new RegExp(`^http[s]?://${`(?:${matchers.map((filter, i) => i === 0 ? filter.source : `|${filter.source}`)})`}`);
if ( location.target && location.target.length > 0) {
return filteredRegex.test(location.target);
}
return false;
}
}
function resolveTarget(type: string, target: string): string {