Skip to content

fix: stop hardcoding the Flask SECRET_KEY in the bookshelf samples - #630

Open
angelcaamal wants to merge 1 commit into
GoogleCloudPlatform:mainfrom
angelcaamal:fix/flask-secret-key-env
Open

angelcaamal wants to merge 1 commit into
GoogleCloudPlatform:mainfrom
angelcaamal:fix/flask-secret-key-env

Conversation

@angelcaamal

Copy link
Copy Markdown

Summary

Removes the hardcoded Flask SECRET_KEY = 'secret' from the two bookshelf samples. Flask signs session cookies with this key, so a value published in the repository lets anyone forge session cookies.

Changes

GKE bookshelf (optional-kubernetes-engine/)

  • config.py: read the key from the FLASK_SECRET_KEY environment variable, with no default, so the app fails at startup instead of running with a predictable key.
  • bookshelf-frontend.yaml, bookshelf-worker.yaml: inject FLASK_SECRET_KEY from a Kubernetes Secret (bookshelf-secrets).
  • README.md, Makefile: add a step and a create-secret target that generate a random key and create that Secret. make deploy now runs it first.
  • conftest.py (new): set a random FLASK_SECRET_KEY for test runs so tests/conftest.py can still import config.

App Engine bookshelf (bookshelf/)

  • main.py: use FLASK_SECRET_KEY when it is set; otherwise generate a random key for the process. The session here only carries flashed messages, and app.yaml has no way to supply a key, so this keeps the tutorial deployable without extra configuration.

Behavior changes

  • The GKE sample no longer starts unless FLASK_SECRET_KEY is set. Existing deployments need the bookshelf-secrets Secret before updating, and local runs need the variable exported.
  • In the App Engine sample, when FLASK_SECRET_KEY is not set, sessions don't survive restarts and aren't shared between instances or workers, so a flashed message can be lost.

Internal bug: b/586645246

@angelcaamal
angelcaamal requested a review from a team as a code owner December 1, 2026 19:45
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.

1 participant