@itgorillaz/configify 3.0.0-alpha.0 → 3.0.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +4 -1
- package/.prettierrc +0 -4
- package/index.d.ts +0 -1
- package/index.js +0 -6
- package/index.ts +0 -1
- package/nest-cli.json +0 -8
- package/src/configify.module.ts +0 -223
- package/src/configuration/configuration-options.interface.ts +0 -43
- package/src/configuration/configuration-parser.interface.ts +0 -15
- package/src/configuration/configuration-providers.interface.ts +0 -9
- package/src/configuration/configuration.registry.ts +0 -70
- package/src/configuration/index.ts +0 -6
- package/src/configuration/parsers/configuration-parser.factory.ts +0 -53
- package/src/configuration/parsers/dotenv-configuration.parser.ts +0 -18
- package/src/configuration/parsers/index.ts +0 -4
- package/src/configuration/parsers/json-configuration.parser.ts +0 -17
- package/src/configuration/parsers/yaml-configuration.parser.ts +0 -18
- package/src/configuration/resolvers/aws/aws-secrets-resolver.factory.ts +0 -32
- package/src/configuration/resolvers/aws/index.ts +0 -3
- package/src/configuration/resolvers/aws/parameter-store-configuration.resolver.ts +0 -115
- package/src/configuration/resolvers/aws/secrets-manager-configuration.resolver.ts +0 -117
- package/src/configuration/resolvers/configuration-resolver.interface.ts +0 -11
- package/src/configuration/resolvers/index.ts +0 -2
- package/src/configuration/resolvers/resolved-value.interface.ts +0 -10
- package/src/decorators/configuration.decorator.ts +0 -26
- package/src/decorators/index.ts +0 -2
- package/src/decorators/value.decorator.ts +0 -57
- package/src/index.ts +0 -3
- package/src/interpolation/variables.ts +0 -107
- package/tsconfig.build.json +0 -11
- package/tsconfig.build.tsbuildinfo +0 -1
- package/tsconfig.json +0 -23
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@itgorillaz/configify",
|
|
3
|
-
"version": "3.0.0-alpha.
|
|
3
|
+
"version": "3.0.0-alpha.1",
|
|
4
4
|
"description": "NestJS Config on Steroids",
|
|
5
5
|
"author": "tommelo",
|
|
6
6
|
"private": false,
|
|
@@ -25,6 +25,9 @@
|
|
|
25
25
|
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
|
|
26
26
|
"test": "jest --config ./test/jest.config.json --coverage --silent=false --verbose --runInBand"
|
|
27
27
|
},
|
|
28
|
+
"files": [
|
|
29
|
+
"dist/**/*"
|
|
30
|
+
],
|
|
28
31
|
"dependencies": {
|
|
29
32
|
"dotenv": "^16.3.1",
|
|
30
33
|
"js-yaml": "^4.1.0"
|
package/.prettierrc
DELETED
package/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './dist';
|
package/index.js
DELETED
package/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './dist';
|
package/nest-cli.json
DELETED
package/src/configify.module.ts
DELETED
|
@@ -1,223 +0,0 @@
|
|
|
1
|
-
import { DynamicModule, Module, Provider } from '@nestjs/common';
|
|
2
|
-
import { validateSync } from 'class-validator';
|
|
3
|
-
import * as fs from 'fs';
|
|
4
|
-
import { resolve } from 'path';
|
|
5
|
-
import {
|
|
6
|
-
ConfigifyModuleOptions,
|
|
7
|
-
ConfigurationParserFactory,
|
|
8
|
-
ConfigurationProviders,
|
|
9
|
-
ConfigurationRegistry,
|
|
10
|
-
DefaultConfigifyModuleOptions,
|
|
11
|
-
} from './configuration';
|
|
12
|
-
import { Variables } from './interpolation/variables';
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* The configify module.
|
|
16
|
-
* A NestJS configuration module on steroids.
|
|
17
|
-
*
|
|
18
|
-
* This module provides out of the box configuration files parsing,
|
|
19
|
-
* remote secrets fetching, variables expansion and configuration validation.
|
|
20
|
-
*
|
|
21
|
-
* The lifecycle of the module consists of:
|
|
22
|
-
* - Reading configuration files
|
|
23
|
-
* - Resolving remote secrets
|
|
24
|
-
* - Expanding variables
|
|
25
|
-
* - Creating object instances decorated with Configuration and assigning its values
|
|
26
|
-
* - Validating the configuration instance
|
|
27
|
-
*
|
|
28
|
-
* All the configuration set to the configuration files will be assigned to the process.env object
|
|
29
|
-
*/
|
|
30
|
-
@Module({})
|
|
31
|
-
export class ConfigifyModule {
|
|
32
|
-
/**
|
|
33
|
-
* The default configuration files.
|
|
34
|
-
* If no configuration files are provided this module will
|
|
35
|
-
* lookup at a .env, application.yml and an application.json files
|
|
36
|
-
* at the root path of the project.
|
|
37
|
-
*/
|
|
38
|
-
private static readonly DEFAULT_CONFIG_FILES = [
|
|
39
|
-
resolve(process.cwd(), '.env'),
|
|
40
|
-
resolve(process.cwd(), 'application.yml'),
|
|
41
|
-
resolve(process.cwd(), 'application.json'),
|
|
42
|
-
];
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Creates the configfy dynamic module.
|
|
46
|
-
*
|
|
47
|
-
* The module will manage the instance of all classes decorated the Configuration decorator,
|
|
48
|
-
* meaning, the module will create the instance and associate the value to attributes according to the
|
|
49
|
-
* keys provided by the Value decorator.
|
|
50
|
-
*
|
|
51
|
-
* The configuration key pair values will also be available on process.env object.
|
|
52
|
-
*
|
|
53
|
-
* @param { ConfigifyModuleOptions } options The module config options
|
|
54
|
-
* @returns { DynamicModule } module The configy module
|
|
55
|
-
*/
|
|
56
|
-
static async forRootAsync(
|
|
57
|
-
options: ConfigifyModuleOptions = {},
|
|
58
|
-
): Promise<DynamicModule> {
|
|
59
|
-
const settings = { ...options, ...DefaultConfigifyModuleOptions };
|
|
60
|
-
const files = this.resolveConfigurationFiles(settings.configFilePath);
|
|
61
|
-
|
|
62
|
-
const envVars = settings.ignoreEnvVars ? {} : process.env;
|
|
63
|
-
const fromFile = settings.ignoreConfigFile
|
|
64
|
-
? {}
|
|
65
|
-
: this.parseConfigurationFiles(files);
|
|
66
|
-
|
|
67
|
-
const container = { ...envVars, ...fromFile };
|
|
68
|
-
const secrets = await this.runSecretsResolverPipeline(container, settings);
|
|
69
|
-
const configuration = { ...container, ...secrets };
|
|
70
|
-
|
|
71
|
-
if (settings.expandConfig) {
|
|
72
|
-
const expanded = Variables.expand(configuration);
|
|
73
|
-
Object.assign(configuration, expanded);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
Object.assign(process.env, configuration);
|
|
77
|
-
|
|
78
|
-
const { exports, providers } = this.buildConfigurationProviders();
|
|
79
|
-
|
|
80
|
-
return {
|
|
81
|
-
exports,
|
|
82
|
-
providers,
|
|
83
|
-
global: true,
|
|
84
|
-
module: ConfigifyModule,
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Runs the secrets resolver pipeline.
|
|
90
|
-
*
|
|
91
|
-
* @param {Record<string, any>} config the configuration object
|
|
92
|
-
* @param {ConfigifyModuleOptions} options the module options
|
|
93
|
-
* @returns {Promise<Record<string, any>>} the resolved secrets
|
|
94
|
-
*/
|
|
95
|
-
private static async runSecretsResolverPipeline(
|
|
96
|
-
config: Record<string, any>,
|
|
97
|
-
options: ConfigifyModuleOptions,
|
|
98
|
-
): Promise<Record<string, any>> {
|
|
99
|
-
const secrets = {};
|
|
100
|
-
if (options.secretsResolverStrategies?.length) {
|
|
101
|
-
for (const resolver of options.secretsResolverStrategies) {
|
|
102
|
-
const result = await resolver.resolve(config);
|
|
103
|
-
Object.assign(secrets, result);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
return secrets;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* Creates the configuration module providers.
|
|
111
|
-
* It creates the configuration instances, assign its value
|
|
112
|
-
* and perform the object validation.
|
|
113
|
-
*
|
|
114
|
-
* @returns {ConfigurationProviders} the module configuration providers
|
|
115
|
-
*/
|
|
116
|
-
private static buildConfigurationProviders(): ConfigurationProviders {
|
|
117
|
-
const exports: unknown[] = [];
|
|
118
|
-
const providers: Provider[] = [];
|
|
119
|
-
|
|
120
|
-
const registry = ConfigurationRegistry.getRegistry();
|
|
121
|
-
for (const ConfigType of registry) {
|
|
122
|
-
const instance = new ConfigType();
|
|
123
|
-
|
|
124
|
-
const attributes =
|
|
125
|
-
ConfigurationRegistry.getValueDecoratedAttributes(instance);
|
|
126
|
-
|
|
127
|
-
for (const attribute of attributes) {
|
|
128
|
-
const metadata = ConfigurationRegistry.getValueDecoratedKey(
|
|
129
|
-
instance,
|
|
130
|
-
attribute,
|
|
131
|
-
);
|
|
132
|
-
|
|
133
|
-
const parse = metadata.options?.parse;
|
|
134
|
-
|
|
135
|
-
const defaultValue =
|
|
136
|
-
process.env[metadata.key] || metadata.options?.default;
|
|
137
|
-
|
|
138
|
-
const value = parse ? parse(defaultValue) : defaultValue;
|
|
139
|
-
|
|
140
|
-
instance[attribute] = value;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
const errors = validateSync(instance);
|
|
144
|
-
if (errors && errors.length) {
|
|
145
|
-
throw new Error(
|
|
146
|
-
`validation constraints violated:\n${errors
|
|
147
|
-
.map((e) =>
|
|
148
|
-
JSON.stringify(
|
|
149
|
-
{ attribute: e.property, constraints: e.constraints },
|
|
150
|
-
null,
|
|
151
|
-
2,
|
|
152
|
-
),
|
|
153
|
-
)
|
|
154
|
-
.join('\n')}`,
|
|
155
|
-
);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
exports.push(ConfigType);
|
|
159
|
-
providers.push({ provide: ConfigType, useValue: instance });
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
return { exports, providers };
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* Flattens a nested object into an one level key value pair object
|
|
167
|
-
*
|
|
168
|
-
* @param {object} source the source object
|
|
169
|
-
* @param {string[]} path the key path
|
|
170
|
-
* @param {Record<string, any>} target the target object
|
|
171
|
-
*/
|
|
172
|
-
private static flattenObjectKeys(
|
|
173
|
-
source: any,
|
|
174
|
-
path: string[] = [],
|
|
175
|
-
target: Record<string, any> = {},
|
|
176
|
-
) {
|
|
177
|
-
if (typeof source === 'object') {
|
|
178
|
-
for (const key in source) {
|
|
179
|
-
this.flattenObjectKeys(source[key], [...path, key], target);
|
|
180
|
-
}
|
|
181
|
-
} else {
|
|
182
|
-
target[path.join('.')] = source;
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
/**
|
|
187
|
-
* Parses configuration files and assign its contents to a configuration object.
|
|
188
|
-
*
|
|
189
|
-
* @param {string[]} files the configuration file paths
|
|
190
|
-
* @returns {object} the object representation of the configuration files
|
|
191
|
-
*/
|
|
192
|
-
private static parseConfigurationFiles(files: string[]): Record<string, any> {
|
|
193
|
-
const kv = {};
|
|
194
|
-
const config = {};
|
|
195
|
-
|
|
196
|
-
for (const file of files) {
|
|
197
|
-
const parser = ConfigurationParserFactory.getParser(file);
|
|
198
|
-
const parsed = parser.parse(file);
|
|
199
|
-
Object.assign(config, parsed);
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
this.flattenObjectKeys(config, [], kv);
|
|
203
|
-
|
|
204
|
-
return kv;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
/**
|
|
208
|
-
* Resolve the path of the configuration files.
|
|
209
|
-
* It ignores files that does not exist or is not
|
|
210
|
-
* suppported by the configuration parsers.
|
|
211
|
-
*
|
|
212
|
-
* @param {string | string[]} path the configuration path
|
|
213
|
-
* @returns {string[]} list of configuration files
|
|
214
|
-
*/
|
|
215
|
-
private static resolveConfigurationFiles(path?: string | string[]): string[] {
|
|
216
|
-
return ([] as string[])
|
|
217
|
-
.concat(path || [], this.DEFAULT_CONFIG_FILES)
|
|
218
|
-
.filter(
|
|
219
|
-
(file) =>
|
|
220
|
-
fs.existsSync(file) && ConfigurationParserFactory.supports(file),
|
|
221
|
-
);
|
|
222
|
-
}
|
|
223
|
-
}
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { ConfigurationResolver } from './resolvers';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* The configuration options interface
|
|
5
|
-
*/
|
|
6
|
-
export interface ConfigifyModuleOptions {
|
|
7
|
-
/**
|
|
8
|
-
* Ignores any config file.
|
|
9
|
-
* The default value is false;
|
|
10
|
-
*/
|
|
11
|
-
ignoreConfigFile?: boolean;
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Ignores environment variables
|
|
15
|
-
* The default value is false;
|
|
16
|
-
*/
|
|
17
|
-
ignoreEnvVars?: boolean;
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* The path of the configuration files
|
|
21
|
-
*/
|
|
22
|
-
configFilePath?: string | string[];
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Expands variables
|
|
26
|
-
* The default value is true
|
|
27
|
-
*/
|
|
28
|
-
expandConfig?: boolean;
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* The secrets resolvers strategies
|
|
32
|
-
*/
|
|
33
|
-
secretsResolverStrategies?: ConfigurationResolver[];
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* The default module options
|
|
38
|
-
*/
|
|
39
|
-
export const DefaultConfigifyModuleOptions: ConfigifyModuleOptions = {
|
|
40
|
-
ignoreConfigFile: false,
|
|
41
|
-
ignoreEnvVars: false,
|
|
42
|
-
expandConfig: true,
|
|
43
|
-
};
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The configuration parser interface.
|
|
3
|
-
* This interface is implemented by all
|
|
4
|
-
* the supported configuration parsers.
|
|
5
|
-
*/
|
|
6
|
-
export interface ConfigurationParser {
|
|
7
|
-
/**
|
|
8
|
-
* Reads a file and assign its values
|
|
9
|
-
* to an object representation;
|
|
10
|
-
*
|
|
11
|
-
* @param {string} file the configuration file path
|
|
12
|
-
* @returns {Record<string, any>} the object representation of the configuration file
|
|
13
|
-
*/
|
|
14
|
-
parse(file: string): Record<string, any>;
|
|
15
|
-
}
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ValueDecoratedKey,
|
|
3
|
-
VALUE_METADATA,
|
|
4
|
-
VALUE_PROPERTIES_METADATA,
|
|
5
|
-
} from '../decorators';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* The configuration registry.
|
|
9
|
-
*
|
|
10
|
-
* The registry keeps track of all classes decorated
|
|
11
|
-
* with Configuration decorator.
|
|
12
|
-
*/
|
|
13
|
-
export class ConfigurationRegistry {
|
|
14
|
-
private static readonly registry: Array<unknown> = [];
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Registers a type.
|
|
18
|
-
*
|
|
19
|
-
* @param {any} type the class type
|
|
20
|
-
*/
|
|
21
|
-
static registerTarget(type: any): void {
|
|
22
|
-
this.registry.push(type);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Registers a class attribute.
|
|
27
|
-
*
|
|
28
|
-
* @param {any} target the class target
|
|
29
|
-
* @param {string} attribute the attribute name
|
|
30
|
-
*/
|
|
31
|
-
static registerAttribute(target: any, attribute: string | symbol): void {
|
|
32
|
-
(
|
|
33
|
-
target[VALUE_PROPERTIES_METADATA] ||
|
|
34
|
-
(target[VALUE_PROPERTIES_METADATA] = [])
|
|
35
|
-
).push(attribute);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Returns the configuration registry.
|
|
40
|
-
*
|
|
41
|
-
* @returns {any[]} the configuration registry
|
|
42
|
-
*/
|
|
43
|
-
static getRegistry(): any[] {
|
|
44
|
-
return this.registry;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Returns the class attribute names decorated with Decorated decorator
|
|
49
|
-
*
|
|
50
|
-
* @param {any} target the instance target;
|
|
51
|
-
* @returns {string[]} the list of attribute names
|
|
52
|
-
*/
|
|
53
|
-
static getValueDecoratedAttributes(target: any): string[] {
|
|
54
|
-
return target[VALUE_PROPERTIES_METADATA];
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Returns the value of the decorated key.
|
|
59
|
-
*
|
|
60
|
-
* @param {any} instance the target instance
|
|
61
|
-
* @param {string} attribute the attribute name
|
|
62
|
-
* @returns {ValueDecoratedKey} the value decorated key
|
|
63
|
-
*/
|
|
64
|
-
static getValueDecoratedKey(
|
|
65
|
-
instance: any,
|
|
66
|
-
attribute: string,
|
|
67
|
-
): ValueDecoratedKey {
|
|
68
|
-
return Reflect.getMetadata(VALUE_METADATA, instance, attribute);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import { ConfigurationParser } from '../configuration-parser.interface';
|
|
2
|
-
import { DotEnvConfigurationParser } from './dotenv-configuration.parser';
|
|
3
|
-
import { JsonConfigurationParser } from './json-configuration.parser';
|
|
4
|
-
import { YamlConfigurationParser } from './yaml-configuration.parser';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* The configuration parser factory.
|
|
8
|
-
* This factory class contains all the supported
|
|
9
|
-
* file configuration parsers.
|
|
10
|
-
*/
|
|
11
|
-
export class ConfigurationParserFactory {
|
|
12
|
-
/**
|
|
13
|
-
* The supported file configuration parsers
|
|
14
|
-
*/
|
|
15
|
-
private static readonly parsers: Record<string, ConfigurationParser> = {
|
|
16
|
-
env: new DotEnvConfigurationParser(),
|
|
17
|
-
yml: new YamlConfigurationParser(),
|
|
18
|
-
yaml: new YamlConfigurationParser(),
|
|
19
|
-
json: new JsonConfigurationParser(),
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Gets the file configuration parser based
|
|
24
|
-
* on the file extension.
|
|
25
|
-
* @param {string} file the configuration file name
|
|
26
|
-
* @returns {ConfigurationParser} the configuration parser
|
|
27
|
-
*/
|
|
28
|
-
static getParser(file: string): ConfigurationParser {
|
|
29
|
-
const ext = this.getFileExt(file) as keyof typeof this.parsers;
|
|
30
|
-
return this.parsers[ext];
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Checks if the given file has a parser registered.
|
|
35
|
-
*
|
|
36
|
-
* @param {string} file the configuration file name
|
|
37
|
-
* @returns {boolean} true if a parser is found, false otherwise
|
|
38
|
-
*/
|
|
39
|
-
static supports(file: string): boolean {
|
|
40
|
-
const ext = this.getFileExt(file);
|
|
41
|
-
return ext ? this.parsers.hasOwnProperty(ext) : false;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Returns the file extension.
|
|
46
|
-
*
|
|
47
|
-
* @param {string} file the file name
|
|
48
|
-
* @returns {string} the file extension
|
|
49
|
-
*/
|
|
50
|
-
private static getFileExt(file: string): string | undefined {
|
|
51
|
-
return file.split('.').pop();
|
|
52
|
-
}
|
|
53
|
-
}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import * as dotenv from 'dotenv';
|
|
2
|
-
import * as fs from 'fs';
|
|
3
|
-
import { ConfigurationParser } from '../configuration-parser.interface';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Dotenv configuration parser.
|
|
7
|
-
*/
|
|
8
|
-
export class DotEnvConfigurationParser implements ConfigurationParser {
|
|
9
|
-
/**
|
|
10
|
-
* Reads the configuration file and assign
|
|
11
|
-
* its contents to an object.
|
|
12
|
-
* @param {string} file the configuration file
|
|
13
|
-
* @returns {Record<string, any>} an object representation of the configuration file
|
|
14
|
-
*/
|
|
15
|
-
public parse(file: string): Record<string, any> {
|
|
16
|
-
return dotenv.parse(fs.readFileSync(file));
|
|
17
|
-
}
|
|
18
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import * as fs from 'fs';
|
|
2
|
-
import { ConfigurationParser } from '../configuration-parser.interface';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* JSON configuration parser
|
|
6
|
-
*/
|
|
7
|
-
export class JsonConfigurationParser implements ConfigurationParser {
|
|
8
|
-
/**
|
|
9
|
-
* Reads the configuration file and assign
|
|
10
|
-
* its contents to an object.
|
|
11
|
-
* @param {string} file the configuration file
|
|
12
|
-
* @returns {Record<string, any>} an object representation of the configuration file
|
|
13
|
-
*/
|
|
14
|
-
public parse(file: string): Record<string, any> {
|
|
15
|
-
return JSON.parse(fs.readFileSync(file, 'utf-8'));
|
|
16
|
-
}
|
|
17
|
-
}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import * as fs from 'fs';
|
|
2
|
-
import * as yaml from 'js-yaml';
|
|
3
|
-
import { ConfigurationParser } from '../configuration-parser.interface';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* YAML configuration parser
|
|
7
|
-
*/
|
|
8
|
-
export class YamlConfigurationParser implements ConfigurationParser {
|
|
9
|
-
/**
|
|
10
|
-
* Reads the configuration file and assign
|
|
11
|
-
* its contents to an object.
|
|
12
|
-
* @param {string} file the configuration file
|
|
13
|
-
* @returns {Record<string, any>} an object representation of the configuration file
|
|
14
|
-
*/
|
|
15
|
-
public parse(file: string): Record<string, any> {
|
|
16
|
-
return yaml.load(fs.readFileSync(file, 'utf-8')) as Record<string, any>;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { SecretsManagerClient } from '@aws-sdk/client-secrets-manager';
|
|
2
|
-
import { SSMClient } from '@aws-sdk/client-ssm';
|
|
3
|
-
import { ConfigurationResolver } from '../configuration-resolver.interface';
|
|
4
|
-
import { AwsParameterStoreConfigurationResolver } from './parameter-store-configuration.resolver';
|
|
5
|
-
import { AwsSecretsManagerConfigurationResolver } from './secrets-manager-configuration.resolver';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* The AWS secrets resolver factory.
|
|
9
|
-
* This class provides the default secrets resolvers for the module.
|
|
10
|
-
* The default secrets resolvers are:
|
|
11
|
-
* - Parameter Store
|
|
12
|
-
* - Secrets Manager
|
|
13
|
-
*/
|
|
14
|
-
export class AwsSecretsResolverFactory {
|
|
15
|
-
/**
|
|
16
|
-
* The default parameter store secrets resolver
|
|
17
|
-
* @returns {ConfigurationResolver} the default parameter store secrets resolver
|
|
18
|
-
*/
|
|
19
|
-
static defaultParameterStoreResolver(): ConfigurationResolver {
|
|
20
|
-
return new AwsParameterStoreConfigurationResolver(new SSMClient());
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* The default secrets manager secrets resolver
|
|
25
|
-
* @returns {ConfigurationResolver} the default secrets manager secrets resolver
|
|
26
|
-
*/
|
|
27
|
-
static defaultSecretsManagerResolver(): ConfigurationResolver {
|
|
28
|
-
return new AwsSecretsManagerConfigurationResolver(
|
|
29
|
-
new SecretsManagerClient(),
|
|
30
|
-
);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
import { GetParameterCommand, SSMClient } from '@aws-sdk/client-ssm';
|
|
2
|
-
import { ConfigurationResolver } from '../configuration-resolver.interface';
|
|
3
|
-
import { ResolvedValue } from '../resolved-value.interface';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* AWS Parameter Store configuration resolver.
|
|
7
|
-
*/
|
|
8
|
-
export class AwsParameterStoreConfigurationResolver
|
|
9
|
-
implements ConfigurationResolver
|
|
10
|
-
{
|
|
11
|
-
private readonly AWS_PARAMETER_STORE_YAML_KEY = 'aws-parameter-store';
|
|
12
|
-
private readonly AWS_PARAMETER_STORE_ENV_PREFIX = 'AWS_PARAMETER_STORE';
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Creates a new instance of parameter store configuration resolver
|
|
16
|
-
*
|
|
17
|
-
* @param {SSMClient} ssm systems manager client
|
|
18
|
-
*/
|
|
19
|
-
constructor(private readonly ssm: SSMClient) {}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Fetches parameters and assign it to an object representation
|
|
23
|
-
* of the configuration.
|
|
24
|
-
*
|
|
25
|
-
* @param {Record<string, any>} config the configuration object
|
|
26
|
-
* @returns {Record<string, any>} the cofiguration with secret values assigned
|
|
27
|
-
* @throws {Error} if unable to fetch the secret
|
|
28
|
-
*/
|
|
29
|
-
async resolve(config: Record<string, any>): Promise<Record<string, any>> {
|
|
30
|
-
const parameters = this.filterConfiguration(config);
|
|
31
|
-
const promises = this.buildBulkRequest(parameters);
|
|
32
|
-
|
|
33
|
-
const results = await Promise.all(promises);
|
|
34
|
-
|
|
35
|
-
const errors = results.filter((r) => !r.success);
|
|
36
|
-
if (errors && errors.length) {
|
|
37
|
-
throw new Error(
|
|
38
|
-
`Unable to resolve parameter:\n${errors
|
|
39
|
-
.map((e) => `${e.key}: ${e.id} - ${e.error?.message}`)
|
|
40
|
-
.join('\n')}`,
|
|
41
|
-
);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
for (const result of results) {
|
|
45
|
-
config[result.key] = result.value;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
return config;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Filters the configuration object by aws parameters store keys.
|
|
53
|
-
*
|
|
54
|
-
* @param {Record<string, any>} config the configuration object
|
|
55
|
-
* @returns {Record<string, any>} the filtered object
|
|
56
|
-
*/
|
|
57
|
-
private filterConfiguration(
|
|
58
|
-
config: Record<string, any>,
|
|
59
|
-
): Record<string, any> {
|
|
60
|
-
return Object.fromEntries(
|
|
61
|
-
Object.entries(config).filter(
|
|
62
|
-
([key]) =>
|
|
63
|
-
key.startsWith(this.AWS_PARAMETER_STORE_YAML_KEY) ||
|
|
64
|
-
key.startsWith(this.AWS_PARAMETER_STORE_ENV_PREFIX),
|
|
65
|
-
),
|
|
66
|
-
);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Resolves parameter values.
|
|
71
|
-
*
|
|
72
|
-
* @param {string} key the object key
|
|
73
|
-
* @param {string} id the secret id
|
|
74
|
-
* @returns
|
|
75
|
-
*/
|
|
76
|
-
private async resolveSecretValue(
|
|
77
|
-
key: string,
|
|
78
|
-
id: string,
|
|
79
|
-
): Promise<ResolvedValue> {
|
|
80
|
-
try {
|
|
81
|
-
const payload = { Name: id, WithDecryption: true };
|
|
82
|
-
const command = new GetParameterCommand(payload);
|
|
83
|
-
const response = await this.ssm.send(command);
|
|
84
|
-
return {
|
|
85
|
-
id,
|
|
86
|
-
key,
|
|
87
|
-
value: response.Parameter?.Value,
|
|
88
|
-
success: true,
|
|
89
|
-
};
|
|
90
|
-
} catch (e) {
|
|
91
|
-
return {
|
|
92
|
-
id,
|
|
93
|
-
key,
|
|
94
|
-
error: e as Error,
|
|
95
|
-
success: false,
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Creates a list of promises to get parameter values.
|
|
102
|
-
*
|
|
103
|
-
* @param {Record<string, any>} config the configuration object
|
|
104
|
-
* @returns {Promise<ResolvedValue>[]} the get parameter value promises
|
|
105
|
-
*/
|
|
106
|
-
private buildBulkRequest(
|
|
107
|
-
config: Record<string, any>,
|
|
108
|
-
): Promise<ResolvedValue>[] {
|
|
109
|
-
const promises: Promise<ResolvedValue>[] = [];
|
|
110
|
-
for (const [key, value] of Object.entries(config)) {
|
|
111
|
-
promises.push(this.resolveSecretValue(key, value));
|
|
112
|
-
}
|
|
113
|
-
return promises;
|
|
114
|
-
}
|
|
115
|
-
}
|