Skip to content

Commit 55f3694

Browse files
authored
feat: aws ecr credential helper (#327)
* feat: add support for aws ecr credential helper * ci: add testing for credential helper * fix: add standard login for public registries * fix: address comments
1 parent 8dd1a67 commit 55f3694

File tree

4 files changed

+114
-10
lines changed

4 files changed

+114
-10
lines changed

.circleci/test-deploy.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ jobs:
2121
- persist_to_workspace:
2222
root: .
2323
paths: [sample/Dockerfile]
24+
2425
tag-ecr-image:
2526
docker:
2627
- image: cimg/base:current
@@ -174,6 +175,36 @@ workflows:
174175
platform: linux/amd64,linux/arm64
175176
filters: *filters
176177
requires: [pre-integration]
178+
- aws-ecr/build_and_push_image:
179+
name: integration-test-aws-ecr-credential-helper
180+
auth:
181+
- aws-cli/setup:
182+
role_arn: arn:aws:iam::122211685980:role/CPE_ECR_OIDC_TEST
183+
attach_workspace: true
184+
workspace_root: workspace
185+
repo: aws-ecr-orb-${CIRCLE_SHA1:0:7}-credential-helper
186+
create_repo: true
187+
context: [CPE-OIDC]
188+
tag: credential-helper
189+
dockerfile: sample/Dockerfile
190+
path: workspace
191+
executor: amd64
192+
post-steps:
193+
- run:
194+
name: Verify ~/.docker/config.json
195+
command: |
196+
if [ -f "$HOME/.docker/config.json" ] && grep 122211685980.dkr.ecr.us-west-2.amazonaws.com < ~/.docker/config.json; then
197+
echo "AWS ECR Credential Helper correctly configured."
198+
exit 0
199+
else
200+
echo "AWS ECR Credential Helper not configured."
201+
exit 1
202+
fi
203+
- run:
204+
name: "Delete repository"
205+
command: aws ecr delete-repository --repository-name --region us-west-2 aws-ecr-orb-${CIRCLE_SHA1:0:7}-credential-helper --force
206+
filters: *filters
207+
requires: [pre-integration]
177208
- aws-ecr/build_and_push_image:
178209
name: integration-test-cache-to-flag
179210
auth:

src/commands/ecr_login.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
description: >
2-
Authenticate into the Amazon ECR service.
2+
Authenticate into the Amazon ECR service. This command requires jq.
33
NOTE: Some commands may not work with AWS CLI Version 1.
44
55
parameters:
@@ -42,4 +42,5 @@ steps:
4242
AWS_ECR_STR_REGION: <<parameters.region>>
4343
AWS_ECR_BOOL_PUBLIC_REGISTRY: <<parameters.public_registry>>
4444
AWS_ECR_STR_AWS_DOMAIN: <<parameters.aws_domain>>
45+
SCRIPT_UTILS: << include(scripts/utils.sh) >>
4546
command: <<include(scripts/ecr_login.sh)>>

src/scripts/ecr_login.sh

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,59 @@ AWS_ECR_EVAL_REGION="$(eval echo "${AWS_ECR_STR_REGION}")"
33
AWS_ECR_EVAL_PROFILE_NAME="$(eval echo "${AWS_ECR_STR_PROFILE_NAME}")"
44
AWS_ECR_EVAL_ACCOUNT_ID="$(eval echo "${AWS_ECR_STR_ACCOUNT_ID}")"
55
AWS_ECR_VAL_ACCOUNT_URL="${AWS_ECR_EVAL_ACCOUNT_ID}.dkr.ecr.${AWS_ECR_EVAL_REGION}.${AWS_ECR_STR_AWS_DOMAIN}"
6+
AWS_ECR_EVAL_PUBLIC_REGISTRY_ALIAS="$(eval echo "${AWS_ECR_STR_PUBLIC_REGISTRY_ALIAS}")"
67
ECR_COMMAND="ecr"
78

9+
eval "$SCRIPT_UTILS"
10+
detect_os
11+
set_sudo
12+
813
if [ -z "${AWS_ECR_EVAL_ACCOUNT_ID}" ]; then
914
echo "The account ID is not found. Please add the account ID before continuing."
1015
exit 1
1116
fi
1217

1318
if [ "$AWS_ECR_BOOL_PUBLIC_REGISTRY" == "1" ]; then
1419
AWS_ECR_EVAL_REGION="us-east-1"
15-
AWS_ECR_VAL_ACCOUNT_URL="public.ecr.aws"
20+
AWS_ECR_VAL_ACCOUNT_URL="public.ecr.aws/${AWS_ECR_EVAL_PUBLIC_REGISTRY_ALIAS}"
1621
ECR_COMMAND="ecr-public"
17-
fi
18-
19-
if [ -n "${AWS_ECR_EVAL_PROFILE_NAME}" ]; then
20-
set -- "$@" --profile "${AWS_ECR_EVAL_PROFILE_NAME}"
22+
aws "${ECR_COMMAND}" get-login-password --region "${AWS_ECR_EVAL_REGION}" --profile "${AWS_ECR_EVAL_PROFILE_NAME}" \
23+
| docker login --username AWS --password-stdin "${AWS_ECR_VAL_ACCOUNT_URL}"
24+
exit 0
2125
fi
2226

2327
if [ -f "$HOME/.docker/config.json" ] && grep "${AWS_ECR_VAL_ACCOUNT_URL}" < ~/.docker/config.json > /dev/null 2>&1 ; then
24-
echo "Credential helper is already installed"
25-
else
26-
docker logout "${AWS_ECR_VAL_ACCOUNT_URL}"
27-
aws "${ECR_COMMAND}" get-login-password --region "${AWS_ECR_EVAL_REGION}" "$@" | docker login --username AWS --password-stdin "${AWS_ECR_VAL_ACCOUNT_URL}"
28+
echo "Credential helper is already installed and configured"
29+
exit 0
2830
fi
31+
32+
configure_config_json(){
33+
echo "Configuring config.json..."
34+
CONFIG_FILE="$HOME/.docker/config.json"
35+
mkdir -p "$(dirname "${CONFIG_FILE}")"
36+
37+
jq_flag=""
38+
if [ ! -s "${CONFIG_FILE}" ]; then
39+
jq_flag="-n"
40+
fi
41+
jq "${jq_flag}" --arg url "${AWS_ECR_VAL_ACCOUNT_URL}" \
42+
--arg helper "ecr-login" '.credHelpers[$url] = $helper' \
43+
"${CONFIG_FILE}" > temp.json && mv temp.json "${CONFIG_FILE}"
44+
}
45+
46+
install_aws_ecr_credential_helper(){
47+
echo "Installing AWS ECR Credential Helper..."
48+
if [[ "$SYS_ENV_PLATFORM" = "linux" ]]; then
49+
$SUDO apt update
50+
$SUDO apt install amazon-ecr-credential-helper
51+
configure_config_json
52+
elif [[ "$SYS_ENV_PLATFORM" = "macos" ]]; then
53+
brew install docker-credential-helper-ecr
54+
configure_config_json
55+
else
56+
docker logout "${AWS_ECR_VAL_ACCOUNT_URL}"
57+
aws "${ECR_COMMAND}" get-login-password --region "${AWS_ECR_EVAL_REGION}" --profile "${AWS_ECR_EVAL_PROFILE_NAME}" | docker login --username AWS --password-stdin "${AWS_ECR_VAL_ACCOUNT_URL}"
58+
fi
59+
}
60+
61+
install_aws_ecr_credential_helper

src/scripts/utils.sh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/bin/bash
2+
3+
detect_os() {
4+
detected_platform="$(uname -s | tr '[:upper:]' '[:lower:]')"
5+
6+
case "$detected_platform" in
7+
linux*)
8+
if grep "Alpine" /etc/issue >/dev/null 2>&1; then
9+
printf '%s\n' "Detected OS: Alpine Linux."
10+
SYS_ENV_PLATFORM=linux_alpine
11+
else
12+
printf '%s\n' "Detected OS: Linux."
13+
SYS_ENV_PLATFORM=linux
14+
fi
15+
;;
16+
darwin*)
17+
printf '%s\n' "Detected OS: macOS."
18+
SYS_ENV_PLATFORM=macos
19+
;;
20+
msys*|cygwin*)
21+
printf '%s\n' "Detected OS: Windows."
22+
SYS_ENV_PLATFORM=windows
23+
;;
24+
*)
25+
printf '%s\n' "Unsupported OS: \"$detected_platform\"."
26+
exit 1
27+
;;
28+
esac
29+
30+
export SYS_ENV_PLATFORM
31+
}
32+
33+
set_sudo(){
34+
if [ "$SYS_ENV_PLATFORM" = "linux_alpine" ]; then
35+
if [ "$ID" = 0 ]; then export SUDO=""; else export SUDO="sudo"; fi
36+
else
37+
if [ "$EUID" = 0 ]; then export SUDO=""; else export SUDO="sudo"; fi
38+
fi
39+
}

0 commit comments

Comments
 (0)