Skip to content

Commit

Permalink
chore(credential-providers): add credential attribution
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhe committed Oct 4, 2024
1 parent dfda129 commit 7a4c3f6
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 9 deletions.
24 changes: 23 additions & 1 deletion packages/core/src/submodules/client/setFeature.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import type { AwsHandlerExecutionContext, AwsSdkFeatures } from "@aws-sdk/types";
import type {
AttributedAwsCredentialIdentity,
AwsHandlerExecutionContext,
AwsSdkCredentialsFeatures,
AwsSdkFeatures,
} from "@aws-sdk/types";

/**
* @internal
Expand All @@ -24,3 +29,20 @@ export function setFeature<F extends keyof AwsSdkFeatures>(
}
context.__aws_sdk_context.features![feature] = value;
}

/**
* @internal
*
* sets feature attribution on the credential object.
*/
export function setCredentialFeature<F extends keyof AwsSdkCredentialsFeatures>(
credentials: AttributedAwsCredentialIdentity,
feature: F,
value: AwsSdkCredentialsFeatures[F]
): AttributedAwsCredentialIdentity {
if (!credentials.$source) {
credentials.$source = {};
}
credentials.$source![feature] = value;
return credentials;
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { setCredentialFeature } from "@aws-sdk/core/client";
import { AttributedAwsCredentialIdentity } from "@aws-sdk/types";
import {
doesIdentityRequireRefresh,
isIdentityExpired,
Expand Down Expand Up @@ -102,9 +104,11 @@ export interface AwsSdkSigV4AuthResolvedConfig {
export const resolveAwsSdkSigV4Config = <T>(
config: T & AwsSdkSigV4AuthInputConfig & AwsSdkSigV4PreviouslyResolved
): T & AwsSdkSigV4AuthResolvedConfig => {
let isUserSupplied = false;
// Normalize credentials
let normalizedCreds: AwsCredentialIdentityProvider | undefined;
if (config.credentials) {
isUserSupplied = true;
normalizedCreds = memoizeIdentityProvider(config.credentials, isIdentityExpired, doesIdentityRequireRefresh);
}
if (!normalizedCreds) {
Expand Down Expand Up @@ -218,7 +222,12 @@ export const resolveAwsSdkSigV4Config = <T>(
...config,
systemClockOffset,
signingEscapePath,
credentials: normalizedCreds!,
credentials: isUserSupplied
? async () =>
normalizedCreds!().then((creds: AttributedAwsCredentialIdentity) =>
setCredentialFeature(creds, "CREDENTIALS_CODE", "e")
)
: normalizedCreds!,
signer,
};
};
Expand Down
12 changes: 9 additions & 3 deletions packages/credential-provider-env/src/fromEnv.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { CredentialProviderOptions } from "@aws-sdk/types";
import { setCredentialFeature } from "@aws-sdk/core/client";
import type { AttributedAwsCredentialIdentity, CredentialProviderOptions } from "@aws-sdk/types";
import { CredentialsProviderError } from "@smithy/property-provider";
import { AwsCredentialIdentityProvider } from "@smithy/types";

Expand Down Expand Up @@ -48,14 +49,19 @@ export const fromEnv =
const accountId: string | undefined = process.env[ENV_ACCOUNT_ID];

if (accessKeyId && secretAccessKey) {
return {
const credentials = {
accessKeyId,
secretAccessKey,
...(sessionToken && { sessionToken }),
...(expiry && { expiration: new Date(expiry) }),
...(credentialScope && { credentialScope }),
...(accountId && { accountId }),
};
} as AttributedAwsCredentialIdentity;
setCredentialFeature(credentials, "CREDENTIALS_ENV_VARS", "g");
if (accountId) {
setCredentialFeature(credentials, "RESOLVED_ACCOUNT_ID", "T");
}
return credentials;
}

throw new CredentialsProviderError("Unable to find environment variable credentials.", { logger: init?.logger });
Expand Down
11 changes: 9 additions & 2 deletions packages/credential-provider-web-identity/src/fromTokenFile.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { CredentialProviderOptions } from "@aws-sdk/types";
import { setCredentialFeature } from "@aws-sdk/core/client";
import { AttributedAwsCredentialIdentity, CredentialProviderOptions } from "@aws-sdk/types";
import { CredentialsProviderError } from "@smithy/property-provider";
import type { AwsCredentialIdentityProvider } from "@smithy/types";
import { readFileSync } from "fs";
Expand Down Expand Up @@ -40,10 +41,16 @@ export const fromTokenFile =
});
}

return fromWebToken({
const credentials: AttributedAwsCredentialIdentity = await fromWebToken({
...init,
webIdentityToken: readFileSync(webIdentityTokenFile, { encoding: "ascii" }),
roleArn,
roleSessionName,
})();

if (process.env[ENV_TOKEN_FILE]) {
setCredentialFeature(credentials, "CREDENTIALS_ENV_VARS_STS_WEB_ID_TOKEN", "h");
}

return credentials;
};
10 changes: 8 additions & 2 deletions packages/types/src/feature-ids.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export type AwsSdkFeatures = Partial<{
ACCOUNT_ID_MODE_DISABLED: "Q";
ACCOUNT_ID_MODE_REQUIRED: "R";
SIGV4A_SIGNING: "S";
RESOLVED_ACCOUNT_ID: "T";
FLEXIBLE_CHECKSUMS_REQ_CRC32: "U";
FLEXIBLE_CHECKSUMS_REQ_CRC32C: "V";
FLEXIBLE_CHECKSUMS_REQ_CRC64: "W";
Expand All @@ -32,8 +31,15 @@ export type AwsSdkFeatures = Partial<{
FLEXIBLE_CHECKSUMS_RES_WHEN_SUPPORTED: "b";
FLEXIBLE_CHECKSUMS_RES_WHEN_REQUIRED: "c";
DDB_MAPPER: "d";
}> &
AwsSdkCredentialsFeatures;

/**
* @internal
*/
export type AwsSdkCredentialsFeatures = Partial<{
RESOLVED_ACCOUNT_ID: "T";
CREDENTIALS_CODE: "e";
// CREDENTIALS_JVM_SYSTEM_PROPERTIES: "f"; // not applicable.
CREDENTIALS_ENV_VARS: "g";
CREDENTIALS_ENV_VARS_STS_WEB_ID_TOKEN: "h";
CREDENTIALS_STS_ASSUME_ROLE: "i";
Expand Down
8 changes: 8 additions & 0 deletions packages/types/src/identity/AwsCredentialIdentity.ts
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
import type { AwsCredentialIdentity } from "@smithy/types";

import type { AwsSdkCredentialsFeatures } from "../feature-ids";

export { AwsCredentialIdentity, AwsCredentialIdentityProvider } from "@smithy/types";

export type AttributedAwsCredentialIdentity = AwsCredentialIdentity & {
$source?: AwsSdkCredentialsFeatures;
};

0 comments on commit 7a4c3f6

Please sign in to comment.