Exemplo n.º 1
0
    def test_fetching_env_var_parsed_as_int(self, mock_env):
        mock_env.return_value = ""
        self.assertEqual(
            get_from_env("test_key", optional=True, type_cast=int), None)

        mock_env.return_value = "4"
        self.assertEqual(get_from_env("test_key", type_cast=int), 4)
Exemplo n.º 2
0
# Web app specific settings/middleware/apps setup
# :NOTE: posthog-cloud modifies some of these values
import os
from datetime import timedelta
from typing import List

from posthog.settings.base_variables import BASE_DIR, DEBUG, TEST
from posthog.settings.statsd import STATSD_HOST
from posthog.settings.utils import get_from_env, str_to_bool

# django-axes settings to lockout after too many attempts

AXES_ENABLED = get_from_env("AXES_ENABLED", not TEST, type_cast=str_to_bool)
AXES_HANDLER = "axes.handlers.cache.AxesCacheHandler"
AXES_FAILURE_LIMIT = int(os.getenv("AXES_FAILURE_LIMIT", 5))
AXES_COOLOFF_TIME = timedelta(minutes=15)
AXES_LOCKOUT_CALLABLE = "posthog.api.authentication.axes_locked_out"
AXES_META_PRECEDENCE_ORDER = [
    "HTTP_X_FORWARDED_FOR",
    "REMOTE_ADDR",
]

# Application definition

INSTALLED_APPS = [
    "whitenoise.runserver_nostatic",  # makes sure that whitenoise handles static files in development
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
Exemplo n.º 3
0
from posthog.settings.data_stores import *
from posthog.settings.dynamic_settings import *
from posthog.settings.feature_flags import *
from posthog.settings.logs import *
from posthog.settings.sentry import *
from posthog.settings.shell_plus import *
from posthog.settings.service_requirements import *
from posthog.settings.statsd import *

from posthog.settings.web import *

from posthog.settings.utils import get_from_env, str_to_bool

USE_PRECALCULATED_CH_COHORT_PEOPLE = not TEST
CALCULATE_X_COHORTS_PARALLEL = get_from_env("CALCULATE_X_COHORTS_PARALLEL",
                                            2,
                                            type_cast=int)

# Instance configuration preferences
# https://posthog.com/docs/self-host/configure/environment-variables
SELF_CAPTURE = get_from_env("SELF_CAPTURE", DEBUG, type_cast=str_to_bool)
debug_queries = get_from_env("DEBUG_QUERIES", False, type_cast=str_to_bool)
disable_paid_fs = get_from_env("DISABLE_PAID_FEATURE_SHOWCASING",
                               False,
                               type_cast=str_to_bool)
INSTANCE_PREFERENCES = {
    "debug_queries": debug_queries,
    "disable_paid_fs": disable_paid_fs,
}

SITE_URL: str = os.getenv("SITE_URL", "http://localhost:8000").rstrip("/")
Exemplo n.º 4
0
import os
from urllib.parse import urlparse

import dj_database_url
from django.core.exceptions import ImproperlyConfigured

from posthog.settings.base_variables import DEBUG, IS_COLLECT_STATIC, TEST
from posthog.settings.utils import get_from_env, str_to_bool

# See https://docs.djangoproject.com/en/3.1/ref/settings/#std:setting-DATABASE-DISABLE_SERVER_SIDE_CURSORS
DISABLE_SERVER_SIDE_CURSORS = get_from_env("USING_PGBOUNCER", False, type_cast=str_to_bool)

# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases

if TEST or DEBUG:
    PG_HOST = os.getenv("PGHOST", "localhost")
    PG_USER = os.getenv("PGUSER", "posthog")
    PG_PASSWORD = os.getenv("PGPASSWORD", "posthog")
    PG_PORT = os.getenv("PGPORT", "5432")
    PG_DATABASE = os.getenv("PGDATABASE", "posthog")
    DATABASE_URL = os.getenv("DATABASE_URL", f"postgres://{PG_USER}:{PG_PASSWORD}@{PG_HOST}:{PG_PORT}/{PG_DATABASE}")
else:
    DATABASE_URL = os.getenv("DATABASE_URL", "")

if DATABASE_URL:
    DATABASES = {"default": dj_database_url.config(default=DATABASE_URL, conn_max_age=600)}
    if DISABLE_SERVER_SIDE_CURSORS:
        DATABASES["default"]["DISABLE_SERVER_SIDE_CURSORS"] = True
elif os.getenv("POSTHOG_DB_NAME"):
    DATABASES = {
Exemplo n.º 5
0
# Settings which restrict access to the site across the app
import os
import sys

from posthog.settings.base_variables import DEBUG, TEST
from posthog.settings.utils import get_from_env, get_list, print_warning, str_to_bool

# SSL & cookie defaults
if os.getenv("SECURE_COOKIES", None) is None:
    # Default to True if in production
    secure_cookies = not DEBUG and not TEST
else:
    secure_cookies = get_from_env("SECURE_COOKIES", True, type_cast=str_to_bool)

TOOLBAR_COOKIE_SECURE = secure_cookies
SESSION_COOKIE_SECURE = secure_cookies
CSRF_COOKIE_SECURE = secure_cookies
SECURE_SSL_REDIRECT = secure_cookies
SECURE_REDIRECT_EXEMPT = [r"^_health/?"]

if get_from_env("DISABLE_SECURE_SSL_REDIRECT", False, type_cast=str_to_bool):
    SECURE_SSL_REDIRECT = False


# Proxy settings
IS_BEHIND_PROXY = get_from_env("IS_BEHIND_PROXY", False, type_cast=str_to_bool)
TRUSTED_PROXIES = os.getenv("TRUSTED_PROXIES", None)
TRUST_ALL_PROXIES = os.getenv("TRUST_ALL_PROXIES", False)


if IS_BEHIND_PROXY:
Exemplo n.º 6
0
# Web app specific settings/middleware/apps setup
# :NOTE: posthog-cloud modifies some of these values
import os
from datetime import timedelta
from typing import List

from posthog.settings.base_variables import BASE_DIR, DEBUG, TEST
from posthog.settings.statsd import STATSD_HOST
from posthog.settings.utils import get_from_env, str_to_bool

# django-axes settings to lockout after too many attempts

AXES_ENABLED = get_from_env("AXES_ENABLED", not TEST, type_cast=str_to_bool)
AXES_HANDLER = "axes.handlers.cache.AxesCacheHandler"
AXES_FAILURE_LIMIT = get_from_env("AXES_FAILURE_LIMIT", 30, type_cast=int)
AXES_COOLOFF_TIME = timedelta(minutes=10)
AXES_LOCKOUT_CALLABLE = "posthog.api.authentication.axes_locked_out"
AXES_META_PRECEDENCE_ORDER = [
    "HTTP_X_FORWARDED_FOR",
    "REMOTE_ADDR",
]

# Application definition

INSTALLED_APPS = [
    "whitenoise.runserver_nostatic",  # makes sure that whitenoise handles static files in development
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
Exemplo n.º 7
0
from posthog.settings.base_variables import DEBUG, IS_COLLECT_STATIC, TEST
from posthog.settings.utils import get_from_env, print_warning, str_to_bool
from posthog.version_requirement import ServiceVersionRequirement

SKIP_SERVICE_VERSION_REQUIREMENTS = get_from_env(
    "SKIP_SERVICE_VERSION_REQUIREMENTS", TEST or IS_COLLECT_STATIC or DEBUG, type_cast=str_to_bool
)

if SKIP_SERVICE_VERSION_REQUIREMENTS and not (TEST or DEBUG):
    print_warning(["Skipping service version requirements. This is dangerous and PostHog might not work as expected!"])

SERVICE_VERSION_REQUIREMENTS = [
    ServiceVersionRequirement(service="postgresql", supported_version=">=11.0.0,<=14.1.0",),
    ServiceVersionRequirement(service="redis", supported_version=">=5.0.0,<=6.3.0",),
    ServiceVersionRequirement(service="clickhouse", supported_version=">=21.6.0,<21.12.0"),
]
Exemplo n.º 8
0
# Overridden in posthog-cloud

import sys

from posthog.settings.utils import get_from_env, print_warning, str_to_bool

# Early exit to avoid issues with cloud not being properly included
if get_from_env("MULTI_TENANCY", False, type_cast=str_to_bool):
    print_warning((
        "️Environment variable MULTI_TENANCY is set, but cloud settings have not been included",
    ))
    sys.exit("[ERROR] Stopping Django server…\n")
Exemplo n.º 9
0
from posthog.constants import AnalyticsDBMS
from posthog.settings.data_stores import PRIMARY_DB
from posthog.settings.utils import get_from_env, str_to_bool

# shell_plus settings
# https://django-extensions.readthedocs.io/en/latest/shell_plus.html

SHELL_PLUS_PRINT_SQL = get_from_env("PRINT_SQL", False, type_cast=str_to_bool)
SHELL_PLUS_POST_IMPORTS = [
    ("posthog.models.filters", ("Filter", )),
    ("posthog.models.property", ("Property", )),
]

if PRIMARY_DB == AnalyticsDBMS.CLICKHOUSE:
    SHELL_PLUS_POST_IMPORTS.append(
        ("ee.clickhouse.client", ("sync_execute", )))
Exemplo n.º 10
0
import os
import sys

from posthog.settings.utils import get_from_env, print_warning, str_to_bool

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(
    os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

DEBUG = get_from_env("DEBUG", False, type_cast=str_to_bool)
TEST = ("test" in sys.argv or sys.argv[0].endswith("pytest")
        or get_from_env("TEST", False, type_cast=str_to_bool))  # type: bool
E2E_TESTING = get_from_env(
    "E2E_TESTING",
    False,
    type_cast=str_to_bool,
)  # whether the app is currently running for E2E tests
BENCHMARK = get_from_env("BENCHMARK", False, type_cast=str_to_bool)
if E2E_TESTING:
    print_warning([
        "️WARNING! E2E_TESTING is set to `True`. This is a security vulnerability unless you are running tests."
    ])

IS_COLLECT_STATIC = len(sys.argv) > 1 and sys.argv[1] == "collectstatic"

if DEBUG and not TEST:
    print_warning((
        "️Environment variable DEBUG is set - PostHog is running in DEVELOPMENT MODE!",
        "Be sure to unset DEBUG if this is supposed to be a PRODUCTION ENVIRONMENT!",
    ))
Exemplo n.º 11
0
CONSTANCE_BACKEND = "constance.backends.database.DatabaseBackend"

CONSTANCE_DATABASE_PREFIX = "constance:posthog:"

# Warning: Dynamically updating these settings should only be done through the API.
# CONSTANCE_CONFIG: https://django-constance.readthedocs.io/en/latest/

CONSTANCE_CONFIG = {
    "RECORDINGS_TTL_WEEKS": (
        3,
        "Number of weeks recordings will be kept before removing them (for all projects). Storing recordings for a shorter timeframe can help reduce Clickhouse disk usage.",
        int,
    ),
    "MATERIALIZED_COLUMNS_ENABLED": (
        get_from_env("MATERIALIZED_COLUMNS_ENABLED",
                     True,
                     type_cast=str_to_bool),
        "Whether materialized columns should be created or used at query time.",
        bool,
    ),
    "COMPUTE_MATERIALIZED_COLUMNS_ENABLED": (
        get_from_env("COMPUTE_MATERIALIZED_COLUMNS_ENABLED",
                     True,
                     type_cast=str_to_bool),
        "Whether materialized columns should be created or updated (existing columns will still be used at query time).",
        bool,
    ),
    "AGGREGATE_BY_DISTINCT_IDS_TEAMS": (
        get_from_env("AGGREGATE_BY_DISTINCT_IDS_TEAMS", ""),
        "Whether unique users should be counted by distinct IDs. Speeds up queries at the cost of accuracy.",
        str,
Exemplo n.º 12
0
from posthog.settings.base_variables import E2E_TESTING, TEST
from posthog.settings.overrides import cmd
from posthog.settings.service_requirements import SKIP_SERVICE_VERSION_REQUIREMENTS
from posthog.settings.utils import get_from_env, str_to_bool

_default_skip_async_migrations_setup = TEST or E2E_TESTING or SKIP_SERVICE_VERSION_REQUIREMENTS or cmd != "runserver"
SKIP_ASYNC_MIGRATIONS_SETUP = get_from_env(
    "SKIP_ASYNC_MIGRATIONS_SETUP",
    _default_skip_async_migrations_setup,
    type_cast=str_to_bool)

ASYNC_MIGRATIONS_DEFAULT_TIMEOUT_SECONDS = get_from_env(
    "ASYNC_MIGRATIONS_DEFAULT_TIMEOUT_SECONDS", 60 * 30, type_cast=int)
Exemplo n.º 13
0
from posthog.settings.utils import get_from_env, str_to_bool

CONSTANCE_BACKEND = "constance.backends.database.DatabaseBackend"

CONSTANCE_DATABASE_PREFIX = "constance:posthog:"

# Warning: Dynamically updating these settings should only be done through the API.
CONSTANCE_CONFIG = {
    "RECORDINGS_TTL_WEEKS": (
        3,
        "Number of weeks recordings will be kept before removing them (for all projects). Storing recordings for a shorter timeframe can help reduce Clickhouse disk usage.",
        int,
    ),
    "MATERIALIZED_COLUMNS_ENABLED": (
        get_from_env("MATERIALIZED_COLUMNS_ENABLED",
                     True,
                     type_cast=str_to_bool),
        "Whether materialized columns should be created or used at query time.",
        bool,
    ),
    "COMPUTE_MATERIALIZED_COLUMNS_ENABLED": (
        get_from_env("COMPUTE_MATERIALIZED_COLUMNS_ENABLED",
                     True,
                     type_cast=str_to_bool),
        "Whether materialized columns should be created or updated (existing columns will still be used at query time).",
        bool,
    ),
    "AUTO_START_ASYNC_MIGRATIONS": (
        get_from_env("AUTO_START_ASYNC_MIGRATIONS",
                     False,
                     type_cast=str_to_bool),