Skip to content

: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.

fix(tasks): cache read-only sandbox GitHub tokens (#123982)

Alessandro Pogliaghi56e22da5e719
diff --git a/products/tasks/backend/temporal/process_task/utils.py b/products/tasks/backend/temporal/process_task/utils.py
index b7e9c9f5b096..67b22e577c25 100644
--- a/products/tasks/backend/temporal/process_task/utils.py
+++ b/products/tasks/backend/temporal/process_task/utils.py
@@ -1028,9 +1028,31 @@ def _scoped_token_cache_key(github_integration: GitHubIntegrationBase, names: Se
return f"task-sandbox-github-token:{github_integration.github_installation_id}:{','.join(names)}"
+def _readonly_token_cache_key(github_integration: GitHubIntegrationBase, names: Sequence[str]) -> str:
+ return f"task-sandbox-github-readonly-token:{github_integration.github_installation_id}:{','.join(names)}"
+
+
+def _cached_github_token(cache_key: str) -> str | None:
+ try:
+ cached = get_tasks_cache().get(cache_key)
+ except Exception:
+ logger.warning("Tasks cache read failed for %s", cache_key, exc_info=True)
+ return None
+ return cached if isinstance(cached, str) and cached else None
+
+
+def _store_github_token(cache_key: str, token: str) -> None:
+ try:
+ get_tasks_cache().set(cache_key, token, timeout=SCOPED_GITHUB_TOKEN_CACHE_TTL_SECONDS)
+ except Exception:
+ logger.warning("Tasks cache write failed for %s", cache_key, exc_info=True)
+
+
def forget_cached_github_tokens(github_integration: GitHubIntegrationBase, repositories: Sequence[str]) -> None:
names = _installation_repository_names(github_integration, _private_repositories(repositories))
- get_tasks_cache().delete(_scoped_token_cache_key(github_integration, names))
+ get_tasks_cache().delete_many(
+ [_scoped_token_cache_key(github_integration, names), _readonly_token_cache_key(github_integration, names)]
+ )
def installation_confirms_repository_denied(github_integration: GitHubIntegrationBase, repository: str) -> bool:
@@ -1154,9 +1176,15 @@ def get_readonly_github_token(team_id: int, repositories: Sequence[str] = ()) ->
names = _installation_repository_names(integration, private_repositories)
if private_repositories and not names:
return None
- return integration.mint_scoped_installation_token(
+ cache_key = _readonly_token_cache_key(integration, names)
+ cached = _cached_github_token(cache_key)
+ if cached:
+ return cached
+ token = integration.mint_scoped_installation_token(
READONLY_SANDBOX_GITHUB_PERMISSIONS, repositories=names or None
)
+ _store_github_token(cache_key, token)
+ return token
except Exception:
logger.warning("Failed to mint read-only GitHub token for team %d", team_id, exc_info=True)
return None
diff --git a/products/tasks/backend/tests/test_sandbox_utils.py b/products/tasks/backend/tests/test_sandbox_utils.py
index 05d0ef1f3f38..4eddc5bc37a8 100644
--- a/products/tasks/backend/tests/test_sandbox_utils.py
+++ b/products/tasks/backend/tests/test_sandbox_utils.py
@@ -20,15 +20,24 @@ from products.tasks.backend.exceptions import (
GitHubRateLimitedError,
ProcessTaskTransientError,
)
+from products.tasks.backend.redis import get_tasks_cache
from products.tasks.backend.temporal.process_task.activities.get_task_processing_context import TaskProcessingContext
from products.tasks.backend.temporal.process_task.utils import (
READONLY_SANDBOX_GITHUB_PERMISSIONS,
can_mint_readonly_github_token,
+ forget_cached_github_tokens,
get_readonly_github_token,
get_sandbox_api_url,
)
[email protected](autouse=True)
+def _isolated_tasks_cache(settings):
+ settings.TASKS_REDIS_URL = ""
+ settings.CACHES = {"default": {"BACKEND": "django.core.cache.backends.locmem.LocMemCache"}}
+ get_tasks_cache().clear()
+
+
@pytest.mark.parametrize(
"sandbox_api_url, expected",
[
@@ -44,6 +53,8 @@ def test_get_sandbox_api_url(sandbox_api_url: str | None, expected: str) -> None
def _team_integration(*, unavailable: bool = False, mint_raises: bool = False) -> MagicMock:
github = MagicMock(spec=GitHubIntegration)
+ github.github_installation_id = "INSTALL"
+ github.mint_scoped_installation_token.return_value = "ghs_readonly"
github.installation_unavailable.return_value = unavailable
github.account_name_needs_heal.return_value = False
github.organization.return_value = "Acme"
@@ -321,6 +332,40 @@ async def test_github_token_failure_maps_to_run_error(
assert raised.value.next_retry_delay == timedelta(seconds=30)
+def test_readonly_token_is_still_minted_when_the_cache_fails():
+ integration = _team_integration()
+ broken_cache = MagicMock()
+ broken_cache.get.side_effect = ConnectionError("redis down")
+ broken_cache.set.side_effect = ConnectionError("redis down")
+ with (
+ patch(
+ "products.tasks.backend.temporal.process_task.utils.resolve_readonly_github_integration",
+ return_value=integration,
+ ),
+ patch("products.tasks.backend.temporal.process_task.utils.get_tasks_cache", return_value=broken_cache),
+ ):
+ token = get_readonly_github_token(1, repositories=["acme/api"])
+
+ assert token == "ghs_readonly"
+ integration.mint_scoped_installation_token.assert_called_once()
+
+
+def test_readonly_token_is_reused_until_forgotten():
+ integration = _team_integration()
+ integration.mint_scoped_installation_token.side_effect = ["ghs_ro_1", "ghs_ro_2"]
+ with patch(
+ "products.tasks.backend.temporal.process_task.utils.resolve_readonly_github_integration",
+ return_value=integration,
+ ):
+ first = [get_readonly_github_token(1, repositories=["acme/api"]) for _ in range(2)]
+ forget_cached_github_tokens(integration, ["acme/api"])
+ second = get_readonly_github_token(1, repositories=["acme/api"])
+
+ assert first == ["ghs_ro_1", "ghs_ro_1"]
+ assert second == "ghs_ro_2"
+ assert integration.mint_scoped_installation_token.call_count == 2
+
+
@pytest.mark.parametrize(
"repositories, mints, expected",
[