@middy/rds-signer 3.0.0-alpha.3 → 3.0.0-alpha.4
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/index.js +42 -48
- package/package.json +4 -4
package/index.js
CHANGED
|
@@ -1,80 +1,74 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
getInternal,
|
|
4
|
-
processCache,
|
|
5
|
-
getCache,
|
|
6
|
-
modifyCache
|
|
7
|
-
} from '@middy/util'
|
|
8
|
-
import RDS from 'aws-sdk/clients/rds.js' // v2
|
|
9
|
-
// import { RDS:{Signer} } from '@aws-sdk/client-rds' // v3
|
|
10
|
-
|
|
1
|
+
import { canPrefetch, getInternal, processCache, getCache, modifyCache } from '@middy/util';
|
|
2
|
+
import RDS from 'aws-sdk/clients/rds.js';
|
|
11
3
|
const defaults = {
|
|
12
4
|
AwsClient: RDS.Signer,
|
|
13
5
|
awsClientOptions: {},
|
|
14
|
-
fetchData: {},
|
|
6
|
+
fetchData: {},
|
|
15
7
|
disablePrefetch: false,
|
|
16
8
|
cacheKey: 'rds-signer',
|
|
17
9
|
cacheExpiry: -1,
|
|
18
10
|
setToContext: false
|
|
19
|
-
}
|
|
11
|
+
};
|
|
20
12
|
|
|
21
13
|
const rdsSignerMiddleware = (opts = {}) => {
|
|
22
|
-
const options = { ...defaults,
|
|
14
|
+
const options = { ...defaults,
|
|
15
|
+
...opts
|
|
16
|
+
};
|
|
23
17
|
|
|
24
18
|
const fetch = (request, cachedValues = {}) => {
|
|
25
|
-
const values = {}
|
|
26
|
-
for (const internalKey of Object.keys(options.fetchData)) {
|
|
27
|
-
if (cachedValues[internalKey]) continue
|
|
19
|
+
const values = {};
|
|
28
20
|
|
|
29
|
-
|
|
30
|
-
|
|
21
|
+
for (const internalKey of Object.keys(options.fetchData)) {
|
|
22
|
+
if (cachedValues[internalKey]) continue;
|
|
23
|
+
const client = new options.AwsClient({ ...options.awsClientOptions,
|
|
31
24
|
...options.fetchData[internalKey]
|
|
32
|
-
})
|
|
33
|
-
// AWS doesn't support getAuthToken.promise() in aws-sdk v2 :( See https://github.com/aws/aws-sdk-js/issues/3595
|
|
25
|
+
});
|
|
34
26
|
values[internalKey] = new Promise((resolve, reject) => {
|
|
35
27
|
client.getAuthToken({}, (e, token) => {
|
|
36
28
|
if (e) {
|
|
37
|
-
reject(e)
|
|
29
|
+
reject(e);
|
|
38
30
|
}
|
|
39
|
-
|
|
31
|
+
|
|
40
32
|
if (!token.includes('X-Amz-Security-Token=')) {
|
|
41
|
-
reject(new Error('X-Amz-Security-Token Missing'))
|
|
33
|
+
reject(new Error('X-Amz-Security-Token Missing'));
|
|
42
34
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
value
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
// values[internalKey] = createClient(awsClientOptions, request).then(client => client.getAuthToken())
|
|
35
|
+
|
|
36
|
+
resolve(token);
|
|
37
|
+
});
|
|
38
|
+
}).catch(e => {
|
|
39
|
+
const value = getCache(options.cacheKey).value ?? {};
|
|
40
|
+
value[internalKey] = undefined;
|
|
41
|
+
modifyCache(options.cacheKey, value);
|
|
42
|
+
throw e;
|
|
43
|
+
});
|
|
53
44
|
}
|
|
54
45
|
|
|
55
|
-
return values
|
|
56
|
-
}
|
|
46
|
+
return values;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
let prefetch;
|
|
57
50
|
|
|
58
|
-
let prefetch
|
|
59
51
|
if (canPrefetch(options)) {
|
|
60
|
-
prefetch = processCache(options, fetch)
|
|
52
|
+
prefetch = processCache(options, fetch);
|
|
61
53
|
}
|
|
62
54
|
|
|
63
|
-
const rdsSignerMiddlewareBefore = async
|
|
64
|
-
const {
|
|
65
|
-
|
|
66
|
-
|
|
55
|
+
const rdsSignerMiddlewareBefore = async request => {
|
|
56
|
+
const {
|
|
57
|
+
value
|
|
58
|
+
} = prefetch ?? processCache(options, fetch, request);
|
|
59
|
+
Object.assign(request.internal, value);
|
|
67
60
|
|
|
68
61
|
if (options.setToContext) {
|
|
69
|
-
const data = await getInternal(Object.keys(options.fetchData), request)
|
|
70
|
-
Object.assign(request.context, data)
|
|
62
|
+
const data = await getInternal(Object.keys(options.fetchData), request);
|
|
63
|
+
Object.assign(request.context, data);
|
|
71
64
|
}
|
|
72
65
|
|
|
73
|
-
prefetch = null
|
|
74
|
-
}
|
|
66
|
+
prefetch = null;
|
|
67
|
+
};
|
|
75
68
|
|
|
76
69
|
return {
|
|
77
70
|
before: rdsSignerMiddlewareBefore
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
export default rdsSignerMiddleware;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/rds-signer",
|
|
3
|
-
"version": "3.0.0-alpha.
|
|
3
|
+
"version": "3.0.0-alpha.4",
|
|
4
4
|
"description": "RDS (Relational Database Service) credentials middleware for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -48,13 +48,13 @@
|
|
|
48
48
|
},
|
|
49
49
|
"homepage": "https://github.com/middyjs/middy#readme",
|
|
50
50
|
"dependencies": {
|
|
51
|
-
"@middy/util": "^3.0.0-alpha.
|
|
51
|
+
"@middy/util": "^3.0.0-alpha.4"
|
|
52
52
|
},
|
|
53
53
|
"devDependencies": {
|
|
54
|
-
"@middy/core": "^3.0.0-alpha.
|
|
54
|
+
"@middy/core": "^3.0.0-alpha.4",
|
|
55
55
|
"@types/node": "^17.0.0",
|
|
56
56
|
"aws-sdk": "^2.939.0",
|
|
57
57
|
"aws-xray-sdk": "^3.3.3"
|
|
58
58
|
},
|
|
59
|
-
"gitHead": "
|
|
59
|
+
"gitHead": "d4bea7f4e21f6a9bbb1f6f6908361169598b9e53"
|
|
60
60
|
}
|