: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(access-control): add per-project config for terraform management (#114549)
Alex Liderf40b7137564d
diff --git a/.github/scripts/check-idor-model-coverage.py b/.github/scripts/check-idor-model-coverage.pyindex 346c34971199..e7ec307be58b 100644--- a/.github/scripts/check-idor-model-coverage.py+++ b/.github/scripts/check-idor-model-coverage.py@@ -203,6 +203,7 @@ def get_scoped_models() -> tuple[dict[str, set[str]], set[str], set[str], set[st"DuckgresServer","EvaluationConfig","RemoteConfig",+ "TeamAccessControlConfig","TeamConversationsSlackConfig","TeamConversationsTeamsChannelSync","TeamConversationsTicketConfig",diff --git a/products/access_control/backend/migrations/0003_teamaccesscontrolconfig.py b/products/access_control/backend/migrations/0003_teamaccesscontrolconfig.pynew file mode 100644index 000000000000..08a58795661c--- /dev/null+++ b/products/access_control/backend/migrations/0003_teamaccesscontrolconfig.py@@ -0,0 +1,50 @@+# Generated by Django 5.2.18 on 2026-10-12 10:40++import django.db.models.deletion+from django.db import migrations, models++import products.access_control.backend.models.team_access_control_config+++class Migration(migrations.Migration):+ dependencies = [+ ("access_control", "0002_alter_propertyaccesscontrol_created_by"),+ ("posthog", "1395_id_jag_identity"),+ ]++ operations = [+ migrations.CreateModel(+ name="TeamAccessControlConfig",+ fields=[+ (+ "team",+ models.OneToOneField(+ db_constraint=False,+ on_delete=django.db.models.deletion.CASCADE,+ primary_key=True,+ related_name="+",+ serialize=False,+ to="posthog.team",+ ),+ ),+ (+ "is_managed_by_terraform",+ models.BooleanField(db_default=False, default=False),+ ),+ (+ "terraform_last_applied_at",+ models.DateTimeField(blank=True, null=True),+ ),+ (+ "terraform_managed_by",+ models.ForeignKey(+ blank=True,+ null=True,+ on_delete=products.access_control.backend.models.team_access_control_config.release_terraform_lock,+ related_name="+",+ to="posthog.organizationmembership",+ ),+ ),+ ],+ ),+ ]diff --git a/products/access_control/backend/migrations/max_migration.txt b/products/access_control/backend/migrations/max_migration.txtindex fbe0e56a1ac4..d73eb2f04ec4 100644--- a/products/access_control/backend/migrations/max_migration.txt+++ b/products/access_control/backend/migrations/max_migration.txt@@ -1 +1 @@-0002_alter_propertyaccesscontrol_created_by+0003_teamaccesscontrolconfigdiff --git a/products/access_control/backend/models/__init__.py b/products/access_control/backend/models/__init__.pyindex 4fa50e59c71e..0f2b23251d2c 100644--- a/products/access_control/backend/models/__init__.py+++ b/products/access_control/backend/models/__init__.py@@ -4,6 +4,7 @@ from .organization_resource_access import OrganizationResourceAccessfrom .property_access_control import PropertyAccessControlfrom .role import Role, RoleMembershipfrom .role_external_reference import RoleExternalReference+from .team_access_control_config import TeamAccessControlConfig__all__ = ["AccessControl",@@ -13,4 +14,5 @@ __all__ = ["Role","RoleExternalReference","RoleMembership",+ "TeamAccessControlConfig",]diff --git a/products/access_control/backend/models/team_access_control_config.py b/products/access_control/backend/models/team_access_control_config.pynew file mode 100644index 000000000000..f22ecfe864c2--- /dev/null+++ b/products/access_control/backend/models/team_access_control_config.py@@ -0,0 +1,49 @@+from typing import Any, cast++from django.db import models+from django.db.models.deletion import Collector++LOCK_FIELD = "is_managed_by_terraform"+++def release_terraform_lock(collector: Collector, field: models.ForeignKey, sub_objs: Any, using: str) -> None:+ """on_delete for terraform_managed_by. When the Terraform account leaves the organization, disable+ the lock and set the account to null in the same delete. An enabled lock with no account refuses+ every write, so without this step nobody could change the access rules of the project.++ The lock update is queued first. The collector runs its field updates in insertion order against+ one queryset, which filters on the key that the second update sets to null.+ """+ lock_field = cast(models.Field, field.model._meta.get_field(LOCK_FIELD))+ collector.add_field_update(lock_field, False, sub_objs)+ collector.add_field_update(field, None, sub_objs)+++release_terraform_lock.lazy_sub_objs = True # type: ignore[attr-defined]+++class TeamAccessControlConfig(models.Model):+ """Per-project access control settings. A row exists only after the first write. Read it+ through get_or_create_team_extension."""++ # db_constraint=False so that the CREATE TABLE takes no lock on posthog_team.+ # related_name="+" keeps both relations off the product boundary, which the model crossing+ # guard enforces.+ team = models.OneToOneField(+ "posthog.Team", on_delete=models.CASCADE, primary_key=True, db_constraint=False, related_name="+"+ )+ # When true, only terraform_managed_by may change this project's access rules. Admins turn it off and on.+ # A rule write from Terraform turns it on.+ is_managed_by_terraform = models.BooleanField(default=False, db_default=False)+ # The account behind the API key Terraform uses for this project, kept while the lock is off so+ # that an admin can turn it on again. When the account leaves the organization, the field+ # becomes null and the lock is disabled, so the rules return to the UI.+ terraform_managed_by = models.ForeignKey(+ "posthog.OrganizationMembership",+ null=True,+ blank=True,+ on_delete=release_terraform_lock,+ related_name="+",+ )+ # Set on every Terraform write of an access rule in this project.+ terraform_last_applied_at = models.DateTimeField(null=True, blank=True)