-
Notifications
You must be signed in to change notification settings - Fork 576
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
chore(middleware-flexible-checksums): add RequestChecksumCalculation and ResponseChecksumValidation #6465
Merged
Merged
chore(middleware-flexible-checksums): add RequestChecksumCalculation and ResponseChecksumValidation #6465
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
853d833
chore(middleware-flexible-checksums): add RequestChecksumCalculation …
trivikr 5156e60
chore: use discriminated unions over string enums
trivikr b6b802a
chore: add utility stringUnionSelector
trivikr c218146
chore: add Node Config Options
trivikr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...ges/middleware-flexible-checksums/src/NODE_REQUEST_CHECKSUM_CALCULATION_CONFIG_OPTIONS.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { LoadedConfigSelectors } from "@smithy/node-config-provider"; | ||
|
||
import { RequestChecksumCalculation } from "./constants"; | ||
import { SelectorType, stringUnionSelector } from "./stringUnionSelector"; | ||
|
||
export const ENV_REQUEST_CHECKSUM_CALCULATION = "AWS_REQUEST_CHECKSUM_CALCULATION"; | ||
export const CONFIG_REQUEST_CHECKSUM_CALCULATION = "request_checksum_calculation"; | ||
export const DEFAULT_REQUEST_CHECKSUM_CALCULATION = RequestChecksumCalculation.WHEN_SUPPORTED; | ||
|
||
export const NODE_REQUEST_CHECKSUM_CALCULATION_CONFIG_OPTIONS: LoadedConfigSelectors<string> = { | ||
environmentVariableSelector: (env) => | ||
stringUnionSelector(env, ENV_REQUEST_CHECKSUM_CALCULATION, RequestChecksumCalculation, SelectorType.ENV), | ||
configFileSelector: (profile) => | ||
stringUnionSelector(profile, CONFIG_REQUEST_CHECKSUM_CALCULATION, RequestChecksumCalculation, SelectorType.CONFIG), | ||
default: DEFAULT_REQUEST_CHECKSUM_CALCULATION, | ||
}; |
16 changes: 16 additions & 0 deletions
16
...ges/middleware-flexible-checksums/src/NODE_RESPONSE_CHECKSUM_VALIDATION_CONFIG_OPTIONS.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { LoadedConfigSelectors } from "@smithy/node-config-provider"; | ||
|
||
import { RequestChecksumCalculation } from "./constants"; | ||
import { SelectorType, stringUnionSelector } from "./stringUnionSelector"; | ||
|
||
export const ENV_RESPONSE_CHECKSUM_VALIDATION = "AWS_RESPONSE_CHECKSUM_VALIDATION"; | ||
export const CONFIG_RESPONSE_CHECKSUM_VALIDATION = "response_checksum_validation"; | ||
export const DEFAULT_RESPONSE_CHECKSUM_VALIDATION = RequestChecksumCalculation.WHEN_SUPPORTED; | ||
|
||
export const NODE_RESPONSE_CHECKSUM_VALIDATION_CONFIG_OPTIONS: LoadedConfigSelectors<string> = { | ||
environmentVariableSelector: (env) => | ||
stringUnionSelector(env, ENV_RESPONSE_CHECKSUM_VALIDATION, RequestChecksumCalculation, SelectorType.ENV), | ||
configFileSelector: (profile) => | ||
stringUnionSelector(profile, CONFIG_RESPONSE_CHECKSUM_VALIDATION, RequestChecksumCalculation, SelectorType.CONFIG), | ||
default: DEFAULT_RESPONSE_CHECKSUM_VALIDATION, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export * from "./NODE_REQUEST_CHECKSUM_CALCULATION_CONFIG_OPTIONS"; | ||
export * from "./NODE_RESPONSE_CHECKSUM_VALIDATION_CONFIG_OPTIONS"; | ||
export * from "./constants"; | ||
export * from "./flexibleChecksumsMiddleware"; | ||
export * from "./getFlexibleChecksumsPlugin"; |
40 changes: 40 additions & 0 deletions
40
packages/middleware-flexible-checksums/src/stringUnionSelector.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { SelectorType, stringUnionSelector } from "./stringUnionSelector"; | ||
|
||
describe(stringUnionSelector.name, () => { | ||
const key = "key"; | ||
const value = "VALUE"; | ||
const obj: { [key]: any } = {} as any; | ||
const union = { [key]: value }; | ||
|
||
describe.each(Object.entries(SelectorType))(`Selector %s`, (selectorKey, selectorValue) => { | ||
beforeEach(() => { | ||
delete obj[key]; | ||
}); | ||
|
||
it(`should return undefined if ${key} is not defined`, () => { | ||
// @ts-expect-error Element implicitly has an 'any' type | ||
expect(stringUnionSelector(obj, key, union, SelectorType[selectorKey])).toBeUndefined(); | ||
}); | ||
|
||
it.each([ | ||
[value, value], | ||
[value, value.toLowerCase()], | ||
[value, [...value].map((c, i) => (i % 2 === 0 ? c.toLowerCase() : c.toUpperCase())).join("")], | ||
])(`should return number %s if ${key}="%s"`, (output, input) => { | ||
obj[key] = input; | ||
// @ts-expect-error Element implicitly has an 'any' type | ||
expect(stringUnionSelector(obj, key, union, SelectorType[selectorKey])).toBe(output); | ||
}); | ||
|
||
// Thows if the value is something other than different case. | ||
it.each(["value1", "1value", [...value].reverse().join("")])(`should throw if ${key}=%s`, (input) => { | ||
obj[key] = input; | ||
// @ts-expect-error Element implicitly has an 'any' type | ||
expect(() => stringUnionSelector(obj, key, union, SelectorType[selectorKey])).toThrow( | ||
new TypeError( | ||
`Cannot load ${selectorValue} '${key}'. Expected one of ${Object.values(union)}, got '${obj[key]}'.` | ||
) | ||
); | ||
}); | ||
}); | ||
}); |
27 changes: 27 additions & 0 deletions
27
packages/middleware-flexible-checksums/src/stringUnionSelector.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
export enum SelectorType { | ||
ENV = "env", | ||
CONFIG = "shared config entry", | ||
} | ||
|
||
/** | ||
* Returns undefined, if obj[key] is not defined. | ||
* Returns string value, if the string is defined in obj[key] and it's uppercase matches union value. | ||
* Throws error for all other cases. | ||
* | ||
* @internal | ||
*/ | ||
export const stringUnionSelector = ( | ||
obj: Record<string, string | undefined>, | ||
key: string, | ||
union: Record<string, string>, | ||
type: SelectorType | ||
) => { | ||
if (!(key in obj)) return undefined; | ||
|
||
const value = obj[key]!.toUpperCase(); | ||
if (!Object.values(union).includes(value)) { | ||
throw new TypeError(`Cannot load ${type} '${key}'. Expected one of ${Object.values(union)}, got '${obj[key]}'.`); | ||
} | ||
|
||
return value; | ||
}; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it looks like this could/should be reused for other enum specifications like AccountIdEndpointMode
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Their validator does not check for casing, but flexible checksums one needs to.
aws-sdk-js-v3/packages/core/src/submodules/account-id-endpoint/AccountIdEndpointModeConstants.ts
Lines 10 to 12 in 6a539fe