diff --git a/app-config.yaml b/app-config.yaml index fd9263a5e8..d68391717e 100644 --- a/app-config.yaml +++ b/app-config.yaml @@ -218,8 +218,7 @@ catalog: - System - Domain - Location - sources: - - github.com/backstage + processors: ldapOrg: diff --git a/plugins/catalog-backend/src/ingestion/CatalogRules.test.ts b/plugins/catalog-backend/src/ingestion/CatalogRules.test.ts index 43c3cd8f2f..c9e0f7c619 100644 --- a/plugins/catalog-backend/src/ingestion/CatalogRules.test.ts +++ b/plugins/catalog-backend/src/ingestion/CatalogRules.test.ts @@ -37,7 +37,7 @@ const entity = { const location: Record = { 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/*'}] }], }, }), ); diff --git a/plugins/catalog-backend/src/ingestion/CatalogRules.ts b/plugins/catalog-backend/src/ingestion/CatalogRules.ts index 398133036a..43d865c4d2 100644 --- a/plugins/catalog-backend/src/ingestion/CatalogRules.ts +++ b/plugins/catalog-backend/src/ingestion/CatalogRules.ts @@ -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(); 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 {