Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start warning about upcoming SECRET_KEY default removal #726

Merged
merged 2 commits into from
Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,8 @@ Other settings that might be interesting, in no particular order:
Defaults to `friendly`.
- `CODE_PREFIX_TO_REMOVE`: what code path that should be shortened in "Friendly errors" to "…" for readability.
A regexp. Defaults to `/etc/puppetlabs/code/environments(/.*?/modules)?`.
- `SECRET_KEY`: Refer to [Flask documentation](https://flask.palletsprojects.com/en/1.1.x/quickstart/#sessions),
section "How to generate good secret keys" for more info. Defaults to a random 24-char string generated by
`os.random(24)`.
- `SECRET_KEY`: Refer to [Flask documentation](https://flask.palletsprojects.com/en/2.0.x/quickstart/#sessions),
section "How to generate good secret keys" for more info. Defaults to a random 64-char string generated by `secrets.token_hex(32)`, prepended with a `default-` string. **Warning** Leaving SECRET_KEY set to a default value WILL cause issues when the app is restarted or has more than 1 replica (f.e. uWSGI workers, k8s replicas etc.) and some features (in particular: queries) are used. Please set SECRET_KEY to your own value, the same for all app replicas. This will be REQUIRED starting with Puppetboard 5.x which will NOT contain the default value anymore. Please see [#721](https://github.com/voxpupuli/puppetboard/issues/721) for more info.
- `PUPPETDB_TIMEOUT`: Defaults to 20 seconds, but you might need to increase this value. It depends on how big the
results are when querying PuppetDB. This behaviour will change in a future release when pagination will be introduced.
- `UNRESPONSIVE_HOURS`: The amount of hours since the last check-in after which a node is considered unresponsive.
Expand Down
3 changes: 2 additions & 1 deletion puppetboard/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@

from puppetboard.core import get_app, get_puppetdb
from puppetboard.version import __version__
from puppetboard.utils import check_db_version
from puppetboard.utils import check_db_version, check_secret_key

app = get_app()
puppetdb = get_puppetdb()
running_as = os.path.basename(sys.argv[0])
if running_as not in ['pytest', 'py.test']:
check_db_version(puppetdb)
check_secret_key(app.config.get('SECRET_KEY'))

logging.basicConfig(level=app.config['LOGLEVEL'].upper())
log = logging.getLogger(__name__)
Expand Down
4 changes: 2 additions & 2 deletions puppetboard/default_settings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import os
import secrets

PUPPETDB_HOST = 'localhost'
PUPPETDB_PORT = 8080
Expand All @@ -8,7 +8,7 @@
PUPPETDB_CERT = None
PUPPETDB_TIMEOUT = 20
DEFAULT_ENVIRONMENT = 'production'
SECRET_KEY = os.urandom(24)
SECRET_KEY = f"default-{secrets.token_hex(32)}"
UNRESPONSIVE_HOURS = 2
ENABLE_QUERY = True
# Uncomment to restrict the enabled PuppetDB endpoints in the query page.
Expand Down
3 changes: 2 additions & 1 deletion puppetboard/docker_settings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import os
import secrets
import tempfile
import base64
import binascii
Expand Down Expand Up @@ -59,7 +60,7 @@ def coerce_bool(v, default):
PUPPETDB_PROTO = os.getenv('PUPPETDB_PROTO', None)
PUPPETDB_TIMEOUT = int(os.getenv('PUPPETDB_TIMEOUT', '20'))
DEFAULT_ENVIRONMENT = os.getenv('DEFAULT_ENVIRONMENT', 'production')
SECRET_KEY = os.getenv('SECRET_KEY', os.urandom(24))
SECRET_KEY = os.getenv('SECRET_KEY', f"default-{secrets.token_hex(32)}")
UNRESPONSIVE_HOURS = int(os.getenv('UNRESPONSIVE_HOURS', '2'))
ENABLE_QUERY = coerce_bool(os.getenv('ENABLE_QUERY'), True)
# Uncomment to restrict the enabled PuppetDB endpoints in the query page.
Expand Down
21 changes: 21 additions & 0 deletions puppetboard/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,27 @@ def check_db_version(puppetdb):
sys.exit(2)


def check_secret_key(secret_key_value):
"""
Check if the secret key value is set to a default value, that will stop
being accepted in v5.x of the app.
"""
if secret_key_value.startswith("default-"):
log.warning(
"Leaving SECRET_KEY set to a default value WILL cause issues"
" when the app is restarted or has more than 1 replica"
" (f.e. uWSGI workers, k8s replicas etc.) and some features"
" (in particular: queries) are used.\n"
"Please set SECRET_KEY to your own value, the same for all app"
" replicas.\n"
"This will be REQUIRED starting with Puppetboard 5.x which"
" will NOT contain the default value anymore.\n"
"Please see"
" https://github.com/voxpupuli/puppetboard/issues/721"
" for more info."
)


def parse_python(value: str):
"""
:param value: any string, number, bool, list or a dict
Expand Down