Repository navigation
feat(agent-vault): create sessions on behalf of users - #66
scott-ray-wilson wants to merge 1 commit into
Conversation
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
[Critical impact] The PR appears safe to merge under the author’s stated backend-deployment requirement.
|
There was a problem hiding this comment.
Beyond the inline finding, I checked that create() depends on post_once in infisical_sdk/infisical_requests.py to leave out None-valued keys. It does: it filters top-level keys with is not None, so a None ttl, label or onBehalfOf is left out of the request, and an empty-string email is still sent.
Extended reasoning...
The change adds on-behalf-of-user session creation to the Agent Vault sessions resource, an optional onBehalfOf field on the AgentVaultSession model, and unit tests. It touches authorization-adjacent behavior: which principal's values a session uses. The ruled-out item is the request layer's None filtering, which matches what the new tests and the PR description assume.
| request_body = { | ||
| "accessBundles": [access_bundle], | ||
| "ttl": ttl, | ||
| "label": label, | ||
| "onBehalfOf": on_behalf_of, | ||
| } | ||
|
|
||
| result = self.requests.post_once( |
There was a problem hiding this comment.
🔴 On a server without #8773, callers asking for a session on behalf of a user silently get a 7d session for the machine identity, using the identity's values and full bundle access. create() sends onBehalfOf and returns the server's session without checking it, so a dropped key raises nothing. The docstring calls this accepted, but self-hosted servers can't be upgraded in step with the SDK. Fix: when either on_behalf_of argument is set, call get(id) and check that onBehalfOf is present. If it isn't, revoke the session and raise. Also send ttl="1h" when ttl is None, so a session that slips through still expires as documented.
Why this was flagged
Trigger: a caller on the new SDK runs create(access_bundle=..., on_behalf_of_user_email=...) against an Infisical server older than #8773, for example a self-hosted install not yet upgraded. The PR says zod there strips the unknown onBehalfOf key. agent_vault.py:289-302 builds the body with ttl dropped (None, removed by post_once at infisical_requests.py:220), posts it and returns result.data.session. Nothing checks that the server honoured onBehalfOf. The server then creates an ordinary machine-identity session with its 7d default. The caller gets back a CreatedAgentVaultSession token with no error. So an agent meant to run with user-scoped, 1h access runs with identity-scoped access for a week. On the base branch create() had no on-behalf parameter, so nobody could believe they had a delegated session. The docstring warning at agent_vault.py:266-268 and the PR's 'release only after #8773 is deployed' plan do not protect self-hosted users, whose servers lag SDK releases.
Verification: With ttl defaulting to None, post_once removes the ttl key (infisical_requests.py:218-220). The older server then applies its 7d default. The code returns result.data.session without checking anything (agent_vault.py:289-302). The return type, CreatedAgentVaultSession (api_types.py:566-573), has no onBehalfOf field.
There was a problem hiding this comment.
This is fine for now. Agent Vault doesn't have adoption yet, so there's no one on an older server for this to affect, and this PR merges only after Infisical/infisical#8773 is deployed to Cloud. The create docstring covers older self-hosted servers in the meantime.
Context
Infisical/infisical#8773 adds sessions on behalf of users: a machine identity with a delegation names a user in
onBehalfOfwhen it creates a session, and the proxy then uses that user's personal values. This adds it to the SDK:client.agent_vault.sessions.createtakeson_behalf_of_user_emailoron_behalf_of_user_id, and sends it asonBehalfOf: {"userEmail": ...}or{"userId": ...}. Passing both raises aValueErrorbefore any request. An empty string is sent, not dropped, so the server rejects it instead of the SDK quietly creating the session for the machine identity.ttlnow defaults toNoneand is left out of the request, so the server picks the default: 7d, or 1h for a session on behalf of a user. Released servers also default a missingttlto 7d, so existing callers get the same sessions.AgentVaultSession.onBehalfOfis the user, as anAgentVaultSessionActor, since the API shapes it like a user actor. It'sNonefor the machine identity's own session, and itsidisNoneonce the user is deleted.get,list,updateandrevokeall return it. It's the last field, so positional construction of the model doesn't change.list'ssearchandupdate's label rule now match the API docs in #8773.For the reviewer
onBehalfOf, because zod strips unknown keys. It then creates the session for the machine identity, with a 7d TTL, and the proxy uses the identity's personal values. The create response doesn't includeonBehalfOf, so the SDK can't detect this. Thecreatedocstring says so. Merge and release this only after #8773 is deployed.onBehalfOfUserEmailoronBehalfOfUserId, and the delegation endpoints. The SDK has no access bundle or delegation methods yet, so each would be a new resource.The docs are in Infisical/infisical#9067, a draft until this PR and #8773 ship.
Steps to verify the change
python -m pytest tests.client.agent_vault.sessions.create(access_bundle="<bundle>", on_behalf_of_user_email="<user-email>").client.agent_vault.sessions.getwith the new session's ID.onBehalfOfis the user, andexpiresAtis about an hour away.Testing done
Unit: 8 new tests, 28 in total, pass on Python 3.8 through 3.12. With each rule broken on purpose (dropping an empty email, not parsing
onBehalfOf, a7ddefault, no check for both arguments), only the test written for it fails. The flake8 checks that CI enforces pass.Live, against #8773: 33 checks on an isolated stack at
e4bb7af1ce, and 35 on a local dev instance whose backend matched the head,b1320d014d, in a throwaway org with a seeded user. They covered:neverTTLsonBehalfOffromget,list,updateandrevoke, and search by the user's name and emailThe server logged no errors.
Type
Checklist
type(scope): short description(scope is optional, e.g.,fix: prevent crash on syncorfix(api): handle null response).🤖 Generated with Claude Code