-
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.
feat(config-resolver): use real region in configuration (#2986)
- Loading branch information
Showing
17 changed files
with
166 additions
and
4,280 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
36 changes: 36 additions & 0 deletions
36
packages/config-resolver/src/regionConfig/getRealRegion.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,36 @@ | ||
import { getRealRegion } from "./getRealRegion"; | ||
import { isFipsRegion } from "./isFipsRegion"; | ||
|
||
jest.mock("./isFipsRegion"); | ||
|
||
describe(getRealRegion.name, () => { | ||
beforeEach(() => { | ||
(isFipsRegion as jest.Mock).mockReturnValue(true); | ||
}); | ||
|
||
afterEach(() => { | ||
expect(isFipsRegion).toHaveBeenCalledTimes(1); | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("returns provided region if it's not FIPS", () => { | ||
const mockRegion = "mockRegion"; | ||
(isFipsRegion as jest.Mock).mockReturnValue(false); | ||
expect(getRealRegion(mockRegion)).toStrictEqual(mockRegion); | ||
}); | ||
|
||
describe("FIPS regions", () => { | ||
it.each(["fips-aws-global", "aws-fips"])(`returns "us-east-1" for "%s"`, (input) => { | ||
expect(getRealRegion(input)).toStrictEqual("us-east-1"); | ||
}); | ||
|
||
it.each([ | ||
["us-west-1", "us-west-1-fips"], | ||
["us-west-1", "fips-us-west-1"], | ||
["us-west-1", "fips-dkr-us-west-1"], | ||
["us-west-1", "fips-prod-us-west-1"], | ||
])(`returns "%s" for "%s"`, (output, input) => { | ||
expect(getRealRegion(input)).toStrictEqual(output); | ||
}); | ||
}); | ||
}); |
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,8 @@ | ||
import { isFipsRegion } from "./isFipsRegion"; | ||
|
||
export const getRealRegion = (region: string) => | ||
isFipsRegion(region) | ||
? ["fips-aws-global", "aws-fips"].includes(region) | ||
? "us-east-1" | ||
: region.replace(/fips-(dkr-|prod-)?|-fips/, "") | ||
: region; |
File renamed without changes.
File renamed without changes.
15 changes: 0 additions & 15 deletions
15
packages/config-resolver/src/regionConfig/normalizeRegion.spec.ts
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
81 changes: 69 additions & 12 deletions
81
packages/config-resolver/src/regionConfig/resolveRegionConfig.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 |
---|---|---|
@@ -1,28 +1,85 @@ | ||
import { normalizeRegion } from "./normalizeRegion"; | ||
import { getRealRegion } from "./getRealRegion"; | ||
import { isFipsRegion } from "./isFipsRegion"; | ||
import { resolveRegionConfig } from "./resolveRegionConfig"; | ||
|
||
jest.mock("./normalizeRegion"); | ||
jest.mock("./getRealRegion"); | ||
jest.mock("./isFipsRegion"); | ||
|
||
describe("RegionConfig", () => { | ||
const mockRegionProvider = () => Promise.resolve("mockRegion"); | ||
const mockRegion = "mockRegion"; | ||
const mockRealRegion = "mockRealRegion"; | ||
const mockUseFipsEndpoint = () => Promise.resolve(false); | ||
|
||
beforeEach(() => { | ||
(normalizeRegion as jest.Mock).mockReturnValue(mockRegionProvider); | ||
(getRealRegion as jest.Mock).mockReturnValue(mockRealRegion); | ||
(isFipsRegion as jest.Mock).mockReturnValue(false); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("assigns value returned by normalizeRegion to region", async () => { | ||
const region = "mockRegion"; | ||
expect(resolveRegionConfig({ region }).region).toBe(mockRegionProvider); | ||
expect(normalizeRegion).toHaveBeenCalledTimes(1); | ||
expect(normalizeRegion).toHaveBeenCalledWith(region); | ||
describe("region", () => { | ||
it("return normalized value with real region if passed as a string", async () => { | ||
const resolvedRegionConfig = resolveRegionConfig({ region: mockRegion, useFipsEndpoint: mockUseFipsEndpoint }); | ||
const resolvedRegion = await resolvedRegionConfig.region(); | ||
expect(resolvedRegion).toBe(mockRealRegion); | ||
expect(getRealRegion).toHaveBeenCalledTimes(1); | ||
expect(getRealRegion).toHaveBeenCalledWith(mockRegion); | ||
}); | ||
|
||
it("return provider with real region if passed as a Provider", async () => { | ||
const resolvedRegionConfig = resolveRegionConfig({ | ||
region: () => Promise.resolve(mockRegion), | ||
useFipsEndpoint: mockUseFipsEndpoint, | ||
}); | ||
const resolvedRegion = await resolvedRegionConfig.region(); | ||
expect(resolvedRegion).toBe(mockRealRegion); | ||
expect(getRealRegion).toHaveBeenCalledTimes(1); | ||
expect(getRealRegion).toHaveBeenCalledWith(mockRegion); | ||
}); | ||
|
||
it("throw if region is not supplied", () => { | ||
expect(() => resolveRegionConfig({ useFipsEndpoint: mockUseFipsEndpoint })).toThrow(); | ||
}); | ||
}); | ||
|
||
it("throw if region is not supplied", () => { | ||
expect(() => resolveRegionConfig({})).toThrow(); | ||
expect(normalizeRegion).not.toHaveBeenCalled(); | ||
describe("useFipsEndpoint", () => { | ||
let mockRegionProvider; | ||
let mockUseFipsEndpoint; | ||
|
||
beforeEach(() => { | ||
mockRegionProvider = jest.fn().mockResolvedValueOnce(Promise.resolve(mockRegion)); | ||
mockUseFipsEndpoint = jest.fn().mockResolvedValueOnce(Promise.resolve(false)); | ||
}); | ||
|
||
afterEach(() => { | ||
expect(isFipsRegion).toHaveBeenCalledTimes(1); | ||
expect(isFipsRegion).toHaveBeenCalledWith(mockRegion); | ||
expect(mockRegionProvider).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it("returns Provider which returns true for FIPS endpoints", async () => { | ||
(isFipsRegion as jest.Mock).mockReturnValue(true); | ||
const resolvedRegionConfig = resolveRegionConfig({ | ||
region: mockRegionProvider, | ||
useFipsEndpoint: mockUseFipsEndpoint, | ||
}); | ||
|
||
const useFipsEndpoint = await resolvedRegionConfig.useFipsEndpoint(); | ||
expect(useFipsEndpoint).toStrictEqual(true); | ||
expect(mockUseFipsEndpoint).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("returns passed Provider if endpoint is not FIPS", async () => { | ||
const resolvedRegionConfig = resolveRegionConfig({ | ||
region: mockRegionProvider, | ||
useFipsEndpoint: mockUseFipsEndpoint, | ||
}); | ||
|
||
const useFipsEndpoint = await resolvedRegionConfig.useFipsEndpoint(); | ||
expect(useFipsEndpoint).toStrictEqual(false); | ||
expect(mockUseFipsEndpoint).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
}); |
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
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.