Skip to content

Commit 320bc3a

Browse files
authored
chore(core): return httpBearerAuth in authSchemePreference if token is set in env (#7067)
1 parent d6920e8 commit 320bc3a

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

packages/core/src/submodules/httpAuthSchemes/aws_sdk/NODE_AUTH_SCHEME_PREFERENCE_OPTIONS.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
import { afterEach, describe, expect, test as it, vi } from "vitest";
22

33
import { getArrayForCommaSeparatedString } from "../utils/getArrayForCommaSeparatedString";
4+
import { getBearerTokenEnvKey } from "../utils/getBearerTokenEnvKey";
45
import { NODE_AUTH_SCHEME_PREFERENCE_OPTIONS } from "./NODE_AUTH_SCHEME_PREFERENCE_OPTIONS";
56

67
vi.mock("../utils/getArrayForCommaSeparatedString");
8+
vi.mock("../utils/getBearerTokenEnvKey");
79

810
describe("NODE_AUTH_SCHEME_PREFERENCE_OPTIONS", () => {
911
const mockInput = "a,b,c";
1012
const mockOutput = ["a", "b", "c"];
13+
const mockBearerTokenEnvKey = "AWS_BEARER_TOKEN_SERVICE_NAME";
1114

1215
vi.mocked(getArrayForCommaSeparatedString).mockReturnValue(mockOutput);
16+
vi.mocked(getBearerTokenEnvKey).mockReturnValue(mockBearerTokenEnvKey);
1317

1418
afterEach(() => {
1519
vi.clearAllMocks();
@@ -19,18 +23,42 @@ describe("NODE_AUTH_SCHEME_PREFERENCE_OPTIONS", () => {
1923
it("returns undefined if no value is provided", () => {
2024
expect(func({})).toEqual(undefined);
2125
expect(getArrayForCommaSeparatedString).not.toBeCalled();
26+
expect(getBearerTokenEnvKey).not.toBeCalled();
2227
});
2328

2429
it("returns list if value is defined", () => {
2530
expect(func({ [key]: mockInput })).toEqual(mockOutput);
2631
expect(getArrayForCommaSeparatedString).toHaveBeenCalledTimes(1);
2732
expect(getArrayForCommaSeparatedString).toBeCalledWith(mockInput);
33+
expect(getBearerTokenEnvKey).not.toBeCalled();
2834
});
2935
};
3036

3137
describe("environmentVariableSelector", () => {
3238
const { environmentVariableSelector } = NODE_AUTH_SCHEME_PREFERENCE_OPTIONS;
3339
test(environmentVariableSelector, "AWS_AUTH_SCHEME_PREFERENCE");
40+
41+
describe("if signingName is defined", () => {
42+
const env = { AWS_AUTH_SCHEME_PREFERENCE: mockInput };
43+
const options = { signingName: "Signing Name" };
44+
45+
afterEach(() => {
46+
expect(getBearerTokenEnvKey).toHaveBeenCalledTimes(1);
47+
expect(getBearerTokenEnvKey).toBeCalledWith(options.signingName);
48+
});
49+
50+
it(`ignores if mockBearerTokenEnvKey is not set`, () => {
51+
expect(environmentVariableSelector(env, options)).toEqual(mockOutput);
52+
expect(getArrayForCommaSeparatedString).toHaveBeenCalledTimes(1);
53+
expect(getArrayForCommaSeparatedString).toBeCalledWith(mockInput);
54+
});
55+
56+
it("returns ['httpBearerAuth'] if mockBearerTokenEnvKey is set", () => {
57+
const envWithToken = { ...env, [mockBearerTokenEnvKey]: "token" };
58+
expect(environmentVariableSelector(envWithToken, options)).toEqual(["httpBearerAuth"]);
59+
expect(getArrayForCommaSeparatedString).not.toHaveBeenCalled();
60+
});
61+
});
3462
});
3563

3664
describe("configFileSelector", () => {

packages/core/src/submodules/httpAuthSchemes/aws_sdk/NODE_AUTH_SCHEME_PREFERENCE_OPTIONS.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { LoadedConfigSelectors } from "@smithy/node-config-provider";
1+
import { EnvOptions, LoadedConfigSelectors } from "@smithy/node-config-provider";
22

33
import { getArrayForCommaSeparatedString } from "../utils/getArrayForCommaSeparatedString";
4+
import { getBearerTokenEnvKey } from "../utils/getBearerTokenEnvKey";
45

56
const NODE_AUTH_SCHEME_PREFERENCE_ENV_KEY = "AWS_AUTH_SCHEME_PREFERENCE";
67
const NODE_AUTH_SCHEME_PREFERENCE_CONFIG_KEY = "auth_scheme_preference";
@@ -14,7 +15,11 @@ export const NODE_AUTH_SCHEME_PREFERENCE_OPTIONS: LoadedConfigSelectors<string[]
1415
* @param env - Node process environment object
1516
* @returns Array of auth scheme strings if preference is set, undefined otherwise
1617
*/
17-
environmentVariableSelector: (env: NodeJS.ProcessEnv) => {
18+
environmentVariableSelector: (env: NodeJS.ProcessEnv, options?: EnvOptions) => {
19+
if (options?.signingName) {
20+
const bearerTokenKey = getBearerTokenEnvKey(options.signingName);
21+
if (bearerTokenKey in env) return ["httpBearerAuth"];
22+
}
1823
if (!(NODE_AUTH_SCHEME_PREFERENCE_ENV_KEY in env)) return undefined;
1924
return getArrayForCommaSeparatedString(env[NODE_AUTH_SCHEME_PREFERENCE_ENV_KEY] as string);
2025
},
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { describe, expect, it } from "vitest";
2+
3+
import { getBearerTokenEnvKey } from "./getBearerTokenEnvKey";
4+
5+
describe("getBearerTokenEnvKey", () => {
6+
it.each([
7+
["SIGNING_NAME", "signing name"],
8+
["SIGNING_NAME", "SigNinG nAmE"],
9+
["SIGNING_NAME", "signing-name"],
10+
["SIGNING_NAME", "signing\tname"],
11+
])("returns AWS_BEARER_TOKEN_%s for '%s'", (output, input) => {
12+
expect(getBearerTokenEnvKey(input)).toEqual(`AWS_BEARER_TOKEN_${output}`);
13+
});
14+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* Returns an environment variable key base on signing name.
3+
* @param signingName - The signing name to use in the key
4+
* @returns The environment variable key in format AWS_BEARER_TOKEN_<SIGNING_NAME>
5+
*/
6+
export const getBearerTokenEnvKey = (signingName: string) =>
7+
`AWS_BEARER_TOKEN_${signingName.replace(/[\s-]/g, "_").toUpperCase()}`;

0 commit comments

Comments
 (0)