Skip to content

Keep sessions across restarts and container upgrades - #673

Merged
compscidr merged 2 commits into
mainfrom
fix/keep-env-at-database-step
Oct 3, 2026
Merged

compscidr merged 2 commits into
mainfrom
fix/keep-env-at-database-step

Conversation

@compscidr

Copy link
Copy Markdown
Collaborator

Closes #667.

Problem

Two separate things signed everybody out:

  1. The database step dropped the session key (The database step drops the session key from .env #667). POST /wizard_db rewrote .env with only the database settings, losing the SESSION_KEY startup had written moments earlier. The first restart after an install generated a new key, so the admin who had just finished was signed out. It would equally have dropped SMTP or admin-pin settings placed in .env beforehand.
  2. The session cookie was named after the hostname. sessions.Sessions(os.Hostname(), …). Docker gives every new container a random hostname, so any upgrade that replaces the container (including an Ansible redeploy) signed everyone out, even with the key intact. I found this when the smoke test's new check failed only in the "replace the container" configuration.

Changes

  • dbConfig.mergedEnvFile replaces the database lines (database, sqlite_db, MYSQL_*, POSTGRES_*) in the existing .env and keeps everything else; updateDB uses it.
  • The session cookie is named goblog_session.

Upgrading

Because the cookie name changes, everyone is signed out once when a site upgrades to the release containing this. After that, upgrades no longer sign anyone out.

Testing

  • TestMergedEnvFile (keeps other lines, comments, idempotent, empty .env), and TestInstallOnly now checks the session key survives the database step.
  • The install smoke test now checks the browser the wizard logged in is still an admin after the container is restarted (old layout) or replaced (data directory). Passes locally for sqlite in both layouts, mysql and postgres. go test ./... passes.

🤖 Generated with Claude Code

Two things signed everybody out:

- The wizard's database step rewrote .env with only the database
  settings, dropping the SESSION_KEY startup had just written. The first
  restart after an install generated a new key. The step now replaces
  only the database lines and keeps the rest of the file (#667).
- The session cookie was named after the machine's hostname, which
  Docker makes up for every new container, so any upgrade that replaced
  the container signed everybody out. The name is now fixed.

The install smoke test now checks that the browser the wizard logged in
is still logged in after the container is restarted or replaced.

Closes #667.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings October 3, 2026 16:58

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot review overview

🔵 Needs a closer look

It changes the session cookie name and .env persistence during install, affecting authentication for all users (a one-time forced sign-out on upgrade), which warrants human sign-off.

Review effort: Balanced
Findings: 1 Low severity

Open (1)
What changed in this PR

This PR fixes two independent causes of admins being signed out, so that sessions survive server restarts and container upgrades. The first is issue #667: the install wizard's database step (POST /wizard_db) rewrote .env with only the database settings, discarding the SESSION_KEY that startup had just written (and any SMTP/admin-pin lines an operator added). The second is that the session cookie was named after os.Hostname(), which Docker regenerates per container, so replacing a container invalidated everyone's cookies. It fits into the existing install-wizard and session-setup flow in main().

Changes:

  • Added dbConfig.mergedEnvFile, which replaces only the database lines in an existing .env and keeps everything else; updateDB now uses it instead of overwriting the whole file.
  • Replaced the hostname-based session cookie name with a fixed constant goblog_session, and removed the now-unused os.Hostname() handling and debug logging.
  • Added/updated tests (TestMergedEnvFile, TestInstallOnly session-key check) and a smoke-test assertion that the wizard's session survives a restart/container replacement.
File Description
db.go New isDatabaseEnvKey/mergedEnvFile helpers that preserve non-database .env lines while replacing database settings.
goblog.go Uses the new merge when writing .env, introduces the fixed sessionCookieName constant, and removes hostname lookup/error handling.
db_test.go Adds TestMergedEnvFile covering preservation, idempotency, and empty-input behavior.
wizard_install_only_test.go Seeds .env with a session key and asserts the database step keeps it (#667).
scripts/​install-smoke-test.sh Adds a post-restart check that the wizard's browser is still an admin.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread goblog.go Outdated
@codecov

codecov Bot commented Oct 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

The hostname cookie name was a separate problem from #667, which is the
database step dropping the session key.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@compscidr
compscidr merged commit 73dd1cd into main Oct 3, 2026
5 checks passed
@compscidr
compscidr deleted the fix/keep-env-at-database-step branch October 3, 2026 17:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The database step drops the session key from .env

2 participants