diff --git a/packages/middleware-bucket-endpoint/package.json b/packages/middleware-bucket-endpoint/package.json index bab71cc0958a..1c008662afca 100644 --- a/packages/middleware-bucket-endpoint/package.json +++ b/packages/middleware-bucket-endpoint/package.json @@ -19,13 +19,13 @@ "license": "Apache-2.0", "dependencies": { "@aws-sdk/protocol-http": "3.38.0", + "@aws-sdk/node-config-provider": "3.39.0", "@aws-sdk/types": "3.38.0", "@aws-sdk/util-arn-parser": "3.37.0", "tslib": "^2.3.0" }, "devDependencies": { "@aws-sdk/middleware-stack": "3.38.0", - "@aws-sdk/node-config-provider": "3.39.0", "@types/jest": "^26.0.4", "jest": "^26.1.0", "typescript": "~4.3.5" diff --git a/packages/middleware-bucket-endpoint/src/NodeDisableMultiregionAccessPointConfigOptions.spec.ts b/packages/middleware-bucket-endpoint/src/NodeDisableMultiregionAccessPointConfigOptions.spec.ts new file mode 100644 index 000000000000..de09c7466fa5 --- /dev/null +++ b/packages/middleware-bucket-endpoint/src/NodeDisableMultiregionAccessPointConfigOptions.spec.ts @@ -0,0 +1,50 @@ +import { booleanSelector, SelectorType } from "@aws-sdk/node-config-provider"; + +import { + NODE_DISABLE_MULTIREGION_ACCESS_POINT_CONFIG_OPTIONS, + NODE_DISABLE_MULTIREGION_ACCESS_POINT_ENV_NAME, + NODE_DISABLE_MULTIREGION_ACCESS_POINT_INI_NAME, +} from "./NodeDisableMultiregionAccessPointConfigOptions"; + +jest.mock("@aws-sdk/node-config-provider"); + +describe("NODE_USE_ARN_REGION_CONFIG_OPTIONS", () => { + afterEach(() => { + jest.clearAllMocks(); + }); + + const test = (func: Function, obj: { [key: string]: string }, key: string, type: SelectorType) => { + it.each([true, false, undefined])("returns %s", (output) => { + (booleanSelector as jest.Mock).mockReturnValueOnce(output); + expect(func(obj)).toEqual(output); + expect(booleanSelector).toBeCalledWith(obj, key, type); + }); + + it("throws error", () => { + const mockError = new Error("error"); + (booleanSelector as jest.Mock).mockImplementationOnce(() => { + throw mockError; + }); + expect(() => { + func(obj); + }).toThrow(mockError); + }); + }; + + describe("calls booleanSelector for environmentVariableSelector", () => { + const env: { [NODE_DISABLE_MULTIREGION_ACCESS_POINT_ENV_NAME]: any } = {} as any; + const { environmentVariableSelector } = NODE_DISABLE_MULTIREGION_ACCESS_POINT_CONFIG_OPTIONS; + test(environmentVariableSelector, env, NODE_DISABLE_MULTIREGION_ACCESS_POINT_ENV_NAME, SelectorType.ENV); + }); + + describe("calls booleanSelector for configFileSelector", () => { + const profileContent: { [NODE_DISABLE_MULTIREGION_ACCESS_POINT_INI_NAME]: any } = {} as any; + const { configFileSelector } = NODE_DISABLE_MULTIREGION_ACCESS_POINT_CONFIG_OPTIONS; + test(configFileSelector, profileContent, NODE_DISABLE_MULTIREGION_ACCESS_POINT_INI_NAME, SelectorType.CONFIG); + }); + + it("returns false for default", () => { + const { default: defaultValue } = NODE_DISABLE_MULTIREGION_ACCESS_POINT_CONFIG_OPTIONS; + expect(defaultValue).toEqual(false); + }); +}); diff --git a/packages/middleware-bucket-endpoint/src/NodeDisableMultiregionAccessPointConfigOptions.ts b/packages/middleware-bucket-endpoint/src/NodeDisableMultiregionAccessPointConfigOptions.ts new file mode 100644 index 000000000000..6ed44c617439 --- /dev/null +++ b/packages/middleware-bucket-endpoint/src/NodeDisableMultiregionAccessPointConfigOptions.ts @@ -0,0 +1,13 @@ +import { LoadedConfigSelectors } from "@aws-sdk/node-config-provider"; +import { booleanSelector, SelectorType } from "@aws-sdk/node-config-provider"; + +export const NODE_DISABLE_MULTIREGION_ACCESS_POINT_ENV_NAME = "AWS_S3_DISABLE_MULTIREGION_ACCESS_POINTS"; +export const NODE_DISABLE_MULTIREGION_ACCESS_POINT_INI_NAME = "s3_disable_multiregion_access_points"; + +export const NODE_DISABLE_MULTIREGION_ACCESS_POINT_CONFIG_OPTIONS: LoadedConfigSelectors = { + environmentVariableSelector: (env: NodeJS.ProcessEnv) => + booleanSelector(env, NODE_DISABLE_MULTIREGION_ACCESS_POINT_ENV_NAME, SelectorType.ENV), + configFileSelector: (profile) => + booleanSelector(profile, NODE_DISABLE_MULTIREGION_ACCESS_POINT_INI_NAME, SelectorType.CONFIG), + default: false, +}; diff --git a/packages/middleware-bucket-endpoint/src/NodeUseArnRegionConfigOptions.spec.ts b/packages/middleware-bucket-endpoint/src/NodeUseArnRegionConfigOptions.spec.ts new file mode 100644 index 000000000000..cc70ed5aa300 --- /dev/null +++ b/packages/middleware-bucket-endpoint/src/NodeUseArnRegionConfigOptions.spec.ts @@ -0,0 +1,50 @@ +import { booleanSelector, SelectorType } from "@aws-sdk/node-config-provider"; + +import { + NODE_USE_ARN_REGION_CONFIG_OPTIONS, + NODE_USE_ARN_REGION_ENV_NAME, + NODE_USE_ARN_REGION_INI_NAME, +} from "./NodeUseArnRegionConfigOptions"; + +jest.mock("@aws-sdk/node-config-provider"); + +describe("NODE_USE_ARN_REGION_CONFIG_OPTIONS", () => { + afterEach(() => { + jest.clearAllMocks(); + }); + + const test = (func: Function, obj: { [key: string]: string }, key: string, type: SelectorType) => { + it.each([true, false, undefined])("returns %s", (output) => { + (booleanSelector as jest.Mock).mockReturnValueOnce(output); + expect(func(obj)).toEqual(output); + expect(booleanSelector).toBeCalledWith(obj, key, type); + }); + + it("throws error", () => { + const mockError = new Error("error"); + (booleanSelector as jest.Mock).mockImplementationOnce(() => { + throw mockError; + }); + expect(() => { + func(obj); + }).toThrow(mockError); + }); + }; + + describe("calls booleanSelector for environmentVariableSelector", () => { + const env: { [NODE_USE_ARN_REGION_ENV_NAME]: any } = {} as any; + const { environmentVariableSelector } = NODE_USE_ARN_REGION_CONFIG_OPTIONS; + test(environmentVariableSelector, env, NODE_USE_ARN_REGION_ENV_NAME, SelectorType.ENV); + }); + + describe("calls booleanSelector for configFileSelector", () => { + const profileContent: { [NODE_USE_ARN_REGION_INI_NAME]: any } = {} as any; + const { configFileSelector } = NODE_USE_ARN_REGION_CONFIG_OPTIONS; + test(configFileSelector, profileContent, NODE_USE_ARN_REGION_INI_NAME, SelectorType.CONFIG); + }); + + it("returns false for default", () => { + const { default: defaultValue } = NODE_USE_ARN_REGION_CONFIG_OPTIONS; + expect(defaultValue).toEqual(false); + }); +}); diff --git a/packages/middleware-bucket-endpoint/src/NodeUseArnRegionConfigOptions.ts b/packages/middleware-bucket-endpoint/src/NodeUseArnRegionConfigOptions.ts new file mode 100644 index 000000000000..cfa9d466c52a --- /dev/null +++ b/packages/middleware-bucket-endpoint/src/NodeUseArnRegionConfigOptions.ts @@ -0,0 +1,17 @@ +import { LoadedConfigSelectors } from "@aws-sdk/node-config-provider"; +import { booleanSelector, SelectorType } from "@aws-sdk/node-config-provider"; + +export const NODE_USE_ARN_REGION_ENV_NAME = "AWS_S3_USE_ARN_REGION"; +export const NODE_USE_ARN_REGION_INI_NAME = "s3_use_arn_region"; + +/** + * Config to load useArnRegion from environment variables and shared INI files + * + * @api private + */ +export const NODE_USE_ARN_REGION_CONFIG_OPTIONS: LoadedConfigSelectors = { + environmentVariableSelector: (env: NodeJS.ProcessEnv) => + booleanSelector(env, NODE_USE_ARN_REGION_ENV_NAME, SelectorType.ENV), + configFileSelector: (profile) => booleanSelector(profile, NODE_USE_ARN_REGION_INI_NAME, SelectorType.CONFIG), + default: false, +}; diff --git a/packages/middleware-bucket-endpoint/src/configuration.spec.ts b/packages/middleware-bucket-endpoint/src/configuration.spec.ts deleted file mode 100644 index f0e43fddf9eb..000000000000 --- a/packages/middleware-bucket-endpoint/src/configuration.spec.ts +++ /dev/null @@ -1,68 +0,0 @@ -import { - NODE_USE_ARN_REGION_CONFIG_OPTIONS as loadOptions, - NODE_USE_ARN_REGION_ENV_NAME, - NODE_USE_ARN_REGION_INI_NAME, -} from "./configurations"; -describe("Node useArnRegion config loader", () => { - describe("environment variable selector", () => { - const env: { [NODE_USE_ARN_REGION_ENV_NAME]: any } = {} as any; - - beforeEach(() => { - delete env[NODE_USE_ARN_REGION_ENV_NAME]; - }); - - it(`should return undefined if the environment variable doesn't have ${NODE_USE_ARN_REGION_ENV_NAME}`, () => { - expect(loadOptions.environmentVariableSelector(env)).toBeUndefined(); - }); - - [ - { envValue: "true", parsed: true }, - { envValue: "false", parsed: false }, - ].forEach(({ envValue, parsed }) => { - it(`should return boolean if the environment variable ${NODE_USE_ARN_REGION_ENV_NAME} is ${envValue}`, () => { - env[NODE_USE_ARN_REGION_ENV_NAME] = envValue; - expect(loadOptions.environmentVariableSelector(env)).toBe(parsed); - }); - }); - - ["0", "1", "yes", "no", undefined, null, void 0, ""].forEach((envValue) => { - it(`should throw if the environment variable ${NODE_USE_ARN_REGION_ENV_NAME} is ${envValue}`, () => { - env[NODE_USE_ARN_REGION_ENV_NAME] = envValue as any; - expect(() => loadOptions.environmentVariableSelector(env)).toThrow( - `Cannot load env ${NODE_USE_ARN_REGION_ENV_NAME}. Expected "true" or "false", got ${envValue}.` - ); - }); - }); - }); - - describe("shared INI files selector", () => { - const profileContent: { [NODE_USE_ARN_REGION_INI_NAME]: any } = {} as any; - - beforeEach(() => { - delete profileContent[NODE_USE_ARN_REGION_INI_NAME]; - }); - - it(`should return undefined if shared config profile doesn't have ${NODE_USE_ARN_REGION_INI_NAME}`, () => { - expect(loadOptions.configFileSelector(profileContent)).toBeUndefined(); - }); - - [ - { value: "true", parsed: true }, - { value: "false", parsed: false }, - ].forEach(({ value, parsed }) => { - it(`should return boolean if the shared config profile ${NODE_USE_ARN_REGION_INI_NAME} is ${value}`, () => { - profileContent[NODE_USE_ARN_REGION_INI_NAME] = value; - expect(loadOptions.configFileSelector(profileContent)).toBe(parsed); - }); - }); - - ["0", "1", "yes", "no", undefined, null, void 0, ""].forEach((value) => { - it(`should throw if the shared config profile ${NODE_USE_ARN_REGION_INI_NAME} is ${value}`, () => { - profileContent[NODE_USE_ARN_REGION_INI_NAME] = value as any; - expect(() => loadOptions.configFileSelector(profileContent)).toThrow( - `Cannot load shared config entry ${NODE_USE_ARN_REGION_INI_NAME}. Expected "true" or "false", got ${value}.` - ); - }); - }); - }); -}); diff --git a/packages/middleware-bucket-endpoint/src/configurations.ts b/packages/middleware-bucket-endpoint/src/configurations.ts index 562ae72534df..d6fa52bf2ca0 100644 --- a/packages/middleware-bucket-endpoint/src/configurations.ts +++ b/packages/middleware-bucket-endpoint/src/configurations.ts @@ -1,4 +1,3 @@ -import { LoadedConfigSelectors } from "@aws-sdk/node-config-provider"; import { Provider, RegionInfoProvider } from "@aws-sdk/types"; export interface BucketEndpointInputConfig { @@ -92,55 +91,3 @@ export function resolveBucketEndpointConfig( : () => Promise.resolve(disableMultiregionAccessPoints), }; } - -export const NODE_USE_ARN_REGION_ENV_NAME = "AWS_S3_USE_ARN_REGION"; -export const NODE_USE_ARN_REGION_INI_NAME = "s3_use_arn_region"; - -export const NODE_DISABLE_MULTIREGION_ACCESS_POINT_ENV_NAME = "AWS_S3_DISABLE_MULTIREGION_ACCESS_POINTS"; -export const NODE_DISABLE_MULTIREGION_ACCESS_POINT_INI_NAME = "s3_disable_multiregion_access_points"; - -/** - * Config to load useArnRegion from environment variables and shared INI files - * - * @api private - */ -export const NODE_USE_ARN_REGION_CONFIG_OPTIONS: LoadedConfigSelectors = { - environmentVariableSelector: (env: NodeJS.ProcessEnv) => { - if (!Object.prototype.hasOwnProperty.call(env, NODE_USE_ARN_REGION_ENV_NAME)) return undefined; - if (env[NODE_USE_ARN_REGION_ENV_NAME] === "true") return true; - if (env[NODE_USE_ARN_REGION_ENV_NAME] === "false") return false; - throw new Error( - `Cannot load env ${NODE_USE_ARN_REGION_ENV_NAME}. Expected "true" or "false", got ${env[NODE_USE_ARN_REGION_ENV_NAME]}.` - ); - }, - configFileSelector: (profile) => { - if (!Object.prototype.hasOwnProperty.call(profile, NODE_USE_ARN_REGION_INI_NAME)) return undefined; - if (profile[NODE_USE_ARN_REGION_INI_NAME] === "true") return true; - if (profile[NODE_USE_ARN_REGION_INI_NAME] === "false") return false; - throw new Error( - `Cannot load shared config entry ${NODE_USE_ARN_REGION_INI_NAME}. Expected "true" or "false", got ${profile[NODE_USE_ARN_REGION_INI_NAME]}.` - ); - }, - default: false, -}; - -export const NODE_DISABLE_MULTIREGION_ACCESS_POINT_CONFIG_OPTIONS: LoadedConfigSelectors = { - environmentVariableSelector: (env: NodeJS.ProcessEnv) => { - if (!Object.prototype.hasOwnProperty.call(env, NODE_DISABLE_MULTIREGION_ACCESS_POINT_ENV_NAME)) return undefined; - if (env[NODE_DISABLE_MULTIREGION_ACCESS_POINT_ENV_NAME] === "true") return true; - if (env[NODE_DISABLE_MULTIREGION_ACCESS_POINT_ENV_NAME] === "false") return false; - throw new Error( - `Cannot load env ${NODE_DISABLE_MULTIREGION_ACCESS_POINT_ENV_NAME}. Expected "true" or "false", got ${env[NODE_DISABLE_MULTIREGION_ACCESS_POINT_ENV_NAME]}.` - ); - }, - configFileSelector: (profile) => { - if (!Object.prototype.hasOwnProperty.call(profile, NODE_DISABLE_MULTIREGION_ACCESS_POINT_INI_NAME)) - return undefined; - if (profile[NODE_DISABLE_MULTIREGION_ACCESS_POINT_INI_NAME] === "true") return true; - if (profile[NODE_DISABLE_MULTIREGION_ACCESS_POINT_INI_NAME] === "false") return false; - throw new Error( - `Cannot load shared config entry ${NODE_DISABLE_MULTIREGION_ACCESS_POINT_INI_NAME}. Expected "true" or "false", got ${profile[NODE_DISABLE_MULTIREGION_ACCESS_POINT_INI_NAME]}.` - ); - }, - default: false, -}; diff --git a/packages/middleware-bucket-endpoint/src/index.ts b/packages/middleware-bucket-endpoint/src/index.ts index 53c2ced7ae55..35457c2b0180 100644 --- a/packages/middleware-bucket-endpoint/src/index.ts +++ b/packages/middleware-bucket-endpoint/src/index.ts @@ -1,3 +1,5 @@ +export * from "./NodeDisableMultiregionAccessPointConfigOptions"; +export * from "./NodeUseArnRegionConfigOptions"; export * from "./bucketEndpointMiddleware"; export * from "./bucketHostname"; export * from "./configurations";