Skip to content

Commit cc0191f

Browse files
authored
Merge pull request #214 from brave-experiments/setup-pin-private-ssh-key
setup: offer the private key, not its .pub, and default to the bot's key
2 parents d127d67 + aea2753 commit cc0191f

3 files changed

Lines changed: 23 additions & 5 deletions

File tree

‎scripts/lib/git-identity.sh‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,12 @@ bot_list_ssh_keys() {
6767
# Private keys first: the .pub pass needs their fingerprints.
6868
for f in "$HOME"/.ssh/*; do
6969
[ -f "$f" ] || continue
70-
[ -f "$f.pub" ] && continue
70+
# A pair is offered as its .pub only when the private half needs ssh-agent:
71+
# ssh cannot sign with a .pub alone, so under cron (no agent) it fails with
72+
# "error in libcrypto". An unencrypted pair is offered as the private key.
73+
if [ -f "$f.pub" ] && bot_identity_needs_agent "$f"; then
74+
continue
75+
fi
7176
case "$f" in
7277
*.pub | */config | */known_hosts*) continue ;;
7378
esac

‎scripts/setup.sh‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,19 @@ if [ "$WRITE_CONFIG" = true ]; then
197197
echo " 0) Use this machine's default key (no pinning)"
198198
echo ""
199199

200+
# With no earlier choice, default to the first key GitHub says is the bot's
201+
# rather than to 0: Enter at the prompt used to leave sshKeyPath null, which
202+
# cron cannot push with (no ssh-agent, no ~/.ssh/config).
203+
if [ "$DEFAULT_CHOICE" = "0" ]; then
204+
for i in "${!SSH_KEYS[@]}"; do
205+
if [ "$(bot_ssh_login "${SSH_KEYS[$i]}")" = "$CFG_BOT_USER" ]; then
206+
DEFAULT_CHOICE=$((i + 1))
207+
echo " ${SSH_KEYS[$i]} authenticates as $CFG_BOT_USER — offered as the default."
208+
break
209+
fi
210+
done
211+
fi
212+
200213
while true; do
201214
read -p "Select the key the bot pushes with [$DEFAULT_CHOICE]: " KEY_CHOICE
202215
KEY_CHOICE="${KEY_CHOICE:-$DEFAULT_CHOICE}"

‎tests/test_scripts.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,17 +1504,17 @@ def test_collapses_pair_whose_halves_are_named_differently(self, tmp_path):
15041504
== 1
15051505
)
15061506

1507-
def test_real_name_matched_pair_still_prefers_the_pub(self, tmp_path):
1508-
# Same assertion as above one level up, but with a real key: the
1509-
# fingerprint pass must not undo the name rule for id_rsa / id_rsa.pub.
1507+
def test_unencrypted_pair_is_offered_as_the_private_key(self, tmp_path):
1508+
# A .pub only authenticates through ssh-agent; cron has none, so an
1509+
# unencrypted pair must be offered as the private key.
15101510
ssh = tmp_path / ".ssh"
15111511
ssh.mkdir()
15121512
subprocess.run(
15131513
["ssh-keygen", "-q", "-t", "ed25519", "-N", "", "-f", str(ssh / "id_x")],
15141514
check=True,
15151515
)
15161516
out = run_identity_snippet("bot_list_ssh_keys", home=tmp_path).stdout
1517-
assert [os.path.basename(line) for line in out.split()] == ["id_x.pub"]
1517+
assert [os.path.basename(line) for line in out.split()] == ["id_x"]
15181518

15191519
def test_excludes_non_keys(self, tmp_path):
15201520
home = self._fake_home(

0 commit comments

Comments
 (0)