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' && ( +
+
+ setForm({ ...form, agentHidden: checked === true })} + /> + +
+

+ Agents see a placeholder. Only HTTPS request headers to the allowed hosts get the real value. Your + terminals get the real value. +

+ {form.agentHidden && ( +
+ + setForm({ ...form, allowedHosts: event.target.value })} + placeholder="api.example.com, *.example.com" + className="font-mono" + autoComplete="off" + /> +
+ )} +
+ )} + {formError && (

{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 ? {entry.environment} : undefined} + badges={ + entry.environment || (kind === 'secret' && entry.agentHidden) ? ( + + {entry.environment && {entry.environment}} + {kind === 'secret' && entry.agentHidden && ( + Hidden from agents + )} + + ) : undefined + } description={ Updated {formatShortRelativeTime(new Date(entry.updatedAt))} {kind === 'variable' && entry.value !== undefined ? ` · ${entry.value}` : ''} + {kind === 'secret' && entry.agentHidden && entry.allowedHosts && entry.allowedHosts.length > 0 + ? ` · ${entry.allowedHosts.join(', ')}` + : ''} } trailing={ diff --git a/frontend/src/hooks/useSecrets.ts b/frontend/src/hooks/useSecrets.ts index 1a87abcf..f751fdc0 100644 --- a/frontend/src/hooks/useSecrets.ts +++ b/frontend/src/hooks/useSecrets.ts @@ -1,5 +1,5 @@ import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query' -import type { SecretEntry, SecretKind } from '@opencode-manager/shared/schemas' +import type { SecretEntry, SecretKind, SetSecretRequest } from '@opencode-manager/shared/schemas' import { deleteSecret, listSecrets, setSecret, type SecretScope } from '@/api/secrets' interface SetSecretVariables { @@ -7,6 +7,24 @@ interface SetSecretVariables { name: string value: string environment?: string + agentHidden?: boolean + allowedHosts?: string[] +} + +function buildSetSecretRequest({ + kind, + value, + environment, + agentHidden, + allowedHosts, +}: Omit): SetSecretRequest { + if (kind !== 'secret') { + return environment ? { value, environment } : { value } + } + const request: SetSecretRequest = { value, agentHidden: agentHidden ?? false } + if (environment) request.environment = environment + if (agentHidden && allowedHosts && allowedHosts.length > 0) request.allowedHosts = allowedHosts + return request } interface DeleteSecretVariables { @@ -34,8 +52,8 @@ export function useSetSecret(scope: SecretScope) { const queryClient = useQueryClient() return useMutation({ - mutationFn: ({ kind, name, value, environment }: SetSecretVariables) => - setSecret(scope, kind, name, environment ? { value, environment } : { value }), + mutationFn: ({ kind, name, ...variables }: SetSecretVariables) => + setSecret(scope, kind, name, buildSetSecretRequest({ kind, ...variables })), onSuccess: (_result, variables) => { queryClient.invalidateQueries({ queryKey: secretsQueryKey(scope, variables.kind) }) },