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

config: do some basic checks on settings before starting app #27

Merged
merged 1 commit into from
Mar 9, 2024
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
4 changes: 2 additions & 2 deletions src/sambal/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
from zope.interface import implementer

from .client import connect_samdb
from .settings import SETTINGS
from .settings import USE_HTTPS


@implementer(ISecurityPolicy)
class SambalSecurityPolicy:
def __init__(self, secret):
self.authtkt = AuthTktCookieHelper(
secret=secret,
secure=SETTINGS["sambal.https"],
secure=USE_HTTPS,
samesite="Strict",
http_only=True,
)
Expand Down
42 changes: 33 additions & 9 deletions src/sambal/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,45 @@
import os

from pyramid.settings import asbool
from redis.connection import parse_url

# Read environment variables then do some sanity checks.
DEBUG = asbool(os.getenv("SAMBAL_DEBUG", default=False))
USE_HTTPS = asbool(os.getenv("SAMBAL_HTTPS", default=False))
USE_HSTS = asbool(os.getenv("SAMBAL_HSTS", default=False))
REDIS_URL = os.getenv("SAMBAL_REDIS_URL")
SESSION_SECRET = os.getenv("SAMBAL_SESSION_SECRET")
AUTH_SECRET = os.getenv("SAMBAL_AUTH_SECRET")

if REDIS_URL is None:
raise ValueError("Missing SAMBAL_REDIS_URL environment variable")

if "password" not in parse_url(REDIS_URL):
raise ValueError("Missing password in SAMBAL_REDIS_URL, please add one")

if SESSION_SECRET is None:
raise ValueError("Missing SAMBAL_SESSION_SECRET environment variable")

if AUTH_SECRET is None:
raise ValueError("Missing SAMBAL_AUTH_SECRET environment variable")

if SESSION_SECRET == AUTH_SECRET:
raise ValueError(
"Use different values for SAMBAL_AUTH_SECRET and SAMBAL_SESSION_SECRET"
)

# Pyramid settings are traditionally loaded via PasteDeploy ini file.
# With this project we went a different way with env vars.
SETTINGS = {
"sambal.debug": asbool(os.getenv("SAMBAL_DEBUG", default=False)),
"sambal.https": asbool(os.getenv("SAMBAL_HTTPS", default=False)),
"sambal.hsts": asbool(os.getenv("SAMBAL_HSTS", default=False)),
"auth.secret": os.getenv("SAMBAL_AUTH_SECRET"),
"redis.sessions.url": os.getenv("SAMBAL_REDIS_URL"),
"redis.sessions.secret": os.getenv("SAMBAL_SESSION_SECRET"),
"sambal.debug": DEBUG,
"sambal.https": USE_HTTPS,
"sambal.hsts": USE_HSTS,
"auth.secret": AUTH_SECRET,
"redis.sessions.url": REDIS_URL,
"redis.sessions.secret": SESSION_SECRET,
"redis.sessions.serialize": lambda s: json.dumps(s).encode("utf-8"),
"redis.sessions.deserialize": lambda s: json.loads(s.decode("utf-8")),
"redis.sessions.cookie_samesite": "Strict",
"redis.sessions.cookie_httponly": True,
"redis.sessions.cookie_secure": USE_HTTPS,
}

# Only if https is used set cookie_secure.
SETTINGS["redis.sessions.cookie_secure"] = SETTINGS["sambal.https"]
Loading