Skip to content

Commit

Permalink
app-backend: validate env config against schema
Browse files Browse the repository at this point in the history
Signed-off-by: Vincenzo Scamporlino <[email protected]>
  • Loading branch information
vinzscam committed Oct 3, 2024
1 parent 6000c69 commit 2c4ee26
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/friendly-coins-approve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@backstage/plugin-app-backend': patch
---

Fixed unexpected behaviour where configuration supplied with `APP_CONFIG_*` environment variables where not filtered by the configuration schema.
99 changes: 99 additions & 0 deletions plugins/app-backend/src/lib/config/readFrontendConfig.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright 2024 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { createMockDirectory } from '@backstage/backend-test-utils';
import { readFrontendConfig } from './readFrontendConfig';
import { ConfigReader } from '@backstage/config';

describe('readFrontendConfig', () => {
const mockDir = createMockDirectory();

afterEach(() => {
mockDir.clear();
});

it('should validate env config', async () => {
mockDir.setContent({
'appDir/.config-schema.json': JSON.stringify({
schemas: [
{
value: {
type: 'object',

properties: {
app: {
type: 'object',
properties: {
secretOfLife: {
type: 'string',
visibility: 'secret',
},
backendConfig: {
type: 'string',
visibility: 'backend',
},
publicValue: {
type: 'string',
visibility: 'frontend',
},
},
},
},
},
},
],
backstageConfigSchemaVersion: 1,
}),
});

const config = new ConfigReader({
app: {
secretOfLife: '42',
backendConfig: 'backend',
publicValue: 'public',
},
});

const frontendConfig = await readFrontendConfig({
env: {
APP_CONFIG_app_secretOfLife: 'ignored',
APP_CONFIG_app_backendConfig: 'ignored',
APP_CONFIG_app_publicValue: 'injected',
},
appDistDir: `${mockDir.path}/appDir`,
config,
});

expect(frontendConfig).toEqual([
{
context: 'env',
data: {
app: {
publicValue: 'injected',
},
},
deprecatedKeys: [],
filteredKeys: undefined,
},
{
context: 'app',
data: { app: { publicValue: 'public' } },
deprecatedKeys: [],
filteredKeys: undefined,
},
]);
});
});
10 changes: 4 additions & 6 deletions plugins/app-backend/src/lib/config/readFrontendConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ export async function readFrontendConfig(options: {
}): Promise<AppConfig[]> {
const { env, appDistDir, config } = options;

const appConfigs = readEnvConfig(env);

const schemaPath = resolvePath(appDistDir, '.config-schema.json');
if (await fs.pathExists(schemaPath)) {
const envConfigs = readEnvConfig(env);
const serializedSchema = await fs.readJson(schemaPath);

try {
Expand All @@ -49,11 +48,10 @@ export async function readFrontendConfig(options: {
serialized: serializedSchema,
}));

const frontendConfigs = await schema.process(
[{ data: config.get() as JsonObject, context: 'app' }],
return await schema.process(
[...envConfigs, { data: config.get() as JsonObject, context: 'app' }],
{ visibility: ['frontend'], withDeprecatedKeys: true },
);
appConfigs.push(...frontendConfigs);
} catch (error) {
throw new Error(
'Invalid app bundle schema. If this error is unexpected you need to run `yarn build` in the app. ' +
Expand All @@ -63,5 +61,5 @@ export async function readFrontendConfig(options: {
}
}

return appConfigs;
return [];
}

0 comments on commit 2c4ee26

Please sign in to comment.