diff --git a/CHANGELOG.md b/CHANGELOG.md index 59a1ee4..b2658bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,17 @@ All notable changes to this project will be documented in this file. +## [0.3.2] - 2026-11-24 + +### Fixed + +- **Package upgrades restart the services:** upgrading the `ziti-ssh-host` or `ziti-ssh-ca` package now restarts the + service if it is running (`systemctl try-restart`; fresh installs and stopped services are left alone). Before, + the old daemon kept running after an upgrade until it was restarted — Ubuntu's `needrestart` usually did that, but + RHEL-family systems and Debian without `needrestart` kept the previous version, including after upgrading to 0.3.0 + for its security fixes. Upgrading to this release restarts the services; a host that was upgraded earlier and not + restarted since may still run its previous version — see `docs/provisioning.md` → Upgrading. + ## [0.3.1] - 2026-11-24 Fixes from an end-to-end test of 0.3.0 on Ubuntu 24.04, Ubuntu 26.04 and RHEL 10. diff --git a/CLAUDE.md b/CLAUDE.md index 13e69f9..caed0d9 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -464,7 +464,7 @@ ziti-ssh/ │ ├── mfa/ # MFA TOTP prompt (terminal, hidden, bounded attempts) │ ├── oidc/ # OIDC authorization code flow (PKCE, loopback callback); flow_test.go: fake IdP │ └── ratelimit/ # Per-identity token-bucket rate limiter -├── packaging/ # nfpm configs, postinstall/preremove/postremove scripts, systemd units +├── packaging/ # nfpm configs, postinstall/preremove/postremove scripts (try-restart on upgrade; scripts_test.go runs them against stubs), systemd units ├── scripts/ │ ├── build-packages.sh # Builds deb and/or rpm packages into dist/ (FORMAT=deb,rpm ARCHS=amd64,arm64) │ ├── docker-build-packages.sh diff --git a/docs/building.md b/docs/building.md index 6785b34..3f9bee0 100644 --- a/docs/building.md +++ b/docs/building.md @@ -22,7 +22,7 @@ curl -sSL https://get.netfoundry.io/linux-install.bash | sudo bash -s ziti-ssh curl -sSL https://get.netfoundry.io/linux-install.bash | sudo bash -s ziti-scp ``` -The install script configures the repository and installs the latest stable package. Subsequent upgrades are handled by the system package manager (`apt upgrade` / `yum update`). +The install script configures the repository and installs the latest stable package. Subsequent upgrades are handled by the system package manager (`apt upgrade` / `yum update`); see [Upgrading](provisioning.md#upgrading) for restarting the services. ### Verifying packages diff --git a/docs/provisioning.md b/docs/provisioning.md index 6f2b941..e6c863f 100644 --- a/docs/provisioning.md +++ b/docs/provisioning.md @@ -514,6 +514,20 @@ go build -o /usr/local/bin/ziti-scp ./cmd/ziti-scp --- +## Upgrading + +Upgrade with the system package manager (`apt-get install --only-upgrade ziti-ssh-host`, `dnf upgrade ziti-ssh-host`, and likewise for `ziti-ssh-ca`). Upgrading `ziti-ssh-host` or `ziti-ssh-ca` to 0.3.2 or later restarts the service if it is running (`systemctl try-restart`, run by the new package's install script), so the new version takes effect at once; a stopped service stays stopped. A restart of `ziti-ssh-host` stops new connections and gives open sessions up to 30 seconds to finish before closing them ([Graceful shutdown](operations.md#graceful-shutdown)), so upgrade hosts when that is acceptable. + +**Hosts upgraded to 0.3.1 or earlier:** upgrades to those versions did not restart the service, so the version that was running before may still be running. Ubuntu's `needrestart` usually restarted it during `apt upgrade`; on RHEL-family systems, and on Debian without `needrestart`, it did not. To check, look at the running process's executable: `(deleted)` means it is the replaced binary. Then restart the service (or upgrade to 0.3.2+, which restarts it): + +```sh +sudo readlink /proc/$(systemctl show -p MainPID --value ziti-ssh-host)/exe # or ziti-ssh-ca +# /usr/bin/ziti-ssh-host (deleted) -> still the previous version +sudo systemctl restart ziti-ssh-host +``` + +--- + ## Uninstalling Packages install binaries to `/usr/bin` (earlier releases used `/usr/local/bin`; upgrading moves them automatically). diff --git a/packaging/scripts/postinstall-ziti-ssh-ca.sh b/packaging/scripts/postinstall-ziti-ssh-ca.sh index 8a3ea1c..e5291c6 100755 --- a/packaging/scripts/postinstall-ziti-ssh-ca.sh +++ b/packaging/scripts/postinstall-ziti-ssh-ca.sh @@ -6,6 +6,18 @@ CONF_DIR="${ZITI_SSH_CA_CONF_DIR:-/etc/ziti-ssh-ca}" DROPIN_DIR="${ZITI_SSH_CA_DROPIN_DIR:-/etc/systemd/system/ziti-ssh-ca.service.d}" DROPIN_FILE="${DROPIN_DIR}/10-controller-user.conf" DROPIN_MARKER="# Managed by the ziti-ssh-ca package" +SYSTEMD_DIR="${ZITI_SSH_SYSTEMD_DIR:-/run/systemd/system}" + +# is_upgrade reports whether this run upgrades an installed package: +# deb postinst "configure ", rpm %post with $1 >= 2. Fresh +# installs, reconfigures without an old version and dpkg abort-* calls are not. +is_upgrade() { + case "${1:-}" in + configure) [ -n "${2:-}" ] ;; + [2-9] | [1-9][0-9]*) true ;; + *) false ;; + esac +} if [ ! -f "${CONF_DIR}/env" ]; then cat > "${CONF_DIR}/env" <<'ENVEOF' @@ -131,6 +143,16 @@ User=ziti. If the controller runs as a different user, run: EOF fi -if [ -d /run/systemd/system ]; then +if [ -d "${SYSTEMD_DIR}" ]; then systemctl daemon-reload || true + # On upgrade, restart the service if it is running so the new binary takes + # effect (try-restart leaves a stopped service stopped). Without this, the + # old daemon keeps running until the next restart or reboot. + if is_upgrade "$@"; then + if systemctl try-restart ziti-ssh-ca.service; then + echo "ziti-ssh-ca: restarted ziti-ssh-ca.service (if it was running) to use the new version" >&2 + else + echo "ziti-ssh-ca: warning: could not restart ziti-ssh-ca.service; run: systemctl restart ziti-ssh-ca.service" >&2 + fi + fi fi diff --git a/packaging/scripts/postinstall-ziti-ssh-host.sh b/packaging/scripts/postinstall-ziti-ssh-host.sh index 8ad0b7b..7c47f06 100755 --- a/packaging/scripts/postinstall-ziti-ssh-host.sh +++ b/packaging/scripts/postinstall-ziti-ssh-host.sh @@ -1,8 +1,23 @@ #!/bin/sh set -e -if [ ! -f /etc/ziti-ssh-host/env ]; then - cat > /etc/ziti-ssh-host/env <<'ENVEOF' +# Paths (overridable only to allow testing this script outside a real install). +CONF_DIR="${ZITI_SSH_HOST_CONF_DIR:-/etc/ziti-ssh-host}" +SYSTEMD_DIR="${ZITI_SSH_SYSTEMD_DIR:-/run/systemd/system}" + +# is_upgrade reports whether this run upgrades an installed package: +# deb postinst "configure ", rpm %post with $1 >= 2. Fresh +# installs, reconfigures without an old version and dpkg abort-* calls are not. +is_upgrade() { + case "${1:-}" in + configure) [ -n "${2:-}" ] ;; + [2-9] | [1-9][0-9]*) true ;; + *) false ;; + esac +} + +if [ ! -f "${CONF_DIR}/env" ]; then + cat > "${CONF_DIR}/env" <<'ENVEOF' # ziti-ssh-host environment configuration # Uncomment and set these variables before starting the service. @@ -60,7 +75,7 @@ if [ ! -f /etc/ziti-ssh-host/env ]; then # RESTRICT_GROUPS: true = config may grant only groups listed in ALLOW_GROUPS. #ZITI_RESTRICT_GROUPS=false ENVEOF - chmod 640 /etc/ziti-ssh-host/env + chmod 640 "${CONF_DIR}/env" fi # Accounts in this group may only log in with a CA certificate (the sshd @@ -70,6 +85,16 @@ if command -v groupadd >/dev/null 2>&1; then groupadd -f --system ziti-ssh-managed fi -if [ -d /run/systemd/system ]; then +if [ -d "${SYSTEMD_DIR}" ]; then systemctl daemon-reload || true + # On upgrade, restart the service if it is running so the new binary takes + # effect (try-restart leaves a stopped service stopped). Without this, the + # old daemon keeps running until the next restart or reboot. + if is_upgrade "$@"; then + if systemctl try-restart ziti-ssh-host.service; then + echo "ziti-ssh-host: restarted ziti-ssh-host.service (if it was running) to use the new version" >&2 + else + echo "ziti-ssh-host: warning: could not restart ziti-ssh-host.service; run: systemctl restart ziti-ssh-host.service" >&2 + fi + fi fi diff --git a/packaging/scripts_test.go b/packaging/scripts_test.go new file mode 100644 index 0000000..922d631 --- /dev/null +++ b/packaging/scripts_test.go @@ -0,0 +1,119 @@ +/* + Copyright NetFoundry Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ +// Package packaging holds the nfpm package definitions and their install +// scripts; this test runs the scripts against stub system commands. +package packaging + +import ( + "os" + "os/exec" + "path/filepath" + "strings" + "testing" +) + +// stubs writes fake system commands that log their arguments to log. +func stubs(t *testing.T, log string) string { + t.Helper() + dir := t.TempDir() + logger := "#!/bin/sh\necho \"$(basename \"$0\") $*\" >> " + log + "\n" + for name, body := range map[string]string{ + "systemctl": logger + "exit 0\n", + "groupadd": logger + "exit 0\n", + "pgrep": "#!/bin/sh\nexit 1\n", // no controller process + "chown": logger + "exit 0\n", + } { + if err := os.WriteFile(filepath.Join(dir, name), []byte(body), 0o755); err != nil { + t.Fatal(err) + } + } + return dir +} + +// The postinstall scripts restart a running service on upgrade only: +// deb "configure " or rpm "$1 >= 2". +func TestPostinstallRestartsOnUpgradeOnly(t *testing.T) { + cases := []struct { + args []string + restart bool + }{ + {[]string{"configure", ""}, false}, // deb fresh install + {[]string{"configure"}, false}, // deb, no old version + {[]string{"configure", "0.3.0-1"}, true}, // deb upgrade + {[]string{"abort-upgrade", "0.3.1-1"}, false}, + {[]string{"1"}, false}, // rpm fresh install + {[]string{"2"}, true}, // rpm upgrade + {[]string{"12"}, true}, // rpm, several versions installed + } + for _, pkg := range []struct{ script, unit, confEnv string }{ + {"scripts/postinstall-ziti-ssh-host.sh", "ziti-ssh-host.service", "ZITI_SSH_HOST_CONF_DIR"}, + {"scripts/postinstall-ziti-ssh-ca.sh", "ziti-ssh-ca.service", "ZITI_SSH_CA_CONF_DIR"}, + } { + for _, c := range cases { + name := filepath.Base(pkg.script) + " " + strings.Join(c.args, " ") + t.Run(name, func(t *testing.T) { + tmp := t.TempDir() + log := filepath.Join(tmp, "calls.log") + conf := filepath.Join(tmp, "conf") + systemd := filepath.Join(tmp, "systemd") + for _, d := range []string{conf, systemd} { + if err := os.MkdirAll(d, 0o755); err != nil { + t.Fatal(err) + } + } + cmd := exec.Command("sh", append([]string{pkg.script}, c.args...)...) + cmd.Env = []string{ + "PATH=" + stubs(t, log) + ":/usr/bin:/bin", + pkg.confEnv + "=" + conf, + "ZITI_SSH_CA_DROPIN_DIR=" + filepath.Join(tmp, "dropin"), + "ZITI_SSH_SYSTEMD_DIR=" + systemd, + } + if out, err := cmd.CombinedOutput(); err != nil { + t.Fatalf("script failed: %v\n%s", err, out) + } + calls, _ := os.ReadFile(log) + if !strings.Contains(string(calls), "systemctl daemon-reload") { + t.Errorf("no daemon-reload; calls:\n%s", calls) + } + restarted := strings.Contains(string(calls), "systemctl try-restart "+pkg.unit) + if restarted != c.restart { + t.Errorf("try-restart called = %v, want %v; calls:\n%s", restarted, c.restart, calls) + } + if _, err := os.Stat(filepath.Join(conf, "env")); err != nil { + t.Errorf("env file not created: %v", err) + } + }) + } + } +} + +// Without systemd (containers, chroots) nothing is reloaded or restarted. +func TestPostinstallWithoutSystemd(t *testing.T) { + tmp := t.TempDir() + log := filepath.Join(tmp, "calls.log") + cmd := exec.Command("sh", "scripts/postinstall-ziti-ssh-host.sh", "2") + cmd.Env = []string{ + "PATH=" + stubs(t, log) + ":/usr/bin:/bin", + "ZITI_SSH_HOST_CONF_DIR=" + tmp, + "ZITI_SSH_SYSTEMD_DIR=" + filepath.Join(tmp, "absent"), + } + if out, err := cmd.CombinedOutput(); err != nil { + t.Fatalf("script failed: %v\n%s", err, out) + } + if calls, _ := os.ReadFile(log); strings.Contains(string(calls), "systemctl") { + t.Errorf("systemctl called without systemd:\n%s", calls) + } +}