Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(parameters): Tokenize attribute names in DynamoDBProvider #1239

Merged
merged 4 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/utilities/parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Depending on the provider you want to use, install the library and the correspon

=== "DynamoDBProvider"
```bash
npm install @aws-lambda-powertools/parameters @aws-sdk/client-dynamodb
npm install @aws-lambda-powertools/parameters @aws-sdk/client-dynamodb @aws-sdk/util-dynamodb
```

???+ tip
Expand Down
30 changes: 26 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/parameters/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@
"aws-sdk-client-mock-jest": "^2.0.1"
},
"dependencies": {
"@aws-sdk/util-base64": "^3.208.0"
"@aws-sdk/util-base64-node": "^3.209.0"
}
}
2 changes: 1 addition & 1 deletion packages/parameters/src/BaseProvider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fromBase64 } from '@aws-sdk/util-base64';
import { fromBase64 } from '@aws-sdk/util-base64-node';
import { GetOptions } from './GetOptions';
import { GetMultipleOptions } from './GetMultipleOptions';
import { ExpirableValue } from './ExpirableValue';
Expand Down
14 changes: 11 additions & 3 deletions packages/parameters/src/dynamodb/DynamoDBProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ class DynamoDBProvider extends BaseProvider {
...(options?.sdkOptions || {}),
TableName: this.tableName,
Key: marshall({ [this.keyAttr]: name }),
ProjectionExpression: this.valueAttr,
ProjectionExpression: '#value',
ExpressionAttributeNames: {
'#value': this.valueAttr,
}
};
const result = await this.client.send(new GetItemCommand(sdkOptions));

Expand All @@ -63,9 +66,14 @@ class DynamoDBProvider extends BaseProvider {
const sdkOptions: QueryCommandInput = {
...(options?.sdkOptions || {}),
TableName: this.tableName,
KeyConditionExpression: `${this.keyAttr} = :key`,
KeyConditionExpression: '#key = :key',
ExpressionAttributeValues: marshall({ ':key': path }),
ProjectionExpression: `${this.sortAttr}, ${this.valueAttr}`,
ExpressionAttributeNames: {
'#key': this.keyAttr,
'#sk': this.sortAttr,
'#value': this.valueAttr,
},
ProjectionExpression: '#sk, #value',
};
const paginationOptions: PaginationConfiguration = {
client: this.client,
Expand Down
2 changes: 1 addition & 1 deletion packages/parameters/tests/unit/BaseProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
GetParameterError,
TransformParameterError
} from '../../src';
import { toBase64 } from '@aws-sdk/util-base64';
import { toBase64 } from '@aws-sdk/util-base64-node';

const encoder = new TextEncoder();

Expand Down
56 changes: 44 additions & 12 deletions packages/parameters/tests/unit/DynamoDBProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ describe('Class: DynamoDBProvider', () => {
Key: marshall({
id: parameterName,
}),
ProjectionExpression: 'value',
ExpressionAttributeNames: {
'#value': 'value',
},
ProjectionExpression: '#value',
});
expect(parameter).toEqual(parameterValue);

Expand Down Expand Up @@ -91,7 +94,10 @@ describe('Class: DynamoDBProvider', () => {
Key: marshall({
key: parameterName,
}),
ProjectionExpression: 'val',
ExpressionAttributeNames: {
'#value': 'val',
},
ProjectionExpression: '#value',
});
expect(parameter).toEqual(parameterValue);

Expand Down Expand Up @@ -125,7 +131,10 @@ describe('Class: DynamoDBProvider', () => {
Key: marshall({
id: parameterName,
}),
ProjectionExpression: 'value',
ExpressionAttributeNames: {
'#value': 'value',
},
ProjectionExpression: '#value',
ConsistentRead: true,
});
expect(parameter).toEqual(parameterValue);
Expand Down Expand Up @@ -164,7 +173,10 @@ describe('Class: DynamoDBProvider', () => {
Key: marshall({
id: parameterName,
}),
ProjectionExpression: 'value',
ExpressionAttributeNames: {
'#value': 'value',
},
ProjectionExpression: '#value',
});

});
Expand Down Expand Up @@ -206,11 +218,16 @@ describe('Class: DynamoDBProvider', () => {
// Assess
expect(client).toReceiveCommandWith(QueryCommand, {
TableName: 'test-table',
KeyConditionExpression: `id = :key`,
KeyConditionExpression: `#key = :key`,
ExpressionAttributeValues: marshall({
':key': parameterPath,
}),
ProjectionExpression: 'sk, value',
ExpressionAttributeNames: {
'#key': 'id',
'#sk': 'sk',
'#value': 'value'
},
ProjectionExpression: '#sk, #value',
});
expect(parameters).toEqual({
a: 'parameter-a',
Expand Down Expand Up @@ -256,11 +273,16 @@ describe('Class: DynamoDBProvider', () => {
// Assess
expect(client).toReceiveCommandWith(QueryCommand, {
TableName: 'test-table',
KeyConditionExpression: `key = :key`,
KeyConditionExpression: `#key = :key`,
ExpressionAttributeValues: marshall({
':key': parameterPath,
}),
ProjectionExpression: 'sort, val',
ExpressionAttributeNames: {
'#key': 'key',
'#sk': 'sort',
'#value': 'val'
},
ProjectionExpression: '#sk, #value',
});
expect(parameters).toEqual({
a: 'parameter-a',
Expand Down Expand Up @@ -308,11 +330,16 @@ describe('Class: DynamoDBProvider', () => {
// Assess
expect(client).toReceiveCommandWith(QueryCommand, {
TableName: 'test-table',
KeyConditionExpression: `id = :key`,
KeyConditionExpression: `#key = :key`,
ExpressionAttributeValues: marshall({
':key': parameterPath,
}),
ProjectionExpression: 'sk, value',
ExpressionAttributeNames: {
'#key': 'id',
'#sk': 'sk',
'#value': 'value'
},
ProjectionExpression: '#sk, #value',
ConsistentRead: true,
});
expect(parameters).toEqual({
Expand Down Expand Up @@ -419,11 +446,16 @@ describe('Class: DynamoDBProvider', () => {
// Assess
expect(client).toReceiveCommandWith(QueryCommand, {
TableName: 'test-table',
KeyConditionExpression: `id = :key`,
KeyConditionExpression: `#key = :key`,
ExpressionAttributeValues: marshall({
':key': parameterPath,
}),
ProjectionExpression: 'sk, value',
ExpressionAttributeNames: {
'#key': 'id',
'#sk': 'sk',
'#value': 'value'
},
ProjectionExpression: '#sk, #value',
ConsistentRead: true,
Limit: 10,
});
Expand Down