:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(data-imports): config repr output (#64928)
Fernando Gomes5b1a73c84c9b
diff --git a/posthog/temporal/data_imports/sources/common/config.py b/posthog/temporal/data_imports/sources/common/config.pyindex 5e0167896676..11aeee13b6d1 100644--- a/posthog/temporal/data_imports/sources/common/config.py+++ b/posthog/temporal/data_imports/sources/common/config.py@@ -1,5 +1,6 @@import typesimport typing+import reprlibimport builtinsimport operatorimport functools@@ -10,6 +11,49 @@ META_KEY = "_SOURCE_CONFIG_META"_T = typing.TypeVar("_T")+_SECRET_FIELD_NAME_SUBSTRINGS: tuple[str, ...] = (+ "password",+ "passphrase",+ "secret",+ "token",+ "key",+ "credential",+ "certificate",+ "root_ca",+ "connection_string",+ "authorization",+ "manifest_json",+)++REDACTED_VALUE_REPR = "'***'"+++def _is_secret_field_name(name: str) -> bool:+ lowered = name.lower()+ return any(substring in lowered for substring in _SECRET_FIELD_NAME_SUBSTRINGS)++[email protected]_repr()+def _redacting_repr(self: typing.Any) -> str:+ """`__repr__` for config classes that redacts the values of secret-bearing fields.++ Nested config values render via their own (also redacting) repr, so secrets stay+ redacted at every level. A `None` value is shown as-is — there is nothing to leak and+ it usefully signals the secret is unset.+ """+ field_reprs = []+ for field in dataclasses.fields(self):+ if not field.repr:+ continue+ value = getattr(self, field.name)+ # A nested config (e.g. BigQuery's `key_file`) renders via its own redacting repr so+ # its non-secret fields stay visible; only a raw secret value is masked wholesale.+ if value is not None and _is_secret_field_name(field.name) and not is_config(value):+ field_reprs.append(f"{field.name}={REDACTED_VALUE_REPR}")+ else:+ field_reprs.append(f"{field.name}={value!r}")+ return f"{type(self).__name__}({', '.join(field_reprs)})"+class _Dataclass(typing.Protocol):__dataclass_fields__: typing.ClassVar[dict[str, typing.Any]]@@ -562,6 +606,9 @@ def config(setattr(cls, "from_dict", classmethod(from_dict)) # noqa: B010setattr(cls, "validate_dict", classmethod(validate_dict)) # noqa: B010setattr(cls, "__source_config_meta", MetaConfig(prefix=prefix)) # noqa: B010+ # Override the dataclass-generated repr so credentials never reach logs or+ # error-tracking stack-frame locals.+ cls.__repr__ = _redacting_repr # type: ignore[method-assign]return clsdiff --git a/posthog/temporal/data_imports/sources/common/test/test_config.py b/posthog/temporal/data_imports/sources/common/test/test_config.pyindex 4b2bd021bedd..9c541a787779 100644--- a/posthog/temporal/data_imports/sources/common/test/test_config.py+++ b/posthog/temporal/data_imports/sources/common/test/test_config.py@@ -486,6 +486,144 @@ def test_from_dict_with_non_mapping_raises_clear_error(bad_input):SourceConfig.from_dict(bad_input)+class _SecretFieldConfig(config.Config):+ password: str | None = None+ passphrase: str | None = None+ secret_key: str | None = None+ client_secret: str | None = None+ api_key: str | None = None+ access_token: str | None = None+ refresh_token: str | None = None+ private_key: str | None = None+ client_certificate: str | None = None+ client_private_key: str | None = None+ server_client_root_ca: str | None = None+ connection_string: str | None = None+ manifest_json: str | None = None+ authorization_header: str | None = None+ signing_secret: str | None = None+++ "field_name",+ [+ "password",+ "passphrase",+ "secret_key",+ "client_secret",+ "api_key",+ "access_token",+ "refresh_token",+ "private_key",+ "client_certificate",+ "client_private_key",+ "server_client_root_ca",+ "connection_string",+ "manifest_json",+ "authorization_header",+ "signing_secret",+ ],+)+def test_repr_redacts_secret_fields(field_name):+ cfg = _SecretFieldConfig()+ setattr(cfg, field_name, "super-secret-value")++ rendered = repr(cfg)+ assert "super-secret-value" not in rendered+ assert f"{field_name}='***'" in rendered+++def test_repr_keeps_non_secret_fields_visible():+ @config.config+ class TestConfig(config.Config):+ host: str+ database: str+ user: str+ password: str++ cfg = TestConfig(host="db.example.com", database="prod", user="reader", password="hunter2")++ rendered = repr(cfg)+ assert "host='db.example.com'" in rendered+ assert "database='prod'" in rendered+ assert "user='reader'" in rendered+ # The credential is the only field redacted.+ assert "hunter2" not in rendered+ assert "password='***'" in rendered+++def test_repr_shows_none_secret_as_none():+ @config.config+ class TestConfig(config.Config):+ password: str | None = None++ # An unset secret has nothing to leak, so it stays visible to aid debugging.+ assert "password=None" in repr(TestConfig())+++def test_repr_redacts_nested_config_secrets():+ @config.config+ class AuthConfig:+ username: str+ password: str | None = None++ @config.config+ class TestConfig(config.Config):+ host: str+ auth: AuthConfig++ cfg = TestConfig(host="db.example.com", auth=AuthConfig(username="reader", password="hunter2"))++ rendered = repr(cfg)+ assert "hunter2" not in rendered+ assert "password='***'" in rendered+ assert "username='reader'" in rendered+++def test_repr_redacts_real_postgres_source_config():+ from posthog.temporal.data_imports.sources.generated_configs import PostgresSourceConfig++ cfg = PostgresSourceConfig(+ host="fadevpn-11499.example.cloud",+ database="defaultdb",+ user="datawarehouse",+ password="hunter2",+ port=26257,+ )++ rendered = repr(cfg)+ assert "hunter2" not in rendered+ assert "password='***'" in rendered+ # Connection metadata stays visible for debugging.+ assert "host='fadevpn-11499.example.cloud'" in rendered+++def test_repr_recurses_into_nested_config_with_secret_field_name():+ @config.config+ class KeyFileConfig:+ project_id: str+ client_email: str+ private_key: str++ @config.config+ class TestConfig(config.Config):+ # Field name contains "key" but holds a nested config, not a raw secret.+ key_file: KeyFileConfig++ cfg = TestConfig(+ key_file=KeyFileConfig(project_id="my-project", client_email="[email protected]", private_key="-----BEGIN-----")+ )++ rendered = repr(cfg)+ # The nested object is not redacted wholesale — its non-secret fields stay visible.+ assert "project_id='my-project'" in rendered+ assert "client_email='[email protected]'" in rendered+ # The actual secret inside the nested config is still redacted.+ assert "BEGIN" not in rendered+ assert "private_key='***'" in rendered++def test_validate_dict():@config.configclass TestConfig(config.Config):