187 lines
6.6 KiB
Bash
Executable File
187 lines
6.6 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -u
|
|
# shellcheck source=harness.sh
|
|
source "$(dirname "$0")/harness.sh"
|
|
DC="$SCRIPTS/docker-creds.sh"
|
|
|
|
setup() {
|
|
export HOME="$T/home"; mkdir -p "$HOME"
|
|
export GNUPGHOME="$T/gnupg"; mkdir -p "$GNUPGHOME"; chmod 700 "$GNUPGHOME"
|
|
export PATH="$SHIMS:$PATH"
|
|
export FAKE_PASS_LOG="$T/pass.log"
|
|
unset DOCKER_CONFIG PASSWORD_STORE_DIR
|
|
CFG="$HOME/.docker/config.json"
|
|
}
|
|
jq_get() { python3 -c 'import json,sys; d=json.load(open(sys.argv[1])); print(json.dumps(d.get(sys.argv[2]), sort_keys=True))' "$CFG" "$1"; }
|
|
secret_key_count() { gpg --list-secret-keys --with-colons 2>/dev/null | grep -c '^sec:' || true; }
|
|
|
|
test_non_pass_store_only_edits_config() {
|
|
setup
|
|
run "$DC" secretservice
|
|
assert_rc 0
|
|
assert_eq '"secretservice"' "$(jq_get credsStore)"
|
|
assert_mode "$CFG" 600
|
|
assert_eq 0 "$(secret_key_count)" "must not create GPG keys for non-pass stores"
|
|
[[ ! -e "$FAKE_PASS_LOG" ]] || _fail "pass should not be invoked"
|
|
assert_err_has "skipping GPG/pass setup"
|
|
}
|
|
|
|
test_preserves_existing_config_keys() {
|
|
setup
|
|
mkdir -p "$HOME/.docker"
|
|
cat > "$CFG" <<'EOF'
|
|
{"auths": {"ghcr.io": {"auth": "c2VjcmV0"}}, "credHelpers": {"gcr.io": "gcloud"}, "currentContext": "colima", "credsStore": "old"}
|
|
EOF
|
|
run "$DC" secretservice
|
|
assert_rc 0
|
|
assert_eq '{"ghcr.io": {"auth": "c2VjcmV0"}}' "$(jq_get auths)"
|
|
assert_eq '{"gcr.io": "gcloud"}' "$(jq_get credHelpers)"
|
|
assert_eq '"colima"' "$(jq_get currentContext)"
|
|
assert_eq '"secretservice"' "$(jq_get credsStore)"
|
|
}
|
|
|
|
test_invalid_json_is_never_overwritten() {
|
|
setup
|
|
mkdir -p "$HOME/.docker"
|
|
printf '{"auths": {"ghcr.io": {"auth": "c2VjcmV0"},}}' > "$CFG" # trailing comma
|
|
local before; before="$(<"$CFG")"
|
|
run "$DC" secretservice
|
|
assert_rc 1; assert_err_has "not valid JSON"
|
|
assert_eq "$before" "$(<"$CFG")" "credentials file was clobbered"
|
|
no_temp_files
|
|
}
|
|
|
|
test_non_object_json_is_refused() {
|
|
setup
|
|
mkdir -p "$HOME/.docker"; echo '["not","an","object"]' > "$CFG"
|
|
run "$DC" secretservice
|
|
assert_rc 1; assert_err_has "JSON object"
|
|
assert_file_eq "$CFG" '["not","an","object"]'
|
|
}
|
|
|
|
test_empty_file_treated_as_empty_config() {
|
|
setup
|
|
mkdir -p "$HOME/.docker"; : > "$CFG"
|
|
run "$DC" secretservice
|
|
assert_rc 0
|
|
assert_eq '"secretservice"' "$(jq_get credsStore)"
|
|
}
|
|
|
|
no_temp_files() { [[ -z "$(find "$HOME/.docker" -name '*.tmp' 2>/dev/null)" ]] || _fail "temp files left behind"; }
|
|
|
|
test_already_set_does_not_rewrite() {
|
|
setup
|
|
mkdir -p "$HOME/.docker"; printf '{\n "credsStore": "secretservice"\n}' > "$CFG" # distinctive formatting
|
|
local before; before="$(<"$CFG")"
|
|
run "$DC" secretservice
|
|
assert_rc 0; assert_out_has "already"
|
|
assert_eq "$before" "$(<"$CFG")"
|
|
}
|
|
|
|
test_readonly_config_file_is_handled() {
|
|
setup
|
|
mkdir -p "$HOME/.docker"; echo '{"auths":{}}' > "$CFG"; chmod 444 "$CFG"
|
|
run "$DC" secretservice
|
|
assert_rc 0
|
|
assert_eq '"secretservice"' "$(jq_get credsStore)"
|
|
assert_mode "$CFG" 600
|
|
}
|
|
|
|
test_symlinked_config_is_replaced_not_written_through() {
|
|
setup
|
|
mkdir -p "$HOME/.docker" "$T/store"
|
|
echo '{"auths":{"x":{}}}' > "$T/store/config.json"; chmod 444 "$T/store/config.json"
|
|
ln -s "$T/store/config.json" "$CFG"
|
|
run "$DC" secretservice
|
|
assert_rc 0; assert_err_has "was a symlink"
|
|
[[ ! -L "$CFG" ]] || _fail "still a symlink"
|
|
assert_eq '"secretservice"' "$(jq_get credsStore)"
|
|
assert_eq '{"x": {}}' "$(jq_get auths)"
|
|
assert_file_eq "$T/store/config.json" '{"auths":{"x":{}}}' # original target untouched
|
|
}
|
|
|
|
test_docker_config_env_is_honored() {
|
|
setup
|
|
DOCKER_CONFIG="$T/alt" run "$DC" secretservice
|
|
assert_rc 0; assert_file "$T/alt/config.json"; assert_no_path "$HOME/.docker"
|
|
}
|
|
|
|
test_pass_flow_end_to_end_and_idempotent() {
|
|
setup
|
|
run "$DC" pass --no-passphrase
|
|
assert_rc 0
|
|
assert_err_has "UNPROTECTED"
|
|
assert_eq 1 "$(secret_key_count)"
|
|
local fpr; fpr="$(<"$HOME/.password-store/.gpg-id")"
|
|
[[ "$fpr" =~ ^[0-9A-F]{40}$ ]] || _fail "bad fingerprint in .gpg-id: $fpr"
|
|
gpg --list-secret-keys "$fpr" >/dev/null 2>&1 || _fail ".gpg-id does not name a real secret key"
|
|
assert_eq '"pass"' "$(jq_get credsStore)"
|
|
|
|
# second run: nothing new
|
|
run "$DC" pass --no-passphrase
|
|
assert_rc 0
|
|
assert_err_has "already exists"; assert_err_has "already initialized"
|
|
assert_eq 1 "$(secret_key_count)" "re-run created a second key"
|
|
assert_eq 1 "$(grep -c 'pass init' "$FAKE_PASS_LOG")" "pass init ran twice"
|
|
}
|
|
|
|
test_uid_is_stable_and_findable() {
|
|
setup
|
|
run "$DC" pass --no-passphrase; assert_rc 0
|
|
gpg --list-secret-keys --with-colons | grep '^uid:' | grep -q "Docker Credential Store <$(id -un)@" \
|
|
|| _fail "unexpected uid: $(gpg --list-secret-keys --with-colons | grep '^uid:')"
|
|
}
|
|
|
|
test_works_when_USER_is_unset() {
|
|
setup
|
|
env -u USER "$DC" secretservice 2>"$T/err" >/dev/null || _fail "failed without USER: $(<"$T/err")"
|
|
}
|
|
|
|
test_missing_tools_checked_before_any_change() {
|
|
setup
|
|
local bin="$T/bin"; mkdir -p "$bin"
|
|
local c; for c in bash dirname readlink env python3 mkdir id hostname uname tty cat; do ln -s "$(command -v $c)" "$bin/$c"; done
|
|
PATH="$bin" run "$DC" pass --no-passphrase
|
|
assert_rc 3; assert_err_has "gpg"
|
|
assert_no_path "$CFG" # nothing was modified
|
|
}
|
|
|
|
test_pass_init_failure_is_reported() {
|
|
setup
|
|
FAKE_PASS_FAIL=1 run "$DC" pass --no-passphrase
|
|
assert_rc 1; assert_err_has "pass init failed"
|
|
}
|
|
|
|
test_passphrase_mode_without_pinentry_fails_cleanly_not_hangs() {
|
|
setup
|
|
# No agent/pinentry/tty here: gpg must fail promptly and we must report it.
|
|
GNUPGHOME="$GNUPGHOME" timeout 60 "$DC" pass >"$T/o" 2>"$T/e" </dev/null; RC=$?
|
|
ERR="$(<"$T/e")"
|
|
[[ $RC == 124 ]] && _fail "hung waiting for a passphrase"
|
|
if ((RC != 0)); then
|
|
assert_err_has "GPG key generation failed"
|
|
assert_no_path "$HOME/.password-store/.gpg-id"
|
|
fi
|
|
}
|
|
|
|
test_argument_validation() {
|
|
setup
|
|
run "$DC" --bogus; assert_rc 2
|
|
run "$DC" pass extra; assert_rc 2
|
|
run "$DC" '../x'; assert_rc 2
|
|
run "$DC" 'a b'; assert_rc 2
|
|
run "$DC" -h; assert_rc 0; assert_out_has "Usage:"
|
|
assert_no_path "$CFG"
|
|
}
|
|
|
|
test_empty_string_args_from_just_are_ignored() {
|
|
setup
|
|
run "$DC" secretservice "" # `just docker-creds secretservice` passes flag=""
|
|
assert_rc 0
|
|
run "$DC" "" "" # defaults -> pass; needs gpg/pass -> use shims
|
|
# (defaults to store=pass: runs real gpg flow without --no-passphrase; just assert it parsed)
|
|
[[ "$ERR" != *"invalid credential store"* ]] || _fail "empty arg rejected"
|
|
}
|
|
|
|
run_all "${1:-}"
|