diff --git a/frontend/src/components/secrets/SecretsManager.test.tsx b/frontend/src/components/secrets/SecretsManager.test.tsx index 2f3f237a..b8754f4c 100644 --- a/frontend/src/components/secrets/SecretsManager.test.tsx +++ b/frontend/src/components/secrets/SecretsManager.test.tsx @@ -80,7 +80,7 @@ describe('SecretsManager', () => { expect(await screen.findByText('Secret saved')).toBeInTheDocument() const put = result.fetchFake.lastRequest('PUT', '/api/repos/7/secrets/:name') expect(put?.path).toBe('/api/repos/7/secrets/MY_KEY') - expect(put?.body).toEqual({ value: 's3cret' }) + expect(put?.body).toEqual({ value: 's3cret', agentHidden: false }) }) it('saves a multi-line secret value through the textarea', async () => { @@ -101,7 +101,10 @@ describe('SecretsManager', () => { await user.click(screen.getByRole('button', { name: 'Save' })) await waitFor(() => - expect(result.fetchFake.lastRequest('PUT', '/api/repos/7/secrets/:name')?.body).toEqual({ value: pem }), + expect(result.fetchFake.lastRequest('PUT', '/api/repos/7/secrets/:name')?.body).toEqual({ + value: pem, + agentHidden: false, + }), ) }) @@ -119,6 +122,7 @@ describe('SecretsManager', () => { expect(result.fetchFake.lastRequest('PUT', '/api/repos/7/secrets/:name')?.body).toEqual({ value: 'value', environment: 'e2e', + agentHidden: false, }), ) }) @@ -135,7 +139,10 @@ describe('SecretsManager', () => { await user.click(screen.getByRole('button', { name: 'Save' })) await waitFor(() => - expect(result.fetchFake.lastRequest('PUT', '/api/secrets/:name')?.body).toEqual({ value: 'value' }), + expect(result.fetchFake.lastRequest('PUT', '/api/secrets/:name')?.body).toEqual({ + value: 'value', + agentHidden: false, + }), ) }) @@ -155,6 +162,7 @@ describe('SecretsManager', () => { expect(result.fetchFake.lastRequest('PUT', '/api/repos/7/secrets/:name')?.body).toEqual({ value: 'new-value', environment: 'e2e', + agentHidden: false, }), ) }) @@ -224,4 +232,100 @@ describe('SecretsManager', () => { expect(await screen.findByRole('alert')).toHaveTextContent('Name is reserved for Manager or the runtime') }) + + it('saves a hidden secret with its allowed hosts', async () => { + const user = userEvent.setup() + const result = renderManager() + + await user.click(await screen.findByRole('button', { name: /Add secret/i })) + await user.type(screen.getByLabelText('Secret name'), 'STRIPE_KEY') + await user.type(screen.getByLabelText('Secret value'), 'sk-live') + await user.click(screen.getByRole('checkbox', { name: 'Hide from agents' })) + await user.type(screen.getByLabelText('Allowed hosts'), 'api.example.com, *.example.com') + await user.click(screen.getByRole('button', { name: 'Save' })) + + await waitFor(() => + expect(result.fetchFake.lastRequest('PUT', '/api/repos/7/secrets/:name')?.body).toEqual({ + value: 'sk-live', + agentHidden: true, + allowedHosts: ['api.example.com', '*.example.com'], + }), + ) + }) + + it('rejects a hidden secret without an allowed host', async () => { + const user = userEvent.setup() + const result = renderManager() + + await user.click(await screen.findByRole('button', { name: /Add secret/i })) + await user.type(screen.getByLabelText('Secret name'), 'STRIPE_KEY') + await user.type(screen.getByLabelText('Secret value'), 'sk-live') + await user.click(screen.getByRole('checkbox', { name: 'Hide from agents' })) + await user.click(screen.getByRole('button', { name: 'Save' })) + + expect(await screen.findByRole('alert')).toHaveTextContent(/at least one allowed host/i) + expect(result.fetchFake.findRequests('PUT', '/api/repos/7/secrets/:name')).toHaveLength(0) + }) + + it('preloads and clears the hidden settings when editing', async () => { + const user = userEvent.setup() + const result = renderManager(repoScope, { + secrets: [ + { + name: 'STRIPE_KEY', + environment: 'e2e', + updatedAt: Date.now(), + agentHidden: true, + allowedHosts: ['api.example.com', '*.example.com'], + }, + ], + }) + + await user.click(await screen.findByRole('button', { name: 'Update STRIPE_KEY' })) + expect(screen.getByRole('checkbox', { name: 'Hide from agents' })).toBeChecked() + expect(screen.getByLabelText('Allowed hosts')).toHaveValue('api.example.com, *.example.com') + + await user.click(screen.getByRole('checkbox', { name: 'Hide from agents' })) + expect(screen.queryByLabelText('Allowed hosts')).not.toBeInTheDocument() + + await user.type(screen.getByLabelText('Secret value'), 'rotated') + await user.click(screen.getByRole('button', { name: 'Save' })) + + await waitFor(() => + expect(result.fetchFake.lastRequest('PUT', '/api/repos/7/secrets/:name')?.body).toEqual({ + value: 'rotated', + environment: 'e2e', + agentHidden: false, + }), + ) + }) + + it('shows hidden secrets with their allowed hosts', async () => { + renderManager(repoScope, { + secrets: [ + { + name: 'STRIPE_KEY', + environment: 'e2e', + updatedAt: Date.now(), + agentHidden: true, + allowedHosts: ['api.stripe.com'], + }, + ], + }) + + expect(await screen.findByText('STRIPE_KEY')).toBeInTheDocument() + expect(screen.getByText('Hidden from agents')).toBeInTheDocument() + expect(screen.getByText(/api\.stripe\.com/)).toBeInTheDocument() + }) + + it('hides the agent controls for variables', async () => { + const user = userEvent.setup() + renderManager() + + await user.click(await screen.findByRole('tab', { name: 'Variables' })) + await user.click(await screen.findByRole('button', { name: /Add variable/i })) + + expect(screen.queryByText('Hide from agents')).not.toBeInTheDocument() + expect(screen.queryByLabelText('Allowed hosts')).not.toBeInTheDocument() + }) }) diff --git a/frontend/src/components/secrets/SecretsManager.tsx b/frontend/src/components/secrets/SecretsManager.tsx index 06a48c30..2c257a40 100644 --- a/frontend/src/components/secrets/SecretsManager.tsx +++ b/frontend/src/components/secrets/SecretsManager.tsx @@ -6,6 +6,7 @@ import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { Textarea } from '@/components/ui/textarea' import { Badge } from '@/components/ui/badge' +import { Checkbox } from '@/components/ui/checkbox' import { SettingsList, SettingsListRow } from '@/components/ui/settings-list' import { DeleteDialog } from '@/components/ui/delete-dialog' import { useDeleteSecret, useSecretEntries, useSetSecret } from '@/hooks/useSecrets' @@ -14,8 +15,10 @@ import { showToast } from '@/lib/toast' import { formatShortRelativeTime } from '@/lib/utils' import { EnvironmentNameSchema, + SecretAllowedHostSchema, SecretNameSchema, SecretValueSchema, + SetSecretRequestSchema, type SecretEntry, type SecretKind, } from '@opencode-manager/shared/schemas' @@ -85,6 +88,8 @@ interface SecretFormState { name: string value: string environment: string + agentHidden: boolean + allowedHosts: string } interface SecretKindPanelProps { @@ -98,6 +103,8 @@ function SecretKindPanel({ scope, kind }: SecretKindPanelProps) { const nameId = useId() const environmentId = useId() const valueId = useId() + const hiddenId = useId() + const hostsId = useId() const entriesQuery = useSecretEntries(scope, kind) const setSecret = useSetSecret(scope) @@ -113,13 +120,19 @@ function SecretKindPanel({ scope, kind }: SecretKindPanelProps) { const startAdd = () => { setIsEditing(false) setFormError(null) - setForm({ name: '', value: '', environment: '' }) + setForm({ name: '', value: '', environment: '', agentHidden: false, allowedHosts: '' }) } const startEdit = (entry: SecretEntry) => { setIsEditing(true) setFormError(null) - setForm({ name: entry.name, value: '', environment: entry.environment ?? '' }) + setForm({ + name: entry.name, + value: '', + environment: entry.environment ?? '', + agentHidden: entry.agentHidden ?? false, + allowedHosts: entry.allowedHosts?.join(', ') ?? '', + }) } const cancelForm = () => { @@ -155,9 +168,43 @@ function SecretKindPanel({ scope, kind }: SecretKindPanelProps) { environment = environmentResult.data } + const allowedHosts: string[] = [] + if (kind === 'secret' && form.agentHidden) { + const rawHosts = form.allowedHosts + .split(/[\n,]+/) + .map((host) => host.trim()) + .filter(Boolean) + for (const rawHost of rawHosts) { + const hostResult = SecretAllowedHostSchema.safeParse(rawHost) + if (!hostResult.success) { + setFormError(hostResult.error.issues[0]?.message ?? 'Invalid allowed host') + return + } + allowedHosts.push(hostResult.data) + } + } + + const requestResult = SetSecretRequestSchema.safeParse({ + value: valueResult.data, + environment, + agentHidden: kind === 'secret' ? form.agentHidden : undefined, + allowedHosts: kind === 'secret' && form.agentHidden ? allowedHosts : undefined, + }) + if (!requestResult.success) { + setFormError(requestResult.error.issues[0]?.message ?? `Invalid ${meta.label.toLowerCase()}`) + return + } + setFormError(null) try { - await setSecret.mutateAsync({ kind, name: nameResult.data, value: valueResult.data, environment }) + await setSecret.mutateAsync({ + kind, + name: nameResult.data, + value: requestResult.data.value, + environment: requestResult.data.environment, + agentHidden: kind === 'secret' ? requestResult.data.agentHidden : undefined, + allowedHosts: requestResult.data.allowedHosts, + }) showToast.success(`${meta.label} ${isEditing ? 'updated' : 'saved'}`) cancelForm() } catch (error) { @@ -229,6 +276,36 @@ function SecretKindPanel({ scope, kind }: SecretKindPanelProps) { /> + {kind === 'secret' && ( +
+ Agents see a placeholder. Only HTTPS request headers to the allowed hosts get the real value. Your + terminals get the real value. +
+ {form.agentHidden && ( +
{formError}
@@ -264,11 +341,23 @@ function SecretKindPanel({ scope, kind }: SecretKindPanelProps) {
key={`${entry.name}\u0000${entry.environment ?? ''}`}
className="flex-row items-center gap-2 py-2.5"
title={{entry.name}}
- badges={entry.environment ?