config-loader: add substitution transform
This commit is contained in:
@@ -16,3 +16,4 @@
|
||||
|
||||
export { applyConfigTransforms } from './apply';
|
||||
export { createIncludeTransform } from './include';
|
||||
export { createSubstitutionTransform } from './substitution';
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright 2020 Spotify AB
|
||||
*
|
||||
* 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 { createSubstitutionTransform } from './substitution';
|
||||
|
||||
const env = jest.fn(async (name: string) => {
|
||||
return ({
|
||||
SECRET: 'my-secret',
|
||||
TOKEN: 'my-token',
|
||||
} as { [name: string]: string })[name];
|
||||
});
|
||||
|
||||
const substituteTransform = createSubstitutionTransform(env);
|
||||
|
||||
describe('substituteTransform', () => {
|
||||
it('should not transform unknown values', async () => {
|
||||
await expect(substituteTransform(false)).resolves.toEqual([
|
||||
false,
|
||||
expect.anything(),
|
||||
]);
|
||||
await expect(substituteTransform([1])).resolves.toEqual([
|
||||
false,
|
||||
expect.anything(),
|
||||
]);
|
||||
await expect(substituteTransform(1)).resolves.toEqual([
|
||||
false,
|
||||
expect.anything(),
|
||||
]);
|
||||
await expect(substituteTransform({ x: 'y' })).resolves.toEqual([
|
||||
false,
|
||||
expect.anything(),
|
||||
]);
|
||||
await expect(substituteTransform(null)).resolves.toEqual([false, null]);
|
||||
});
|
||||
|
||||
it('should substitute env var', async () => {
|
||||
await expect(substituteTransform('hello ${SECRET}')).resolves.toEqual([
|
||||
true,
|
||||
'hello my-secret',
|
||||
]);
|
||||
await expect(
|
||||
substituteTransform('${SECRET } $${} ${TOKEN }'),
|
||||
).resolves.toEqual([true, 'my-secret $${} my-token']);
|
||||
await expect(substituteTransform('foo ${MISSING}')).resolves.toEqual([
|
||||
true,
|
||||
undefined,
|
||||
]);
|
||||
await expect(
|
||||
substituteTransform('foo ${MISSING} ${SECRET}'),
|
||||
).resolves.toEqual([true, undefined]);
|
||||
await expect(
|
||||
substituteTransform('foo ${SECRET} ${SECRET}'),
|
||||
).resolves.toEqual([true, 'foo my-secret my-secret']);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright 2020 Spotify AB
|
||||
*
|
||||
* 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 { JsonValue } from '@backstage/config';
|
||||
import { TransformFunc, EnvFunc } from './types';
|
||||
|
||||
/**
|
||||
* A environment variable substitution transform that transforms e.g. 'token ${MY_TOKEN}'
|
||||
* to 'token abc' if MY_TOKEN is 'abc'. If any of the substituted variables are undefined,
|
||||
* the entire expression ends up undefined.
|
||||
*/
|
||||
export function createSubstitutionTransform(env: EnvFunc): TransformFunc {
|
||||
return async (input: JsonValue) => {
|
||||
if (typeof input !== 'string') {
|
||||
return [false, input];
|
||||
}
|
||||
|
||||
const parts: (string | undefined)[] = input.split(/(?<!\$)\$\{([^{}]+)\}/);
|
||||
for (let i = 1; i < parts.length; i += 2) {
|
||||
parts[i] = await env(parts[i]!.trim());
|
||||
}
|
||||
if (parts.some(part => part === undefined)) {
|
||||
return [true, undefined];
|
||||
}
|
||||
return [true, parts.join('')];
|
||||
};
|
||||
}
|
||||
@@ -19,6 +19,8 @@ import mockFs from 'mock-fs';
|
||||
|
||||
describe('loadConfig', () => {
|
||||
beforeAll(() => {
|
||||
process.env.MY_SECRET = 'is-secret';
|
||||
|
||||
mockFs({
|
||||
'/root/app-config.yaml': `
|
||||
app:
|
||||
@@ -29,8 +31,14 @@ describe('loadConfig', () => {
|
||||
'/root/app-config.development.yaml': `
|
||||
app:
|
||||
sessionKey: development-key
|
||||
backend:
|
||||
$include: ./included.yaml
|
||||
`,
|
||||
'/root/secrets/session-key.txt': 'abc123',
|
||||
'/root/included.yaml': `
|
||||
foo:
|
||||
bar: token \${MY_SECRET}
|
||||
`,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -104,6 +112,11 @@ describe('loadConfig', () => {
|
||||
app: {
|
||||
sessionKey: 'development-key',
|
||||
},
|
||||
backend: {
|
||||
foo: {
|
||||
bar: 'token is-secret',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
]);
|
||||
|
||||
@@ -22,6 +22,7 @@ import {
|
||||
applyConfigTransforms,
|
||||
readEnvConfig,
|
||||
createIncludeTransform,
|
||||
createSubstitutionTransform,
|
||||
} from './lib';
|
||||
|
||||
export type LoadConfigOptions = {
|
||||
@@ -78,6 +79,7 @@ export async function loadConfig(
|
||||
const input = yaml.parse(await readFile(configPath));
|
||||
const data = await applyConfigTransforms(input, [
|
||||
createIncludeTransform(env, readFile),
|
||||
createSubstitutionTransform(env),
|
||||
]);
|
||||
|
||||
configs.push({ data, context: basename(configPath) });
|
||||
|
||||
Reference in New Issue
Block a user