Skip to content

Commit 0fc8144

Browse files
committed
fix(install): register snap gateway for target user
Register the gateway as the target user, instead of for root. Also, account for the snap installation method when doing stale registration cleanup, and ensure error exit codes are properly propagated from `gateway add`. Signed-off-by: Oliver Calder <oliver.calder@canonical.com>
1 parent 204ffa9 commit 0fc8144

3 files changed

Lines changed: 80 additions & 9 deletions

File tree

‎architecture/gateway.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,9 @@ infrastructure:
815815

816816
Store-installed OpenShell Snaps auto-connect `openshell:docker` to the system
817817
`:docker` slot. This interface does not depend on how Docker was packaged.
818+
Snap CLI registration is per-user under `$SNAP_USER_COMMON`; the installer runs
819+
registration and replacement as the invoking target user even when privilege
820+
escalation is needed for Snap package operations.
818821

819822
## Configuration
820823

‎install.sh‎

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -867,12 +867,12 @@ wait_for_local_gateway_status() {
867867
error "openshell status did not report connected within ${_timeout}s"
868868
}
869869

870-
remove_local_gateway_registration() {
870+
remove_local_gateway_registration_from() {
871+
_config_dir="$1"
871872
[ -n "$TARGET_HOME" ] || error "cannot resolve home directory for ${TARGET_USER}"
872-
_config_dir="${TARGET_HOME}/.config/openshell"
873873

874-
# The install-dev gateway is a user service. Replace the CLI registration
875-
# directly instead of asking `gateway destroy` to tear down Docker resources.
874+
# Replace the CLI registration directly instead of asking `gateway destroy`
875+
# to tear down package-managed resources.
876876
# shellcheck disable=SC2016
877877
as_target_user sh -c '
878878
config_dir=$1
@@ -891,6 +891,15 @@ remove_local_gateway_registration() {
891891
' sh "$_config_dir"
892892
}
893893

894+
remove_local_gateway_registration() {
895+
remove_local_gateway_registration_from "${TARGET_HOME}/.config/openshell"
896+
}
897+
898+
remove_local_gateway_registration_snap() {
899+
remove_local_gateway_registration_from \
900+
"${TARGET_HOME}/snap/openshell/common/.config/openshell"
901+
}
902+
894903
register_local_gateway() {
895904
_register_bin="${OPENSHELL_REGISTER_BIN:-openshell}"
896905
_endpoint="$(local_gateway_endpoint)"
@@ -1072,18 +1081,18 @@ wait_for_docker_daemon() {
10721081
register_local_gateway_snap() {
10731082
_register_bin="${OPENSHELL_REGISTER_BIN:-openshell}"
10741083

1075-
if _add_output="$($_register_bin gateway add "http://127.0.0.1:${LOCAL_GATEWAY_PORT}" --local --name openshell 2>&1)"; then
1084+
if _add_output="$(as_target_user "$_register_bin" gateway add "http://127.0.0.1:${LOCAL_GATEWAY_PORT}" --local --name openshell 2>&1)"; then
10761085
[ -z "$_add_output" ] || print_gateway_add_output "$_add_output"
10771086
return 0
1087+
else
1088+
_add_status=$?
10781089
fi
10791090

1080-
_add_status=$?
1081-
10821091
case "$_add_output" in
10831092
*"already exists"*)
10841093
info "local gateway already exists; removing and re-adding it..."
1085-
remove_local_gateway_registration
1086-
"$_register_bin" gateway add "http://127.0.0.1:${LOCAL_GATEWAY_PORT}" --local --name openshell
1094+
remove_local_gateway_registration_snap
1095+
as_target_user "$_register_bin" gateway add "http://127.0.0.1:${LOCAL_GATEWAY_PORT}" --local --name openshell
10871096
;;
10881097
*)
10891098
printf '%s\n' "$_add_output" >&2

‎tasks/scripts/test-install-sh.sh‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,4 +397,63 @@ if ! grep -Fq "Docker daemon did not become reachable within 2s" "$err"; then
397397
exit 1
398398
fi
399399

400+
assert_snap_registration_uses_target_user() {
401+
local calls_file="${tmpdir}/target-user-calls"
402+
: > "$calls_file"
403+
(
404+
as_target_user() { printf 'target:%s\n' "$*" >> "$calls_file"; }
405+
print_gateway_add_output() { :; }
406+
register_local_gateway_snap
407+
)
408+
local calls
409+
calls="$(<"$calls_file")"
410+
if [ "$calls" != "target:openshell gateway add http://127.0.0.1:17670 --local --name openshell" ]; then
411+
echo "FAIL: Snap registration did not run as target user" >&2
412+
printf '%s\n' "$calls" >&2
413+
exit 1
414+
fi
415+
}
416+
417+
assert_snap_registration_uses_target_user
418+
419+
assert_snap_registration_cleanup() {
420+
local target_home="${tmpdir}/target-home"
421+
local snap_config="${target_home}/snap/openshell/common/.config/openshell"
422+
local native_config="${target_home}/.config/openshell"
423+
mkdir -p "$snap_config/gateways/openshell" "$native_config/gateways/openshell"
424+
printf 'snap\n' > "$snap_config/gateways/openshell/metadata.json"
425+
printf 'openshell\n' > "$snap_config/active_gateway"
426+
printf 'native\n' > "$native_config/gateways/openshell/metadata.json"
427+
428+
export TARGET_HOME="$target_home"
429+
export TARGET_USER=test-user
430+
as_target_user() { "$@"; }
431+
remove_local_gateway_registration_snap
432+
433+
if [ -e "$snap_config/gateways/openshell/metadata.json" ] || [ -e "$snap_config/active_gateway" ]; then
434+
echo "FAIL: Snap registration cleanup did not remove Snap metadata" >&2
435+
exit 1
436+
fi
437+
if [ ! -e "$native_config/gateways/openshell/metadata.json" ]; then
438+
echo "FAIL: Snap registration cleanup modified native-package metadata" >&2
439+
exit 1
440+
fi
441+
}
442+
443+
assert_snap_registration_cleanup
444+
445+
if (
446+
as_target_user() { return 17; }
447+
register_local_gateway_snap
448+
) >"$out" 2>"$err"; then
449+
echo "FAIL: unexpected Snap registration error should propagate" >&2
450+
exit 1
451+
else
452+
status=$?
453+
if [ "$status" -ne 17 ]; then
454+
echo "FAIL: Snap registration returned ${status}, expected 17" >&2
455+
exit 1
456+
fi
457+
fi
458+
400459
echo "install.sh focused tests passed"

0 commit comments

Comments
 (0)