Skip to content

feat(automations): short-lived secret lookup references for the automations service - #1205

Merged
ak684 merged 3 commits into
mainfrom
alona/secret-lookups-service-route
Nov 5, 2026
Merged

ak684 merged 3 commits into
mainfrom
alona/secret-lookups-service-route

Conversation

@ak684

@ak684 ak684 commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Why

The automations service needs to fetch a plugin from a private repository as the user or service account an automation runs as (OpenHands/automation#694). It has to do this when an automation is created or edited and before each run, when no sandbox exists yet. Until now only a conversation launch could hand out a reference to a member's git token, and no reference could name a custom secret or an organization.

Summary

  • New service route, POST /api/service/users/{user_id}/orgs/{org_id}/secret-lookups (service key only). It takes 1 to 20 secret names and returns one short-lived reference per name that resolves: a JWS with audience SECRET_LOOKUP that names user_id, org_id and exactly one of provider_type, oauth_provider_id or secret_name. TTL is 900 s by default and 3600 s at most. The response never contains a value.
    • Names resolve the way a conversation of that principal in that org sees them: a linked OAuth v2 provider's named secret first, then <PROVIDER_TYPE>_TOKEN for a connected provider, then the org's personal and org-shared custom secrets. A provider or OAuth secret wins a name collision, as at conversation launch. The lowercase <provider>_token that the sandbox secrets endpoint uses also names the provider token, unless a custom secret has exactly that name: a conversation keeps such a secret under its own name, so it wins here too. OPENHANDS_API_KEY is never handed out. Names that resolve to nothing are listed in missing.
    • Only an active member gets references: the route returns 404 for an unknown or disabled user, a non-member, and an inactive or invited membership, as the membership route reports them. A missing user is not provisioned. Service-key errors are 401 and 503, as on the other service routes.
    • lookup_url is the URL conversations give sandboxes. When there is none (no web URL, or localhost with remote sandboxes) the route returns 503.
  • GET /api/v1/webhooks/secrets resolves the new references.
    • A reference with org_id must carry the SECRET_LOOKUP audience and an expiry; tokens minted before audiences are not accepted for it.
    • Each redemption first checks the same rule the route applied when it signed the reference: the user exists, is not disabled or being deleted, and is an active member of the org. There is no super-admin exception, and the check does not depend on ENABLE_SUPER_ADMIN. Anyone else gets 403 before anything is read.
    • With secret_name, it returns that custom secret from the org's merged view, or 404.
    • Every org-bound resolution writes an audit log line with user, org, kind and name, never the value.
    • Tokens without org_id (all tokens conversations sign today) work exactly as before.
  • secret_lookup_url() is shared with the conversation service, so both build the sandbox lookup URL the same way.

Issue Number

Fixes #1202. Related: OpenHands/automation#694, Linear OSS-15139.

How to Test

  • uv run pytest tests/unit/server/routes/test_secret_lookups.py (62 passed). These are new tests on real PostgreSQL rows. They cover every acceptance bullet:

    • references for provider, named OAuth, personal and org-shared custom secrets, and the missing list;
    • a human member and a service account;
    • 404 for a non-member and for an inactive or invited membership;
    • an org A reference read while the user's current org is B;
    • 404 after the secret is deleted, 403 after the principal leaves the org, and 401 after exp;
    • a reference signed while active, redeemed after the membership turns inactive or invited, or the user is disabled or being deleted: 403, with ENABLE_SUPER_ADMIN off and on, and for an instance super admin;
    • a custom github_token with GitHub connected resolves to the custom secret, while GITHUB_TOKEN still resolves to the provider token;
    • org-bound references with no audience, another audience, an audience list or no expiry: 401, while conversation references, including ones minted before audiences, resolve as before;
    • no value in any response body or log line.
      An end-to-end test signs a reference, passes it as an SDK LookupSecret to preflight_plugin against a private git server served over HTTP, and the clone succeeds. With a rotated secret it fails as auth_failed, with no value in the error.
  • uv run pytest tests/unit/app_server/test_webhook_router_secrets.py tests/unit/routes/test_service.py tests/unit/app_server/test_sandbox_org_secret_binding.py tests/unit/app_server/test_live_status_app_conversation_service.py tests/unit/app_server/test_webhook_router_auth.py (496 passed, 1 skipped). test_webhook_router_secrets.py is unchanged.

  • The new route is in the service-account guard's allowlist, like the other service-key routes (tests/unit/server/routes/test_service_account_guard.py passes).

  • Live check on an OHE test install (enterprise 1.85.0, DB 212, agent server 1.59.0), with this PR's image at 0828f1d1 (before the redemption and naming checks above) on every enterprise-server container and feat: plugin-spec automations from private repositories automation#696's image:

    • The route signed references (TTL 900 s, purpose plugin_source, missing empty) for a Bitbucket Data Center provider token, a GitLab provider token and two custom secrets.
    • The lookup handler resolved the references for the automation service's own preflight, at create and before each run, and for the agent server in each run's sandbox, which called the URL from lookup_url.
    • Plugin-spec automations from a private Bitbucket Data Center repository and a private GitLab project were created, and their runs completed.
    • After a custom secret was deleted, the route returned no reference for it, and the automation reported "Missing secret".
    • The audit lines (service:secret_lookups_signed, webhooks:secret_lookup) carry names, kinds and found only. No token value, raw or percent-encoded, appears in the app-server or automation logs.
    • No migration ran; the DB stayed at 212.
  • Live re-check on the same install with this PR's image at 7a7a7b2f and automation main 85343cbc (agent server 1.59.0). Service accounts were turned on for this check only, then turned off again.

    • Conversation runner, private plugin. The plugin uses the stock conversation runner and lives in a private GitLab project.
      • Run 1 completed. The app server started the conversation from the user's agent profile, and the plugin's command reached the agent from the pinned checkout.
      • After the secret was deleted, the next run failed with "Missing secret: RS226P_PLUGIN_TOKEN" (permanent). No conversation started.
    • Script runner. A plugin fetched with the user's GitLab OAuth token completed.
    • Membership. The principal was a service account in the user's org.
      • While its membership was active, a reference minted for it resolved, and its automation's run completed.
      • After the membership was set to inactive, the same reference got 403 and a new reference could not be minted (404). The automation's next run failed with "not an active member of the organization", and the automation was paused.
      • After the membership was restored, references resolved again.
    • Lowercase alias. A custom secret named gitlab_token resolved to the custom value, while GITLAB_TOKEN resolved to the provider token.
    • No leaks. No token value appears in the automation database or in the automation and app-server logs.

    Screenshots from the test install (hosts and identities masked):

    Run 1 of the conversation-runner automation: the private plugin's command ran in a conversation the app server started.

    Run 1 of the conversation-runner automation

    The same automation after its secret was deleted: the next run failed with "Missing secret: RS226P_PLUGIN_TOKEN".

    The same automation after its secret was deleted

    The service account's automation after its membership was set to inactive: the run failed and the automation is paused.

    The service account's automation after its membership was set to inactive

    The script runner with the user's GitLab OAuth token: the run succeeded.

    The script runner with the user's GitLab OAuth token

Type

  • Bug fix
  • Feature
  • Refactor
  • Breaking change
  • Docs / chore

Notes

This adds a credential-issuing route, so it needs a human security review before it merges. Authorization is checked at the start of each redemption, and revocation takes effect for the next request. Please check:

  • the membership checks on the route and in the lookup handler;
  • that the route accepts only the service key, never a user API key;
  • the claims and TTL of the references;
  • that custom secrets are read from the org named in the token and not from the user's current org.

No migration and no configuration change.


Enterprise server image for this PR:

ghcr.io/openhands/enterprise-server:sha-7a7a7b2

@all-hands-bot

all-hands-bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Review posted for 7a7a7b2fa03b

Verdict: ✅ APPROVED. See the review.

This is an automated status.

@github-actions

github-actions Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  openhands/app_server/app_conversation
  live_status_app_conversation_service.py
  openhands/app_server/event_callback
  webhook_router.py 696-697, 702, 745-747, 788
  server/routes
  service.py
  server/services
  secret_lookup_service.py 100-101, 210-217, 229
Project Total  

This report was generated by python-coverage-comment-action

@all-hands-bot all-hands-bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This review was posted by an AI agent (OpenHands).

I found no material issues on 0828f1d1b135.

I checked the areas the PR Notes ask about, plus name precedence:

  • Service key only. create_secret_lookups uses validate_service_api_key, the same check as the other /api/service routes. No user-auth dependency can reach the route. Its entry in the service-account guard allowlist matches the existing api-keys routes.
  • Membership at signing. The route requires an existing user who is not disabled and not pending deletion, and an active membership (_membership_status). It does not provision a missing user. After that, user_auth_for_org sets the org override and resolves it before it reads anything. A suspended org therefore also fails at signing.
  • Membership at resolution. _resolve_org_user_context resolves the override before it reads a secret. _resolve_and_verify_override_org returns 403 when the membership row is gone. assert_org_usable_for_product returns 403 for an inactive membership or a suspended org, and get_for_user gives 403 for a disabled user. Inactive memberships are only set through the Super Admin directory, and that same flag gates the check, so the lookup handler applies the same gates as sandbox credential binding.
  • Org scoping. Custom secrets come from SaasSecretsStore with effective_org_id taken from the override, never from current_org_id. Provider and OAuth tokens are stored per user, and the org claim only adds the membership check.
  • Claims and TTL. Each reference is HS256 with aud=SECRET_LOOKUP and exactly one target claim; the handler rejects 0 or 2+ targets with 401. TTL is limited to 1 to 3600 s. accept_unscoped still requires provider_type or oauth_provider_id, so a secret_name reference without an audience is rejected. A secret_name reference without org_id is also rejected. OPENHANDS_API_KEY is excluded both at signing and at resolution.
  • Precedence. OAuth named secret, then <TYPE>_TOKEN (skipped for OAuth-linked types), then custom. This matches _get_provider_token_secrets and _setup_secrets_for_git_providers. Legacy tokens without org_id take the old path unchanged.

Verification on this head (local PostgreSQL via testcontainers):

  • uv run pytest tests/unit/server/routes/test_secret_lookups.py tests/unit/server/routes/test_service_account_guard.py: 76 passed. This includes the SDK preflight_plugin clone against the private git server.
  • uv run pytest tests/unit/app_server/test_webhook_router_secrets.py tests/unit/routes/test_service.py tests/unit/app_server/test_sandbox_org_secret_binding.py tests/unit/app_server/test_live_status_app_conversation_service.py tests/unit/app_server/test_webhook_router_auth.py: 496 passed, 1 skipped.
  • Every GitHub Actions check on this head passed or was skipped.

The PR Notes ask for a human security review because this route issues credentials. This approval covers the code review only and does not replace that review. The live check on a test install is still listed as pending in the PR body.

✅ APPROVED

@ak684
ak684 marked this pull request as ready for review November 5, 2026 12:53
@ak684
ak684 requested a review from all-hands-bot November 5, 2026 14:20

@all-hands-bot all-hands-bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This review was posted by an AI agent (OpenHands).

My earlier review of 0828f1d1b135 had no findings and opened no threads, so nothing carries over. This review covers only what changed in 7a7a7b2fa03b. I found no new material issues.

  • Redemption check (is_active_org_member). _resolve_org_user_context now checks the signing-time rule before it builds the user auth: the user exists, is not disabled or being deleted, and has status in (None, 'active'). This no longer depends on ENABLE_SUPER_ADMIN and has no super-admin bypass. An invited or unknown status is denied, the same as the route's _membership_status. The route now calls the same helper, so signing and redemption cannot drift apart. The override check still runs afterwards and keeps the suspended-org gate.
  • Org-bound references need an audience and exp. _is_unscoped_secret_lookup_token now excludes org_id and secret_name, so the legacy no-aud path cannot reach _get_org_secret. _is_scoped_org_reference requires aud to be exactly the SECRET_LOOKUP string, which rejects audience lists. It also requires a numeric exp, because PyJWT does not require exp by default. Tokens without org_id take the unchanged path.
  • Lowercase alias. A custom secret named exactly <provider>_token now wins over the provider token. This matches a conversation launch, which exposes the provider token only as <PROVIDER_TYPE>_TOKEN and keeps custom secrets under their own names. <PROVIDER_TYPE>_TOKEN and OAuth named secrets still win collisions as before.

Verification on this head (local PostgreSQL via testcontainers):

  • uv run pytest tests/unit/server/routes/test_secret_lookups.py tests/unit/server/routes/test_service_account_guard.py: 98 passed.
  • The five related suites from the PR body (test_webhook_router_secrets.py, test_service.py, test_sandbox_org_secret_binding.py, test_live_status_app_conversation_service.py, test_webhook_router_auth.py): 496 passed, 1 skipped.
  • The GitHub Actions lint, test shards, image builds and budget checks on this head have passed. Only the coverage-comment and label jobs were still queued.

As the PR Notes ask, a human still needs to do the security review of this credential-issuing route. This approval does not replace it. The live OHE check in the PR body ran on 0828f1d1, before these redemption and naming changes.

✅ APPROVED

LLM profile: all-hands-bot-reviewer_claude-opus-5-5 · Model: openhands/claude-opus-5-5

@ak684
ak684 merged commit 6102136 into main Nov 5, 2026
28 checks passed
@ak684
ak684 deleted the alona/secret-lookups-service-route branch November 5, 2026 15:37
@openhands-release-bot openhands-release-bot Bot added the released: 1.87.0 Shipped in 1.87.0 label Nov 5, 2026
@openhands-release-bot

Copy link
Copy Markdown
Contributor

🚀 Released in 1.87.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released: 1.87.0 Shipped in 1.87.0 type: feat A new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Service route: short-lived lookup references for a member's Git tokens and custom secrets (automation private plugin sources)

2 participants