Skip to content

Commit

Permalink
chore(nested-clients): break cycle
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhe committed Jan 10, 2025
1 parent deff8de commit 0cc2832
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ export const resolveAssumeRoleCredentials = async (
const { source_profile, region } = profileData;

if (!options.roleAssumer) {
// @ts-ignore Cannot find module '@aws-sdk/client-sts'
const { getDefaultRoleAssumer } = await import("@aws-sdk/nested-clients");
options.roleAssumer = getDefaultRoleAssumer(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ export const fromWebToken =
let { roleAssumerWithWebIdentity } = init;

if (!roleAssumerWithWebIdentity) {
// @ts-ignore Cannot find module '@aws-sdk/client-sts'
const { getDefaultRoleAssumerWithWebIdentity } = await import("@aws-sdk/nested-clients");
roleAssumerWithWebIdentity = getDefaultRoleAssumerWithWebIdentity(
{
Expand Down
2 changes: 0 additions & 2 deletions packages/nested-clients/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@
"dependencies": {
"@aws-crypto/sha256-browser": "5.2.0",
"@aws-crypto/sha256-js": "5.2.0",
"@aws-sdk/client-sso-oidc": "*",
"@aws-sdk/core": "*",
"@aws-sdk/credential-provider-node": "*",
"@aws-sdk/middleware-host-header": "*",
"@aws-sdk/middleware-logger": "*",
"@aws-sdk/middleware-recursion-detection": "*",
Expand Down
6 changes: 3 additions & 3 deletions packages/nested-clients/src/nested-sts/runtimeConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import packageInfo from "../../package.json"; // eslint-disable-line

import { AwsSdkSigV4Signer, emitWarningIfUnsupportedVersion as awsCheckVersion } from "@aws-sdk/core";
import { defaultProvider as credentialDefaultProvider } from "@aws-sdk/credential-provider-node";

import { NODE_APP_ID_CONFIG_OPTIONS, createDefaultUserAgentProvider } from "@aws-sdk/util-user-agent-node";
import {
NODE_REGION_CONFIG_FILE_OPTIONS,
Expand Down Expand Up @@ -41,7 +41,7 @@ export const getRuntimeConfig = (config: STSClientConfig) => {
runtime: "node",
defaultsMode,
bodyLengthChecker: config?.bodyLengthChecker ?? calculateBodyLength,
credentialDefaultProvider: config?.credentialDefaultProvider ?? credentialDefaultProvider,

defaultUserAgentProvider:
config?.defaultUserAgentProvider ??
createDefaultUserAgentProvider({ serviceId: clientSharedValues.serviceId, clientVersion: packageInfo.version }),
Expand All @@ -50,7 +50,7 @@ export const getRuntimeConfig = (config: STSClientConfig) => {
schemeId: "aws.auth#sigv4",
identityProvider: (ipc: IdentityProviderConfig) =>
ipc.getIdentityProvider("aws.auth#sigv4") ||
(async (idProps) => await credentialDefaultProvider(idProps?.__config || {})()),
(async (idProps) => await config!.credentialDefaultProvider!(idProps?.__config || {})()),
signer: new AwsSdkSigV4Signer(),
},
{
Expand Down
1 change: 0 additions & 1 deletion packages/token-providers/src/getNewSsoOidcToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { getSsoOidcClient } from "./getSsoOidcClient";
* @internal
*/
export const getNewSsoOidcToken = async (ssoToken: SSOToken, ssoRegion: string, init: FromSsoInit = {}) => {
// @ts-ignore Cannot find module '@aws-sdk/client-sso-oidc'
const { CreateTokenCommand } = await import("@aws-sdk/nested-clients");

const ssoOidcClient = await getSsoOidcClient(ssoRegion, init);
Expand Down
1 change: 0 additions & 1 deletion packages/token-providers/src/getSsoOidcClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { FromSsoInit } from "./fromSso";
* @internal
*/
export const getSsoOidcClient = async (ssoRegion: string, init: FromSsoInit = {}) => {
// @ts-ignore Cannot find module '@aws-sdk/client-sso-oidc'
const { SSOOIDCClient } = await import("@aws-sdk/nested-clients");

const ssoOidcClient = new SSOOIDCClient(
Expand Down
8 changes: 7 additions & 1 deletion scripts/generate-clients/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { emptyDirSync, rmdirSync } = require("fs-extra");
const { generateClients, generateGenericClient, generateProtocolTests } = require("./code-gen");
const { codeOrdering } = require("./code-ordering");
const { copyToClients, copyServerTests } = require("./copy-to-clients");
const generateNestedClients = require("./nested-clients/generate-nested-clients");
const {
CODE_GEN_SDK_OUTPUT_DIR,
CODE_GEN_GENERIC_CLIENT_OUTPUT_DIR,
Expand Down Expand Up @@ -99,6 +100,7 @@ const {

if (!protocolTestsOnly) {
await generateClients(models || globs || DEFAULT_CODE_GEN_INPUT_DIR, batchSize);
await generateNestedClients();
}

if (!noPrivateClients) {
Expand Down Expand Up @@ -142,7 +144,11 @@ const {
}

require("./customizations/workspaces-thin-client")();
await spawnProcess("yarn", ["install", "--no-immutable"], { cwd: REPO_ROOT, stdio: "inherit", env: { ...process.env, CI: "" } });
await spawnProcess("yarn", ["install", "--no-immutable"], {
cwd: REPO_ROOT,
stdio: "inherit",
env: { ...process.env, CI: "" },
});
require("../runtime-dependency-version-check/runtime-dep-version-check");
} catch (e) {
console.log(e);
Expand Down
21 changes: 21 additions & 0 deletions scripts/generate-clients/nested-clients/generate-nested-clients.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ async function generateNestedClients() {

replacePackageJsonImport(join(destinationFolder, "runtimeConfig.browser.ts"));
replacePackageJsonImport(join(destinationFolder, "runtimeConfig.ts"));

replaceCredentialDefaultProvider(join(destinationFolder, "runtimeConfig.browser.ts"));
replaceCredentialDefaultProvider(join(destinationFolder, "runtimeConfig.ts"));
}
}

Expand Down Expand Up @@ -113,6 +116,9 @@ async function generateNestedClient(clientName, operations) {
rmSync(join(__dirname, "..", "..", "..", "codegen", "sdk-codegen", `smithy-build-${clientName}.json`));
}

/**
* Fix package json import filesystem level.
*/
function replacePackageJsonImport(file) {
writeFileSync(
file,
Expand All @@ -123,6 +129,21 @@ function replacePackageJsonImport(file) {
);
}

/**
* Breaks the circular dependency of STS and the default credential chain.
* STS has an auth operation but the portion of it used for credential resolution does
* not need the default chain.
*/
function replaceCredentialDefaultProvider(file) {
writeFileSync(
file,
readFileSync(file, "utf-8")
.replace(`import { defaultProvider as credentialDefaultProvider } from "@aws-sdk/credential-provider-node";`, ``)
.replace(`credentialDefaultProvider: config?.credentialDefaultProvider ?? credentialDefaultProvider,`, ``)
.replace(`await credentialDefaultProvider(`, `await config!.credentialDefaultProvider!(`)
);
}

if (process.argv.includes("--exec")) {
generateNestedClients().catch(console.error);
}

0 comments on commit 0cc2832

Please sign in to comment.