-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
opm does not allow multiple authentications in the same registry, e.g. quay.io, quay.io/ns, quay.io/ns/repo as reported in operator-framework/operator-registry#935 The wrapper will split the auths and try one at a time to attempt the authentication. Change-Id: If7daa16647f295697a887585f7d03454ba5b1207
- Loading branch information
1 parent
0e022e9
commit b57cd6a
Showing
1 changed file
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
#!/bin/bash | ||
# | ||
# Copyright (C) 2023 Red Hat, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
# not use this file except in compliance with the License. You may obtain | ||
# a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations | ||
# under the License. | ||
|
||
|
||
function help(){ | ||
echo "$(basename "${0}") <opm_path> <opm_args>" | ||
echo | ||
echo "A wrapper to allow opm to use multiple auths in the same registry" | ||
echo "Example: quay.io, quay.io/namespace, quay.io/namespace/repo" | ||
# https://github.com/operator-framework/operator-registry/issues/935 | ||
echo | ||
echo "Options:" | ||
echo " opm_path: The path to opm binary" | ||
echo " opm_args: Arguments to pass to opm" | ||
echo | ||
echo "Env:" | ||
echo " DOCKER_CONFIG: The directory with a docker config file (config.json)" | ||
echo " Default: ~/.docker/config.json" | ||
echo | ||
} | ||
|
||
function loop_auths() { | ||
tmp_docker_config=$(mktemp -d) | ||
result=1 | ||
auths=$(jq -r '.auths | keys[]' "${docker_config_file}") | ||
# shellcheck disable=SC2068 | ||
for auth in ${auths[@]}; do | ||
jq -r '.auths."'"${auth}"'" | {auths: {"'"${auth}"'": {auth: .auth}}}' "${docker_config_file}" \ | ||
>"${tmp_docker_config}/config.json" | ||
# shellcheck disable=SC2086 | ||
if ! DOCKER_CONFIG="${tmp_docker_config}" "${opm}" ${opm_args}; then | ||
continue | ||
else | ||
result=0 | ||
break | ||
fi | ||
done | ||
|
||
rm -Rf "${tmp_docker_config}" | ||
if [[ ${result} != 0 ]]; then | ||
echo "Error: Unable to authenticate with any credential" >&2 | ||
exit 1 | ||
fi | ||
} | ||
|
||
if [[ $# -lt 1 ]]; then | ||
echo "Error: Missing opm path" >&2 | ||
help | ||
exit 1 | ||
fi | ||
|
||
opm="${1}" | ||
if [[ ! -x ${opm} ]]; then | ||
echo "Error: ${opm} can't execute, skipping" >&2 | ||
exit 1 | ||
fi | ||
|
||
opm_args="${*:2}" | ||
if [[ -n "${DOCKER_CONFIG}" ]]; then | ||
docker_config_file="${DOCKER_CONFIG}/config.json" | ||
else | ||
docker_config_file="${HOME}/.docker/config.json" | ||
fi | ||
|
||
# Run without DOCKER_CONFIG | ||
if [[ ! -r ${docker_config_file} ]]; then | ||
# shellcheck disable=SC2086 | ||
"${opm}" ${opm_args} | ||
exit $? | ||
fi | ||
|
||
# Run with DOCKER_CONFIG | ||
# shellcheck disable=SC2086 | ||
if ! DOCKER_CONFIG="${DOCKER_CONFIG}" "${opm}" ${opm_args}; then | ||
loop_auths | ||
else | ||
exit 0 | ||
fi |