From 3f7d08e97e5b4ce8f6e72551f7c0fb65edf39554 Mon Sep 17 00:00:00 2001 From: bearyjd <28201062+bearyjd@users.noreply.github.com> Date: Sun, 22 Nov 2026 20:40:01 -0500 Subject: [PATCH] fix(installer): honor effective multi-user assignments --- install.sh | 42 +++++++++++++++++++++++------- test/install-sh-invariants.test.ts | 30 +++++++++++++++++++++ 2 files changed, 63 insertions(+), 9 deletions(-) diff --git a/install.sh b/install.sh index 88645059..fbed753d 100755 --- a/install.sh +++ b/install.sh @@ -2340,6 +2340,27 @@ xml_unescape() { printf '%s' "$1" | sed -e 's/<//g' -e 's/&/\&/g' } +# Read one assignment from the unit's Environment= directives. Each line can +# hold several assignments, quoted as whole words. Later assignments and empty +# directives override earlier values. The unit is read as data. +read_systemd_environment() { + awk -v key="$2=" ' + /^[[:space:]]*Environment[[:space:]]*=/ { + sub(/^[[:space:]]*Environment[[:space:]]*=[[:space:]]*/, "") + if ($0 == "") value = "" + while (match($0, /^("([^"\\]|\\.)*"|\047([^\047\\]|\\.)*\047|[^[:space:]"\047][^[:space:]]*)([[:space:]]|$)/)) { + word = substr($0, 1, RLENGTH) + $0 = substr($0, RLENGTH + 1) + sub(/^[[:space:]]+/, "") + sub(/[[:space:]]$/, "", word) + if (word ~ /^["\047]/) word = substr(word, 2, length(word) - 2) + if (index(word, key) == 1) value = substr(word, length(key) + 1) + } + } + END { print value } + ' "$1" +} + # Read the binding out of an already-installed service file, if any. A service # file WITHOUT our CODEMAN_HOST line is a pre-1.8 install, which effectively # ran loopback (the server default), so it reports 127.0.0.1. Also reads back @@ -2353,14 +2374,16 @@ read_existing_binding() { if [[ -f "$unit" ]]; then EXISTING_FOUND="1" - EXISTING_HOST=$(sed -n 's/^Environment=CODEMAN_HOST=//p' "$unit" | head -1) + EXISTING_HOST=$(read_systemd_environment "$unit" CODEMAN_HOST) local pwline - pwline=$(sed -n 's/^Environment="CODEMAN_PASSWORD=\(.*\)"$/\1/p' "$unit" | head -1) + pwline=$(read_systemd_environment "$unit" CODEMAN_PASSWORD) [[ -n "$pwline" ]] && EXISTING_PASSWORD=$(systemd_env_unescape "$pwline") - grep -q '^Environment=CODEMAN_ALLOW_UNAUTHENTICATED_NETWORK=1' "$unit" && EXISTING_ACK="1" - EXISTING_BASE_URL=$(sed -n 's/^Environment=CODEMAN_BASE_URL=//p' "$unit" | head -1) - grep -Eq '^Environment="?CODEMAN_MULTIUSER=(1|true)"?$' "$unit" && EXISTING_MULTIUSER="1" - existing_port=$(sed -n 's/^Environment=CODEMAN_PORT=//p' "$unit" | head -1) + [[ "$(read_systemd_environment "$unit" CODEMAN_ALLOW_UNAUTHENTICATED_NETWORK)" == "1" ]] && EXISTING_ACK="1" + EXISTING_BASE_URL=$(read_systemd_environment "$unit" CODEMAN_BASE_URL) + case "$(read_systemd_environment "$unit" CODEMAN_MULTIUSER)" in + 1|true) EXISTING_MULTIUSER="1" ;; + esac + existing_port=$(read_systemd_environment "$unit" CODEMAN_PORT) elif [[ -f "$plist" ]]; then EXISTING_FOUND="1" EXISTING_HOST=$(awk '/CODEMAN_HOST<\/key>/{getline; print}' "$plist" | sed -n 's/.*\(.*\)<\/string>.*/\1/p') @@ -2371,9 +2394,10 @@ read_existing_binding() { EXISTING_BASE_URL=$(awk '/CODEMAN_BASE_URL<\/key>/{getline; print}' "$plist" | sed -n 's/.*\(.*\)<\/string>.*/\1/p') if awk '/CODEMAN_MULTIUSER<\/key>/ { sub(/.*CODEMAN_MULTIUSER<\/key>[[:space:]]*/, "") - if ($0 == "") getline - print - }' "$plist" | grep -Eq '^[[:space:]]*(1|true)'; then + while ($0 ~ /^[[:space:]]*$/ && getline > 0) {} + enabled = ($0 ~ /^[[:space:]]*(1|true)<\/string>/) + } + END { exit !enabled }' "$plist"; then EXISTING_MULTIUSER="1" fi existing_port=$(awk '/CODEMAN_PORT<\/key>/{getline; print}' "$plist" | sed -n 's/.*\(.*\)<\/string>.*/\1/p') diff --git a/test/install-sh-invariants.test.ts b/test/install-sh-invariants.test.ts index e1aaedd5..7561d82c 100644 --- a/test/install-sh-invariants.test.ts +++ b/test/install-sh-invariants.test.ts @@ -1011,10 +1011,18 @@ describe('install.sh multi-user guidance and reconfiguration', () => { it.each([ ['systemd', unit, 'Environment=CODEMAN_MULTIUSER=1', true], ['systemd', unit, 'Environment="CODEMAN_MULTIUSER=true"', true], + ['systemd', unit, ' Environment = CODEMAN_MULTIUSER=1 ', true], + ['systemd', unit, 'Environment="NOTE=two words" CODEMAN_MULTIUSER=1 NODE_ENV=production', true], + ['systemd', unit, "Environment='CODEMAN_MULTIUSER=1' NODE_ENV=production", true], + ['systemd', unit, 'Environment=CODEMAN_MULTIUSER=1 CODEMAN_MULTIUSER=0', false], + ['systemd', unit, 'Environment=CODEMAN_MULTIUSER=1\nEnvironment=CODEMAN_MULTIUSER=false', false], + ['systemd', unit, 'Environment=CODEMAN_MULTIUSER=1\nEnvironment=', false], + ['systemd', unit, 'Environment="NOTE=some text CODEMAN_MULTIUSER=1 more text"', false], ['systemd', unit, 'Environment=CODEMAN_MULTIUSER=0', false], ['systemd', unit, '', false], ['launchd', agent, 'CODEMAN_MULTIUSER1', true], ['launchd', agent, 'CODEMAN_MULTIUSER\ntrue', true], + ['launchd', agent, 'CODEMAN_MULTIUSER\n \n1', true], ['launchd', agent, 'CODEMAN_MULTIUSER\n0', false], ['launchd', agent, '', false], ])('reconfigures %s with existing setting %s %s (enabled %s)', (supervisor, path, setting, enabled) => { @@ -1042,6 +1050,28 @@ describe('install.sh multi-user guidance and reconfiguration', () => { expect(service).toBe(''); } }); + + it('preserves a combined systemd binding without reintroducing a disabled multi-user assignment', () => { + const output = drive( + ` + parse_flags --port 4000 --service + choose_network_binding >/dev/null + setup_systemd_service >/dev/null + cat "$HOME/${unit}" + `, + { + path: unit, + content: [ + '[Service]', + 'Environment=CODEMAN_HOST=0.0.0.0 CODEMAN_MULTIUSER=1 "CODEMAN_PASSWORD=test password"', + 'Environment=CODEMAN_MULTIUSER=0', + ].join('\n'), + } + ); + expect(output).toMatch(/^Environment=CODEMAN_HOST=0\.0\.0\.0$/m); + expect(output).toContain('Environment="CODEMAN_PASSWORD=test password"'); + expect(output).not.toContain('CODEMAN_MULTIUSER'); + }); }); it('install.sh never contains the unauthenticated tunnel opt-in', () => {