@@ -123,10 +123,62 @@ blackbox.framework.inventory() {
123123 blackbox.framework.inventory.ecr () {
124124 # @section blackbox.framework.inventory.ecr.*
125125
126+ # Reads an ECR docker-login password from the host credential handoff published under "$BLACKBOX_ECR_TOKEN_DIR"
127+ #
128+ # @arg $1 type=enum<private-password|public-password> Token file name
129+ #
130+ # @exitcode 0 If a non-empty token was read, which is printed to stdout
131+ # @exitcode 1 If the token was missing or empty
132+ #
133+ # @example
134+ # # Read the private ECR password
135+ # blackbox.framework.inventory.ecr.token.read private-password
136+ #
137+ # @see blackbox.framework.inventory.ecr.token.login
138+ function blackbox.framework.inventory.ecr.token.read() {
139+ typeset -r name=$1
140+ typeset token
141+
142+ if token=$( cat " ${BLACKBOX_ECR_TOKEN_DIR} /${name} " 2> /dev/null) && [ -n " $token " ]; then
143+ printf " %s" " $token "
144+ return 0
145+ fi
146+
147+ printf " error: *** ECR token '%s/%s' is not available\n" " $BLACKBOX_ECR_TOKEN_DIR " " $name " >&2
148+ return 1
149+ }
150+
151+ # Logs in to a Docker registry, for both root and "$BLACKBOX_USER_NAME", with a password from the host credential handoff
152+ #
153+ # @arg $1 type=string Registry
154+ # @arg $2 type=enum<private-password|public-password> Token file name
155+ #
156+ # @exitcode 0 If both the root and "$BLACKBOX_USER_NAME" logins succeeded
157+ # @exitcode 1 If no token could be read, or either login failed
158+ #
159+ # @example
160+ # # Log in to the private registry
161+ # blackbox.framework.inventory.ecr.token.login 134148934511.dkr.ecr.us-east-1.amazonaws.com private-password
162+ #
163+ # @see blackbox.framework.inventory.ecr.token.read
164+ function blackbox.framework.inventory.ecr.token.login() {
165+ typeset -r registry=$1
166+ typeset -r name=$2
167+ typeset token
168+
169+ token=$( blackbox.framework.inventory.ecr.token.read " $name " ) || return 1
170+
171+ docker login --username AWS --password-stdin " $registry " <<< " $token" \
172+ && sudo -u " $BLACKBOX_USER_NAME " docker login --username AWS --password-stdin " $registry " <<< " $token"
173+ }
174+
126175 # Logs in to Amazon ECR
127176 #
128177 # @noargs
129178 #
179+ # @exitcode 0 If the private registry login succeeded
180+ # @exitcode 1 If the private registry login failed
181+ #
130182 # @example
131183 # # Logs in to Amazon ECR
132184 # blackbox.framework.inventory.ecr.login
@@ -138,26 +190,15 @@ blackbox.framework.inventory() {
138190 }
139191
140192 blackbox.framework.inventory.provision awscli
141- (
142- export AWS_DEFAULT_REGION=" us-east-1"
143- # shellcheck disable=SC2155
144- export AWS_ACCESS_KEY_ID=$( base64 -d <( base64 -d <<< " UVV0SlFWSTJUemRIU2s1WVZGbEtOVFJNU1U4PQo=" ) )
145- # shellcheck disable=SC2155
146- export AWS_SECRET_ACCESS_KEY=$( base64 -d <( base64 -d <<< " YVZWNlRtcExlWEpJWVhOeE5UUjVUR05QTlZSbFRraEhlVWxsYzJOMVkxRkJSazVqWTJJeFpRPT0K" ) )
147193
148- # shellcheck disable=SC2155
149- local ECR_PASSWORD=$( /usr/local/aws-cli/v2/current/bin/aws ecr get-login-password --region " $AWS_DEFAULT_REGION " )
150-
151- docker login --username AWS --password-stdin 134148934511.dkr.ecr.us-east-1.amazonaws.com <<< " $ECR_PASSWORD"
152- sudo -u " $BLACKBOX_USER_NAME " docker login --username AWS --password-stdin 134148934511.dkr.ecr.us-east-1.amazonaws.com <<< " $ECR_PASSWORD"
194+ (
195+ blackbox.framework.inventory.ecr.token.login 134148934511.dkr.ecr.us-east-1.amazonaws.com private-password || exit 1
153196
154197 {
155198 # TODO: for backwards compatibility with public ECR repositories, and should be removed when all questions are moved to private ECR repositories
156- # shellcheck disable=SC2155
157- local ECR_PASSWORD_PUBLIC=$( /usr/local/aws-cli/v2/current/bin/aws ecr-public get-login-password --region " $AWS_DEFAULT_REGION " )
158-
159- docker login --username AWS --password-stdin public.ecr.aws/b0k9n8x8 <<< " $ECR_PASSWORD_PUBLIC"
160- sudo -u " $BLACKBOX_USER_NAME " docker login --username AWS --password-stdin public.ecr.aws/b0k9n8x8 <<< " $ECR_PASSWORD_PUBLIC"
199+ blackbox.framework.inventory.ecr.token.login public.ecr.aws/b0k9n8x8 public-password || {
200+ printf " warn: *** public ECR login failed, questions hosted on public repositories will not pull\n" >&2
201+ }
161202 }
162203 ) 2>&1 # ¯\_(ツ)_/¯
163204 }
0 commit comments