packages/config-loader: move readSecret and secret reading switch to context

This commit is contained in:
Patrik Oldsberg
2020-06-12 18:13:56 +02:00
parent d223b28419
commit 6da3cc8fec
5 changed files with 48 additions and 19 deletions
+1
View File
@@ -17,3 +17,4 @@
export { resolveStaticConfig } from './resolver';
export { readConfigFile } from './reader';
export { readEnv } from './env';
export { readSecret } from './secrets';
+1 -2
View File
@@ -16,7 +16,6 @@
import yaml from 'yaml';
import { isObject } from './utils';
import { readSecret } from './secrets';
import { JsonValue, JsonObject } from '@backstage/config';
import { ReaderContext } from './types';
@@ -51,7 +50,7 @@ export async function readConfigFile(filePath: string, ctx: ReaderContext) {
}
try {
return await readSecret(obj.$secret, ctx);
return await ctx.readSecret(obj.$secret);
} catch (error) {
throw new Error(`Invalid secret at ${path}: ${error.message}`);
}
@@ -87,10 +87,6 @@ export async function readSecret(
data: JsonObject,
ctx: ReaderContext,
): Promise<string | undefined> {
if (!ctx.shouldReadSecrets) {
return undefined;
}
const secret = secretSchema.validateSync(data) as Secret;
if ('file' in secret) {
+5 -2
View File
@@ -14,10 +14,13 @@
* limitations under the License.
*/
import { JsonObject } from '@backstage/config';
export type ReadFileFunc = (path: string) => Promise<string>;
export type ReadSecretFunc = (desc: JsonObject) => Promise<string | undefined>;
export type ReaderContext = {
shouldReadSecrets: boolean;
readFile: ReadFileFunc;
env: { [name in string]?: string };
readFile: ReadFileFunc;
readSecret: ReadSecretFunc;
};
+41 -11
View File
@@ -16,8 +16,13 @@
import fs from 'fs-extra';
import { resolve as resolvePath, dirname } from 'path';
import { AppConfig } from '@backstage/config';
import { resolveStaticConfig, readConfigFile, readEnv } from './lib';
import { AppConfig, JsonObject } from '@backstage/config';
import {
resolveStaticConfig,
readConfigFile,
readEnv,
readSecret,
} from './lib';
export type LoadConfigOptions = {
// Config path, defaults to app-config.yaml in project root
@@ -27,6 +32,32 @@ export type LoadConfigOptions = {
shouldReadSecrets?: boolean;
};
class Context {
constructor(
private readonly options: {
env: { [name in string]?: string };
rootPath: string;
shouldReadSecrets: boolean;
},
) {}
get env() {
return this.options.env;
}
async readFile(path: string): Promise<string> {
return fs.readFile(resolvePath(this.options.rootPath, path), 'utf8');
}
async readSecret(desc: JsonObject): Promise<string | undefined> {
if (!this.options.shouldReadSecrets) {
return undefined;
}
return readSecret(desc, this);
}
}
export async function loadConfig(
options: LoadConfigOptions = {},
): Promise<AppConfig[]> {
@@ -38,15 +69,14 @@ export async function loadConfig(
try {
for (const configPath of configPaths) {
const rootPath = dirname(configPath);
const config = await readConfigFile(configPath, {
env: process.env,
shouldReadSecrets: Boolean(options.shouldReadSecrets),
readFile: (path: string) => {
return fs.readFile(resolvePath(rootPath, path), 'utf8');
},
});
const config = await readConfigFile(
configPath,
new Context({
env: process.env,
rootPath: dirname(configPath),
shouldReadSecrets: Boolean(options.shouldReadSecrets),
}),
);
configs.push(config);
}