@winible/winible-typed 2.120.0 → 2.121.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.
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.clearCredentialsCache = exports.loadCredentials = void 0;
|
|
4
4
|
const client_secrets_manager_1 = require("@aws-sdk/client-secrets-manager");
|
|
5
|
-
|
|
5
|
+
const cachedCredentials = new Map();
|
|
6
6
|
/**
|
|
7
7
|
* Loads credentials from AWS Secrets Manager
|
|
8
8
|
* Caches the result to avoid multiple API calls within the same Lambda execution
|
|
@@ -13,10 +13,10 @@ let cachedCredentials = null;
|
|
|
13
13
|
* @returns The parsed secret as a strongly-typed object containing only the requested keys, or all credentials if requiredKeys is empty
|
|
14
14
|
*/
|
|
15
15
|
async function loadCredentials(secretArn, region = 'us-east-1', requiredKeys = []) {
|
|
16
|
-
// Return cached credentials if available
|
|
17
|
-
if (cachedCredentials) {
|
|
18
|
-
console.log(
|
|
19
|
-
return cachedCredentials;
|
|
16
|
+
// Return cached credentials if available for this specific secret
|
|
17
|
+
if (cachedCredentials.has(secretArn)) {
|
|
18
|
+
console.log(`📦 Using cached credentials for ${secretArn}`);
|
|
19
|
+
return cachedCredentials.get(secretArn);
|
|
20
20
|
}
|
|
21
21
|
console.log('🔐 Loading credentials from AWS Secrets Manager');
|
|
22
22
|
try {
|
|
@@ -28,13 +28,14 @@ async function loadCredentials(secretArn, region = 'us-east-1', requiredKeys = [
|
|
|
28
28
|
}
|
|
29
29
|
const allCredentials = JSON.parse(response.SecretString);
|
|
30
30
|
// If requiredKeys is specified, only extract those keys
|
|
31
|
+
let credentialsToCache;
|
|
31
32
|
if (requiredKeys && requiredKeys.length > 0) {
|
|
32
|
-
|
|
33
|
+
credentialsToCache = {};
|
|
33
34
|
const missingKeys = [];
|
|
34
35
|
for (const key of requiredKeys) {
|
|
35
36
|
const keyStr = String(key);
|
|
36
37
|
if (keyStr in allCredentials) {
|
|
37
|
-
|
|
38
|
+
credentialsToCache[keyStr] = allCredentials[keyStr];
|
|
38
39
|
}
|
|
39
40
|
else {
|
|
40
41
|
missingKeys.push(keyStr);
|
|
@@ -48,10 +49,12 @@ async function loadCredentials(secretArn, region = 'us-east-1', requiredKeys = [
|
|
|
48
49
|
}
|
|
49
50
|
else {
|
|
50
51
|
// Otherwise, return all credentials
|
|
51
|
-
|
|
52
|
+
credentialsToCache = allCredentials;
|
|
52
53
|
}
|
|
54
|
+
// Cache the credentials for this specific secret
|
|
55
|
+
cachedCredentials.set(secretArn, credentialsToCache);
|
|
53
56
|
console.log('✅ Successfully loaded credentials from Secrets Manager');
|
|
54
|
-
return
|
|
57
|
+
return credentialsToCache;
|
|
55
58
|
}
|
|
56
59
|
catch (error) {
|
|
57
60
|
// Re-throw if it's already our custom error
|
|
@@ -67,7 +70,7 @@ exports.loadCredentials = loadCredentials;
|
|
|
67
70
|
* Clears the cached credentials (useful for testing)
|
|
68
71
|
*/
|
|
69
72
|
function clearCredentialsCache() {
|
|
70
|
-
cachedCredentials
|
|
73
|
+
cachedCredentials.clear();
|
|
71
74
|
}
|
|
72
75
|
exports.clearCredentialsCache = clearCredentialsCache;
|
|
73
76
|
//# sourceMappingURL=secretsManager.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"secretsManager.js","sourceRoot":"","sources":["../../../utils/lambdaUtils/secretsManager.ts"],"names":[],"mappings":";;;AAAA,4EAA8F;AAE9F,
|
|
1
|
+
{"version":3,"file":"secretsManager.js","sourceRoot":"","sources":["../../../utils/lambdaUtils/secretsManager.ts"],"names":[],"mappings":";;;AAAA,4EAA8F;AAE9F,MAAM,iBAAiB,GAAG,IAAI,GAAG,EAA+B,CAAC;AAEjE;;;;;;;;GAQG;AACI,KAAK,UAAU,eAAe,CACnC,SAAiB,EACjB,SAAiB,WAAW,EAC5B,eAA4B,EAAE;IAE9B,kEAAkE;IAClE,IAAI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AC,IAAI,CAAC,EAAE,CAAC,CAAC;gBACtF,OAAO,CAAC,KAAK,CAAC,mDAAmD,EAAE,KAAK,CAAC,CAAC;gBAC1E,MAAM,KAAK,CAAC;aACb;SACF;aAAM;YACL,oCAAoC;YACpC,kBAAkB,GAAG,cAAc,CAAC;SACrC;QAED,iDAAiD;QACjD,iBAAiB,CAAC,GAAG,CAAC,SAAS,EAAE,kBAAkB,CAAC,CAAC;QAErD,OAAO,CAAC,GAAG,CAAC,wDAAwD,CAAC,CAAC;QACtE,OAAO,kBAAuB,CAAC;KAChC;IAAC,OAAO,KAAK,EAAE;QACd,4CAA4C;QAC5C,IAAI,KAAK,YAAY,KAAK,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,uBAAuB,CAAC,IAAI,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,uBAAuB,CAAC,CAAC,EAAE;YAClI,MAAM,KAAK,CAAC;SACb;QACD,OAAO,CAAC,KAAK,CAAC,mDAAmD,EAAE,KAAK,CAAC,CAAC;QAC1E,MAAM,IAAI,KAAK,CAAC,0DAA0D,CAAC,CAAC;KAC7E;AACH,CAAC;AA7DD,0CA6DC;AAED;;GAEG;AACH,SAAgB,qBAAqB;IACnC,iBAAiB,CAAC,KAAK,EAAE,CAAC;AAC5B,CAAC;AAFD,sDAEC"}
|
package/package.json
CHANGED
|
@@ -131,7 +131,7 @@ describe('SecretsManager', () => {
|
|
|
131
131
|
|
|
132
132
|
expect(result1).toEqual(result2);
|
|
133
133
|
expect(mockSend).toHaveBeenCalledTimes(1); // Only called once due to caching
|
|
134
|
-
expect(consoleLogSpy).toHaveBeenCalledWith('📦 Using cached credentials');
|
|
134
|
+
expect(consoleLogSpy).toHaveBeenCalledWith('📦 Using cached credentials for test-secret-arn');
|
|
135
135
|
});
|
|
136
136
|
});
|
|
137
137
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { SecretsManagerClient, GetSecretValueCommand } from '@aws-sdk/client-secrets-manager';
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
const cachedCredentials = new Map<string, Record<string, any>>();
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Loads credentials from AWS Secrets Manager
|
|
@@ -16,10 +16,10 @@ export async function loadCredentials<T extends Record<string, any>>(
|
|
|
16
16
|
region: string = 'us-east-1',
|
|
17
17
|
requiredKeys: (keyof T)[] = []
|
|
18
18
|
): Promise<T> {
|
|
19
|
-
// Return cached credentials if available
|
|
20
|
-
if (cachedCredentials) {
|
|
21
|
-
console.log(
|
|
22
|
-
return cachedCredentials as T;
|
|
19
|
+
// Return cached credentials if available for this specific secret
|
|
20
|
+
if (cachedCredentials.has(secretArn)) {
|
|
21
|
+
console.log(`📦 Using cached credentials for ${secretArn}`);
|
|
22
|
+
return cachedCredentials.get(secretArn) as T;
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
console.log('🔐 Loading credentials from AWS Secrets Manager');
|
|
@@ -35,14 +35,15 @@ export async function loadCredentials<T extends Record<string, any>>(
|
|
|
35
35
|
const allCredentials = JSON.parse(response.SecretString);
|
|
36
36
|
|
|
37
37
|
// If requiredKeys is specified, only extract those keys
|
|
38
|
+
let credentialsToCache: Record<string, any>;
|
|
38
39
|
if (requiredKeys && requiredKeys.length > 0) {
|
|
39
|
-
|
|
40
|
+
credentialsToCache = {} as T;
|
|
40
41
|
const missingKeys: string[] = [];
|
|
41
42
|
|
|
42
43
|
for (const key of requiredKeys) {
|
|
43
44
|
const keyStr = String(key);
|
|
44
45
|
if (keyStr in allCredentials) {
|
|
45
|
-
|
|
46
|
+
credentialsToCache[keyStr] = allCredentials[keyStr];
|
|
46
47
|
} else {
|
|
47
48
|
missingKeys.push(keyStr);
|
|
48
49
|
}
|
|
@@ -55,11 +56,14 @@ export async function loadCredentials<T extends Record<string, any>>(
|
|
|
55
56
|
}
|
|
56
57
|
} else {
|
|
57
58
|
// Otherwise, return all credentials
|
|
58
|
-
|
|
59
|
+
credentialsToCache = allCredentials;
|
|
59
60
|
}
|
|
60
61
|
|
|
62
|
+
// Cache the credentials for this specific secret
|
|
63
|
+
cachedCredentials.set(secretArn, credentialsToCache);
|
|
64
|
+
|
|
61
65
|
console.log('✅ Successfully loaded credentials from Secrets Manager');
|
|
62
|
-
return
|
|
66
|
+
return credentialsToCache as T;
|
|
63
67
|
} catch (error) {
|
|
64
68
|
// Re-throw if it's already our custom error
|
|
65
69
|
if (error instanceof Error && (error.message.includes('Missing required keys') || error.message.includes('Secret value is empty'))) {
|
|
@@ -74,5 +78,5 @@ export async function loadCredentials<T extends Record<string, any>>(
|
|
|
74
78
|
* Clears the cached credentials (useful for testing)
|
|
75
79
|
*/
|
|
76
80
|
export function clearCredentialsCache(): void {
|
|
77
|
-
cachedCredentials
|
|
81
|
+
cachedCredentials.clear();
|
|
78
82
|
}
|