Skip to content

Commit 5c68d02

Browse files
authored
Merge pull request #49 from wunderio/feature/aws-compatibility
Feature/aws compatibility
2 parents f339545 + eec257b commit 5c68d02

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

cmd/ciImageLogin.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Available flags and environment variables:
3232
3333
* Amazon Web Services:
3434
- "--aws-secret-access-key" flag or "AWS_SECRET_ACCESS_KEY" environment variable: Amazon Web Services IAM account key (string value)
35+
- "--aws-region" flag or "AWS_REGION" environment variable: Region of the container repository
3536
3637
* Azure Services:
3738
- "--aks-tenant-id" flag or "AKS_TENANT_ID" environment variable: Azure Services tenant id
@@ -47,6 +48,7 @@ Available flags and environment variables:
4748
imageRepoPass, _ := cmd.Flags().GetString("image-repo-pass")
4849
gcpKeyJson, _ := cmd.Flags().GetString("gcp-key-json")
4950
awsSecretAccessKey, _ := cmd.Flags().GetString("aws-secret-access-key")
51+
awsRegion, _ := cmd.Flags().GetString("aws-region")
5052
aksTenantID, _ := cmd.Flags().GetString("aks-tenant-id")
5153
aksSPAppID, _ := cmd.Flags().GetString("aks-sp-app-id")
5254
aksSPPass, _ := cmd.Flags().GetString("aks-sp-password")
@@ -62,6 +64,9 @@ Available flags and environment variables:
6264
if len(awsSecretAccessKey) == 0 {
6365
awsSecretAccessKey = os.Getenv("AWS_SECRET_ACCESS_KEY")
6466
}
67+
if len(awsRegion) == 0 {
68+
awsRegion = os.Getenv("AWS_REGION")
69+
}
6570
if len(imageRepoHost) == 0 {
6671
imageRepoHost = os.Getenv("IMAGE_REPO_HOST")
6772
}
@@ -93,6 +98,7 @@ Available flags and environment variables:
9398
fmt.Println("IMAGE_REPO_PASS:", imageRepoPass)
9499
fmt.Println("GCLOUD_KEY_JSON:", gcpKeyJson)
95100
fmt.Println("AWS_SECRET_ACCESS_KEY:", awsSecretAccessKey)
101+
fmt.Println("AWS_REGION:", awsRegion)
96102
fmt.Println("AKS_TENANT_ID:", aksTenantID)
97103
fmt.Println("AKS_SP_APP_ID:", aksSPAppID)
98104
fmt.Println("AKS_SP_PASSWORD:", aksSPPass)
@@ -121,8 +127,15 @@ Available flags and environment variables:
121127
command = fmt.Sprintf("echo %q | docker login --username %q --password-stdin %s", gcpKeyJson, "_json_key", imageRepoHost)
122128

123129
} else if awsSecretAccessKey != "" {
130+
//Get AWS Account ID
131+
awsAccountId, err := exec.Command("bash", "-c", "aws sts get-caller-identity --query \"Account\" --output text --no-cli-pager").CombinedOutput()
132+
if err != nil {
133+
log.Fatal("Error:", err)
134+
}
135+
//Remove newline
136+
awsAccountId = []byte(strings.TrimSuffix(string(awsAccountId), "\n"))
124137
// ECR login
125-
command = fmt.Sprintf("aws ecr get-login --no-include-email | bash")
138+
command = fmt.Sprintf("aws ecr get-login-password --region %s | docker login --username AWS --password-stdin %s.dkr.ecr.%s.amazonaws.com", awsRegion, awsAccountId, awsRegion)
126139
// TODO: use aws cli v2
127140
// command = fmt.Sprintf("echo %q | docker login --username AWS --password-stdin %s", awsSecretAccessKey, imageRepoHost)
128141

@@ -155,6 +168,7 @@ func init() {
155168
ciImageLoginCmd.Flags().String("image-repo-pass", "", "(Docker) container image repository password")
156169
ciImageLoginCmd.Flags().String("gcp-key-json", "", "Google Cloud service account key (plaintext, json)")
157170
ciImageLoginCmd.Flags().String("aws-secret-access-key", "", "Amazon Web Services IAM account key (string value)")
171+
ciImageLoginCmd.Flags().String("aws-region", "", "Elastic Container Registry region (string value)")
158172
ciImageLoginCmd.Flags().String("aks-tenant-id", "", "Azure Services tenant id")
159173
ciImageLoginCmd.Flags().String("aks-sp-app-id", "", "Azure Services servicePrincipal app id")
160174
ciImageLoginCmd.Flags().String("aks-sp-password", "", "Azure Services servicePrincipal password")

tests/image_test.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,21 @@ func TestImageLoginCmd(t *testing.T) {
2828
"IMAGE_REPO_PASS=222",
2929
"GCLOUD_KEY_JSON=333",
3030
"AWS_SECRET_ACCESS_KEY=444",
31-
"AKS_TENANT_ID=555",
32-
"AKS_SP_APP_ID=666",
33-
"AKS_SP_PASSWORD=777",
31+
"AWS_REGION=555",
32+
"AKS_TENANT_ID=666",
33+
"AKS_SP_APP_ID=777",
34+
"AKS_SP_PASSWORD=888",
3435
}
3536
testString = `IMAGE_REPO_HOST: foo.bar
3637
IMAGE_REPO_TLS: true
3738
IMAGE_REPO_USER: 111
3839
IMAGE_REPO_PASS: 222
3940
GCLOUD_KEY_JSON: 333
4041
AWS_SECRET_ACCESS_KEY: 444
41-
AKS_TENANT_ID: 555
42-
AKS_SP_APP_ID: 666
43-
AKS_SP_PASSWORD: 777
42+
AWS_REGION: 555
43+
AKS_TENANT_ID: 666
44+
AKS_SP_APP_ID: 777
45+
AKS_SP_PASSWORD: 888
4446
Command (not executed): echo "222" | docker login --username "111" --password-stdin https://foo.bar`
4547

4648
CliExecTest(t, command, environment, testString, false)
@@ -64,9 +66,10 @@ Command (not executed): echo "222" | docker login --username "111" --password-st
6466
--image-repo-pass 222 \
6567
--gcp-key-json 333 \
6668
--aws-secret-access-key 444 \
67-
--aks-tenant-id 555 \
68-
--aks-sp-app-id 666 \
69-
--aks-sp-password 777 \
69+
--aws-region 555 \
70+
--aks-tenant-id 666 \
71+
--aks-sp-app-id 777 \
72+
--aks-sp-password 888 \
7073
--debug`
7174

7275
environment = []string{}
@@ -76,9 +79,10 @@ IMAGE_REPO_USER: 111
7679
IMAGE_REPO_PASS: 222
7780
GCLOUD_KEY_JSON: 333
7881
AWS_SECRET_ACCESS_KEY: 444
79-
AKS_TENANT_ID: 555
80-
AKS_SP_APP_ID: 666
81-
AKS_SP_PASSWORD: 777
82+
AWS_REGION: 555
83+
AKS_TENANT_ID: 666
84+
AKS_SP_APP_ID: 777
85+
AKS_SP_PASSWORD: 888
8286
Command (not executed): echo "222" | docker login --username "111" --password-stdin https://foo.bar`
8387
CliExecTest(t, command, environment, testString, false)
8488

0 commit comments

Comments
 (0)