@@ -249,10 +249,62 @@ blackbox.framework.inventory() {
249249 blackbox.framework.inventory.ecr () {
250250 # @section blackbox.framework.inventory.ecr.*
251251
252+ # Reads an ECR docker-login password from the host credential handoff published under "$BLACKBOX_ECR_TOKEN_DIR"
253+ #
254+ # @arg $1 type=enum<private-password|public-password> Token file name
255+ #
256+ # @exitcode 0 If a non-empty token was read, which is printed to stdout
257+ # @exitcode 1 If the token was missing or empty
258+ #
259+ # @example
260+ # # Read the private ECR password
261+ # blackbox.framework.inventory.ecr.token.read private-password
262+ #
263+ # @see blackbox.framework.inventory.ecr.token.login
264+ function blackbox.framework.inventory.ecr.token.read() {
265+ typeset -r name=$1
266+ typeset token
267+
268+ if token=$( cat " ${BLACKBOX_ECR_TOKEN_DIR} /${name} " 2> /dev/null) && [ -n " $token " ]; then
269+ printf " %s" " $token "
270+ return 0
271+ fi
272+
273+ printf " error: *** ECR token '%s/%s' is not available\n" " $BLACKBOX_ECR_TOKEN_DIR " " $name " >&2
274+ return 1
275+ }
276+
277+ # Logs in to a Docker registry, for both root and "$BLACKBOX_USER_NAME", with a password from the host credential handoff
278+ #
279+ # @arg $1 type=string Registry
280+ # @arg $2 type=enum<private-password|public-password> Token file name
281+ #
282+ # @exitcode 0 If both the root and "$BLACKBOX_USER_NAME" logins succeeded
283+ # @exitcode 1 If no token could be read, or either login failed
284+ #
285+ # @example
286+ # # Log in to the private registry
287+ # blackbox.framework.inventory.ecr.token.login 134148934511.dkr.ecr.us-east-1.amazonaws.com private-password
288+ #
289+ # @see blackbox.framework.inventory.ecr.token.read
290+ function blackbox.framework.inventory.ecr.token.login() {
291+ typeset -r registry=$1
292+ typeset -r name=$2
293+ typeset token
294+
295+ token=$( blackbox.framework.inventory.ecr.token.read " $name " ) || return 1
296+
297+ docker login --username AWS --password-stdin " $registry " <<< " $token" \
298+ && sudo -u " $BLACKBOX_USER_NAME " docker login --username AWS --password-stdin " $registry " <<< " $token"
299+ }
300+
252301 # Logs in to Amazon ECR
253302 #
254303 # @noargs
255304 #
305+ # @exitcode 0 If the private registry login succeeded
306+ # @exitcode 1 If the private registry login failed
307+ #
256308 # @example
257309 # # Logs in to Amazon ECR
258310 # blackbox.framework.inventory.ecr.login
@@ -264,26 +316,15 @@ blackbox.framework.inventory() {
264316 }
265317
266318 blackbox.framework.inventory.provision awscli
267- (
268- export AWS_DEFAULT_REGION=" us-east-1"
269- # shellcheck disable=SC2155
270- export AWS_ACCESS_KEY_ID=$( base64 -d <( base64 -d <<< " UVV0SlFWSTJUemRIU2s1WVZGbEtOVFJNU1U4PQo=" ) )
271- # shellcheck disable=SC2155
272- export AWS_SECRET_ACCESS_KEY=$( base64 -d <( base64 -d <<< " YVZWNlRtcExlWEpJWVhOeE5UUjVUR05QTlZSbFRraEhlVWxsYzJOMVkxRkJSazVqWTJJeFpRPT0K" ) )
273-
274- # shellcheck disable=SC2155
275- local ECR_PASSWORD=$( /usr/local/aws-cli/v2/current/bin/aws ecr get-login-password --region " $AWS_DEFAULT_REGION " )
276319
277- docker login --username AWS --password-stdin 134148934511.dkr.ecr.us-east-1.amazonaws.com <<< " $ECR_PASSWORD "
278- sudo -u " $BLACKBOX_USER_NAME " docker login --username AWS --password-stdin 134148934511.dkr.ecr.us-east-1.amazonaws.com <<< " $ECR_PASSWORD "
320+ (
321+ blackbox.framework.inventory.ecr.token. login 134148934511.dkr.ecr.us-east-1.amazonaws.com private-password || exit 1
279322
280323 {
281324 # TODO: for backwards compatibility with public ECR repositories, and should be removed when all questions are moved to private ECR repositories
282- # shellcheck disable=SC2155
283- local ECR_PASSWORD_PUBLIC=$( /usr/local/aws-cli/v2/current/bin/aws ecr-public get-login-password --region " $AWS_DEFAULT_REGION " )
284-
285- docker login --username AWS --password-stdin public.ecr.aws/b0k9n8x8 <<< " $ECR_PASSWORD_PUBLIC"
286- sudo -u " $BLACKBOX_USER_NAME " docker login --username AWS --password-stdin public.ecr.aws/b0k9n8x8 <<< " $ECR_PASSWORD_PUBLIC"
325+ blackbox.framework.inventory.ecr.token.login public.ecr.aws/b0k9n8x8 public-password || {
326+ printf " warn: *** public ECR login failed, questions hosted on public repositories will not pull\n" >&2
327+ }
287328 }
288329 ) 2>&1 # ¯\_(ツ)_/¯
289330 }
0 commit comments