fix(installer): honor effective multi-user assignments

This commit is contained in:
bearyjd committed 2026-11-22 20:40:01 -05:00
1 parent c22bbd70ac
commit 3f7d08e97e
2 files changed
+63 -9

No files matched your search

+33 -9
View File
@@ -2340,6 +2340,27 @@ xml_unescape() {
printf '%s' "$1" | sed -e 's/&lt;/</g' -e 's/&gt;/>/g' -e 's/&amp;/\&/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 '/<key>CODEMAN_HOST<\/key>/{getline; print}' "$plist" | sed -n 's/.*<string>\(.*\)<\/string>.*/\1/p')
@@ -2371,9 +2394,10 @@ read_existing_binding() {
EXISTING_BASE_URL=$(awk '/<key>CODEMAN_BASE_URL<\/key>/{getline; print}' "$plist" | sed -n 's/.*<string>\(.*\)<\/string>.*/\1/p')
if awk '/<key>CODEMAN_MULTIUSER<\/key>/ {
sub(/.*<key>CODEMAN_MULTIUSER<\/key>[[:space:]]*/, "")
if ($0 == "") getline
print
}' "$plist" | grep -Eq '^[[:space:]]*<string>(1|true)</string>'; then
while ($0 ~ /^[[:space:]]*$/ && getline > 0) {}
enabled = ($0 ~ /^[[:space:]]*<string>(1|true)<\/string>/)
}
END { exit !enabled }' "$plist"; then
EXISTING_MULTIUSER="1"
fi
existing_port=$(awk '/<key>CODEMAN_PORT<\/key>/{getline; print}' "$plist" | sed -n 's/.*<string>\(.*\)<\/string>.*/\1/p')
+30
View File
@@ -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, '<key>CODEMAN_MULTIUSER</key><string>1</string>', true],
['launchd', agent, '<key>CODEMAN_MULTIUSER</key>\n<string>true</string>', true],
['launchd', agent, '<key>CODEMAN_MULTIUSER</key>\n \n<string>1</string>', true],
['launchd', agent, '<key>CODEMAN_MULTIUSER</key>\n<string>0</string>', 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', () => {