1- import { throwIfEnvironmentVariableUnset } from './helpers' ;
21import * as kubectl from '../../helpers/kubectl' ;
32import { execWrapper as exec } from '../../helpers/exec' ;
3+ import { throwIfEnvironmentVariableUnset } from './helpers' ;
44
55export async function validateRequiredEnvironment ( ) : Promise < void > {
66 console . log (
@@ -14,9 +14,6 @@ export async function validateRequiredEnvironment(): Promise<void> {
1414}
1515
1616export async function setupTester ( ) : Promise < void > {
17- // update the `aws` CLI, the one in CircleCI's default image is outdated and doens't support eks
18- await exec ( 'pip install awscli --ignore-installed six' ) ;
19-
2017 // TODO: assert all the vars are present before starting the setup?
2118 // TODO: wipe out the data during teardown?
2219 await exec (
@@ -48,24 +45,24 @@ export async function loadImageInCluster(
4845) : Promise < string > {
4946 console . log ( `Loading image ${ imageNameAndTag } in ECR...` ) ;
5047
51- const ecrLogin = await exec (
52- 'aws ecr get-login --region us-east-2 --no-include-email ' ,
48+ const accountIdResult = await exec (
49+ 'aws sts get-caller-identity --query Account --output text ' ,
5350 ) ;
51+ const accountId = accountIdResult . stdout . trim ( ) ;
52+ const ecrURL = `${ accountId } .dkr.ecr.${ process . env . AWS_REGION } .amazonaws.com` ;
5453
55- // aws ecr get-login returns something that looks like:
56- // docker login -U AWS -p <secret> https://the-address-of-ecr-we-should-use.com
57- // `docker tag` wants just the last part without https://
58- // `docker login` wants everything
54+ const ecrLogin = await exec (
55+ `aws ecr get-login-password | docker login --username AWS --password-stdin "${ ecrURL } "` ,
56+ ) ;
5957
6058 // validate output so we don't execute malicious stuff
61- if ( ecrLogin . stdout . indexOf ( 'docker login -u AWS -p' ) !== 0 ) {
62- throw new Error ( 'aws ecr get-login returned an unexpected output' ) ;
59+ if ( ! ecrLogin . stdout . includes ( 'Login Succeeded' ) ) {
60+ throw new Error ( 'aws ecr get-login-password returned an unexpected output' ) ;
6361 }
6462
65- const targetImage = targetImageFromLoginDetails ( ecrLogin . stdout ) ;
63+ const targetImage = ` ${ ecrURL } /snyk/kubernetes-monitor:local` ;
6664
6765 await exec ( `docker tag ${ imageNameAndTag } ${ targetImage } ` ) ;
68- await exec ( ecrLogin . stdout ) ;
6966 await exec ( `docker push ${ targetImage } ` ) ;
7067
7168 console . log ( `Loaded image ${ targetImage } in ECR` ) ;
@@ -78,11 +75,3 @@ export async function clean(): Promise<void> {
7875 kubectl . deleteNamespace ( 'snyk-monitor' ) ,
7976 ] ) ;
8077}
81-
82- function targetImageFromLoginDetails ( ecrLoginOutput : string ) : string {
83- const split = ecrLoginOutput . split ( ' ' ) ;
84- const targetImagePrefix = split [ split . length - 1 ]
85- . replace ( 'https://' , '' )
86- . trim ( ) ;
87- return `${ targetImagePrefix } /snyk/kubernetes-monitor:local` ;
88- }
0 commit comments