fix: bulletproof CD ssh key normalization

Dung lai PEM tu moi kieu paste hong (literal backslash-n, CRLF, space, mat separator) bang cach boc base64 va re-wrap. Dam bao trailing newline. Them diagnostic byte/line count.
This commit is contained in:
hailt 2026-11-02 11:28:19 +07:00
parent 5f362ec7d3
commit 74567eb1c4

View file

@ -51,16 +51,23 @@ jobs:
run: | run: |
set -euo pipefail set -euo pipefail
install -m 700 -d ~/.ssh install -m 700 -d ~/.ssh
# Chuan hoa secret truoc khi ghi key: # Chuan hoa secret thanh PEM chuan: chiu duoc moi kieu paste hong
# - Loc ky tu CR (paste tu Windows/chat hay dinh CRLF). # kieu xuong dong (mot dong voi \n chu, CRLF, space, mat separator).
# - Neu secret bi paste dang 1 dong voi \n chu (thay vi xuong # Base64 khong bao gio chua backslash nen backslash chac chan la rac.
# dong that) thi dung lai newline that. # Dau vao dung '%s' de giu nguyen secret; file dau ra dung '%s\n'
KEY="$SSH_PRIVATE_KEY" # vi OpenSSH bat buoc trailing newline (thieu la bao not a key file).
if [ "$(printf '%s' "$KEY" | wc -l)" -eq 0 ]; then KEY="$(printf '%s' "$SSH_PRIVATE_KEY" | python3 -c '
case "$KEY" in import re, sys
*\\n*) KEY="$(printf '%b' "$KEY")" ;; s = sys.stdin.read()
esac s = re.sub(r"\\+n", "\n", s)
fi s = s.replace("-----BEGIN OPENSSH PRIVATE KEY-----", "")
s = s.replace("-----END OPENSSH PRIVATE KEY-----", "")
body = "".join(s.split())
sys.stdout.write("-----BEGIN OPENSSH PRIVATE KEY-----\n")
sys.stdout.write("\n".join(body[i:i + 64] for i in range(0, len(body), 64)))
sys.stdout.write("\n-----END OPENSSH PRIVATE KEY-----\n")
')"
printf 'secret bytes=%s lines=%s\n' "$(printf '%s' "$SSH_PRIVATE_KEY" | wc -c)" "$(printf '%s' "$SSH_PRIVATE_KEY" | wc -l)"
printf '%s\n' "$KEY" | tr -d '\r' > ~/.ssh/id_ed25519 printf '%s\n' "$KEY" | tr -d '\r' > ~/.ssh/id_ed25519
chmod 600 ~/.ssh/id_ed25519 chmod 600 ~/.ssh/id_ed25519
# Fail som neu key hong: in armor + fingerprint (khong lo secret) de doi chieu. # Fail som neu key hong: in armor + fingerprint (khong lo secret) de doi chieu.