Skip to content

Commit 594c19c

Browse files
authored
chore(core): add Auth Scheme Preference config selector (#7037)
1 parent c2c5635 commit 594c19c

File tree

5 files changed

+117
-0
lines changed

5 files changed

+117
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { afterEach, describe, expect, test as it, vi } from "vitest";
2+
3+
import { getArrayForCommaSeparatedString } from "../utils/getArrayForCommaSeparatedString";
4+
import { NODE_AUTH_SCHEME_PREFERENCE_OPTIONS } from "./NODE_AUTH_SCHEME_PREFERENCE_OPTIONS";
5+
6+
vi.mock("../utils/getArrayForCommaSeparatedString");
7+
8+
describe("NODE_AUTH_SCHEME_PREFERENCE_OPTIONS", () => {
9+
const mockInput = "a,b,c";
10+
const mockOutput = ["a", "b", "c"];
11+
12+
vi.mocked(getArrayForCommaSeparatedString).mockReturnValue(mockOutput);
13+
14+
afterEach(() => {
15+
vi.clearAllMocks();
16+
});
17+
18+
const test = (func: Function, key: string) => {
19+
it("returns undefined if no value is provided", () => {
20+
expect(func({})).toEqual(undefined);
21+
expect(getArrayForCommaSeparatedString).not.toBeCalled();
22+
});
23+
24+
it("returns list if value is defined", () => {
25+
expect(func({ [key]: mockInput })).toEqual(mockOutput);
26+
expect(getArrayForCommaSeparatedString).toHaveBeenCalledTimes(1);
27+
expect(getArrayForCommaSeparatedString).toBeCalledWith(mockInput);
28+
});
29+
};
30+
31+
describe("environmentVariableSelector", () => {
32+
const { environmentVariableSelector } = NODE_AUTH_SCHEME_PREFERENCE_OPTIONS;
33+
test(environmentVariableSelector, "AWS_AUTH_SCHEME_PREFERENCE");
34+
});
35+
36+
describe("configFileSelector", () => {
37+
const { configFileSelector } = NODE_AUTH_SCHEME_PREFERENCE_OPTIONS;
38+
test(configFileSelector, "auth_scheme_preference");
39+
});
40+
41+
it("returns false for default", () => {
42+
const { default: defaultValue } = NODE_AUTH_SCHEME_PREFERENCE_OPTIONS;
43+
expect(defaultValue).toEqual([]);
44+
});
45+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { LoadedConfigSelectors } from "@smithy/node-config-provider";
2+
3+
import { getArrayForCommaSeparatedString } from "../utils/getArrayForCommaSeparatedString";
4+
5+
const NODE_AUTH_SCHEME_PREFERENCE_ENV_KEY = "AWS_AUTH_SCHEME_PREFERENCE";
6+
const NODE_AUTH_SCHEME_PREFERENCE_CONFIG_KEY = "auth_scheme_preference";
7+
8+
/**
9+
* @public
10+
*/
11+
export const NODE_AUTH_SCHEME_PREFERENCE_OPTIONS: LoadedConfigSelectors<string[]> = {
12+
/**
13+
* Retrieves auth scheme preference from environment variables
14+
* @param env - Node process environment object
15+
* @returns Array of auth scheme strings if preference is set, undefined otherwise
16+
*/
17+
environmentVariableSelector: (env: NodeJS.ProcessEnv) => {
18+
if (!(NODE_AUTH_SCHEME_PREFERENCE_ENV_KEY in env)) return undefined;
19+
return getArrayForCommaSeparatedString(env[NODE_AUTH_SCHEME_PREFERENCE_ENV_KEY] as string);
20+
},
21+
22+
/**
23+
* Retrieves auth scheme preference from config file
24+
* @param profile - Config profile object
25+
* @returns Array of auth scheme strings if preference is set, undefined otherwise
26+
*/
27+
configFileSelector: (profile) => {
28+
if (!(NODE_AUTH_SCHEME_PREFERENCE_CONFIG_KEY in profile)) return undefined;
29+
return getArrayForCommaSeparatedString(profile[NODE_AUTH_SCHEME_PREFERENCE_CONFIG_KEY] as string);
30+
},
31+
32+
/**
33+
* Default auth scheme preference if not specified in environment or config
34+
*/
35+
default: [],
36+
};
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export { AwsSdkSigV4Signer, AWSSDKSigV4Signer, validateSigningProperties } from "./AwsSdkSigV4Signer";
22
export { AwsSdkSigV4ASigner } from "./AwsSdkSigV4ASigner";
3+
export * from "./NODE_AUTH_SCHEME_PREFERENCE_OPTIONS";
34
export * from "./resolveAwsSdkSigV4AConfig";
45
export * from "./resolveAwsSdkSigV4Config";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { describe, expect, it } from "vitest";
2+
3+
import { getArrayForCommaSeparatedString } from "./getArrayForCommaSeparatedString";
4+
5+
describe(getArrayForCommaSeparatedString.name, () => {
6+
it("should return empty array for empty string", () => {
7+
expect(getArrayForCommaSeparatedString("")).toEqual([]);
8+
});
9+
10+
it("should return empty array for null/undefined input", () => {
11+
expect(getArrayForCommaSeparatedString(null as unknown as string)).toEqual([]);
12+
expect(getArrayForCommaSeparatedString(undefined as unknown as string)).toEqual([]);
13+
});
14+
15+
it("should split comma separated string into array", () => {
16+
expect(getArrayForCommaSeparatedString("a,b,c")).toEqual(["a", "b", "c"]);
17+
});
18+
19+
it("should trim whitespace from array items", () => {
20+
expect(getArrayForCommaSeparatedString(" a , b , c ")).toEqual(["a", "b", "c"]);
21+
});
22+
23+
it("should handle single item", () => {
24+
expect(getArrayForCommaSeparatedString("a")).toEqual(["a"]);
25+
});
26+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Converts a comma-separated string into an array of trimmed strings
3+
* @param str The comma-separated input string to split
4+
* @returns Array of trimmed strings split from the input
5+
*
6+
* @internal
7+
*/
8+
export const getArrayForCommaSeparatedString = (str: string) =>
9+
typeof str === "string" && str.length > 0 ? str.split(",").map((item) => item.trim()) : [];

0 commit comments

Comments
 (0)