Skip to content

Commit 9d9315c

Browse files
authored
refactor(toolkit-lib): use credential types from @aws-cdk/cli-plugin-contract instead of @smithy/types (#470)
The `aws-auth` ap in `toolkit-lib` requires types defining how SDKv3 credentials are shaped and how a SDKv3 credentials provider is shaped. Previously we used these types directly from `@smithy/types`. To avoid taking a peer dependency on that package, we now use the CDK's version of these types from `@aws-cdk/cli-plugin-contract`. These versions are a hard copy (and rename) of the same types. Aligning on our version of the types from `@aws-cdk/cli-plugin-contract` will improve consistency and later on allow us to make them available in jsii as well. The change therefore also moves the `@aws-cdk/cli-plugin-contract` from a dependency to a peer dependency. Also includes somewhat related clean-up: - `toolkit-lib` - remove unused `@aws-cdk/region-info` dependency, widened the version constraint of `@aws-cdk/cx-api` dependency to `^2` - `cli-integ` - use latest versions of `smithy` packages as they use a different MV compared to SDK packages - CLI - use `@aws-cdk/cli-plugin-contract` types in a mock, we cannot fully remove `@smithy/types` from the CLI yet since they are part of the legacy exports - `cdk-assets` - Remove `@smithy/types` devDep - the auth types have already been hard copied into this packages and the dependency was unused --- By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license
1 parent 40a3137 commit 9d9315c

File tree

23 files changed

+64
-107
lines changed

23 files changed

+64
-107
lines changed

.projenrc.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,6 @@ const cdkAssets = configureProject(
647647
'jszip',
648648
'@types/mock-fs@^4',
649649
'mock-fs@^5',
650-
'@smithy/types',
651650
'aws-sdk-client-mock',
652651
'aws-sdk-client-mock-jest',
653652
],
@@ -730,14 +729,14 @@ const toolkitLib = configureProject(
730729
rootDir: '.', // shouldn't be required but something broke... check again once we have gotten rid of the tmpToolkitHelpers package
731730
},
732731
},
732+
peerDeps: [
733+
cliPluginContract.customizeReference({ versionType: 'major' }), // allow consumers to easily de-depulicate this
734+
],
733735
deps: [
734-
cliPluginContract,
735-
cloudAssemblySchema,
736-
// Purposely a ^ dependency so that clients selecting old toolkit library
737-
// versions still might get upgrades to this dependency.
738-
cloudFormationDiff,
739-
cxApi,
740-
'@aws-cdk/region-info',
736+
cloudAssemblySchema, // @todo need to find the minmal required version
737+
cloudFormationDiff.customizeReference({ versionType: 'major' }), // allow consumers with old toolkit-lib versions to get upgrades
738+
cdkAssets.customizeReference({ versionType: 'major' }), // allow consumers with old toolkit-lib versions to get upgrades
739+
`${cxApi}@^2`, // allow consumers with old toolkit-lib versions to get upgrades
741740
`@aws-sdk/client-appsync@${CLI_SDK_V3_RANGE}`,
742741
`@aws-sdk/client-cloudformation@${CLI_SDK_V3_RANGE}`,
743742
`@aws-sdk/client-cloudwatch-logs@${CLI_SDK_V3_RANGE}`,
@@ -766,8 +765,6 @@ const toolkitLib = configureProject(
766765
'@smithy/util-retry',
767766
'@smithy/util-waiter',
768767
'archiver',
769-
// Purposely a ^ dependency so that clients get upgrades to this library.
770-
cdkAssets,
771768
'cdk-from-cfn',
772769
'chalk@^4',
773770
'chokidar@^3',
@@ -789,7 +786,6 @@ const toolkitLib = configureProject(
789786
'@jest/globals',
790787
'@jest/types',
791788
'@microsoft/api-extractor',
792-
'@smithy/types',
793789
'@smithy/util-stream',
794790
'@types/fs-extra',
795791
'@types/split2',
@@ -1593,9 +1589,9 @@ const cliInteg = configureProject(
15931589
`@aws-sdk/client-sso@${CLI_SDK_V3_RANGE}`,
15941590
`@aws-sdk/client-sts@${CLI_SDK_V3_RANGE}`,
15951591
`@aws-sdk/credential-providers@${CLI_SDK_V3_RANGE}`,
1596-
`@smithy/util-retry@${CLI_SDK_V3_RANGE}`,
1597-
`@smithy/types@${CLI_SDK_V3_RANGE}`,
15981592
'@cdklabs/cdk-atmosphere-client',
1593+
'@smithy/util-retry', // smithy packages don't have the same major version as SDK packages
1594+
'@smithy/types', // smithy packages don't have the same major version as SDK packages
15991595
'axios@^1',
16001596
'chalk@^4',
16011597
'fs-extra@^9',

packages/@aws-cdk-testing/cli-integ/.projen/deps.json

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/cli-integ/.projen/tasks.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/cli-integ/package.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/toolkit-lib/.projen/deps.json

Lines changed: 2 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/toolkit-lib/.projen/tasks.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/toolkit-lib/build-tools/package.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ mkdir -p dist/js
1212
npm pkg set version=0.0.0-alpha.$commit
1313
npm pkg set dependencies.@aws-cdk/cloud-assembly-schema=$version
1414
npm pkg set dependencies.@aws-cdk/cloudformation-diff=$version
15-
npm pkg set dependencies.@aws-cdk/cli-plugin-contract=$version
15+
npm pkg set peerDependencies.@aws-cdk/cli-plugin-contract=\*
1616
npm pack --pack-destination dist/js
1717
npm pkg set version=$reset
18-
npm pkg set dependencies.@aws-cdk/cli-plugin-contract=^$reset
18+
npm pkg set peerDependencies.@aws-cdk/cli-plugin-contract=^$reset
1919
npm pkg set dependencies.@aws-cdk/cloudformation-diff=^$reset
2020
npm pkg set dependencies.@aws-cdk/cloud-assembly-schema=^$reset

packages/@aws-cdk/toolkit-lib/lib/api/aws-auth/awscli-compatible.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { format } from 'node:util';
2+
import type { SDKv3CompatibleCredentialProvider } from '@aws-cdk/cli-plugin-contract';
23
import { createCredentialChain, fromEnv, fromIni, fromNodeProviderChain } from '@aws-sdk/credential-providers';
34
import { MetadataService } from '@aws-sdk/ec2-metadata-service';
45
import type { NodeHttpHandlerOptions } from '@smithy/node-http-handler';
56
import { loadSharedConfigFiles } from '@smithy/shared-ini-file-loader';
6-
import type { AwsCredentialIdentityProvider } from '@smithy/types';
77
import * as promptly from 'promptly';
88
import { makeCachingProvider } from './provider-caching';
99
import { ProxyAgentProvider } from './proxy-agent';
@@ -34,7 +34,7 @@ export class AwsCliCompatible {
3434
this.logger = logger;
3535
}
3636

37-
public async baseConfig(profile?: string): Promise<{ credentialProvider: AwsCredentialIdentityProvider; defaultRegion: string }> {
37+
public async baseConfig(profile?: string): Promise<{ credentialProvider: SDKv3CompatibleCredentialProvider; defaultRegion: string }> {
3838
const credentialProvider = await this.credentialChainBuilder({
3939
profile,
4040
logger: this.logger,
@@ -50,7 +50,7 @@ export class AwsCliCompatible {
5050
*/
5151
public async credentialChainBuilder(
5252
options: CredentialChainOptions = {},
53-
): Promise<AwsCredentialIdentityProvider> {
53+
): Promise<SDKv3CompatibleCredentialProvider> {
5454
const clientConfig = {
5555
requestHandler: this.requestHandler,
5656
customUserAgent: 'aws-cdk',

packages/@aws-cdk/toolkit-lib/lib/api/aws-auth/credential-plugins.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { inspect } from 'util';
22
import type { CredentialProviderSource, ForReading, ForWriting, PluginProviderResult, SDKv2CompatibleCredentials, SDKv3CompatibleCredentialProvider, SDKv3CompatibleCredentials } from '@aws-cdk/cli-plugin-contract';
3-
import type { AwsCredentialIdentity, AwsCredentialIdentityProvider } from '@smithy/types';
43
import { credentialsAboutToExpire, makeCachingProvider } from './provider-caching';
54
import { AuthenticationError } from '../../toolkit/toolkit-error';
65
import { formatErrorMessage } from '../../util';
@@ -87,7 +86,7 @@ export interface PluginCredentialsFetchResult {
8786
/**
8887
* SDK-v3 compatible credential provider
8988
*/
90-
readonly credentials: AwsCredentialIdentityProvider;
89+
readonly credentials: SDKv3CompatibleCredentialProvider;
9190

9291
/**
9392
* Name of plugin that successfully provided credentials
@@ -110,7 +109,7 @@ export interface PluginCredentialsFetchResult {
110109
* - If the result is a static credential that expires, we will wrap it in an SDKv3 provider
111110
* that will query the plugin again when the credential expires.
112111
*/
113-
async function v3ProviderFromPlugin(producer: () => Promise<PluginProviderResult>): Promise<AwsCredentialIdentityProvider> {
112+
async function v3ProviderFromPlugin(producer: () => Promise<PluginProviderResult>): Promise<SDKv3CompatibleCredentialProvider> {
114113
const initial = await producer();
115114

116115
if (isV3Provider(initial)) {
@@ -133,7 +132,7 @@ async function v3ProviderFromPlugin(producer: () => Promise<PluginProviderResult
133132
/**
134133
* Converts a V2 credential into a V3-compatible provider
135134
*/
136-
function v3ProviderFromV2Credentials(x: SDKv2CompatibleCredentials): AwsCredentialIdentityProvider {
135+
function v3ProviderFromV2Credentials(x: SDKv2CompatibleCredentials): SDKv3CompatibleCredentialProvider {
137136
return async () => {
138137
// Get will fetch or refresh as necessary
139138
await x.getPromise();
@@ -147,7 +146,10 @@ function v3ProviderFromV2Credentials(x: SDKv2CompatibleCredentials): AwsCredenti
147146
};
148147
}
149148

150-
function refreshFromPluginProvider(current: AwsCredentialIdentity, producer: () => Promise<PluginProviderResult>): AwsCredentialIdentityProvider {
149+
function refreshFromPluginProvider(
150+
current: SDKv3CompatibleCredentials,
151+
producer: () => Promise<PluginProviderResult>,
152+
): SDKv3CompatibleCredentialProvider {
151153
return async () => {
152154
if (credentialsAboutToExpire(current)) {
153155
const newCreds = await producer();

packages/@aws-cdk/toolkit-lib/lib/api/aws-auth/provider-caching.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import type { SDKv3CompatibleCredentialProvider, SDKv3CompatibleCredentials } from '@aws-cdk/cli-plugin-contract';
12
import { memoize } from '@smithy/property-provider';
2-
import type { AwsCredentialIdentity, AwsCredentialIdentityProvider } from '@smithy/types';
33

44
/**
55
* Wrap a credential provider in a cache
@@ -11,15 +11,15 @@ import type { AwsCredentialIdentity, AwsCredentialIdentityProvider } from '@smit
1111
* MFA prompts or what have you, we are going to liberally wrap providers
1212
* in caches which will return the cached value until it expires.
1313
*/
14-
export function makeCachingProvider(provider: AwsCredentialIdentityProvider): AwsCredentialIdentityProvider {
14+
export function makeCachingProvider(provider: SDKv3CompatibleCredentialProvider): SDKv3CompatibleCredentialProvider {
1515
return memoize(
1616
provider,
1717
credentialsAboutToExpire,
1818
(token) => !!token.expiration,
1919
);
2020
}
2121

22-
export function credentialsAboutToExpire(token: AwsCredentialIdentity) {
22+
export function credentialsAboutToExpire(token: SDKv3CompatibleCredentials) {
2323
const expiryMarginSecs = 5;
2424
// token.expiration is sometimes null
2525
return !!token.expiration && token.expiration.getTime() - Date.now() < expiryMarginSecs * 1000;

packages/@aws-cdk/toolkit-lib/lib/api/aws-auth/sdk-provider.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import * as os from 'os';
2+
import type { SDKv3CompatibleCredentialProvider } from '@aws-cdk/cli-plugin-contract';
23
import type { ContextLookupRoleOptions } from '@aws-cdk/cloud-assembly-schema';
34
import type { Environment } from '@aws-cdk/cx-api';
45
import { EnvironmentUtils, UNKNOWN_ACCOUNT, UNKNOWN_REGION } from '@aws-cdk/cx-api';
56
import type { AssumeRoleCommandInput } from '@aws-sdk/client-sts';
67
import { fromTemporaryCredentials } from '@aws-sdk/credential-providers';
78
import type { NodeHttpHandlerOptions } from '@smithy/node-http-handler';
8-
import type { AwsCredentialIdentityProvider } from '@smithy/types';
99
import { AwsCliCompatible } from './awscli-compatible';
1010
import { cached } from './cached';
1111
import { CredentialPlugins } from './credential-plugins';
@@ -99,14 +99,14 @@ export class SdkProvider {
9999
}
100100

101101
public readonly defaultRegion: string;
102-
private readonly defaultCredentialProvider: AwsCredentialIdentityProvider;
102+
private readonly defaultCredentialProvider: SDKv3CompatibleCredentialProvider;
103103
private readonly plugins;
104104
private readonly requestHandler: NodeHttpHandlerOptions;
105105
private readonly ioHelper: IoHelper;
106106
private readonly logger?: ISdkLogger;
107107

108108
public constructor(
109-
defaultCredentialProvider: AwsCredentialIdentityProvider,
109+
defaultCredentialProvider: SDKv3CompatibleCredentialProvider,
110110
defaultRegion: string | undefined,
111111
services: SdkProviderServices,
112112
) {
@@ -384,7 +384,7 @@ export class SdkProvider {
384384
* @internal
385385
*/
386386
public _makeSdk(
387-
credProvider: AwsCredentialIdentityProvider,
387+
credProvider: SDKv3CompatibleCredentialProvider,
388388
region: string,
389389
) {
390390
return new SDK(credProvider, region, this.requestHandler, this.ioHelper, this.logger);
@@ -446,11 +446,11 @@ export interface CredentialsOptions {
446446
* Result of obtaining base credentials
447447
*/
448448
type ObtainBaseCredentialsResult =
449-
| { source: 'correctDefault'; credentials: AwsCredentialIdentityProvider }
450-
| { source: 'plugin'; pluginName: string; credentials: AwsCredentialIdentityProvider }
449+
| { source: 'correctDefault'; credentials: SDKv3CompatibleCredentialProvider }
450+
| { source: 'plugin'; pluginName: string; credentials: SDKv3CompatibleCredentialProvider }
451451
| {
452452
source: 'incorrectDefault';
453-
credentials: AwsCredentialIdentityProvider;
453+
credentials: SDKv3CompatibleCredentialProvider;
454454
accountId: string;
455455
unusedPlugins: string[];
456456
}

packages/@aws-cdk/toolkit-lib/lib/api/aws-auth/sdk.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { SDKv3CompatibleCredentialProvider } from '@aws-cdk/cli-plugin-contract';
12
import type {
23
FunctionConfiguration,
34
GetSchemaCreationStatusCommandInput,
@@ -340,7 +341,6 @@ import { GetCallerIdentityCommand, STSClient } from '@aws-sdk/client-sts';
340341
import { Upload } from '@aws-sdk/lib-storage';
341342
import { getEndpointFromInstructions } from '@smithy/middleware-endpoint';
342343
import type { NodeHttpHandlerOptions } from '@smithy/node-http-handler';
343-
import type { AwsCredentialIdentityProvider } from '@smithy/types';
344344
import { ConfiguredRetryStrategy } from '@smithy/util-retry';
345345
import type { WaiterResult } from '@smithy/util-waiter';
346346
import { AccountAccessKeyCache } from './account-cache';
@@ -382,7 +382,7 @@ export interface SdkOptions {
382382

383383
export interface ConfigurationOptions {
384384
region: string;
385-
credentials: AwsCredentialIdentityProvider;
385+
credentials: SDKv3CompatibleCredentialProvider;
386386
requestHandler: NodeHttpHandlerOptions;
387387
retryStrategy: ConfiguredRetryStrategy;
388388
customUserAgent: string;
@@ -587,7 +587,7 @@ export class SDK {
587587
private readonly debug: (msg: string) => Promise<void>;
588588

589589
constructor(
590-
private readonly credProvider: AwsCredentialIdentityProvider,
590+
private readonly credProvider: SDKv3CompatibleCredentialProvider,
591591
region: string,
592592
requestHandler: NodeHttpHandlerOptions,
593593
ioHelper: IoHelper,

packages/@aws-cdk/toolkit-lib/lib/api/aws-auth/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { AwsCredentialIdentityProvider } from '@smithy/types';
1+
import type { SDKv3CompatibleCredentialProvider } from '@aws-cdk/cli-plugin-contract';
22
import type { SdkProviderServices } from '../shared-private';
33
import { AwsCliCompatible } from '../shared-private';
44

@@ -146,7 +146,7 @@ export interface CustomBaseCredentialsOption {
146146
* sure to also configure the necessary HTTP options (like proxy and user
147147
* agent) on the STS client directly; the toolkit code cannot do this for you.
148148
*/
149-
readonly provider: AwsCredentialIdentityProvider;
149+
readonly provider: SDKv3CompatibleCredentialProvider;
150150

151151
/**
152152
* The default region to synthesize for
@@ -161,7 +161,7 @@ export interface CustomBaseCredentialsOption {
161161
}
162162

163163
export interface SdkBaseConfig {
164-
readonly credentialProvider: AwsCredentialIdentityProvider;
164+
readonly credentialProvider: SDKv3CompatibleCredentialProvider;
165165

166166
readonly defaultRegion?: string;
167167
}

packages/@aws-cdk/toolkit-lib/package.json

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)