-
Notifications
You must be signed in to change notification settings - Fork 620
chore(token-providers): add provider to retrieve token from env var #7097
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
240f587
chore(token-providers): add provider to retrieve token from environme…
trivikr 181885b
chore: add public tag for FromEnvBearerTokenInit
trivikr 387c0b3
fix: use optional chaining for logger.debug
trivikr a8107db
chore: change name from fromEnvBearerToken to fromEnvSigningName
trivikr b41bdf4
docs: add documentation about fromEnvSigningName
trivikr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 +1,2 @@ | ||
export * from "./aws_sdk"; | ||
export * from "./utils/getBearerTokenEnvKey"; |
This file contains hidden or 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 hidden or 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,46 @@ | ||
import { getBearerTokenEnvKey } from "@aws-sdk/core"; | ||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
||
import { fromEnvBearerToken } from "./fromEnvBearerToken"; | ||
|
||
vi.mock("@aws-sdk/core"); | ||
|
||
describe("fromEnvBearerToken", () => { | ||
const originalEnv = process.env; | ||
const mockInit = { signingName: "signing name" }; | ||
const mockBearerTokenEnvKey = "AWS_BEARER_TOKEN_SIGNING_NAME"; | ||
|
||
beforeEach(() => { | ||
process.env = { ...originalEnv }; | ||
vi.mocked(getBearerTokenEnvKey).mockReturnValue(mockBearerTokenEnvKey); | ||
}); | ||
|
||
afterEach(() => { | ||
process.env = originalEnv; | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
describe("throws error", () => { | ||
it("when signingName is not passed", async () => { | ||
await expect(fromEnvBearerToken()()).rejects.toThrow( | ||
"Please pass 'signingName' to compute environment variable key" | ||
); | ||
expect(getBearerTokenEnvKey).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("when token is not present in environment variable", async () => { | ||
await expect(fromEnvBearerToken(mockInit)()).rejects.toThrow( | ||
`Token not present in '${mockBearerTokenEnvKey}' environment variable` | ||
); | ||
expect(getBearerTokenEnvKey).toHaveBeenCalledWith(mockInit.signingName); | ||
}); | ||
}); | ||
|
||
it("returns token from environment variable", async () => { | ||
const mockBearerToken = "mock-bearer-token"; | ||
process.env[mockBearerTokenEnvKey] = mockBearerToken; | ||
const token = await fromEnvBearerToken(mockInit)(); | ||
expect(token).toEqual({ token: mockBearerToken }); | ||
expect(getBearerTokenEnvKey).toHaveBeenCalledWith(mockInit.signingName); | ||
}); | ||
}); |
This file contains hidden or 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,34 @@ | ||
import { getBearerTokenEnvKey } from "@aws-sdk/core"; | ||
import type { CredentialProviderOptions, TokenIdentityProvider } from "@aws-sdk/types"; | ||
import { TokenProviderError } from "@smithy/property-provider"; | ||
|
||
export interface FromEnvBearerTokenInit extends CredentialProviderOptions { | ||
signingName?: string; | ||
} | ||
|
||
/** | ||
* Creates a TokenIdentityProvider that retrieves bearer token from environment variable | ||
* | ||
* @param options - Configuration options for the token provider | ||
* @param options.logger - Optional logger for debug messages | ||
* @param options.signingName - Service signing name used to determine environment variable key | ||
* @returns TokenIdentityProvider that provides bearer token from environment variable | ||
* | ||
* @public | ||
*/ | ||
export const fromEnvBearerToken = | ||
trivikr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
({ logger, signingName }: FromEnvBearerTokenInit = {}): TokenIdentityProvider => | ||
async () => { | ||
logger?.debug("@aws-sdk/token-providers - fromEnvBearerToken"); | ||
trivikr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (!signingName) { | ||
throw new TokenProviderError("Please pass 'signingName' to compute environment variable key", { logger }); | ||
} | ||
|
||
const bearerTokenKey = getBearerTokenEnvKey(signingName); | ||
if (!(bearerTokenKey in process.env)) { | ||
throw new TokenProviderError(`Token not present in '${bearerTokenKey}' environment variable`, { logger }); | ||
} | ||
|
||
return { token: process.env[bearerTokenKey]! }; | ||
}; |
This file contains hidden or 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,3 +1,4 @@ | ||
export * from "./fromEnvBearerToken"; | ||
export * from "./fromSso"; | ||
export * from "./fromStatic"; | ||
export * from "./nodeProvider"; |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.