1+
2+ import { promises as fs } from 'fs' ;
3+ import * as path from 'path' ;
4+ import { integTest , withDefaultFixture } from '../../../lib' ;
5+
6+ jest . setTimeout ( 2 * 60 * 60_000 ) ; // Includes the time to acquire locks, worst-case single-threaded runtime
7+
8+ integTest ( `cdk-assets uses profile when specified` , withDefaultFixture ( async ( fixture ) => {
9+
10+ const currentCreds = await fixture . aws . credentials ( ) ;
11+
12+ await fixture . shell ( [ 'npm' , 'init' , '-y' ] ) ;
13+ await fixture . shell ( [ 'npm' , 'install' , `cdk-assets@latest` ] ) ;
14+
15+ const account = await fixture . aws . account ( ) ;
16+ const region = fixture . aws . region ;
17+ const bucketName = `cdk-hnb659fds-assets-${ account } -${ region } ` ;
18+
19+ // Write some asset files. Its important to have more than 1 because cdk-assets
20+ // code has some funky state mutations that happens on each asset publishing.
21+ const assetFile1 = 'testfile.txt' ;
22+ const assetFile2 = 'testfile.txt' ;
23+ await fs . writeFile ( path . join ( fixture . integTestDir , assetFile1 ) , 'some asset file' ) ;
24+ await fs . writeFile ( path . join ( fixture . integTestDir , assetFile2 ) , 'some asset file' ) ;
25+
26+ // Write an asset JSON file to publish to the bootstrapped environment
27+ const assetsJson = {
28+ version : "38.0.1" ,
29+ files : {
30+ testfile1 : {
31+ source : {
32+ path : assetFile1 ,
33+ packaging : 'file' ,
34+ } ,
35+ destinations : {
36+ current : {
37+ region,
38+ assumeRoleArn : `arn:\${AWS::Partition}:iam::${ account } :role/cdk-hnb659fds-file-publishing-role-${ account } -${ region } ` ,
39+ bucketName,
40+ objectKey : `test-file1-${ Date . now ( ) } .json` ,
41+ }
42+ }
43+ } ,
44+ testfile2 : {
45+ source : {
46+ path : assetFile2 ,
47+ packaging : 'file' ,
48+ } ,
49+ destinations : {
50+ current : {
51+ region,
52+ assumeRoleArn : `arn:\${AWS::Partition}:iam::${ account } :role/cdk-hnb659fds-file-publishing-role-${ account } -${ region } ` ,
53+ bucketName,
54+ objectKey : `test-file2-${ Date . now ( ) } .json` ,
55+ }
56+ }
57+ }
58+ }
59+ } ;
60+
61+ // create a profile with our current credentials.
62+ //
63+ // if you're wondering why can't we do the reverse (i.e write a bogus profile and assert a failure),
64+ // its because when cdk-assets discovers the current account, it DOES consider the profile.
65+ // writing a bogus profile would fail this operation and we won't be able to reach the code
66+ // we're trying to test.
67+ const credentialsFile = path . join ( fixture . integTestDir , 'aws.credentials' ) ;
68+ const profile = 'cdk-assets' ;
69+
70+ // this kind sucks but its what it is given we need to write a working profile
71+ await fs . writeFile ( credentialsFile , `[${ profile } ]
72+ aws_access_key_id=${ currentCreds . accessKeyId }
73+ aws_secret_access_key=${ currentCreds . secretAccessKey }
74+ aws_session_token=${ currentCreds . sessionToken } ` ) ;
75+
76+ await fs . writeFile ( path . join ( fixture . integTestDir , 'assets.json' ) , JSON . stringify ( assetsJson , undefined , 2 ) ) ;
77+ await fixture . shell ( [ 'npx' , 'cdk-assets' , '--path' , 'assets.json' , 'publish' , '--profile' , profile ] , {
78+ modEnv : {
79+ ...fixture . cdkShellEnv ( ) ,
80+ AWS_SHARED_CREDENTIALS_FILE : credentialsFile ,
81+
82+ // remove the default creds so that if the command doesn't use
83+ // the profile, it will fail with "Could not load credentials from any providers"
84+ AWS_ACCESS_KEY_ID : '' ,
85+ AWS_SECRET_ACCESS_KEY : '' ,
86+ AWS_SESSION_TOKEN : ''
87+
88+ } ,
89+ } ) ;
90+
91+ } ) ,
92+ ) ;
0 commit comments