Skip to content

Commit 289569b

Browse files
committed
feat: added decrypt option to get param
1 parent 5cf7815 commit 289569b

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed

Diff for: packages/parameters/src/BaseProvider.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { ExpirableValue } from './ExpirableValue';
55
import { TRANSFORM_METHOD_BINARY, TRANSFORM_METHOD_JSON } from './constants';
66
import { GetParameterError, TransformParameterError } from './Exceptions';
77
import type { BaseProviderInterface, GetMultipleOptionsInterface, GetOptionsInterface, TransformOptions } from './types';
8+
import { isSSMGetOptionsInterface } from './types/SSMProvider';
9+
import type { SSMGetOptionsInterface } from './types/SSMProvider';
810

911
abstract class BaseProvider implements BaseProviderInterface {
1012
protected store: Map<string, ExpirableValue>;
@@ -35,8 +37,9 @@ abstract class BaseProvider implements BaseProviderInterface {
3537
* this should be an acceptable tradeoff.
3638
*
3739
* @param {string} name - Parameter name
38-
* @param {GetOptionsInterface} options - Options to configure maximum age, trasformation, AWS SDK options, or force fetch
40+
* @param {GetOptionsInterface|SSMGetOptionsInterface} options - Options to configure maximum age, trasformation, AWS SDK options, or force fetch
3941
*/
42+
public async get(name: string, options?: SSMGetOptionsInterface): Promise<undefined | string | Record<string, unknown>>;
4043
public async get(name: string, options?: GetOptionsInterface): Promise<undefined | string | Record<string, unknown>> {
4144
const configs = new GetOptions(options);
4245
const key = [ name, configs.transform ].toString();
@@ -49,6 +52,11 @@ abstract class BaseProvider implements BaseProviderInterface {
4952

5053
let value;
5154
try {
55+
// Type assertion is needed because the SSM provider has a different signature for the get method
56+
if (options && isSSMGetOptionsInterface(options) && options.decrypt) {
57+
options.sdkOptions = options.sdkOptions || {};
58+
options.sdkOptions.WithDecryption = true;
59+
}
5260
value = await this._get(name, options?.sdkOptions);
5361
} catch (error) {
5462
throw new GetParameterError((error as Error).message);

Diff for: packages/parameters/src/types/SSMProvider.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type { GetParameterCommandInput } from '@aws-sdk/client-ssm';
2+
import type { TransformOptions } from 'types/BaseProvider';
3+
4+
/**
5+
* Options for the SSMProvider get method.
6+
*
7+
* @interface SSMGetOptionsInterface
8+
* @extends {GetOptionsInterface}
9+
* @property {boolean} decrypt - If true, the parameter will be decrypted.
10+
* @property {Partial<GetParameterCommandInput>} sdkOptions - Options for the AWS SDK.
11+
*/
12+
interface SSMGetOptionsInterface {
13+
maxAge?: number
14+
sdkOptions?: Partial<GetParameterCommandInput>
15+
forceFetch?: boolean
16+
decrypt?: boolean
17+
transform?: TransformOptions
18+
}
19+
20+
const isSSMGetOptionsInterface = (options: unknown): options is SSMGetOptionsInterface => (options as SSMGetOptionsInterface).decrypt !== undefined;
21+
22+
export {
23+
SSMGetOptionsInterface,
24+
isSSMGetOptionsInterface,
25+
};

Diff for: packages/parameters/tests/unit/BaseProvider.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ describe('Class: BaseProvider', () => {
111111

112112
});
113113

114-
test('when called and values cached are expired, it returns the remote values', async () => {
114+
test('when called and cached value is expired, it returns the remote value', async () => {
115115

116116
// Prepare
117117
const mockData = 'my-remote-value';

Diff for: packages/parameters/tests/unit/SSMProvider.test.ts

+18
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,24 @@ describe('Class: SSMProvider', () => {
5757

5858
});
5959

60+
test('when called with the decrypt option, the WithDecryption parameter is passed to the sdk client', async () => {
61+
62+
// Prepare
63+
const provider = new SSMProvider();
64+
const client = mockClient(SSMClient).on(GetParameterCommand).resolves({});
65+
const parameterName = 'foo';
66+
67+
// Act
68+
provider.get(parameterName, { decrypt: true });
69+
70+
// Assess
71+
expect(client).toReceiveCommandWith(GetParameterCommand, {
72+
Name: parameterName,
73+
WithDecryption: true,
74+
});
75+
76+
});
77+
6078
});
6179

6280
describe('Method: _getMultiple', () => {

0 commit comments

Comments
 (0)