Repository navigation
[AuthZ: 2/N] Add external agent authorizer #585
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
74e8270
fix(trueforge): remove TrueFoundry global admin
chiragjn af421b6
feat(trueforge): add external agent authorizer
chiragjn 9c4f177
Address review comments
chiragjn d864fd9
Address review comments
chiragjn 93ceeb1
Fix api path
chiragjn 6277550
Fix permissions parsing
chiragjn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import type { AgentRecord } from '../db/agentStore'; | ||
| import type { RequestContext } from './identity'; | ||
|
|
||
| export type AgentAction = 'read' | 'manage'; | ||
|
|
||
| export type AgentListAccess = { kind: 'all' } | { kind: 'agent_external_ids'; agent_external_ids: readonly string[] }; | ||
|
|
||
| export interface Authorizer { | ||
| listAgentAccess(input: { context: RequestContext; action: AgentAction }): Promise<AgentListAccess>; | ||
| canAccessAgent(input: { context: RequestContext; action: AgentAction; agent: AgentRecord }): Promise<boolean>; | ||
| } | ||
|
|
||
| /** Standalone and OIDC: tenant-local agents. Read is unconstrained; manage is creator-only. */ | ||
| export class TrueForgeAuthorizer implements Authorizer { | ||
| listAgentAccess(): Promise<AgentListAccess> { | ||
| return Promise.resolve({ kind: 'all' }); | ||
| } | ||
|
|
||
| canAccessAgent(input: { context: RequestContext; action: AgentAction; agent: AgentRecord }): Promise<boolean> { | ||
| if (input.action === 'read') { | ||
| return Promise.resolve(true); | ||
| } | ||
| return Promise.resolve(input.agent.created_by_subject.subject_id === input.context.subject.id); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
packages/trueforge/src/truefoundry/TrueFoundryAuthorizer.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import { HTTPException } from 'hono/http-exception'; | ||
|
|
||
| import type { AgentAction, AgentListAccess, Authorizer } from '../auth/authorizer'; | ||
| import type { RequestContext } from '../auth/identity'; | ||
| import type { AgentRecord } from '../db/agentStore'; | ||
| import type { AgentPermission, TrueFoundryServiceFoundryServerClient } from './TrueFoundryServiceFoundryServerClient'; | ||
|
|
||
| function allowsAction(permissions: readonly AgentPermission[], action: AgentAction): boolean { | ||
| return action === 'manage' | ||
| ? permissions.includes('MANAGE_AGENT') | ||
| : permissions.includes('READ_AGENT') || permissions.includes('MANAGE_AGENT'); | ||
| } | ||
|
|
||
| function requireUserCredential(context: RequestContext): string { | ||
| if (context.user_credential === null) { | ||
| throw new HTTPException(401, { message: 'Authentication token required for agent authorization' }); | ||
| } | ||
| return context.user_credential; | ||
| } | ||
|
|
||
| export class TrueFoundryAuthorizer implements Authorizer { | ||
| readonly #client: TrueFoundryServiceFoundryServerClient; | ||
|
|
||
| constructor(client: TrueFoundryServiceFoundryServerClient) { | ||
| this.#client = client; | ||
| } | ||
|
|
||
| async listAgentAccess(input: { context: RequestContext; action: AgentAction }): Promise<AgentListAccess> { | ||
| const permissions = await this.#client.getAgentPermissions({ | ||
| accessToken: requireUserCredential(input.context), | ||
| }); | ||
| return { | ||
| kind: 'agent_external_ids', | ||
| agent_external_ids: Object.entries(permissions) | ||
| .filter(([, granted]) => allowsAction(granted, input.action)) | ||
| .map(([externalId]) => externalId), | ||
| }; | ||
| } | ||
|
|
||
| async canAccessAgent(input: { context: RequestContext; action: AgentAction; agent: AgentRecord }): Promise<boolean> { | ||
| if (input.agent.external_id === null) { | ||
| return false; | ||
| } | ||
| const permissions = await this.#client.getAgentPermissions({ | ||
| accessToken: requireUserCredential(input.context), | ||
| externalIds: [input.agent.external_id], | ||
| }); | ||
| return allowsAction(permissions[input.agent.external_id] ?? [], input.action); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.