From 19f6c6c32acf38e48e12cd22310d381f25fca218 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Thu, 7 Apr 2022 18:05:54 +0200 Subject: [PATCH] config-loader,backend-common: do not fail backend startup on config schema errors Signed-off-by: Patrik Oldsberg --- .changeset/heavy-stingrays-talk.md | 5 ++++ .changeset/thirty-icons-buy.md | 5 ++++ packages/backend-common/src/config.ts | 2 +- packages/config-loader/api-report.md | 1 + .../config-loader/src/lib/schema/load.test.ts | 7 +++++ packages/config-loader/src/lib/schema/load.ts | 26 ++++++++++++------- .../config-loader/src/lib/schema/types.ts | 5 ++++ 7 files changed, 41 insertions(+), 10 deletions(-) create mode 100644 .changeset/heavy-stingrays-talk.md create mode 100644 .changeset/thirty-icons-buy.md diff --git a/.changeset/heavy-stingrays-talk.md b/.changeset/heavy-stingrays-talk.md new file mode 100644 index 0000000000..d17a85090b --- /dev/null +++ b/.changeset/heavy-stingrays-talk.md @@ -0,0 +1,5 @@ +--- +'@backstage/backend-common': patch +--- + +The backend will no longer fail to start up when configured secrets do not match the configuration schema. diff --git a/.changeset/thirty-icons-buy.md b/.changeset/thirty-icons-buy.md new file mode 100644 index 0000000000..dd5a9eec19 --- /dev/null +++ b/.changeset/thirty-icons-buy.md @@ -0,0 +1,5 @@ +--- +'@backstage/config-loader': minor +--- + +Added `ignoreSchemaErrors` to `schema.process`. diff --git a/packages/backend-common/src/config.ts b/packages/backend-common/src/config.ts index 9081b99284..94b362d99d 100644 --- a/packages/backend-common/src/config.ts +++ b/packages/backend-common/src/config.ts @@ -41,7 +41,7 @@ const updateRedactionList = ( ) => { const secretAppConfigs = schema.process(configs, { visibility: ['secret'], - withDeprecatedKeys: true, + ignoreSchemaErrors: true, }); const secretConfig = ConfigReader.fromConfigs(secretAppConfigs); const values = new Set(); diff --git a/packages/config-loader/api-report.md b/packages/config-loader/api-report.md index 06868e0f47..225b3ddd22 100644 --- a/packages/config-loader/api-report.md +++ b/packages/config-loader/api-report.md @@ -19,6 +19,7 @@ export type ConfigSchema = { // @public export type ConfigSchemaProcessingOptions = { visibility?: ConfigVisibility[]; + ignoreSchemaErrors?: boolean; valueTransform?: TransformFunc; withFilteredKeys?: boolean; withDeprecatedKeys?: boolean; diff --git a/packages/config-loader/src/lib/schema/load.test.ts b/packages/config-loader/src/lib/schema/load.test.ts index 41971f7d01..475339be4e 100644 --- a/packages/config-loader/src/lib/schema/load.test.ts +++ b/packages/config-loader/src/lib/schema/load.test.ts @@ -275,5 +275,12 @@ describe('loadConfigSchema', () => { ).toThrow( "Config must have required property 'x a' { missingProperty=x a } at /other", ); + + expect( + schema.process([{ data: { other: {} }, context: 'test' }], { + visibility: ['frontend'], + ignoreSchemaErrors: true, + }), + ).toEqual([{ data: {}, context: 'test' }]); }); }); diff --git a/packages/config-loader/src/lib/schema/load.ts b/packages/config-loader/src/lib/schema/load.ts index 959b438371..0309f64ea0 100644 --- a/packages/config-loader/src/lib/schema/load.ts +++ b/packages/config-loader/src/lib/schema/load.ts @@ -82,18 +82,26 @@ export async function loadConfigSchema( return { process( configs: AppConfig[], - { visibility, valueTransform, withFilteredKeys, withDeprecatedKeys } = {}, + { + visibility, + valueTransform, + withFilteredKeys, + withDeprecatedKeys, + ignoreSchemaErrors, + } = {}, ): AppConfig[] { const result = validate(configs); - const visibleErrors = filterErrorsByVisibility( - result.errors, - visibility, - result.visibilityByDataPath, - result.visibilityBySchemaPath, - ); - if (visibleErrors.length > 0) { - throw errorsToError(visibleErrors); + if (!ignoreSchemaErrors) { + const visibleErrors = filterErrorsByVisibility( + result.errors, + visibility, + result.visibilityByDataPath, + result.visibilityBySchemaPath, + ); + if (visibleErrors.length > 0) { + throw errorsToError(visibleErrors); + } } let processedConfigs = configs; diff --git a/packages/config-loader/src/lib/schema/types.ts b/packages/config-loader/src/lib/schema/types.ts index cc62192e30..95576cf5aa 100644 --- a/packages/config-loader/src/lib/schema/types.ts +++ b/packages/config-loader/src/lib/schema/types.ts @@ -117,6 +117,11 @@ export type ConfigSchemaProcessingOptions = { */ visibility?: ConfigVisibility[]; + /** + * When set to `true`, any schema errors in the provided configuration will be ignored. + */ + ignoreSchemaErrors?: boolean; + /** * A transform function that can be used to transform primitive configuration values * during validation. The value returned from the transform function will be used