updating-secrets 0.0.0 → 0.1.0
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/LICENSE-CC0 +121 -0
- package/LICENSE-MIT +21 -0
- package/README.md +122 -0
- package/dist/adapters/all-adapters.d.ts +13 -0
- package/dist/adapters/all-adapters.js +13 -0
- package/dist/adapters/aws-secrets-manager.adapter.d.ts +32 -0
- package/dist/adapters/aws-secrets-manager.adapter.js +71 -0
- package/dist/adapters/base.adapter.d.ts +31 -0
- package/dist/adapters/base.adapter.js +29 -0
- package/dist/adapters/secrets-json-file.adapter.d.ts +51 -0
- package/dist/adapters/secrets-json-file.adapter.js +49 -0
- package/dist/adapters/static-secrets.adapter.d.ts +23 -0
- package/dist/adapters/static-secrets.adapter.js +26 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +10 -0
- package/dist/public-mocks/mock-aws-secrets-manager.d.ts +41 -0
- package/dist/public-mocks/mock-aws-secrets-manager.js +39 -0
- package/dist/public-mocks/mock-fs.d.ts +28 -0
- package/dist/public-mocks/mock-fs.js +47 -0
- package/dist/secret-load.error.d.ts +17 -0
- package/dist/secret-load.error.js +19 -0
- package/dist/secrets-definition/define-secrets.d.ts +142 -0
- package/dist/secrets-definition/define-secrets.js +53 -0
- package/dist/updating-secrets.d.ts +193 -0
- package/dist/updating-secrets.js +320 -0
- package/package.json +95 -11
package/LICENSE-CC0
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
CC0 1.0 Universal
|
|
2
|
+
|
|
3
|
+
Creative Commons Legal Code
|
|
4
|
+
|
|
5
|
+
CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
|
|
6
|
+
LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
|
|
7
|
+
ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
|
|
8
|
+
INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
|
|
9
|
+
REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
|
|
10
|
+
PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
|
|
11
|
+
THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
|
|
12
|
+
HEREUNDER.
|
|
13
|
+
|
|
14
|
+
Statement of Purpose
|
|
15
|
+
|
|
16
|
+
The laws of most jurisdictions throughout the world automatically confer
|
|
17
|
+
exclusive Copyright and Related Rights (defined below) upon the creator
|
|
18
|
+
and subsequent owner(s) (each and all, an "owner") of an original work of
|
|
19
|
+
authorship and/or a database (each, a "Work").
|
|
20
|
+
|
|
21
|
+
Certain owners wish to permanently relinquish those rights to a Work for
|
|
22
|
+
the purpose of contributing to a commons of creative, cultural and
|
|
23
|
+
scientific works ("Commons") that the public can reliably and without fear
|
|
24
|
+
of later claims of infringement build upon, modify, incorporate in other
|
|
25
|
+
works, reuse and redistribute as freely as possible in any form whatsoever
|
|
26
|
+
and for any purposes, including without limitation commercial purposes.
|
|
27
|
+
These owners may contribute to the Commons to promote the ideal of a free
|
|
28
|
+
culture and the further production of creative, cultural and scientific
|
|
29
|
+
works, or to gain reputation or greater distribution for their Work in
|
|
30
|
+
part through the use and efforts of others.
|
|
31
|
+
|
|
32
|
+
For these and/or other purposes and motivations, and without any
|
|
33
|
+
expectation of additional consideration or compensation, the person
|
|
34
|
+
associating CC0 with a Work (the "Affirmer"), to the extent that he or she
|
|
35
|
+
is an owner of Copyright and Related Rights in the Work, voluntarily
|
|
36
|
+
elects to apply CC0 to the Work and publicly distribute the Work under its
|
|
37
|
+
terms, with knowledge of his or her Copyright and Related Rights in the
|
|
38
|
+
Work and the meaning and intended legal effect of CC0 on those rights.
|
|
39
|
+
|
|
40
|
+
1. Copyright and Related Rights. A Work made available under CC0 may be
|
|
41
|
+
protected by copyright and related or neighboring rights ("Copyright and
|
|
42
|
+
Related Rights"). Copyright and Related Rights include, but are not
|
|
43
|
+
limited to, the following:
|
|
44
|
+
|
|
45
|
+
i. the right to reproduce, adapt, distribute, perform, display,
|
|
46
|
+
communicate, and translate a Work;
|
|
47
|
+
ii. moral rights retained by the original author(s) and/or performer(s);
|
|
48
|
+
iii. publicity and privacy rights pertaining to a person's image or
|
|
49
|
+
likeness depicted in a Work;
|
|
50
|
+
iv. rights protecting against unfair competition in regards to a Work,
|
|
51
|
+
subject to the limitations in paragraph 4(a), below;
|
|
52
|
+
v. rights protecting the extraction, dissemination, use and reuse of data
|
|
53
|
+
in a Work;
|
|
54
|
+
vi. database rights (such as those arising under Directive 96/9/EC of the
|
|
55
|
+
European Parliament and of the Council of 11 March 1996 on the legal
|
|
56
|
+
protection of databases, and under any national implementation
|
|
57
|
+
thereof, including any amended or successor version of such
|
|
58
|
+
directive); and
|
|
59
|
+
vii. other similar, equivalent or corresponding rights throughout the
|
|
60
|
+
world based on applicable law or treaty, and any national
|
|
61
|
+
implementations thereof.
|
|
62
|
+
|
|
63
|
+
2. Waiver. To the greatest extent permitted by, but not in contravention
|
|
64
|
+
of, applicable law, Affirmer hereby overtly, fully, permanently,
|
|
65
|
+
irrevocably and unconditionally waives, abandons, and surrenders all of
|
|
66
|
+
Affirmer's Copyright and Related Rights and associated claims and causes
|
|
67
|
+
of action, whether now known or unknown (including existing as well as
|
|
68
|
+
future claims and causes of action), in the Work (i) in all territories
|
|
69
|
+
worldwide, (ii) for the maximum duration provided by applicable law or
|
|
70
|
+
treaty (including future time extensions), (iii) in any current or future
|
|
71
|
+
medium and for any number of copies, and (iv) for any purpose whatsoever,
|
|
72
|
+
including without limitation commercial, advertising or promotional
|
|
73
|
+
purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each
|
|
74
|
+
member of the public at large and to the detriment of Affirmer's heirs and
|
|
75
|
+
successors, fully intending that such Waiver shall not be subject to
|
|
76
|
+
revocation, rescission, cancellation, termination, or any other legal or
|
|
77
|
+
equitable action to disrupt the quiet enjoyment of the Work by the public
|
|
78
|
+
as contemplated by Affirmer's express Statement of Purpose.
|
|
79
|
+
|
|
80
|
+
3. Public License Fallback. Should any part of the Waiver for any reason
|
|
81
|
+
be judged legally invalid or ineffective under applicable law, then the
|
|
82
|
+
Waiver shall be preserved to the maximum extent permitted taking into
|
|
83
|
+
account Affirmer's express Statement of Purpose. In addition, to the
|
|
84
|
+
extent the Waiver is so judged Affirmer hereby grants to each affected
|
|
85
|
+
person a royalty-free, non transferable, non sublicensable, non exclusive,
|
|
86
|
+
irrevocable and unconditional license to exercise Affirmer's Copyright and
|
|
87
|
+
Related Rights in the Work (i) in all territories worldwide, (ii) for the
|
|
88
|
+
maximum duration provided by applicable law or treaty (including future
|
|
89
|
+
time extensions), (iii) in any current or future medium and for any number
|
|
90
|
+
of copies, and (iv) for any purpose whatsoever, including without
|
|
91
|
+
limitation commercial, advertising or promotional purposes (the
|
|
92
|
+
"License"). The License shall be deemed effective as of the date CC0 was
|
|
93
|
+
applied by Affirmer to the Work. Should any part of the License for any
|
|
94
|
+
reason be judged legally invalid or ineffective under applicable law, such
|
|
95
|
+
partial invalidity or ineffectiveness shall not invalidate the remainder
|
|
96
|
+
of the License, and in such case Affirmer hereby affirms that he or she
|
|
97
|
+
will not (i) exercise any of his or her remaining Copyright and Related
|
|
98
|
+
Rights in the Work or (ii) assert any associated claims and causes of
|
|
99
|
+
action with respect to the Work, in either case contrary to Affirmer's
|
|
100
|
+
express Statement of Purpose.
|
|
101
|
+
|
|
102
|
+
4. Limitations and Disclaimers.
|
|
103
|
+
|
|
104
|
+
a. No trademark or patent rights held by Affirmer are waived, abandoned,
|
|
105
|
+
surrendered, licensed or otherwise affected by this document.
|
|
106
|
+
b. Affirmer offers the Work as-is and makes no representations or
|
|
107
|
+
warranties of any kind concerning the Work, express, implied,
|
|
108
|
+
statutory or otherwise, including without limitation warranties of
|
|
109
|
+
title, merchantability, fitness for a particular purpose, non
|
|
110
|
+
infringement, or the absence of latent or other defects, accuracy, or
|
|
111
|
+
the present or absence of errors, whether or not discoverable, all to
|
|
112
|
+
the greatest extent permissible under applicable law.
|
|
113
|
+
c. Affirmer disclaims responsibility for clearing rights of other persons
|
|
114
|
+
that may apply to the Work or any use thereof, including without
|
|
115
|
+
limitation any person's Copyright and Related Rights in the Work.
|
|
116
|
+
Further, Affirmer disclaims responsibility for obtaining any necessary
|
|
117
|
+
consents, permissions or other rights required for any use of the
|
|
118
|
+
Work.
|
|
119
|
+
d. Affirmer understands and acknowledges that Creative Commons is not a
|
|
120
|
+
party to this document and has no duty or obligation with respect to
|
|
121
|
+
this CC0 or use of the Work.
|
package/LICENSE-MIT
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 electrovir
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# updating-secrets
|
|
2
|
+
|
|
3
|
+
Automatically update secrets on an interval with support for seamless secret rotation.
|
|
4
|
+
|
|
5
|
+
Reference docs: https://electrovir.github.io/updating-secrets
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm i updating-secrets
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
### Usage
|
|
14
|
+
|
|
15
|
+
#### Basics
|
|
16
|
+
|
|
17
|
+
First, define your set of secrets:
|
|
18
|
+
|
|
19
|
+
<!-- example-link: src/examples/define-secrets.example.ts -->
|
|
20
|
+
|
|
21
|
+
```TypeScript
|
|
22
|
+
import {defineSecrets, rotatableSecretShape} from 'updating-secrets';
|
|
23
|
+
|
|
24
|
+
// example collection of secrets
|
|
25
|
+
export const mySecrets = defineSecrets({
|
|
26
|
+
databaseCredentials: {
|
|
27
|
+
description: 'All credentials and access information needed for accessing the database.',
|
|
28
|
+
whereToFind:
|
|
29
|
+
'These values are automatically generated by RDS and only stored in AWS Secrets Manager.',
|
|
30
|
+
adapterConfig: {
|
|
31
|
+
aws: {
|
|
32
|
+
rootOf: 'prod/DatabaseCredentials',
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
shape: {
|
|
36
|
+
password: '',
|
|
37
|
+
dbname: '',
|
|
38
|
+
port: -1,
|
|
39
|
+
host: '',
|
|
40
|
+
username: '',
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
stripeSecret: {
|
|
44
|
+
description: 'Keys for accessing and authenticating with Stripe.',
|
|
45
|
+
whereToFind: 'Navigate to Developers > API keys > Standard keys > Secret key.',
|
|
46
|
+
adapterConfig: {
|
|
47
|
+
aws: {
|
|
48
|
+
keyIn: 'prod/BackendSecrets',
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
adminPassword: {
|
|
53
|
+
description:
|
|
54
|
+
'Password required by the admin to access sensitive information on the website.',
|
|
55
|
+
whereToFind: 'This is randomly generated and stored in AWS Secrets Manager.',
|
|
56
|
+
adapterConfig: {
|
|
57
|
+
aws: {
|
|
58
|
+
keyIn: 'prod/BackendSecrets',
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
shape: rotatableSecretShape,
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Second, choose your secrets adapters:
|
|
67
|
+
|
|
68
|
+
- [`AwsSecretsManagerAdapter`](https://electrovir.github.io/updating-secrets/classes/AwsSecretsManagerAdapter.html): loads secrets from AWS Secrets Manager.
|
|
69
|
+
- [`SecretsJsonFileAdapter`](https://electrovir.github.io/updating-secrets/classes/SecretsJsonFileAdapter.html): loads secrets from a JSON file.
|
|
70
|
+
- [`StaticSecretsAdapter`](https://electrovir.github.io/updating-secrets/classes/StaticSecretsAdapter.html): allows you to define all secrets values in-place.
|
|
71
|
+
|
|
72
|
+
Or create your own:
|
|
73
|
+
|
|
74
|
+
<!-- example-link: src/examples/custom-adapter.example.ts -->
|
|
75
|
+
|
|
76
|
+
```TypeScript
|
|
77
|
+
import {BaseSecretsAdapter, type ProcessedSecretDefinitions} from 'updating-secrets';
|
|
78
|
+
|
|
79
|
+
export class MyCustomSecretsAdapter extends BaseSecretsAdapter {
|
|
80
|
+
constructor() {
|
|
81
|
+
super('MyCustomSecretsAdapter');
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
public override loadSecrets(secrets: Readonly<ProcessedSecretDefinitions>) {
|
|
85
|
+
// load secrets here
|
|
86
|
+
return {};
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Lastly, create an instance of [`UpdatingSecrets`](https://electrovir.github.io/updating-secrets/classes/UpdatingSecrets.html) (with [`createUpdatingSecrets`](https://electrovir.github.io/updating-secrets/functions/createUpdatingSecrets.html)):
|
|
92
|
+
|
|
93
|
+
<!-- example-link: src/examples/updating-secrets.example.ts -->
|
|
94
|
+
|
|
95
|
+
```TypeScript
|
|
96
|
+
import {SecretsManager} from '@aws-sdk/client-secrets-manager';
|
|
97
|
+
import {AwsSecretsManagerAdapter, createUpdatingSecrets} from 'updating-secrets';
|
|
98
|
+
import {mySecrets} from './define-secrets.example.js';
|
|
99
|
+
|
|
100
|
+
const updatingSecrets = await createUpdatingSecrets(mySecrets, [
|
|
101
|
+
new AwsSecretsManagerAdapter(
|
|
102
|
+
new SecretsManager({
|
|
103
|
+
region: 'us-east-1',
|
|
104
|
+
}),
|
|
105
|
+
),
|
|
106
|
+
]);
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
#### Rotatable secrets
|
|
110
|
+
|
|
111
|
+
To create a seamlessly rotatable secret, use [`rotatableSecretShape`](https://electrovir.github.io/updating-secrets/variables/rotatableSecretShape.html) in the secret definition's shape property, either as the root (`shape: rotatableSecretShape`) or as a sub-property (`secret: {id: '', secret: rotatableSecretShape}`).
|
|
112
|
+
|
|
113
|
+
Use this secret in the following way:
|
|
114
|
+
|
|
115
|
+
- always store the secret with JSON like `{current: 'latest-value'}`
|
|
116
|
+
- when rotation is needed, move the old value to the `legacy` property: `{current: 'new-value', legacy: 'old-value'}`
|
|
117
|
+
- in your code, always access the secret `.current`
|
|
118
|
+
- if you need to compare a third party's usage of the secret (for example, if the secret is an API key that you distributed to them), use [`UpdatingSecrets.compareRotatableSecret()`](https://electrovir.github.io/updating-secrets/classes/UpdatingSecrets.html#comparerotatablesecret) to allow both the current and the legacy secret to pass comparisons:
|
|
119
|
+
```ts
|
|
120
|
+
// assuming the `apiKey` secret is defined with `shape: rotatableSecretShape`
|
|
121
|
+
updatingSecrets.compareRotatableSecret(request.headers.apiKey, updatingSecrets.get.apiKey);
|
|
122
|
+
```
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { AwsSecretsManagerAdapter } from './aws-secrets-manager.adapter.js';
|
|
2
|
+
import { SecretsJsonFileAdapter } from './secrets-json-file.adapter.js';
|
|
3
|
+
import { StaticSecretsAdapter } from './static-secrets.adapter.js';
|
|
4
|
+
/**
|
|
5
|
+
* All built-in secrets adapters.
|
|
6
|
+
*
|
|
7
|
+
* @category Internal
|
|
8
|
+
*/
|
|
9
|
+
export declare const allSecretAdapters: {
|
|
10
|
+
AwsSecretsManagerAdapter: typeof AwsSecretsManagerAdapter;
|
|
11
|
+
SecretsJsonFileAdapter: typeof SecretsJsonFileAdapter;
|
|
12
|
+
StaticSecretsAdapter: typeof StaticSecretsAdapter;
|
|
13
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { AwsSecretsManagerAdapter } from './aws-secrets-manager.adapter.js';
|
|
2
|
+
import { SecretsJsonFileAdapter } from './secrets-json-file.adapter.js';
|
|
3
|
+
import { StaticSecretsAdapter } from './static-secrets.adapter.js';
|
|
4
|
+
/**
|
|
5
|
+
* All built-in secrets adapters.
|
|
6
|
+
*
|
|
7
|
+
* @category Internal
|
|
8
|
+
*/
|
|
9
|
+
export const allSecretAdapters = {
|
|
10
|
+
AwsSecretsManagerAdapter,
|
|
11
|
+
SecretsJsonFileAdapter,
|
|
12
|
+
StaticSecretsAdapter,
|
|
13
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { GetSecretValueCommandOutput, type GetSecretValueCommand } from '@aws-sdk/client-secrets-manager';
|
|
2
|
+
import { ProcessedSecretDefinitions } from '../secrets-definition/define-secrets.js';
|
|
3
|
+
import { BaseSecretsAdapter } from './base.adapter.js';
|
|
4
|
+
/**
|
|
5
|
+
* Minimal subset of AWS's `SecretsManagerClient` from the
|
|
6
|
+
* [`@aws-sdk/client-secrets-manager`](https://www.npmjs.com/package/@aws-sdk/client-secrets-manager)
|
|
7
|
+
* package required for {@link AwsSecretsManagerAdapter}.
|
|
8
|
+
*
|
|
9
|
+
* For testing purposes use `MockAwsSecretsManagerClient` to create a mock instance of this.
|
|
10
|
+
*
|
|
11
|
+
* @category Internal
|
|
12
|
+
*/
|
|
13
|
+
export type NeededAwsSecretsManagerClient = {
|
|
14
|
+
/**
|
|
15
|
+
* Same as AWS's `SecretsManagerClient.send()` method but this only accepts the
|
|
16
|
+
* `GetSecretValueCommand` command.
|
|
17
|
+
*/
|
|
18
|
+
send(command: GetSecretValueCommand): Promise<GetSecretValueCommandOutput>;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Loads secrets from AWS Secrets Manager. A `SecretsManagerClient` instance must be provided.
|
|
22
|
+
*
|
|
23
|
+
* @category Adapters
|
|
24
|
+
*/
|
|
25
|
+
export declare class AwsSecretsManagerAdapter extends BaseSecretsAdapter {
|
|
26
|
+
protected readonly awsSecretsManager: Readonly<NeededAwsSecretsManagerClient>;
|
|
27
|
+
constructor(awsSecretsManager: Readonly<NeededAwsSecretsManagerClient>);
|
|
28
|
+
/** Loads secrets from the provided `SecretsManagerClient`. */
|
|
29
|
+
loadSecrets(secrets: ProcessedSecretDefinitions): Promise<{
|
|
30
|
+
[x: string]: Error | Promise<any>;
|
|
31
|
+
}>;
|
|
32
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { assert } from '@augment-vir/assert';
|
|
2
|
+
import { ensureError, getOrSet, mapObjectValuesSync, parseWithJson5, wrapInTry, } from '@augment-vir/common';
|
|
3
|
+
import { BaseSecretsAdapter } from './base.adapter.js';
|
|
4
|
+
/**
|
|
5
|
+
* Loads secrets from AWS Secrets Manager. A `SecretsManagerClient` instance must be provided.
|
|
6
|
+
*
|
|
7
|
+
* @category Adapters
|
|
8
|
+
*/
|
|
9
|
+
export class AwsSecretsManagerAdapter extends BaseSecretsAdapter {
|
|
10
|
+
awsSecretsManager;
|
|
11
|
+
constructor(awsSecretsManager) {
|
|
12
|
+
super('AwsSecretsManagerAdapter');
|
|
13
|
+
this.awsSecretsManager = awsSecretsManager;
|
|
14
|
+
}
|
|
15
|
+
/** Loads secrets from the provided `SecretsManagerClient`. */
|
|
16
|
+
async loadSecrets(secrets) {
|
|
17
|
+
/* node:coverage ignore next 1: dynamic imports are not branches */
|
|
18
|
+
const { GetSecretValueCommand } = await import('@aws-sdk/client-secrets-manager');
|
|
19
|
+
const cachedSecrets = {};
|
|
20
|
+
return mapObjectValuesSync(secrets, (secretName, secretDefinition) => {
|
|
21
|
+
const awsConfig = secretDefinition.adapterConfig.aws;
|
|
22
|
+
if (!awsConfig) {
|
|
23
|
+
return new Error(`No AWS adapter config (required for using AwsSecretsManagerAdapter) defined for secret '${secretDefinition.secretName}'.`);
|
|
24
|
+
}
|
|
25
|
+
const awsSecretName = awsConfig.keyIn || awsConfig.rootOf;
|
|
26
|
+
if (!awsSecretName) {
|
|
27
|
+
return new Error(`Invalid AWS adapter key config for '${secretDefinition.secretName}'.`);
|
|
28
|
+
}
|
|
29
|
+
const secretValue = getOrSet(cachedSecrets, awsSecretName, () => {
|
|
30
|
+
const sendCommand = new GetSecretValueCommand({
|
|
31
|
+
SecretId: awsSecretName,
|
|
32
|
+
});
|
|
33
|
+
return this.awsSecretsManager.send(sendCommand).then((result) => {
|
|
34
|
+
try {
|
|
35
|
+
const secretValue = result.SecretString;
|
|
36
|
+
if (secretValue) {
|
|
37
|
+
return wrapInTry(() => parseWithJson5(secretValue), {
|
|
38
|
+
fallbackValue: secretValue,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
throw new Error(`AWS SecretsManager secret '${awsSecretName}' has no string value.`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
return ensureError(error);
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
return secretValue
|
|
51
|
+
.then((awsSecretValue) => {
|
|
52
|
+
if (awsSecretValue instanceof Error) {
|
|
53
|
+
throw awsSecretValue;
|
|
54
|
+
}
|
|
55
|
+
else if (awsConfig.keyIn) {
|
|
56
|
+
assert.isObject(awsSecretValue, `AWS secret at '${awsSecretName}' is not an object.`);
|
|
57
|
+
return awsSecretValue[secretDefinition.secretName];
|
|
58
|
+
}
|
|
59
|
+
else if (secretDefinition.shapeDefinition) {
|
|
60
|
+
return wrapInTry(() => parseWithJson5(awsSecretValue), {
|
|
61
|
+
fallbackValue: awsSecretValue,
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
return awsSecretValue;
|
|
66
|
+
}
|
|
67
|
+
})
|
|
68
|
+
.catch((reason) => ensureError(reason));
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { JsonCompatibleValue, MaybePromise } from '@augment-vir/common';
|
|
2
|
+
import { ProcessedSecretDefinitions } from '../secrets-definition/define-secrets.js';
|
|
3
|
+
/**
|
|
4
|
+
* Raw secret values as returned by an adapter's `loadSecrets` method.
|
|
5
|
+
*
|
|
6
|
+
* @category Internal
|
|
7
|
+
*/
|
|
8
|
+
export type RawSecrets = {
|
|
9
|
+
[SecretName in string]: MaybePromise<JsonCompatibleValue | Error>;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* This is the base secrets adapter class. This doesn't actually connect to anything. Only use it as
|
|
13
|
+
* a base class for adapters that _do_ connect to something.
|
|
14
|
+
*
|
|
15
|
+
* @category Internal
|
|
16
|
+
*/
|
|
17
|
+
export declare class BaseSecretsAdapter {
|
|
18
|
+
readonly adapterName: string;
|
|
19
|
+
constructor(adapterName: string);
|
|
20
|
+
/**
|
|
21
|
+
* Load secrets from the adapter. This base implementation should never be used and, thus,
|
|
22
|
+
* simply throws an error. It is expected that this class will be extended by actual adapters
|
|
23
|
+
* and this method will be overridden.
|
|
24
|
+
*/
|
|
25
|
+
loadSecrets(secrets: Readonly<ProcessedSecretDefinitions>): MaybePromise<RawSecrets>;
|
|
26
|
+
/**
|
|
27
|
+
* Clean up all resources created by the adapter. This should not cause the adapter to destroy
|
|
28
|
+
* any resources passed to it in its constructor.
|
|
29
|
+
*/
|
|
30
|
+
destroy(): void;
|
|
31
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This is the base secrets adapter class. This doesn't actually connect to anything. Only use it as
|
|
3
|
+
* a base class for adapters that _do_ connect to something.
|
|
4
|
+
*
|
|
5
|
+
* @category Internal
|
|
6
|
+
*/
|
|
7
|
+
export class BaseSecretsAdapter {
|
|
8
|
+
adapterName;
|
|
9
|
+
constructor(adapterName) {
|
|
10
|
+
this.adapterName = adapterName;
|
|
11
|
+
if (!adapterName) {
|
|
12
|
+
throw new Error(`Cannot have empty adapter name in '${this.constructor.name}'.`);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Load secrets from the adapter. This base implementation should never be used and, thus,
|
|
17
|
+
* simply throws an error. It is expected that this class will be extended by actual adapters
|
|
18
|
+
* and this method will be overridden.
|
|
19
|
+
*/
|
|
20
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
21
|
+
loadSecrets(secrets) {
|
|
22
|
+
throw new Error('Do not try to load secrets from the base secrets adapter.');
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Clean up all resources created by the adapter. This should not cause the adapter to destroy
|
|
26
|
+
* any resources passed to it in its constructor.
|
|
27
|
+
*/
|
|
28
|
+
destroy() { }
|
|
29
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { type MaybePromise, type PartialWithUndefined } from '@augment-vir/common';
|
|
2
|
+
import type { SecretDefinitions, SecretValues } from '../secrets-definition/define-secrets.js';
|
|
3
|
+
import { BaseSecretsAdapter } from './base.adapter.js';
|
|
4
|
+
/**
|
|
5
|
+
* Options for {@link SecretsJsonFileAdapter}.
|
|
6
|
+
*
|
|
7
|
+
* @category Internal
|
|
8
|
+
*/
|
|
9
|
+
export type SecretsJsonFileAdapterOptions<Secrets extends SecretDefinitions = any> = {
|
|
10
|
+
/**
|
|
11
|
+
* Optional override for Node.js's `fs` for mocking, testing, or other purposes.
|
|
12
|
+
*
|
|
13
|
+
* @default import * as fs from 'node:fs';
|
|
14
|
+
*/
|
|
15
|
+
fsOverride: {
|
|
16
|
+
/** `'node:fs/promises'` */
|
|
17
|
+
promises: {
|
|
18
|
+
/** `readFile` from `'node:fs/promises'` */
|
|
19
|
+
readFile: (filePath: string) => Promise<string | Buffer>;
|
|
20
|
+
/** `writeFile` from `'node:fs/promises'` */
|
|
21
|
+
writeFile: (filePath: string, contents: string | Buffer) => Promise<void>;
|
|
22
|
+
};
|
|
23
|
+
/** `existsSync` from `'node:fs'` */
|
|
24
|
+
existsSync: (filePath: string) => boolean;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Optional function that will automatically generate and save new secrets if the JSON file is
|
|
28
|
+
* missing. This is particularly useful for dev or testing environments.
|
|
29
|
+
*/
|
|
30
|
+
generateValues: (() => MaybePromise<SecretValues<Secrets>>) | undefined;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Loads all secrets from a single JSON file. This should rarely be used in production environments.
|
|
34
|
+
* Make sure that your secrets JSON file is not committed to your source code.
|
|
35
|
+
*
|
|
36
|
+
* This will not error out if any secrets are missing, but since `UpdatingSecrets` itself will, it's
|
|
37
|
+
* recommended to set the `lazyFailure` option on `UpdatingSecrets` when using this adapter (or use
|
|
38
|
+
* other adapters as well).
|
|
39
|
+
*
|
|
40
|
+
* @category Adapters
|
|
41
|
+
*/
|
|
42
|
+
export declare class SecretsJsonFileAdapter<const Secrets extends SecretDefinitions = any> extends BaseSecretsAdapter {
|
|
43
|
+
/** Path to the JSON */
|
|
44
|
+
protected readonly jsonFilePath: string;
|
|
45
|
+
protected readonly options: SecretsJsonFileAdapterOptions;
|
|
46
|
+
constructor(
|
|
47
|
+
/** Path to the JSON */
|
|
48
|
+
jsonFilePath: string, options?: PartialWithUndefined<SecretsJsonFileAdapterOptions<Secrets>>);
|
|
49
|
+
/** Loads secrets from the given JSON file path. */
|
|
50
|
+
loadSecrets(): Promise<any>;
|
|
51
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { mergeDefinedProperties, parseWithJson5, } from '@augment-vir/common';
|
|
2
|
+
import { existsSync as existsSyncImport } from 'node:fs';
|
|
3
|
+
import { readFile as readFileImport, writeFile as writeFileImport } from 'node:fs/promises';
|
|
4
|
+
import { BaseSecretsAdapter } from './base.adapter.js';
|
|
5
|
+
const defaultSecretsJsonFileAdapterOptions = {
|
|
6
|
+
fsOverride: {
|
|
7
|
+
existsSync: existsSyncImport,
|
|
8
|
+
promises: {
|
|
9
|
+
readFile: readFileImport,
|
|
10
|
+
writeFile: writeFileImport,
|
|
11
|
+
},
|
|
12
|
+
},
|
|
13
|
+
generateValues: undefined,
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Loads all secrets from a single JSON file. This should rarely be used in production environments.
|
|
17
|
+
* Make sure that your secrets JSON file is not committed to your source code.
|
|
18
|
+
*
|
|
19
|
+
* This will not error out if any secrets are missing, but since `UpdatingSecrets` itself will, it's
|
|
20
|
+
* recommended to set the `lazyFailure` option on `UpdatingSecrets` when using this adapter (or use
|
|
21
|
+
* other adapters as well).
|
|
22
|
+
*
|
|
23
|
+
* @category Adapters
|
|
24
|
+
*/
|
|
25
|
+
export class SecretsJsonFileAdapter extends BaseSecretsAdapter {
|
|
26
|
+
jsonFilePath;
|
|
27
|
+
options;
|
|
28
|
+
constructor(
|
|
29
|
+
/** Path to the JSON */
|
|
30
|
+
jsonFilePath, options = {}) {
|
|
31
|
+
super('SecretsJsonFileAdapter');
|
|
32
|
+
this.jsonFilePath = jsonFilePath;
|
|
33
|
+
this.options = mergeDefinedProperties(defaultSecretsJsonFileAdapterOptions, options);
|
|
34
|
+
}
|
|
35
|
+
/** Loads secrets from the given JSON file path. */
|
|
36
|
+
async loadSecrets() {
|
|
37
|
+
if (!this.options.fsOverride.existsSync(this.jsonFilePath)) {
|
|
38
|
+
if (this.options.generateValues) {
|
|
39
|
+
const newSecrets = await this.options.generateValues();
|
|
40
|
+
await this.options.fsOverride.promises.writeFile(this.jsonFilePath, JSON.stringify(newSecrets));
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
throw new Error(`Missing secrets JSON file at '${this.jsonFilePath}'`);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
const fileContents = String(await this.options.fsOverride.promises.readFile(this.jsonFilePath));
|
|
47
|
+
return parseWithJson5(fileContents);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { JsonCompatibleValue } from '@augment-vir/common';
|
|
2
|
+
import { BaseSecretsAdapter } from './base.adapter.js';
|
|
3
|
+
/**
|
|
4
|
+
* This adapter is constructed with a static set of secrets and that static set of secrets is always
|
|
5
|
+
* directly returned when loading secrets.
|
|
6
|
+
*
|
|
7
|
+
* This is primarily designed for the following use cases:
|
|
8
|
+
*
|
|
9
|
+
* - Providing default values for secrets, which are intended to be overridden in following adapters
|
|
10
|
+
* (in the adapters array given to an instance of `UpdatingSecrets`).
|
|
11
|
+
* - Setting mock secret values for testing purposes.
|
|
12
|
+
*
|
|
13
|
+
* @category Adapters
|
|
14
|
+
*/
|
|
15
|
+
export declare class StaticSecretsAdapter extends BaseSecretsAdapter {
|
|
16
|
+
/** Static secrets that will always be directly returned as the latest set of loaded secrets. */
|
|
17
|
+
protected readonly staticSecrets: Record<string, JsonCompatibleValue>;
|
|
18
|
+
constructor(
|
|
19
|
+
/** Static secrets that will always be directly returned as the latest set of loaded secrets. */
|
|
20
|
+
staticSecrets: Record<string, JsonCompatibleValue>);
|
|
21
|
+
/** Directly returns the static secrets given. */
|
|
22
|
+
loadSecrets(): Record<string, JsonCompatibleValue>;
|
|
23
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { BaseSecretsAdapter } from './base.adapter.js';
|
|
2
|
+
/**
|
|
3
|
+
* This adapter is constructed with a static set of secrets and that static set of secrets is always
|
|
4
|
+
* directly returned when loading secrets.
|
|
5
|
+
*
|
|
6
|
+
* This is primarily designed for the following use cases:
|
|
7
|
+
*
|
|
8
|
+
* - Providing default values for secrets, which are intended to be overridden in following adapters
|
|
9
|
+
* (in the adapters array given to an instance of `UpdatingSecrets`).
|
|
10
|
+
* - Setting mock secret values for testing purposes.
|
|
11
|
+
*
|
|
12
|
+
* @category Adapters
|
|
13
|
+
*/
|
|
14
|
+
export class StaticSecretsAdapter extends BaseSecretsAdapter {
|
|
15
|
+
staticSecrets;
|
|
16
|
+
constructor(
|
|
17
|
+
/** Static secrets that will always be directly returned as the latest set of loaded secrets. */
|
|
18
|
+
staticSecrets) {
|
|
19
|
+
super('StaticSecretsAdapter');
|
|
20
|
+
this.staticSecrets = staticSecrets;
|
|
21
|
+
}
|
|
22
|
+
/** Directly returns the static secrets given. */
|
|
23
|
+
loadSecrets() {
|
|
24
|
+
return this.staticSecrets;
|
|
25
|
+
}
|
|
26
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from './adapters/all-adapters.js';
|
|
2
|
+
export * from './adapters/aws-secrets-manager.adapter.js';
|
|
3
|
+
export * from './adapters/base.adapter.js';
|
|
4
|
+
export * from './adapters/secrets-json-file.adapter.js';
|
|
5
|
+
export * from './adapters/static-secrets.adapter.js';
|
|
6
|
+
export * from './public-mocks/mock-aws-secrets-manager.js';
|
|
7
|
+
export * from './public-mocks/mock-fs.js';
|
|
8
|
+
export * from './secret-load.error.js';
|
|
9
|
+
export * from './secrets-definition/define-secrets.js';
|
|
10
|
+
export * from './updating-secrets.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from './adapters/all-adapters.js';
|
|
2
|
+
export * from './adapters/aws-secrets-manager.adapter.js';
|
|
3
|
+
export * from './adapters/base.adapter.js';
|
|
4
|
+
export * from './adapters/secrets-json-file.adapter.js';
|
|
5
|
+
export * from './adapters/static-secrets.adapter.js';
|
|
6
|
+
export * from './public-mocks/mock-aws-secrets-manager.js';
|
|
7
|
+
export * from './public-mocks/mock-fs.js';
|
|
8
|
+
export * from './secret-load.error.js';
|
|
9
|
+
export * from './secrets-definition/define-secrets.js';
|
|
10
|
+
export * from './updating-secrets.js';
|