Skip to content

Commit 1ff602e

Browse files
JLKwongdogi
andauthored
treehouses sshtunnel key name (fixes #2273) (#2274)
Co-authored-by: dogi <dogi@users.noreply.github.com>
1 parent a120163 commit 1ff602e

File tree

3 files changed

+32
-15
lines changed

3 files changed

+32
-15
lines changed

_treehouses

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ treehouses sshtunnel check
361361
treehouses sshtunnel key
362362
treehouses sshtunnel key receive private
363363
treehouses sshtunnel key receive public
364+
treehouses sshtunnel key name
364365
treehouses sshtunnel key send private
365366
treehouses sshtunnel key send public
366367
treehouses sshtunnel key verify

modules/sshtunnel.sh

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ function sshtunnel {
88
fi
99

1010
re='^[0-9]+$'
11+
sshkeyname=$(treehouses config | grep keyName | sed "s/keyName=//")
12+
if [ -z "$sshkeyname" ]; then
13+
sshkeyname="rsa_id"
14+
fi
1115

1216
case "$1" in
1317
add)
@@ -46,10 +50,10 @@ function sshtunnel {
4650
portweb=$((portinterval + 80 - portint_offset))
4751
portnewcouchdb=$((portinterval + 82 - portint_offset))
4852

49-
if [ ! -f "/root/.ssh/id_rsa" ]; then
53+
if [ ! -f "/root/.ssh/$sshkeyname" ]; then
5054
ssh-keygen -q -N "" > "$LOGFILE" < /dev/zero
5155
fi
52-
cat /root/.ssh/id_rsa.pub
56+
cat /root/.ssh/$sshkeyname.pub
5357
echo "Port successfully added"
5458

5559
keys=$(ssh-keyscan -H "$hostname" 2>"$LOGFILE")
@@ -453,15 +457,15 @@ function sshtunnel {
453457
key)
454458
case "$2" in
455459
"")
456-
if [ ! -f "/root/.ssh/id_rsa" ]; then
460+
if [ ! -f "/root/.ssh/$sshkeyname" ]; then
457461
ssh-keygen -q -N "" > "$LOGFILE" < /dev/zero
458462
fi
459-
cat /root/.ssh/id_rsa.pub
463+
cat /root/.ssh/$sshkeyname.pub
460464
;;
461465
verify)
462466
checkargn $# 2
463-
if [ -f "/root/.ssh/id_rsa" ] && [ -f "/root/.ssh/id_rsa.pub" ]; then
464-
verify=$(diff <( ssh-keygen -y -e -f "/root/.ssh/id_rsa" ) <( ssh-keygen -y -e -f "/root/.ssh/id_rsa.pub" ))
467+
if [ -f "/root/.ssh/$sshkeyname" ] && [ -f "/root/.ssh/$sshkeyname.pub" ]; then
468+
verify=$(diff <( ssh-keygen -y -e -f "/root/.ssh/$sshkeyname" ) <( ssh-keygen -y -e -f "/root/.ssh/$sshkeyname.pub" ))
465469
if [ "$verify" != "" ]; then
466470
echo -e "Public and private rsa keys ${RED}do not match${NC}"
467471
else
@@ -487,8 +491,8 @@ function sshtunnel {
487491
tag=".pub"
488492
fi
489493

490-
if [ -f /root/.ssh/id_rsa${profile}${tag} ]; then
491-
cat /root/.ssh/id_rsa${profile}${tag}
494+
if [ -f /root/.ssh/${sshkeyname}${profile}${tag} ]; then
495+
cat /root/.ssh/${sshkeyname}${profile}${tag}
492496
else
493497
log_and_exit1 "No $3 key found"
494498
fi
@@ -515,21 +519,32 @@ function sshtunnel {
515519
tag=".pub"
516520
fi
517521

518-
if [ -f /root/.ssh/id_rsa${profile}${tag} ]; then
522+
if [ -f /root/.ssh/${sshkeyname}${profile}${tag} ]; then
519523
timestamp=$(date +%Y%m%d%H%M)
520-
mv "/root/.ssh/id_rsa${profile}${tag}" "/root/.ssh/id_rsa${profile}.${timestamp}${tag}"
521-
echo "Created backup of 'id_rsa${profile}${tag}' as 'id_rsa${profile}.${timestamp}${tag}'"
524+
mv "/root/.ssh/${sshkeyname}${profile}${tag}" "/root/.ssh/${sshkeyname}${profile}.${timestamp}${tag}"
525+
echo "Created backup of '${sshkeyname}${profile}${tag}' as '${sshkeyname}${profile}.${timestamp}${tag}'"
522526
fi
523527

524-
echo -e "$key" > "/root/.ssh/id_rsa${profile}${tag}"
525-
echo "Saved $3 key to 'id_rsa${profile}${tag}'"
528+
echo -e "$key" > "/root/.ssh/${sshkeyname}${profile}${tag}"
529+
echo "Saved $3 key to '${sshkeyname}${profile}${tag}'"
526530
;;
527531
*)
528532
log_comment_and_exit1 "Error: unknown command" "Usage: $BASENAME sshtunnel key receive <public | private> <\$key> [profile]"
529533
;;
530534
esac
531535
;;
532-
*)
536+
name)
537+
case "$3" in
538+
"")
539+
echo "Current SSH key name: $sshkeyname"
540+
;;
541+
*)
542+
treehouses config update keyName "$3"
543+
echo "Set the SSH key name to: $3"
544+
;;
545+
esac
546+
;;
547+
*)
533548
log_comment_and_exit1 "Error: unknown command" "Usage: $BASENAME sshtunnel key [verify | send | receive]"
534549
;;
535550
esac
@@ -708,6 +723,7 @@ function sshtunnel_help {
708723
echo " check runs a checklist of tests"
709724
echo
710725
echo " key shows the public key"
726+
echo " [name] [sshkeyfile] sets default SSH key to desired filename"
711727
echo " [verify] verifies that the public and private rsa keys match"
712728
echo " [send] <public | private> [profile] sends public / private key"
713729
echo " [receive] <public | private> <\$key> [profile] saves \$key as public / private key"

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@treehouses/cli",
3-
"version": "1.26.6",
3+
"version": "1.26.7",
44
"remote": "4000",
55
"description": "Thin command-line interface for Raspberry Pi low level configuration.",
66
"main": "cli.sh",

0 commit comments

Comments
 (0)