From 61c5a387b65e767dec909a254a72bd7cbaf5024d Mon Sep 17 00:00:00 2001 From: iliapolo Date: Wed, 18 Sep 2024 16:24:18 +0300 Subject: [PATCH] mid work --- lib/private/handlers/container-images.ts | 6 ++---- lib/private/handlers/files.ts | 24 ++++++------------------ lib/private/handlers/index.ts | 14 ++++++++++++++ 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/lib/private/handlers/container-images.ts b/lib/private/handlers/container-images.ts index 39fce56..b0ff312 100644 --- a/lib/private/handlers/container-images.ts +++ b/lib/private/handlers/container-images.ts @@ -1,6 +1,7 @@ import * as path from 'path'; import { DockerImageDestination } from '@aws-cdk/cloud-assembly-schema'; import type * as AWS from 'aws-sdk'; +import { destinationToClientOptions } from '.'; import { DockerImageManifestEntry } from '../../asset-manifest'; import { EventType } from '../../progress'; import { IAssetHandler, IHandlerHost, IHandlerOptions } from '../asset-handler'; @@ -105,10 +106,7 @@ export class ContainerImageAssetHandler implements IAssetHandler { const destination = await replaceAwsPlaceholders(this.asset.destination, this.host.aws); const ecr = await this.host.aws.ecrClient({ - assumeRoleArn: destination.assumeRoleArn, - assumeRoleExternalId: destination.assumeRoleExternalId, - assumeRoleAdditionalOptions: destination.assumeRoleAdditionalOptions, - region: destination.region, + ...destinationToClientOptions(destination), quiet: options.quiet, }); const account = async () => (await this.host.aws.discoverCurrentAccount())?.accountId; diff --git a/lib/private/handlers/files.ts b/lib/private/handlers/files.ts index d3152ce..1716217 100644 --- a/lib/private/handlers/files.ts +++ b/lib/private/handlers/files.ts @@ -2,6 +2,7 @@ import { createReadStream, promises as fs } from 'fs'; import * as path from 'path'; import { FileAssetPackaging, FileSource } from '@aws-cdk/cloud-assembly-schema'; import * as mime from 'mime'; +import { destinationToClientOptions } from '.'; import { FileManifestEntry } from '../../asset-manifest'; import { EventType } from '../../progress'; import { zipDirectory } from '../archive'; @@ -35,10 +36,7 @@ export class FileAssetHandler implements IAssetHandler { const s3Url = `s3://${destination.bucketName}/${destination.objectKey}`; try { const s3 = await this.host.aws.s3Client({ - assumeRoleArn: destination.assumeRoleArn, - assumeRoleExternalId: destination.assumeRoleExternalId, - assumeRoleAdditionalOptions: destination.assumeRoleAdditionalOptions, - region: destination.region, + ...destinationToClientOptions(destination), quiet: true, }); this.host.emitMessage(EventType.CHECK, `Check ${s3Url}`); @@ -56,12 +54,9 @@ export class FileAssetHandler implements IAssetHandler { public async publish(): Promise { const destination = await replaceAwsPlaceholders(this.asset.destination, this.host.aws); const s3Url = `s3://${destination.bucketName}/${destination.objectKey}`; - const s3 = await this.host.aws.s3Client({ - assumeRoleArn: destination.assumeRoleArn, - assumeRoleExternalId: destination.assumeRoleExternalId, - assumeRoleAdditionalOptions: destination.assumeRoleAdditionalOptions, - region: destination.region, - }); + + const clientOptions = destinationToClientOptions(destination); + const s3 = await this.host.aws.s3Client(clientOptions); this.host.emitMessage(EventType.CHECK, `Check ${s3Url}`); const bucketInfo = BucketInformation.for(this.host); @@ -69,14 +64,7 @@ export class FileAssetHandler implements IAssetHandler { // A thunk for describing the current account. Used when we need to format an error // message, not in the success case. const account = async () => - ( - await this.host.aws.discoverTargetAccount({ - assumeRoleArn: destination.assumeRoleArn, - assumeRoleExternalId: destination.assumeRoleExternalId, - assumeRoleAdditionalOptions: destination.assumeRoleAdditionalOptions, - region: destination.region, - }) - )?.accountId; + (await this.host.aws.discoverTargetAccount(clientOptions))?.accountId; switch (await bucketInfo.bucketOwnership(s3, destination.bucketName)) { case BucketOwnership.MINE: break; diff --git a/lib/private/handlers/index.ts b/lib/private/handlers/index.ts index 0eccd0c..d45e660 100644 --- a/lib/private/handlers/index.ts +++ b/lib/private/handlers/index.ts @@ -1,3 +1,4 @@ +import { AwsDestination } from '@aws-cdk/cloud-assembly-schema'; import { ContainerImageAssetHandler } from './container-images'; import { FileAssetHandler } from './files'; import { @@ -6,6 +7,7 @@ import { FileManifestEntry, IManifestEntry, } from '../../asset-manifest'; +import type { ClientOptions } from '../../aws'; import { IAssetHandler, IHandlerHost, IHandlerOptions } from '../asset-handler'; export function makeAssetHandler( @@ -23,3 +25,15 @@ export function makeAssetHandler( throw new Error(`Unrecognized asset type: '${asset}'`); } + +export function destinationToClientOptions(destination: AwsDestination): ClientOptions { + // Explicitly build ClientOptions from AwsDestination. The fact they are structurally compatible is coincidental. + // This also enforces better type checking that cdk-assets depends on the appropriate version of + // @aws-cdk/cloud-assembly-schema. + return { + assumeRoleArn: destination.assumeRoleArn, + assumeRoleExternalId: destination.assumeRoleExternalId, + assumeRoleAdditionalOptions: destination.assumeRoleAdditionalOptions, + region: destination.region, + }; +}