Repository navigation
feat(mothership): expose owner entitlements to the workbench as SIM_ENTITLEMENTS #10218
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
3 commits
Select commit
Hold shift + click to select a range
eeb971f
feat(mothership): expose owner entitlements to the workbench as SIM_E…
waleedlatif1 be2ac7f
chore(workspace-source): drop unused imports and use template literal…
waleedlatif1 268f70c
fix(mothership): read workbench entitlements through a sim:workspaces…
waleedlatif1 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
186 changes: 186 additions & 0 deletions
186
apps/sim/lib/mothership/tools/sandbox-session.integration.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,186 @@ | ||
| /** | ||
| * Real PostgreSQL coverage for the workbench's `SIM_ENTITLEMENTS`: a Mothership Function | ||
| * execution carries a Copilot delegation scoped to Function executions, and the availability use | ||
| * cases behind each entitlement authorize it for real. Only the workbench transport (CLI bundle, | ||
| * worker bootstrap, callback endpoint) and the AppConfig fetch are replaced. | ||
| */ | ||
|
|
||
| import { db } from '@sim/db' | ||
| import { member, organization, permissions, user, workspace } from '@sim/db/schema' | ||
| import { deleteWorkspaceFixture, insertWorkspaceFixture } from '@sim/db/testing/workspace-fixtures' | ||
| import { envFlagsMock, resetEnvFlagsMock, setEnvFlags } from '@sim/testing/mocks/env-flags.mock' | ||
| import { | ||
| mothershipAgentUrlMock, | ||
| mothershipAgentUrlMockFns, | ||
| } from '@sim/testing/mocks/mothership-agent-url.mock' | ||
| import { | ||
| mothershipGoFetchMock, | ||
| mothershipGoFetchMockFns, | ||
| } from '@sim/testing/mocks/mothership-go-fetch.mock' | ||
| import { generateId } from '@sim/utils/id' | ||
| import { inArray } from 'drizzle-orm' | ||
| import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| const { mockFetchProfile } = vi.hoisted(() => ({ mockFetchProfile: vi.fn() })) | ||
|
|
||
| vi.mock('@/lib/core/config/env-flags', () => envFlagsMock) | ||
| vi.mock('@/lib/core/config/appconfig', () => ({ fetchAppConfigProfile: mockFetchProfile })) | ||
| vi.mock('node:fs/promises', () => ({ readFile: async () => 'runtime' })) | ||
| vi.mock('@/lib/mothership/transport/connection', () => ({ | ||
| getSimConnection: () => ({ mode: 'direct' }), | ||
| })) | ||
| vi.mock('@/lib/mothership/request/go/fetch', () => mothershipGoFetchMock) | ||
| vi.mock('@/lib/mothership/request/headers', () => ({ mothershipRequestHeaders: () => ({}) })) | ||
| vi.mock('@/lib/mothership/server/agent-url', () => mothershipAgentUrlMock) | ||
| vi.mock('@/lib/mothership/tools/sandbox-resources', () => ({ | ||
| sandboxResourceEndpoint: async (url: string) => `${url}/api/mothership/sandbox/owned-token`, | ||
| activeSandboxChatOwner: () => undefined, | ||
| activeSandboxFileOwnerProtocol: () => undefined, | ||
| activeSandboxWorkspaceFileCopy: () => undefined, | ||
| })) | ||
|
|
||
| import { FUNCTION_EXECUTION_DELEGATION_AUDIENCE } from '@/lib/function-execution/application/authorization' | ||
| import { createTrustedOrganizationCopilotPrincipal } from '@/lib/mothership/auth/application-delegation' | ||
| import { buildMothershipSandboxSession } from '@/lib/mothership/tools/sandbox-session' | ||
|
|
||
| const tenant = { orgId: generateId(), ownerId: generateId(), workspaceId: generateId() } | ||
| const notRolledOut = { orgId: generateId(), ownerId: generateId(), workspaceId: generateId() } | ||
| const tenants = [tenant, notRolledOut] | ||
| const chatId = generateId() | ||
|
|
||
| /** The principal `lib/internal/function/execute.ts` builds for a workspace Copilot run. */ | ||
| function workspaceExecutionPrincipal(owner = tenant) { | ||
| const issuedAt = new Date() | ||
| return { | ||
| kind: 'delegated' as const, | ||
| serviceId: 'copilot' as const, | ||
| subjectUserId: owner.ownerId, | ||
| workspaceId: owner.workspaceId, | ||
| delegationId: `function-execute:${generateId()}`, | ||
| audience: FUNCTION_EXECUTION_DELEGATION_AUDIENCE, | ||
| issuedAt, | ||
| expiresAt: new Date(issuedAt.getTime() + 60_000), | ||
| resourceScope: { executionId: generateId() }, | ||
| } | ||
| } | ||
|
|
||
| describe('workbench entitlements for a Mothership Function execution', () => { | ||
| beforeAll(async () => { | ||
| setEnvFlags({ isAppConfigEnabled: true }) | ||
| mockFetchProfile.mockImplementation(async (_ids: unknown, parse: (doc: unknown) => unknown) => | ||
| parse({ 'workflow-code': { enabled: false, orgIds: [tenant.orgId] } }) | ||
| ) | ||
| mothershipAgentUrlMockFns.mockGetMothershipBaseURL.mockResolvedValue('https://worker.test') | ||
| mothershipGoFetchMockFns.mockFetchGo.mockImplementation(async () => | ||
| Response.json({ version: 1, entrypoint: 'entry' }) | ||
| ) | ||
| const now = new Date() | ||
| await db.insert(user).values( | ||
| tenants.map((owner) => ({ | ||
| id: owner.ownerId, | ||
| name: 'Sandbox entitlements fixture', | ||
| email: `${owner.ownerId}@sandbox-entitlements.invalid`, | ||
| emailVerified: true, | ||
| createdAt: now, | ||
| updatedAt: now, | ||
| })) | ||
| ) | ||
| await db.insert(organization).values( | ||
| tenants.map((owner) => ({ | ||
| id: owner.orgId, | ||
| name: 'Sandbox entitlements', | ||
| slug: owner.orgId, | ||
| createdAt: now, | ||
| })) | ||
| ) | ||
| await db.insert(member).values( | ||
| tenants.map((owner) => ({ | ||
| id: generateId(), | ||
| organizationId: owner.orgId, | ||
| userId: owner.ownerId, | ||
| role: 'owner', | ||
| createdAt: now, | ||
| })) | ||
| ) | ||
| await insertWorkspaceFixture( | ||
| db, | ||
| tenants.map((owner) => ({ | ||
| id: owner.workspaceId, | ||
| organizationId: owner.orgId, | ||
| workspaceMode: 'organization' as const, | ||
| name: 'Sandbox entitlements', | ||
| ownerId: owner.ownerId, | ||
| billedAccountUserId: owner.ownerId, | ||
| })) | ||
| ) | ||
| await db.insert(permissions).values( | ||
| tenants.map((owner) => ({ | ||
| id: generateId(), | ||
| userId: owner.ownerId, | ||
| entityType: 'workspace' as const, | ||
| entityId: owner.workspaceId, | ||
| permissionType: 'admin' as const, | ||
| })) | ||
| ) | ||
| }) | ||
|
|
||
| afterAll(async () => { | ||
| const workspaceIds = tenants.map((owner) => owner.workspaceId) | ||
| await db.delete(permissions).where(inArray(permissions.entityId, workspaceIds)) | ||
| await deleteWorkspaceFixture(db, inArray(workspace.id, workspaceIds)) | ||
| await db.delete(organization).where( | ||
| inArray( | ||
| organization.id, | ||
| tenants.map((owner) => owner.orgId) | ||
| ) | ||
| ) | ||
| await db.delete(user).where( | ||
| inArray( | ||
| user.id, | ||
| tenants.map((owner) => owner.ownerId) | ||
| ) | ||
| ) | ||
| resetEnvFlagsMock() | ||
| }) | ||
|
|
||
| it('grants a workspace chat workflow-code through its Function delegation', async () => { | ||
| const session = await buildMothershipSandboxSession({ | ||
| sessionKey: chatId, | ||
| workspaceId: tenant.workspaceId, | ||
| userId: tenant.ownerId, | ||
| principal: workspaceExecutionPrincipal(), | ||
| }) | ||
| expect(session.envs?.SIM_WORKSPACE).toBe(tenant.workspaceId) | ||
| expect((session.envs?.SIM_ENTITLEMENTS ?? '').split(',')).toContain('workflow-code') | ||
| }) | ||
|
|
||
| it('grants an organization chat workflow-code through its Function delegation', async () => { | ||
| const session = await buildMothershipSandboxSession({ | ||
| sessionKey: chatId, | ||
| organizationId: tenant.orgId, | ||
| userId: tenant.ownerId, | ||
| principal: createTrustedOrganizationCopilotPrincipal( | ||
| { | ||
| userId: tenant.ownerId, | ||
| organizationId: tenant.orgId, | ||
| chatId, | ||
| delegationId: `function-execute:${generateId()}`, | ||
| }, | ||
| { audience: FUNCTION_EXECUTION_DELEGATION_AUDIENCE, ttlMs: 60_000 } | ||
| ), | ||
| }) | ||
| expect(session.envs?.SIM_ORGANIZATION_ID).toBe(tenant.orgId) | ||
| expect((session.envs?.SIM_ENTITLEMENTS ?? '').split(',')).toContain('workflow-code') | ||
| }) | ||
|
|
||
| it('omits workflow-code where the workspace is not rolled out', async () => { | ||
| const session = await buildMothershipSandboxSession({ | ||
| sessionKey: chatId, | ||
| workspaceId: notRolledOut.workspaceId, | ||
| userId: notRolledOut.ownerId, | ||
| principal: workspaceExecutionPrincipal(notRolledOut), | ||
| }) | ||
| expect(session.envs?.SIM_WORKSPACE).toBe(notRolledOut.workspaceId) | ||
| expect(session.envs?.SIM_ENTITLEMENTS ?? '').not.toContain('workflow-code') | ||
| }) | ||
| }) |
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
Oops, something went wrong.
Oops, something went wrong.
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.