Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions ssh-uuid.sh
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ function parse_args {
local args=("$@")
local nargs=${#args[@]}
local i
SSUU_PROXY=1
SSUU_USER=''
SSUU_OPT_ARGS=()
SSUU_POS_ARGS=()
Expand All @@ -184,20 +185,28 @@ function parse_args {
# Is arg a UUID.balena hostname specification?
# For ssh:
# '[user@]UUID.balena'
# '[user@]UUID.local'
# 'ssh://[user@]UUID.balena[:port]'
# 'ssh://[user@]UUID.local[:port]'
# For scp:
# '[user@]UUID.balena:'
# '[user@]UUID.local:'
# 'scp://[user@]UUID.balena[:port][/path]'
# 'scp://[user@]UUID.local[:port][/path]'
# where UUID is a hexadecimal number with exactly 32 or 62 characters,
# where 62 is not typo meant to read 64, it really is 62.
# where 62 is not typo meant to read 64, it really is 62. 7-character
# abbreviated UUIDs are also accepted for local-mode devices.
if [[ "${SSUU_SCP}" = 0 &&
"${arg}" =~ ^(ssh://)?((.+)@)?([[:xdigit:]]{32}|[[:xdigit:]]{62})\.balena(:[0-9]+)?$
"${arg}" =~ ^(ssh://)?((.+)@)?([[:xdigit:]]{7}|[[:xdigit:]]{32}|[[:xdigit:]]{62})\.(balena|local)(:[0-9]+)?$
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a previous comment, I asked you to update the README file to clarify that 7-character UUIDs can be used with .local hostnames. However, hang on — I’ve thought of something. For a remote device reachable through the Balena cloud proxy server, the UUID has a special meaning as the identifier of the device in the cloud. Therefore, a regex ensuring a hexadecimal string of a certain length makes sense. However, for a .local hostname like abc1234.local (7-char UUID), the UUID is merely the device’s hostname discoverable through mDNS / Ahavi and configurable through the device’s config.json file. In this case, abc1234.local (7-char hex) is just as good as foo.local (if config.json specified "hostname": "foo"), and a regex requiring a 7-char hex string would be unnecessarily restrictive.

Does that make sense? So the code could / should be changed to simply check for a .local hostname suffix, in which case any string should be allowed as the hostname prefix. Then the README could be updated along the following lines:

Before:

The device's ssh/scp hostname has the format '<device-UUID>.balena', using the device's
-full UUID (not a shortened form).

After:

The device’s ssh/scp hostname should have one of the following formats:

  • '<device-UUID>.balena' - Remote devices reachable through Balena’s cloud proxy server.
  • '<hostname>.local' - Devices on the same local network as the client.

For remote devices, <device-UUID> must be the device’s “full” UUID (32 or 62 characters long). For local devices, <hostname> is the device’s hostname configured in its config.json file — by default, it is the device’s 7-character short UUID.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, in this PR’s initial approach, abc1234.balena would not fail the regex check, but it should fail the regex check in order to alert the user of a hostname problem.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing! That sounds like a very reasonable approach.

]]; then
SSUU_USER="${BASH_REMATCH[3]}"
SSUU_POS_ARGS=("${args[@]:i}")
if [[ "${BASH_REMATCH[5]}" == "local" ]]; then
SSUU_PROXY=0
fi
break
elif [[ "${SSUU_SCP}" = 1 &&
"${arg}" =~ ^scp://((.+)@)?([[:xdigit:]]{32}|[[:xdigit:]]{62})\.balena(:[0-9]+)?(/.*)?$
"${arg}" =~ ^scp://((.+)@)?([[:xdigit:]]{7}|[[:xdigit:]]{32}|[[:xdigit:]]{62})\.(balena|local)(:[0-9]+)?(/.*)?$
]]; then
SSUU_USER="${BASH_REMATCH[2]}"
if [ -z "${SSUU_USER}" ] && [ -n "${BALENA_USERNAME}" ]; then
Expand All @@ -206,9 +215,12 @@ function parse_args {
args[i]="${arg}"
fi
SSUU_POS_ARGS=("${args[@]:i}")
if [[ "${BASH_REMATCH[4]}" == "local" ]]; then
SSUU_PROXY=0
fi
break
elif [[ "${SSUU_SCP}" = 1 &&
"${arg}" =~ ^((.+)@)?([[:xdigit:]]{32}|[[:xdigit:]]{62})\.balena:.*$
"${arg}" =~ ^((.+)@)?([[:xdigit:]]{7}|[[:xdigit:]]{32}|[[:xdigit:]]{62})\.(balena|local):.*$
]]; then
SSUU_USER="${BASH_REMATCH[2]}"
if [ -z "${SSUU_USER}" ] && [ -n "${BALENA_USERNAME}" ]; then
Expand All @@ -217,6 +229,9 @@ function parse_args {
args[i]="${arg}"
fi
SSUU_POS_ARGS=("${args[@]:i}")
if [[ "${BASH_REMATCH[4]}" == "local" ]]; then
SSUU_PROXY=0
fi
break
fi
if [ "${arg:0:1}" = '-' ] && [ "${arg:1:1}" != '-' ]; then
Expand Down Expand Up @@ -264,12 +279,17 @@ function run_ssh {
l_arg=('-l' "${BALENA_USERNAME}")
fi
opt_args=(
-o "ProxyCommand='$0' do_proxy %h %p"
-p 22222
"${l_arg[@]}"
"${t_arg[@]}"
"${opt_args[@]}"
)
if [[ "${SSUU_PROXY}" == 1 ]]; then
opt_args=(
-o "ProxyCommand='$0' do_proxy %h %p"
"${opt_args[@]}"
)
fi
set +e
[ -n "${DEBUG}" ] && set -x
# shellcheck disable=SC2029
Expand Down Expand Up @@ -341,10 +361,15 @@ function run_scp {
fi
args=(
-P 22222
-o "ProxyCommand='$0' do_proxy %h %p"
"${SSUU_OPT_ARGS[@]}"
"${SSUU_POS_ARGS[@]}"
)
if [[ "${SSUU_PROXY}" == 1 ]]; then
args=(
-o "ProxyCommand='$0' do_proxy %h %p"
"${args[@]}"
)
fi
set +e
[ -n "${DEBUG}" ] && set -x
scp "${args[@]}"
Expand Down