@openstax/ts-utils 1.1.28 → 1.1.30
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/dist/cjs/config/awsAccountConfig.d.ts +5 -0
- package/dist/cjs/config/awsAccountConfig.js +35 -0
- package/dist/cjs/config/awsParameterConfig.d.ts +2 -0
- package/dist/cjs/config/awsParameterConfig.js +18 -0
- package/dist/cjs/config/envConfig.d.ts +3 -0
- package/dist/cjs/config/envConfig.js +35 -0
- package/dist/cjs/config/index.d.ts +21 -0
- package/dist/cjs/config/index.js +39 -0
- package/dist/cjs/config/lambdaParameterConfig.d.ts +2 -0
- package/dist/cjs/config/lambdaParameterConfig.js +36 -0
- package/dist/cjs/config/replaceConfig.d.ts +4 -0
- package/dist/cjs/config/replaceConfig.js +12 -0
- package/dist/cjs/config/resolveConfigValue.d.ts +2 -0
- package/dist/cjs/config/resolveConfigValue.js +12 -0
- package/dist/cjs/services/authProvider/decryption.js +4 -4
- package/dist/cjs/tsconfig.withoutspecs.cjs.tsbuildinfo +1 -1
- package/dist/esm/config/awsAccountConfig.d.ts +5 -0
- package/dist/esm/config/awsAccountConfig.js +31 -0
- package/dist/esm/config/awsParameterConfig.d.ts +2 -0
- package/dist/esm/config/awsParameterConfig.js +14 -0
- package/dist/esm/config/envConfig.d.ts +3 -0
- package/dist/esm/config/envConfig.js +31 -0
- package/dist/esm/config/index.d.ts +21 -0
- package/dist/esm/config/index.js +21 -0
- package/dist/esm/config/lambdaParameterConfig.d.ts +2 -0
- package/dist/esm/config/lambdaParameterConfig.js +29 -0
- package/dist/esm/config/replaceConfig.d.ts +4 -0
- package/dist/esm/config/replaceConfig.js +8 -0
- package/dist/esm/config/resolveConfigValue.d.ts +2 -0
- package/dist/esm/config/resolveConfigValue.js +8 -0
- package/dist/esm/services/authProvider/decryption.js +1 -1
- package/dist/esm/tsconfig.withoutspecs.esm.tsbuildinfo +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.awsAccountConfig = void 0;
|
|
4
|
+
const client_sts_1 = require("@aws-sdk/client-sts");
|
|
5
|
+
const assertions_1 = require("../assertions");
|
|
6
|
+
const securityTokenService_1 = require("../aws/securityTokenService");
|
|
7
|
+
const envConfig_1 = require("./envConfig");
|
|
8
|
+
const resolveConfigValue_1 = require("./resolveConfigValue");
|
|
9
|
+
/*
|
|
10
|
+
* uses a different given value depending on which aws account the code is deployed in.
|
|
11
|
+
* this resolves the given value recursively, so its composable with more configProviders in the
|
|
12
|
+
* conditional cases.
|
|
13
|
+
*
|
|
14
|
+
* eg:
|
|
15
|
+
* const config = {
|
|
16
|
+
* configValue: awsAccountConfig({sandbox: 'inTheSandboxConfig', production: 'inProductionConfig'}),
|
|
17
|
+
* configValue: awsAccountConfig({sandbox: envConfig('an_environment_variable'), production: 'inProductionConfig'}),
|
|
18
|
+
* };
|
|
19
|
+
* */
|
|
20
|
+
const awsAccountConfig = (config) => {
|
|
21
|
+
const production = (0, envConfig_1.envConfig)('PRODUCTION_AWS');
|
|
22
|
+
const sandbox = (0, envConfig_1.envConfig)('SANDBOX_AWS');
|
|
23
|
+
return async () => {
|
|
24
|
+
const identity = await (0, securityTokenService_1.securityTokenService)().send(new client_sts_1.GetCallerIdentityCommand({}));
|
|
25
|
+
switch (identity.Account) {
|
|
26
|
+
case await (0, resolveConfigValue_1.resolveConfigValue)(sandbox):
|
|
27
|
+
return await (0, resolveConfigValue_1.resolveConfigValue)((0, assertions_1.assertDefined)(config.sandbox, 'a sandbox config was not provided'));
|
|
28
|
+
case await (0, resolveConfigValue_1.resolveConfigValue)(production):
|
|
29
|
+
return await (0, resolveConfigValue_1.resolveConfigValue)((0, assertions_1.assertDefined)(config.production, 'a production config was not provided'));
|
|
30
|
+
default:
|
|
31
|
+
throw new Error('unknown aws account');
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
exports.awsAccountConfig = awsAccountConfig;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.awsParameterConfig = void 0;
|
|
4
|
+
const client_ssm_1 = require("@aws-sdk/client-ssm");
|
|
5
|
+
const assertions_1 = require("../assertions");
|
|
6
|
+
const ssmService_1 = require("../aws/ssmService");
|
|
7
|
+
const resolveConfigValue_1 = require("./resolveConfigValue");
|
|
8
|
+
const awsParameterConfig = (parameterName) => {
|
|
9
|
+
return async () => {
|
|
10
|
+
const command = new client_ssm_1.GetParameterCommand({ Name: await (0, resolveConfigValue_1.resolveConfigValue)(parameterName), WithDecryption: true });
|
|
11
|
+
// send() throws ParameterNotFound if the parameter is missing,
|
|
12
|
+
// so it's not clear what missing Parameter or Value mean
|
|
13
|
+
const response = await (0, ssmService_1.ssmService)().send(command);
|
|
14
|
+
const parameter = (0, assertions_1.assertDefined)(response.Parameter, `aws GetParameter response missing Parameter key for ${parameterName}"`);
|
|
15
|
+
return (0, assertions_1.assertDefined)(parameter.Value, `aws GetParameter response missing Parameter.Value key for ${parameterName}"`);
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
exports.awsParameterConfig = awsParameterConfig;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.envConfig = exports.ENV_BUILD_CONFIGS = void 0;
|
|
4
|
+
const assertions_1 = require("../assertions");
|
|
5
|
+
const guards_1 = require("../guards");
|
|
6
|
+
/*
|
|
7
|
+
* uses a config from the process environment.
|
|
8
|
+
*
|
|
9
|
+
* there are two modes for this, by default it expects the value to be provided at build time,
|
|
10
|
+
* and there is access built in here for webpack to provide the configs. if you pass `runtime`
|
|
11
|
+
* as the second argument it skips the build logic and will try to literally pull it from
|
|
12
|
+
* `process.env` when the config is used at runtime
|
|
13
|
+
*
|
|
14
|
+
* the value is read from the environment when something tries to use it, not when `envConfig` is called.
|
|
15
|
+
*
|
|
16
|
+
* eg:
|
|
17
|
+
* const config = {
|
|
18
|
+
* configValue: envConfig('environment_variable_name'),
|
|
19
|
+
* };
|
|
20
|
+
* */
|
|
21
|
+
exports.ENV_BUILD_CONFIGS = [];
|
|
22
|
+
const envConfig = (name, type = 'build', defaultValue) => {
|
|
23
|
+
if (type === 'build') {
|
|
24
|
+
exports.ENV_BUILD_CONFIGS.push(name);
|
|
25
|
+
}
|
|
26
|
+
return () => {
|
|
27
|
+
/*global __PROCESS_ENV*/
|
|
28
|
+
// @ts-ignore - hack to get around the way webpack/define works
|
|
29
|
+
// - https://github.com/webpack/webpack/issues/14800
|
|
30
|
+
// - https://github.com/webpack/webpack/issues/5392
|
|
31
|
+
const envs = { ...process.env, ...(typeof __PROCESS_ENV !== 'undefined' ? __PROCESS_ENV : {}) };
|
|
32
|
+
return (0, assertions_1.assertDefined)((0, guards_1.ifDefined)(envs[name], defaultValue), `expected to find environment variable with name: ${name}`);
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
exports.envConfig = envConfig;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export declare type ConfigValue = string;
|
|
2
|
+
export declare type Config = {
|
|
3
|
+
[key: string]: Config | ConfigValue;
|
|
4
|
+
};
|
|
5
|
+
export declare type ConfigValueProvider<V extends ConfigValue = ConfigValue> = (() => Promise<V> | V) | V;
|
|
6
|
+
export declare type ConfigProvider = {
|
|
7
|
+
[key: string]: ConfigProvider | ConfigValueProvider;
|
|
8
|
+
};
|
|
9
|
+
export declare type ConfigForConfigProvider<T> = T extends ConfigValue ? T : T extends ConfigProvider ? {
|
|
10
|
+
[key in keyof T]: ConfigForConfigProvider<T[key]>;
|
|
11
|
+
} : T extends ConfigValueProvider<infer R> ? R : never;
|
|
12
|
+
export declare type ConfigProviderForConfig<T> = T extends ConfigValue ? ConfigValueProvider<T> : T extends Config ? {
|
|
13
|
+
[key in keyof T]: ConfigProviderForConfig<T[key]>;
|
|
14
|
+
} : never;
|
|
15
|
+
export * from './resolveConfigValue';
|
|
16
|
+
export declare const stubConfig: <V extends string>(configValue: V) => ConfigValueProvider<V>;
|
|
17
|
+
export * from './envConfig';
|
|
18
|
+
export * from './replaceConfig';
|
|
19
|
+
export * from './awsAccountConfig';
|
|
20
|
+
export * from './awsParameterConfig';
|
|
21
|
+
export * from './lambdaParameterConfig';
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.stubConfig = void 0;
|
|
18
|
+
__exportStar(require("./resolveConfigValue"), exports);
|
|
19
|
+
/*
|
|
20
|
+
* ===========
|
|
21
|
+
* re-usable config providers
|
|
22
|
+
* ===========
|
|
23
|
+
* */
|
|
24
|
+
/*
|
|
25
|
+
* stub, mostly for testing. sometimes it helps please typescript to use this if you have
|
|
26
|
+
* two configs you want to have the same type but one is a fixed string and one is a complicated provider
|
|
27
|
+
*
|
|
28
|
+
* eg:
|
|
29
|
+
* const config = {
|
|
30
|
+
* configValue: stubConfig('just-a-string'),
|
|
31
|
+
* };
|
|
32
|
+
* */
|
|
33
|
+
const stubConfig = (configValue) => configValue;
|
|
34
|
+
exports.stubConfig = stubConfig;
|
|
35
|
+
__exportStar(require("./envConfig"), exports);
|
|
36
|
+
__exportStar(require("./replaceConfig"), exports);
|
|
37
|
+
__exportStar(require("./awsAccountConfig"), exports);
|
|
38
|
+
__exportStar(require("./awsParameterConfig"), exports);
|
|
39
|
+
__exportStar(require("./lambdaParameterConfig"), exports);
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.lambdaParameterConfig = void 0;
|
|
7
|
+
const node_fetch_1 = __importDefault(require("node-fetch"));
|
|
8
|
+
const assertions_1 = require("../assertions");
|
|
9
|
+
const envConfig_1 = require("./envConfig");
|
|
10
|
+
const _1 = require(".");
|
|
11
|
+
const lambdaExtensionUrl = 'http://localhost:2773';
|
|
12
|
+
let lambdaExtensionReadyPromise;
|
|
13
|
+
// NOTE: Can only be used during in AWS Lambda
|
|
14
|
+
// The AWS Parameters and Secrets Lambda Extension Layer must be included in the Lambda function
|
|
15
|
+
const lambdaParameterConfig = (parameterName) => async () => {
|
|
16
|
+
const token = await (0, _1.resolveConfigValue)((0, envConfig_1.envConfig)('AWS_SESSION_TOKEN', 'runtime'));
|
|
17
|
+
const name = await (0, _1.resolveConfigValue)(parameterName);
|
|
18
|
+
if (!lambdaExtensionReadyPromise) {
|
|
19
|
+
// This request will return 400 Bad Request,
|
|
20
|
+
// but we only care that it'll block until the extension is ready
|
|
21
|
+
lambdaExtensionReadyPromise = (0, node_fetch_1.default)(lambdaExtensionUrl);
|
|
22
|
+
}
|
|
23
|
+
await lambdaExtensionReadyPromise;
|
|
24
|
+
const resp = await (0, node_fetch_1.default)(
|
|
25
|
+
// Port 2773 is the default port for the extension
|
|
26
|
+
`${lambdaExtensionUrl}/systemsmanager/parameters/get?name=${name}&withDecryption=true`, { headers: { 'X-Aws-Parameters-Secrets-Token': token } });
|
|
27
|
+
if (resp.ok) {
|
|
28
|
+
const response = await resp.json();
|
|
29
|
+
const parameter = (0, assertions_1.assertDefined)(response.Parameter, `aws GetParameter response missing Parameter key for ${name}"`);
|
|
30
|
+
return (0, assertions_1.assertDefined)(parameter.Value, `aws GetParameter response missing Parameter.Value key for ${name}"`);
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
throw new Error(`HTTP Error Response ${resp.status} ${resp.statusText} while fetching parameter ${name}`);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
exports.lambdaParameterConfig = lambdaParameterConfig;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.replaceConfig = void 0;
|
|
4
|
+
const resolveConfigValue_1 = require("./resolveConfigValue");
|
|
5
|
+
const replaceConfig = (base, replacements) => {
|
|
6
|
+
return async () => {
|
|
7
|
+
const resolved = await Promise.all(Object.entries(replacements)
|
|
8
|
+
.map(async ([token, replacement]) => [token, await (0, resolveConfigValue_1.resolveConfigValue)(replacement)]));
|
|
9
|
+
return resolved.reduce((result, [token, replacement]) => result.replace(token, replacement), await (0, resolveConfigValue_1.resolveConfigValue)(base));
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
exports.replaceConfig = replaceConfig;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.resolveConfigValue = void 0;
|
|
4
|
+
/*
|
|
5
|
+
* resolves a config value into a string, to be used inside of things that are provided configurations
|
|
6
|
+
* */
|
|
7
|
+
const resolveConfigValue = async (provider) => {
|
|
8
|
+
return typeof provider === 'function'
|
|
9
|
+
? await provider()
|
|
10
|
+
: provider;
|
|
11
|
+
};
|
|
12
|
+
exports.resolveConfigValue = resolveConfigValue;
|
|
@@ -3,14 +3,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.decryptionAuthProvider = void 0;
|
|
4
4
|
const jose_1 = require("jose");
|
|
5
5
|
const __1 = require("../..");
|
|
6
|
-
const
|
|
6
|
+
const resolveConfigValue_1 = require("../../config/resolveConfigValue");
|
|
7
7
|
const guards_1 = require("../../guards");
|
|
8
8
|
const _1 = require(".");
|
|
9
9
|
const decryptionAuthProvider = (initializer) => (configProvider) => {
|
|
10
10
|
const config = configProvider[(0, guards_1.ifDefined)(initializer.configSpace, 'decryption')];
|
|
11
|
-
const cookieName = (0, __1.once)(() => (0,
|
|
12
|
-
const encryptionPrivateKey = (0, __1.once)(() => (0,
|
|
13
|
-
const signaturePublicKey = (0, __1.once)(() => (0,
|
|
11
|
+
const cookieName = (0, __1.once)(() => (0, resolveConfigValue_1.resolveConfigValue)(config.cookieName));
|
|
12
|
+
const encryptionPrivateKey = (0, __1.once)(() => (0, resolveConfigValue_1.resolveConfigValue)(config.encryptionPrivateKey));
|
|
13
|
+
const signaturePublicKey = (0, __1.once)(() => (0, resolveConfigValue_1.resolveConfigValue)(config.signaturePublicKey));
|
|
14
14
|
const decryptAndVerify = async (jwt) => {
|
|
15
15
|
try {
|
|
16
16
|
// Decrypt SSO cookie
|