s
This commit is contained in:
Executable
+42
@@ -0,0 +1,42 @@
|
||||
#!/bin/bash
|
||||
# 验证 SSH push 到不存在的仓库被拒。
|
||||
# 仓库只能通过 web API 显式创建(CreateRepo);EnsureRepo 的"push 自动创建"
|
||||
# 已删除,所以 push 到不存在的仓库必须报 "repository not found"。
|
||||
#
|
||||
# 假设后端 HTTP :8091 + SSH :2222 已启动(见 api_e2e.sh 顶部)。
|
||||
set -u
|
||||
BASE="${BASE:-http://localhost:8091/api/mgmt}"
|
||||
SSH_PORT="${SSH_PORT:-2222}"
|
||||
USER="ssh${RANDOM:-$$}"
|
||||
|
||||
WORK=$(mktemp -d)
|
||||
KEYFILE="$WORK/sg_key"
|
||||
trap 'rm -rf "$WORK"' EXIT
|
||||
|
||||
# 生成临时 ed25519 key 对
|
||||
ssh-keygen -t ed25519 -f "$KEYFILE" -N "" -q
|
||||
PUB=$(cat "$KEYFILE.pub")
|
||||
|
||||
# 注册用户并添加公钥(这样 SSH 公钥认证能通过,才会走到仓库存在性检查)
|
||||
TOKEN=$(curl -s -X POST "$BASE/auth/register" -H 'Content-Type: application/json' -d "{\"username\":\"$USER\",\"password\":\"pass\"}" | grep -o '"token":"[^"]*"' | sed 's/"token":"//;s/"//')
|
||||
if [ -z "$TOKEN" ]; then echo "FAIL: register did not return a token"; exit 1; fi
|
||||
curl -s -X POST "$BASE/me/keys" -H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' -d "{\"public_key\":\"$PUB\"}" >/dev/null
|
||||
|
||||
# 本地 git 仓库 + 一个提交
|
||||
git -C "$WORK" init -q
|
||||
git -C "$WORK" config user.email t@t
|
||||
git -C "$WORK" config user.name t
|
||||
echo hi > "$WORK/a"
|
||||
git -C "$WORK" add . && git -C "$WORK" commit -qm x
|
||||
|
||||
# push 到不存在的远程仓库,期望被拒并报 "repository not found"
|
||||
export GIT_SSH_COMMAND="ssh -i $KEYFILE -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
|
||||
ERR=$(git -C "$WORK" push "ssh://localhost:$SSH_PORT/$USER/nonexistent.git" HEAD 2>&1 >/dev/null)
|
||||
if echo "$ERR" | grep -q "repository not found"; then
|
||||
echo "PASS: SSH push to nonexistent repo rejected (repository not found)"
|
||||
exit 0
|
||||
else
|
||||
echo "FAIL: expected 'repository not found', got:"
|
||||
echo "$ERR"
|
||||
exit 1
|
||||
fi
|
||||
Reference in New Issue
Block a user