Skip to content

Commit

Permalink
chore(credential-providers): attribute credential feature sources
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhe committed Oct 7, 2024
1 parent 7a4c3f6 commit be37764
Show file tree
Hide file tree
Showing 25 changed files with 108 additions and 32 deletions.
12 changes: 10 additions & 2 deletions clients/client-sts/src/defaultStsRoleAssumers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// smithy-typescript generated code
// Please do not touch this file. It's generated from template in:
// https://github.com/aws/aws-sdk-js-v3/blob/main/codegen/smithy-aws-typescript-codegen/src/main/resources/software/amazon/smithy/aws/typescript/codegen/sts-client-defaultStsRoleAssumers.ts
import { setCredentialFeature } from "@aws-sdk/core";
import type { CredentialProviderOptions } from "@aws-sdk/types";
import { AwsCredentialIdentity, Logger, Provider } from "@smithy/types";

Expand Down Expand Up @@ -118,7 +119,7 @@ export const getDefaultRoleAssumer = (

const accountId = getAccountIdFromAssumedRoleUser(AssumedRoleUser);

return {
const credentials = {
accessKeyId: Credentials.AccessKeyId,
secretAccessKey: Credentials.SecretAccessKey,
sessionToken: Credentials.SessionToken,
Expand All @@ -127,6 +128,8 @@ export const getDefaultRoleAssumer = (
...((Credentials as any).CredentialScope && { credentialScope: (Credentials as any).CredentialScope }),
...(accountId && { accountId }),
};
setCredentialFeature(credentials, "CREDENTIALS_STS_ASSUME_ROLE", "i");
return credentials;
};
};

Expand Down Expand Up @@ -174,7 +177,7 @@ export const getDefaultRoleAssumerWithWebIdentity = (

const accountId = getAccountIdFromAssumedRoleUser(AssumedRoleUser);

return {
const credentials = {
accessKeyId: Credentials.AccessKeyId,
secretAccessKey: Credentials.SecretAccessKey,
sessionToken: Credentials.SessionToken,
Expand All @@ -183,6 +186,11 @@ export const getDefaultRoleAssumerWithWebIdentity = (
...((Credentials as any).CredentialScope && { credentialScope: (Credentials as any).CredentialScope }),
...(accountId && { accountId }),
};
if (accountId) {
setCredentialFeature(credentials, "RESOLVED_ACCOUNT_ID", "T");
}
setCredentialFeature(credentials, "CREDENTIALS_STS_ASSUME_ROLE_WEB_ID", "k");
return credentials;
};
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { setCredentialFeature } from "@aws-sdk/core";
import type { CredentialProviderOptions } from "@aws-sdk/types";
import { AwsCredentialIdentity, Logger, Provider } from "@smithy/types";

Expand Down Expand Up @@ -115,7 +116,7 @@ export const getDefaultRoleAssumer = (

const accountId = getAccountIdFromAssumedRoleUser(AssumedRoleUser);

return {
const credentials = {
accessKeyId: Credentials.AccessKeyId,
secretAccessKey: Credentials.SecretAccessKey,
sessionToken: Credentials.SessionToken,
Expand All @@ -124,6 +125,8 @@ export const getDefaultRoleAssumer = (
...((Credentials as any).CredentialScope && { credentialScope: (Credentials as any).CredentialScope }),
...(accountId && { accountId }),
};
setCredentialFeature(credentials, "CREDENTIALS_STS_ASSUME_ROLE", "i");
return credentials;
};
};

Expand Down Expand Up @@ -171,7 +174,7 @@ export const getDefaultRoleAssumerWithWebIdentity = (

const accountId = getAccountIdFromAssumedRoleUser(AssumedRoleUser);

return {
const credentials = {
accessKeyId: Credentials.AccessKeyId,
secretAccessKey: Credentials.SecretAccessKey,
sessionToken: Credentials.SessionToken,
Expand All @@ -180,6 +183,11 @@ export const getDefaultRoleAssumerWithWebIdentity = (
...((Credentials as any).CredentialScope && { credentialScope: (Credentials as any).CredentialScope }),
...(accountId && { accountId }),
};
if (accountId) {
setCredentialFeature(credentials, "RESOLVED_ACCOUNT_ID", "T");
}
setCredentialFeature(credentials, "CREDENTIALS_STS_ASSUME_ROLE_WEB_ID", "k");
return credentials;
};
};

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/submodules/client/setFeature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function setFeature<F extends keyof AwsSdkFeatures>(
/**
* @internal
*
* sets feature attribution on the credential object.
* @returns the credentials with source feature attribution.
*/
export function setCredentialFeature<F extends keyof AwsSdkCredentialsFeatures>(
credentials: AttributedAwsCredentialIdentity,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { setCredentialFeature } from "@aws-sdk/core/client";
import { setCredentialFeature } from "@aws-sdk/core";
import { AttributedAwsCredentialIdentity } from "@aws-sdk/types";
import {
doesIdentityRequireRefresh,
Expand Down
1 change: 1 addition & 0 deletions packages/credential-provider-env/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
},
"license": "Apache-2.0",
"dependencies": {
"@aws-sdk/core": "*",
"@aws-sdk/types": "*",
"@smithy/property-provider": "^3.1.7",
"@smithy/types": "^3.5.0",
Expand Down
5 changes: 1 addition & 4 deletions packages/credential-provider-env/src/fromEnv.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { setCredentialFeature } from "@aws-sdk/core/client";
import { setCredentialFeature } from "@aws-sdk/core";
import type { AttributedAwsCredentialIdentity, CredentialProviderOptions } from "@aws-sdk/types";
import { CredentialsProviderError } from "@smithy/property-provider";
import { AwsCredentialIdentityProvider } from "@smithy/types";
Expand Down Expand Up @@ -58,9 +58,6 @@ export const fromEnv =
...(accountId && { accountId }),
} as AttributedAwsCredentialIdentity;
setCredentialFeature(credentials, "CREDENTIALS_ENV_VARS", "g");
if (accountId) {
setCredentialFeature(credentials, "RESOLVED_ACCOUNT_ID", "T");
}
return credentials;
}

Expand Down
1 change: 1 addition & 0 deletions packages/credential-provider-http/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
},
"license": "Apache-2.0",
"dependencies": {
"@aws-sdk/core": "*",
"@aws-sdk/types": "*",
"@smithy/fetch-http-handler": "^3.2.9",
"@smithy/node-http-handler": "^3.2.4",
Expand Down
3 changes: 2 additions & 1 deletion packages/credential-provider-http/src/fromHttp/fromHttp.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { setCredentialFeature } from "@aws-sdk/core";
import { NodeHttpHandler } from "@smithy/node-http-handler";
import { CredentialsProviderError } from "@smithy/property-provider";
import { AwsCredentialIdentity, AwsCredentialIdentityProvider } from "@smithy/types";
Expand Down Expand Up @@ -81,7 +82,7 @@ Set AWS_CONTAINER_CREDENTIALS_FULL_URI or AWS_CONTAINER_CREDENTIALS_RELATIVE_URI
}
try {
const result = await requestHandler.handle(request);
return getCredentials(result.response);
return getCredentials(result.response).then((creds) => setCredentialFeature(creds, "CREDENTIALS_HTTP", "z"));
} catch (e: unknown) {
throw new CredentialsProviderError(String(e), { logger: options.logger });
}
Expand Down
1 change: 1 addition & 0 deletions packages/credential-provider-ini/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
},
"license": "Apache-2.0",
"dependencies": {
"@aws-sdk/core": "*",
"@aws-sdk/credential-provider-env": "*",
"@aws-sdk/credential-provider-http": "*",
"@aws-sdk/credential-provider-process": "*",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { setCredentialFeature } from "@aws-sdk/core";
import { CredentialsProviderError } from "@smithy/property-provider";
import { getProfileName } from "@smithy/shared-ini-file-loader";
import { AwsCredentialIdentity, IniSection, Logger, ParsedIniData, Profile } from "@smithy/types";
Expand Down Expand Up @@ -159,7 +160,7 @@ export const resolveAssumeRoleCredentials = async (
* can use its role_arn instead of redundantly needing another role_arn at
* this final layer.
*/
return sourceCredsProvider;
return sourceCredsProvider.then((creds) => setCredentialFeature(creds, "CREDENTIALS_PROFILE_SOURCE_PROFILE", "o"));
} else {
const params: AssumeRoleParams = {
RoleArn: data.role_arn!,
Expand All @@ -181,7 +182,9 @@ export const resolveAssumeRoleCredentials = async (
}

const sourceCreds = await sourceCredsProvider;
return options.roleAssumer!(sourceCreds, params);
return options.roleAssumer!(sourceCreds, params).then((creds) =>
setCredentialFeature(creds, "CREDENTIALS_PROFILE_SOURCE_PROFILE", "o")
);
}
};

Expand Down
12 changes: 8 additions & 4 deletions packages/credential-provider-ini/src/resolveCredentialSource.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";
import type { AwsCredentialIdentity, CredentialProviderOptions } from "@aws-sdk/types";
import { chain, CredentialsProviderError } from "@smithy/property-provider";
import { AwsCredentialIdentityProvider, Logger } from "@smithy/types";

Expand All @@ -21,17 +22,17 @@ export const resolveCredentialSource = (
const { fromHttp } = await import("@aws-sdk/credential-provider-http");
const { fromContainerMetadata } = await import("@smithy/credential-provider-imds");
logger?.debug("@aws-sdk/credential-provider-ini - credential_source is EcsContainer");
return chain(fromHttp(options ?? {}), fromContainerMetadata(options));
return async () => chain(fromHttp(options ?? {}), fromContainerMetadata(options))().then(setNamedProvider);
},
Ec2InstanceMetadata: async (options?: CredentialProviderOptions) => {
logger?.debug("@aws-sdk/credential-provider-ini - credential_source is Ec2InstanceMetadata");
const { fromInstanceMetadata } = await import("@smithy/credential-provider-imds");
return fromInstanceMetadata(options);
return async () => fromInstanceMetadata(options)().then(setNamedProvider);
},
Environment: async (options?: CredentialProviderOptions) => {
logger?.debug("@aws-sdk/credential-provider-ini - credential_source is Environment");
const { fromEnv } = await import("@aws-sdk/credential-provider-env");
return fromEnv(options);
return async () => fromEnv(options)().then(setNamedProvider);
},
};
if (credentialSource in sourceProvidersMap) {
Expand All @@ -44,3 +45,6 @@ export const resolveCredentialSource = (
);
}
};

const setNamedProvider = (creds: AwsCredentialIdentity) =>
setCredentialFeature(creds, "CREDENTIALS_PROFILE_NAMED_PROVIDER", "p");
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { setCredentialFeature } from "@aws-sdk/core";
import { Credentials, Profile } from "@aws-sdk/types";

import { FromIniInit } from "./fromIni";
Expand All @@ -23,5 +24,5 @@ export const resolveProcessCredentials = async (options: FromIniInit, profile: s
fromProcess({
...options,
profile,
})()
})().then((creds) => setCredentialFeature(creds, "CREDENTIALS_PROFILE_PROCESS", "v"))
);
2 changes: 1 addition & 1 deletion packages/credential-provider-ini/src/resolveProfileData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const resolveProfileData = async (
}

if (isSsoProfile(data)) {
return await resolveSsoCredentials(profileName, options);
return await resolveSsoCredentials(profileName, data, options);
}

// If the profile cannot be parsed or contains neither static credentials
Expand Down
17 changes: 14 additions & 3 deletions packages/credential-provider-ini/src/resolveSsoCredentials.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
import { setCredentialFeature } from "@aws-sdk/core";
import type { SsoProfile } from "@aws-sdk/credential-provider-sso";
import type { CredentialProviderOptions } from "@aws-sdk/types";
import type { Profile } from "@smithy/types";
import type { IniSection, Profile } from "@smithy/types";

/**
* @internal
*/
export const resolveSsoCredentials = async (profile: string, options: CredentialProviderOptions = {}) => {
export const resolveSsoCredentials = async (
profile: string,
profileData: IniSection,
options: CredentialProviderOptions = {}
) => {
const { fromSSO } = await import("@aws-sdk/credential-provider-sso");
return fromSSO({
profile,
logger: options.logger,
})();
})().then((creds) => {
if (profileData.sso_session) {
return setCredentialFeature(creds, "CREDENTIALS_PROFILE_SSO", "r");
} else {
return setCredentialFeature(creds, "CREDENTIALS_PROFILE_SSO_LEGACY", "t");
}
});
};

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { setCredentialFeature } from "@aws-sdk/core";
import { AwsCredentialIdentity, Profile } from "@smithy/types";

import { FromIniInit } from "./fromIni";
Expand Down Expand Up @@ -32,11 +33,14 @@ export const resolveStaticCredentials = (
options?: FromIniInit
): Promise<AwsCredentialIdentity> => {
options?.logger?.debug("@aws-sdk/credential-provider-ini - resolveStaticCredentials");
return Promise.resolve({

const credentials = {
accessKeyId: profile.aws_access_key_id,
secretAccessKey: profile.aws_secret_access_key,
sessionToken: profile.aws_session_token,
...(profile.aws_credential_scope && { credentialScope: profile.aws_credential_scope }),
...(profile.aws_account_id && { accountId: profile.aws_account_id }),
});
};

return Promise.resolve(setCredentialFeature(credentials, "CREDENTIALS_PROFILE", "n"));
};
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { setCredentialFeature } from "@aws-sdk/core";
import { AwsCredentialIdentity, Profile } from "@smithy/types";

import { FromIniInit } from "./fromIni";
Expand Down Expand Up @@ -36,5 +37,5 @@ export const resolveWebIdentityCredentials = async (
roleAssumerWithWebIdentity: options.roleAssumerWithWebIdentity,
logger: options.logger,
parentClientConfig: options.parentClientConfig,
})()
})().then((creds) => setCredentialFeature(creds, "CREDENTIALS_PROFILE_STS_WEB_ID_TOKEN", "q"))
);
1 change: 1 addition & 0 deletions packages/credential-provider-process/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
},
"license": "Apache-2.0",
"dependencies": {
"@aws-sdk/core": "*",
"@aws-sdk/types": "*",
"@smithy/property-provider": "^3.1.7",
"@smithy/shared-ini-file-loader": "^3.1.8",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { setCredentialFeature } from "@aws-sdk/core";
import { AwsCredentialIdentity, ParsedIniData } from "@smithy/types";

import { ProcessCredentials } from "./ProcessCredentials";
Expand Down Expand Up @@ -31,12 +32,16 @@ export const getValidatedProcessCredentials = (
accountId = profiles[profileName].aws_account_id;
}

return {
const credentials = {
accessKeyId: data.AccessKeyId,
secretAccessKey: data.SecretAccessKey,
...(data.SessionToken && { sessionToken: data.SessionToken }),
...(data.Expiration && { expiration: new Date(data.Expiration) }),
...(data.CredentialScope && { credentialScope: data.CredentialScope }),
...(accountId && { accountId }),
};

setCredentialFeature(credentials, "CREDENTIALS_PROCESS", "w");

return credentials;
};
1 change: 1 addition & 0 deletions packages/credential-provider-sso/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
},
"license": "Apache-2.0",
"dependencies": {
"@aws-sdk/core": "*",
"@aws-sdk/client-sso": "*",
"@aws-sdk/token-providers": "*",
"@aws-sdk/types": "*",
Expand Down
11 changes: 10 additions & 1 deletion packages/credential-provider-sso/src/resolveSSOCredentials.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { setCredentialFeature } from "@aws-sdk/core";
import { fromSso as getSsoTokenProvider } from "@aws-sdk/token-providers";
import { CredentialsProviderError } from "@smithy/property-provider";
import { getSSOTokenFromFile, SSOToken } from "@smithy/shared-ini-file-loader";
Expand Down Expand Up @@ -103,12 +104,20 @@ export const resolveSSOCredentials = async ({
});
}

return {
const credentials = {
accessKeyId,
secretAccessKey,
sessionToken,
expiration: new Date(expiration),
...(credentialScope && { credentialScope }),
...(accountId && { accountId }),
};

if (ssoSession) {
setCredentialFeature(credentials, "CREDENTIALS_SSO", "s");
} else {
setCredentialFeature(credentials, "CREDENTIALS_SSO_LEGACY", "u");
}

return credentials;
};
1 change: 1 addition & 0 deletions packages/credential-provider-web-identity/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
},
"license": "Apache-2.0",
"dependencies": {
"@aws-sdk/core": "*",
"@aws-sdk/types": "*",
"@smithy/property-provider": "^3.1.7",
"@smithy/types": "^3.5.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { setCredentialFeature } from "@aws-sdk/core/client";
import { setCredentialFeature } from "@aws-sdk/core";
import { AttributedAwsCredentialIdentity, CredentialProviderOptions } from "@aws-sdk/types";
import { CredentialsProviderError } from "@smithy/property-provider";
import type { AwsCredentialIdentityProvider } from "@smithy/types";
Expand Down Expand Up @@ -48,7 +48,7 @@ export const fromTokenFile =
roleSessionName,
})();

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

Expand Down
Loading

0 comments on commit be37764

Please sign in to comment.