@middy/ssm 2.5.3 → 3.0.0-alpha.2

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2017-2021 Luciano Mammino, will Farrell and the [Middy team](https://github.com/middyjs/middy/graphs/contributors)
3
+ Copyright (c) 2017-2022 Luciano Mammino, will Farrell and the [Middy team](https://github.com/middyjs/middy/graphs/contributors)
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -29,11 +29,11 @@ This middleware fetches parameters from [AWS Systems Manager Parameter Store](ht
29
29
 
30
30
  Parameters to fetch can be defined by path and by name (not mutually exclusive). See AWS docs [here](https://aws.amazon.com/blogs/mt/organize-parameters-by-hierarchy-tags-or-amazon-cloudwatch-events-with-amazon-ec2-systems-manager-parameter-store/).
31
31
 
32
- Parameters can be assigned to the Node.js `process.env` object by setting the `setToEnv` flag to `true`. They can also be assigned to the function handler's `context` object by setting the `setToContext` flag to `true`. By default all parameters are added with uppercase names.
32
+ Parameters can be assigned to the function handler's `context` object by setting the `setToContext` flag to `true`. By default all parameters are added with uppercase names.
33
33
 
34
34
  The Middleware makes a single API request to fetch all the parameters defined by name, but must make an additional request per specified path. This is because the AWS SDK currently doesn't expose a method to retrieve parameters from multiple paths.
35
35
 
36
- For each parameter defined by name, you also provide the name under which its value should be added to `process.env` or `context`. For each path, you instead provide a prefix, and by default the value import each parameter returned from that path will be added to `process.env` or `context` with a name equal to what's left of the parameter's full name _after_ the defined path, with the prefix prepended. If the prefix is an empty string, nothing is prepended. You can override this behaviour by providing your own mapping function with the `getParamNameFromPath` config option.
36
+ For each parameter defined by name, you also provide the name under which its value should be added to `context`. For each path, you instead provide a prefix, and by default the value import each parameter returned from that path will be added to `context` with a name equal to what's left of the parameter's full name _after_ the defined path, with the prefix prepended. If the prefix is an empty string, nothing is prepended. You can override this behaviour by providing your own mapping function with the `getParamNameFromPath` config option.
37
37
 
38
38
 
39
39
  ## Install
@@ -55,14 +55,11 @@ npm install --save @middy/ssm
55
55
  - `disablePrefetch` (boolean) (default `false`): On cold start requests will trigger early if they can. Setting `awsClientAssumeRole` disables prefetch.
56
56
  - `cacheKey` (string) (default `ssm`): Cache key for the fetched data responses. Must be unique across all middleware.
57
57
  - `cacheExpiry` (number) (default `-1`): How long fetch data responses should be cached for. `-1`: cache forever, `0`: never cache, `n`: cache for n ms.
58
- - `setToEnv` (boolean) (default `false`): Store role tokens to `process.env`. **Storing secrets in `process.env` is considered security bad practice**
59
58
  - `setToContext` (boolean) (default `false`): Store role tokens to `request.context`.
60
59
 
61
60
  NOTES:
62
61
  - Lambda is required to have IAM permission for `ssm:GetParameters` and/or `ssm:GetParametersByPath` depending on what you're requesting.
63
62
  - `SSM` has [throughput limitations](https://docs.aws.amazon.com/general/latest/gr/ssm.html). Switching to Advanced Parameter type or increasing `maxRetries` and `retryDelayOptions.base` in `awsClientOptions` may be required.
64
- - `setToEnv` and `setToContext` are included for legacy support and should be avoided for performance and security reasons. See main documentation for best practices.
65
- - `setToEnv` can only assign secrets of type string
66
63
 
67
64
  ## Sample usage
68
65
 
@@ -134,7 +131,7 @@ Everyone is very welcome to contribute to this repository. Feel free to [raise i
134
131
 
135
132
  ## License
136
133
 
137
- Licensed under [MIT License](LICENSE). Copyright (c) 2017-2021 Luciano Mammino, will Farrell, and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
134
+ Licensed under [MIT License](LICENSE). Copyright (c) 2017-2022 Luciano Mammino, will Farrell, and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
138
135
 
139
136
  <a href="https://app.fossa.io/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy?ref=badge_large">
140
137
  <img src="https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy.svg?type=large" alt="FOSSA Status" style="max-width:100%;">
package/index.d.ts CHANGED
@@ -1,19 +1,8 @@
1
1
  import { SSM } from 'aws-sdk'
2
- import { captureAWSClient } from 'aws-xray-sdk'
2
+ import { Options as MiddyOptions } from '@middy/util'
3
3
  import middy from '@middy/core'
4
4
 
5
- interface Options<S = SSM> {
6
- AwsClient?: new() => S
7
- awsClientOptions?: Partial<SSM.Types.ClientConfiguration>
8
- awsClientAssumeRole?: string
9
- awsClientCapture?: typeof captureAWSClient
10
- fetchData?: { [key: string]: string }
11
- disablePrefetch?: boolean
12
- cacheKey?: string
13
- cacheExpiry?: number
14
- setToEnv?: boolean
15
- setToContext?: boolean
16
- }
5
+ interface Options<S = SSM> extends MiddyOptions<S, SSM.Types.ClientConfiguration> {}
17
6
 
18
7
  declare function ssm (options?: Options): middy.MiddlewareObj
19
8
 
package/index.js CHANGED
@@ -1,34 +1,15 @@
1
- "use strict";
2
-
3
- const {
4
- canPrefetch,
5
- createPrefetchClient,
6
- createClient,
7
- processCache,
8
- getCache,
9
- modifyCache,
10
- jsonSafeParse,
11
- getInternal,
12
- sanitizeKey
13
- } = require('@middy/util');
14
-
15
- const SSM = require('aws-sdk/clients/ssm'); // v2
16
- // const { SSM } = require('@aws-sdk/client-ssm') // v3
17
-
18
-
1
+ import { canPrefetch, createPrefetchClient, createClient, processCache, getCache, modifyCache, jsonSafeParse, getInternal, sanitizeKey } from '@middy/util';
2
+ import SSM from 'aws-sdk/clients/ssm.js';
19
3
  const awsRequestLimit = 10;
20
4
  const defaults = {
21
5
  AwsClient: SSM,
22
- // Allow for XRay
23
6
  awsClientOptions: {},
24
7
  awsClientAssumeRole: undefined,
25
8
  awsClientCapture: undefined,
26
9
  fetchData: {},
27
- // { contextKey: fetchKey, contextPrefix: fetchPath/ }
28
10
  disablePrefetch: false,
29
11
  cacheKey: 'ssm',
30
12
  cacheExpiry: -1,
31
- setToEnv: false,
32
13
  setToContext: false
33
14
  };
34
15
 
@@ -54,10 +35,9 @@ const ssmMiddleware = (opts = {}) => {
54
35
  for (const [idx, internalKey] of internalKeys.entries()) {
55
36
  if (cachedValues[internalKey]) continue;
56
37
  const fetchKey = options.fetchData[internalKey];
57
- if (fetchKey.substr(-1) === '/') continue; // Skip path passed in
58
-
38
+ if (fetchKey.substr(-1) === '/') continue;
59
39
  batchInternalKeys.push(internalKey);
60
- batchFetchKeys.push(fetchKey); // from the first to the batch size skip, unless it's the last entry
40
+ batchFetchKeys.push(fetchKey);
61
41
 
62
42
  if ((!idx || (idx + 1) % awsRequestLimit !== 0) && !(idx + 1 === internalKeys.length)) {
63
43
  continue;
@@ -66,28 +46,27 @@ const ssmMiddleware = (opts = {}) => {
66
46
  batchReq = client.getParameters({
67
47
  Names: batchFetchKeys,
68
48
  WithDecryption: true
69
- }).promise() // Required for aws-sdk v2
70
- .then(resp => {
71
- var _resp$InvalidParamete, _resp$Parameters;
72
-
73
- // Don't sanitize key, mapped to set value in options
74
- return Object.assign(...((_resp$InvalidParamete = resp.InvalidParameters) !== null && _resp$InvalidParamete !== void 0 ? _resp$InvalidParamete : []).map(fetchKey => {
49
+ }).promise().then(resp => {
50
+ return Object.assign(...(resp.InvalidParameters ?? []).map(fetchKey => {
75
51
  return {
76
52
  [fetchKey]: new Promise(() => {
77
- var _getCache$value, _getCache;
78
-
79
53
  const internalKey = internalKeys[fetchKeys.indexOf(fetchKey)];
80
- const value = (_getCache$value = (_getCache = getCache(options.cacheKey)) === null || _getCache === void 0 ? void 0 : _getCache.value) !== null && _getCache$value !== void 0 ? _getCache$value : {};
54
+ const value = getCache(options.cacheKey).value ?? {};
81
55
  value[internalKey] = undefined;
82
56
  modifyCache(options.cacheKey, value);
83
57
  throw new Error('ssm.InvalidParameter ' + fetchKey);
84
58
  })
85
59
  };
86
- }), ...((_resp$Parameters = resp.Parameters) !== null && _resp$Parameters !== void 0 ? _resp$Parameters : []).map(param => {
60
+ }), ...(resp.Parameters ?? []).map(param => {
87
61
  return {
88
62
  [param.Name]: parseValue(param)
89
63
  };
90
64
  }));
65
+ }).catch(e => {
66
+ const value = getCache(options.cacheKey).value ?? {};
67
+ value[internalKey] = undefined;
68
+ modifyCache(options.cacheKey, value);
69
+ throw e;
91
70
  });
92
71
 
93
72
  for (const internalKey of batchInternalKeys) {
@@ -110,12 +89,9 @@ const ssmMiddleware = (opts = {}) => {
110
89
  for (const internalKey in options.fetchData) {
111
90
  if (cachedValues[internalKey]) continue;
112
91
  const fetchKey = options.fetchData[internalKey];
113
- if (fetchKey.substr(-1) !== '/') continue; // Skip not path passed in
114
-
92
+ if (fetchKey.substr(-1) !== '/') continue;
115
93
  values[internalKey] = fetchPath(fetchKey).catch(e => {
116
- var _getCache$value2, _getCache2;
117
-
118
- const value = (_getCache$value2 = (_getCache2 = getCache(options.cacheKey)) === null || _getCache2 === void 0 ? void 0 : _getCache2.value) !== null && _getCache$value2 !== void 0 ? _getCache$value2 : {};
94
+ const value = getCache(options.cacheKey).value ?? {};
119
95
  value[internalKey] = undefined;
120
96
  modifyCache(options.cacheKey, value);
121
97
  throw e;
@@ -131,8 +107,7 @@ const ssmMiddleware = (opts = {}) => {
131
107
  NextToken: nextToken,
132
108
  Recursive: true,
133
109
  WithDecryption: true
134
- }).promise() // Required for aws-sdk v2
135
- .then(resp => {
110
+ }).promise().then(resp => {
136
111
  Object.assign(values, ...resp.Parameters.map(param => {
137
112
  return {
138
113
  [sanitizeKey(param.Name.replace(path, ''))]: parseValue(param)
@@ -159,21 +134,18 @@ const ssmMiddleware = (opts = {}) => {
159
134
  }
160
135
 
161
136
  const ssmMiddlewareBefore = async request => {
162
- var _prefetch;
163
-
164
137
  if (!client) {
165
138
  client = await createClient(options, request);
166
139
  }
167
140
 
168
141
  const {
169
142
  value
170
- } = (_prefetch = prefetch) !== null && _prefetch !== void 0 ? _prefetch : processCache(options, fetch, request);
143
+ } = prefetch ?? processCache(options, fetch, request);
171
144
  Object.assign(request.internal, value);
172
145
 
173
- if (options.setToContext || options.setToEnv) {
146
+ if (options.setToContext) {
174
147
  const data = await getInternal(Object.keys(options.fetchData), request);
175
- if (options.setToEnv) Object.assign(process.env, data);
176
- if (options.setToContext) Object.assign(request.context, data);
148
+ Object.assign(request.context, data);
177
149
  }
178
150
 
179
151
  prefetch = null;
@@ -184,4 +156,4 @@ const ssmMiddleware = (opts = {}) => {
184
156
  };
185
157
  };
186
158
 
187
- module.exports = ssmMiddleware;
159
+ export default ssmMiddleware;
package/package.json CHANGED
@@ -1,18 +1,19 @@
1
1
  {
2
2
  "name": "@middy/ssm",
3
- "version": "2.5.3",
3
+ "version": "3.0.0-alpha.2",
4
4
  "description": "SSM (EC2 Systems Manager) parameters middleware for the middy framework",
5
- "type": "commonjs",
5
+ "type": "module",
6
6
  "engines": {
7
- "node": ">=12"
7
+ "node": ">=14"
8
8
  },
9
9
  "engineStrict": true,
10
10
  "publishConfig": {
11
11
  "access": "public"
12
12
  },
13
- "main": "index.js",
13
+ "exports": "./index.js",
14
14
  "types": "index.d.ts",
15
15
  "files": [
16
+ "index.js",
16
17
  "index.d.ts"
17
18
  ],
18
19
  "scripts": {
@@ -46,12 +47,12 @@
46
47
  },
47
48
  "homepage": "https://github.com/middyjs/middy#readme",
48
49
  "dependencies": {
49
- "@middy/util": "^2.5.3"
50
+ "@middy/util": "^3.0.0-alpha.2"
50
51
  },
51
52
  "devDependencies": {
52
- "@middy/core": "^2.5.3",
53
+ "@middy/core": "^3.0.0-alpha.2",
53
54
  "aws-sdk": "^2.939.0",
54
55
  "aws-xray-sdk": "^3.3.3"
55
56
  },
56
- "gitHead": "690884d43b9cd632aeca9a5eba1612160b987cd4"
57
+ "gitHead": "de30419273ecbff08f367f47c7e320ec981cf145"
57
58
  }