-
Notifications
You must be signed in to change notification settings - Fork 559
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(config-resolver): get signingRegion from regionRegex if not prese…
…nt for FIPS (#2936)
- Loading branch information
Showing
8 changed files
with
299 additions
and
156 deletions.
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
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
80 changes: 80 additions & 0 deletions
80
packages/config-resolver/src/regionInfo/getResolvedSigningRegion.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,80 @@ | ||
import { getResolvedSigningRegion } from "./getResolvedSigningRegion"; | ||
import { isFipsRegion } from "./isFipsRegion"; | ||
|
||
jest.mock("./isFipsRegion"); | ||
|
||
describe(getResolvedSigningRegion.name, () => { | ||
const mockRegion = "mockRegion"; | ||
const mockSigningRegion = "mockSigningRegion"; | ||
const mockHostname = "mockHostname"; | ||
const mockRegionRegex = "mockRegionRegex"; | ||
|
||
const mockOptions = { | ||
hostname: mockHostname, | ||
regionRegex: mockRegionRegex, | ||
}; | ||
|
||
beforeEach(() => { | ||
(isFipsRegion as jest.Mock).mockReturnValue(false); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("returns signingRegion if passed in options", () => { | ||
expect(getResolvedSigningRegion(mockRegion, { ...mockOptions, signingRegion: mockSigningRegion })).toEqual( | ||
mockSigningRegion | ||
); | ||
expect(isFipsRegion).not.toHaveBeenCalled(); | ||
}); | ||
|
||
describe("returns undefined if signingRegion is not present and", () => { | ||
it("region is not FIPS", () => { | ||
expect(getResolvedSigningRegion(mockRegion, mockOptions)).not.toBeDefined(); | ||
expect(isFipsRegion).toHaveBeenCalledTimes(1); | ||
expect(isFipsRegion).toHaveBeenCalledWith(mockRegion); | ||
}); | ||
|
||
it("regionRegex does not return a match in hostname", () => { | ||
(isFipsRegion as jest.Mock).mockReturnValueOnce(true); | ||
const matchSpy = jest.spyOn(String.prototype, "match").mockReturnValueOnce(null); | ||
|
||
expect(getResolvedSigningRegion(mockRegion, mockOptions)).not.toBeDefined(); | ||
expect(matchSpy).toHaveBeenCalledTimes(1); | ||
expect(matchSpy).toHaveBeenCalledWith(mockRegionRegex); | ||
expect(isFipsRegion).toHaveBeenCalledTimes(1); | ||
expect(isFipsRegion).toHaveBeenCalledWith(mockRegion); | ||
}); | ||
|
||
it("region is not present between dots in a hostname", () => { | ||
const regionInHostname = "us-east-1"; | ||
(isFipsRegion as jest.Mock).mockReturnValueOnce(true); | ||
|
||
expect( | ||
getResolvedSigningRegion(mockRegion, { | ||
...mockOptions, | ||
hostname: `test-${regionInHostname}.amazonaws.com`, | ||
regionRegex: "^(us|eu|ap|sa|ca|me|af)\\-\\w+\\-\\d+$", | ||
}) | ||
).not.toBeDefined(); | ||
expect(isFipsRegion).toHaveBeenCalledTimes(1); | ||
expect(isFipsRegion).toHaveBeenCalledWith(mockRegion); | ||
}); | ||
}); | ||
|
||
it("returns region from hostname if signingRegion is not present", () => { | ||
const regionInHostname = "us-east-1"; | ||
(isFipsRegion as jest.Mock).mockReturnValueOnce(true); | ||
|
||
expect( | ||
getResolvedSigningRegion(mockRegion, { | ||
...mockOptions, | ||
hostname: `test.${regionInHostname}.amazonaws.com`, | ||
regionRegex: "^(us|eu|ap|sa|ca|me|af)\\-\\w+\\-\\d+$", | ||
}) | ||
).toEqual(regionInHostname); | ||
expect(isFipsRegion).toHaveBeenCalledTimes(1); | ||
expect(isFipsRegion).toHaveBeenCalledWith(mockRegion); | ||
}); | ||
}); |
22 changes: 22 additions & 0 deletions
22
packages/config-resolver/src/regionInfo/getResolvedSigningRegion.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,22 @@ | ||
import { isFipsRegion } from "./isFipsRegion"; | ||
|
||
export interface GetResolvedSigningRegionOptions { | ||
hostname: string; | ||
regionRegex: string; | ||
signingRegion?: string; | ||
} | ||
|
||
export const getResolvedSigningRegion = ( | ||
region: string, | ||
{ hostname, signingRegion, regionRegex }: GetResolvedSigningRegionOptions | ||
) => { | ||
if (signingRegion) { | ||
return signingRegion; | ||
} else if (isFipsRegion(region)) { | ||
const regionRegexJs = regionRegex.replace("\\\\", "\\").replace(/^\^/g, "\\.").replace(/\$$/g, "\\."); | ||
const regionRegexmatchArray = hostname.match(regionRegexJs); | ||
if (regionRegexmatchArray) { | ||
return regionRegexmatchArray[0].slice(1, -1); | ||
} | ||
} | ||
}; |
15 changes: 15 additions & 0 deletions
15
packages/config-resolver/src/regionInfo/isFipsRegion.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,15 @@ | ||
import { isFipsRegion } from "./isFipsRegion"; | ||
|
||
describe(isFipsRegion.name, () => { | ||
it.each([ | ||
[true, "fips-us-east-1"], | ||
[true, "us-east-1-fips"], | ||
[false, "us-east-1"], | ||
])(`returns %s for region "%s"`, (output, input) => { | ||
expect(isFipsRegion(input)).toEqual(output); | ||
}); | ||
|
||
it.each([undefined, null])("returns false for %s", (input) => { | ||
expect(isFipsRegion(input)).toEqual(false); | ||
}); | ||
}); |
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,2 @@ | ||
export const isFipsRegion = (region: string) => | ||
typeof region === "string" && (region.startsWith("fips-") || region.endsWith("-fips")); |
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
Oops, something went wrong.