Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
test(control-ui): add mocked-API Playwright coverage for Secret reuse
Mc9f21762376e
diff --git a/control-ui/e2e/connector-secret-reuse.spec.ts b/control-ui/e2e/connector-secret-reuse.spec.tsnew file mode 100644index 000000000..6a0e3ba8a--- /dev/null+++ b/control-ui/e2e/connector-secret-reuse.spec.ts@@ -0,0 +1,544 @@+import { type Page, type Route, expect, test } from '@playwright/test'+import fs from 'node:fs'+import path from 'node:path'++// ─── Connector Secret reuse — mocked-API frontend E2E ───────────────────────+//+// These specs drive the REAL Control UI (built app served by `next start`,+// real routes /connectors/new and /connectors/:name/edit/credentials, real+// Chromium) while the Control API is replaced at the browser network layer+// with `page.route`. They prove what a live browser actually sends and+// renders for the Secret-reuse flows; they are NOT live-backend E2E and NOT+// T2 certification — no cluster, no real credentials, no shared state.+//+// Secret values are never part of these flows: the mocks only ever carry+// names, key names, and UIDs, mirroring the production listing contract.++const EVIDENCE_DIR = path.resolve(+ __dirname,+ '../../.local-notes/infra/runs/connector-secrets-ui/playwright'+)++/** Provisioned connector Secrets as GET /admin/mcp-secrets returns them:+ * names, key names, and UIDs only — never values. */+const LEGACY_SECRET = {+ name: 'linear-legacy',+ uid: 'uid-legacy',+ keys: ['api-key', 'workspace-id'],+}+const SHARED_SECRET = {+ name: 'team-shared-credentials',+ uid: 'uid-shared',+ keys: ['api-key', 'workspace-id'],+}+const PARTIAL_SECRET = { name: 'partial-shared', uid: 'uid-partial', keys: ['api-key'] }++const LINEAR_REFERENCE = {+ name: LEGACY_SECRET.name,+ keys: [+ { secretKey: 'api-key', envVar: 'LINEAR_API_KEY' },+ { secretKey: 'workspace-id', envVar: 'LINEAR_WORKSPACE' },+ ],+}++/** An McpServerResource as the page and the change flow read it. */+function connectorResource(+ name: string,+ envSecret: { name: string; keys: Array<{ secretKey: string; envVar: string }> } | undefined,+ overrides: {+ uid?: string+ resourceVersion?: string+ generation?: number+ image?: string+ } = {}+) {+ return {+ metadata: {+ name,+ namespace: 'mcp-server',+ uid: overrides.uid ?? `${name}-uid`,+ resourceVersion: overrides.resourceVersion ?? '10',+ generation: overrides.generation ?? 2,+ },+ spec: {+ image: overrides.image ?? 'ghcr.io/example/linear-mcp:1.4.0',+ contextRef: `ctx-${name}`,+ managed: true,+ enabled: true,+ ...(envSecret ? { envSecret } : {}),+ },+ status: { conditions: [] },+ }+}++/** The PUT answer for an accepted assignment: new reference, bumped+ * generation, and a fresh DeploymentReady the poll can correlate. */+function reassignedConnector(name: string, atIso: string) {+ const saved = connectorResource(name, LINEAR_REFERENCE, {+ resourceVersion: '11',+ generation: 3,+ })+ return {+ ...saved,+ spec: {+ ...saved.spec,+ envSecret: { name: SHARED_SECRET.name, keys: LINEAR_REFERENCE.keys },+ },+ status: {+ conditions: [+ {+ type: 'DeploymentReady',+ status: 'True',+ reason: 'RolloutComplete',+ message: 'ready',+ lastTransitionTime: atIso,+ observedGeneration: 3,+ },+ ],+ },+ }+}++type RecordedRequest = { method: string; pathname: string; body?: unknown }++type Mocks = {+ /** Every admin API request the browser actually made. */+ requests: RecordedRequest[]+ /** Non-GET requests against /admin/mcp-secrets — must stay empty in+ * every reference-only journey. */+ secretWrites: RecordedRequest[]+ /** GET /admin/mcp-secrets fails this many times before succeeding. */+ listSecretsFailures: number+ /** Supplies GET /admin/mcp-servers/:name responses in order; the last+ * supplier repeats once the queue is exhausted. */+ serverReads: Array<() => unknown>+}++function mocks(initialServer: () => unknown): Mocks {+ return {+ requests: [],+ secretWrites: [],+ listSecretsFailures: 0,+ serverReads: [initialServer],+ }+}++/** Installs the browser-level Control API replacement. Only metadata ever+ * crosses: names, key names, UIDs, and spec references — no values. */+function installApiMocks(page: Page, state: Mocks, serverName: string) {+ async function handle(route: Route) {+ const request = route.request()+ const url = new URL(request.url())+ const pathname = url.pathname+ const method = request.method()+ let body: unknown+ if (method !== 'GET' && request.postData()) {+ try {+ body = request.postDataJSON()+ } catch {+ body = request.postData()+ }+ }+ state.requests.push({ method, pathname, body })++ const marker = '/api/v1/admin/'+ const index = pathname.indexOf(marker)+ const adminPath = index === -1 ? '' : pathname.slice(index + marker.length)++ const json = (status: number, payload: unknown) =>+ route.fulfill({ status, contentType: 'application/json', body: JSON.stringify(payload) })++ if (adminPath === 'auth/me') {+ await json(200, { me: { id: 'e2e-admin', username: 'e2e-admin', role: 'admin' } })+ return+ }+ if (adminPath === 'hosts') {+ await json(200, { items: [] })+ return+ }+ if (adminPath === 'contexts' && method === 'GET') {+ await json(200, { items: [] })+ return+ }+ if (adminPath === 'contexts' && method === 'POST') {+ await json(201, { metadata: { name: body?.metadata?.name ?? 'ctx-fallback' } })+ return+ }+ if (adminPath === 'registry/images') {+ await json(200, { org: 'evenfire-dev', images: [] })+ return+ }+ if (adminPath === 'mcp-secrets') {+ if (method === 'GET') {+ if (state.listSecretsFailures > 0) {+ state.listSecretsFailures -= 1+ await json(500, { message: 'simulated listing outage' })+ return+ }+ await json(200, { items: [LEGACY_SECRET, SHARED_SECRET, PARTIAL_SECRET] })+ return+ }+ // Record any Secret mutation so reference-only journeys can prove none+ // happened; answer 201 so a bug does not cascade before the assertion.+ state.secretWrites.push({ method, pathname, body })+ await json(201, { name: body?.name ?? 'unexpected', namespace: 'mcp-server' })+ return+ }+ if (adminPath === 'mcp-servers' && method === 'POST') {+ await json(201, { metadata: { name: body?.metadata?.name ?? 'connector' }, spec: body?.spec })+ return+ }+ if (adminPath === `mcp-servers/${serverName}`) {+ if (method === 'GET') {+ const supplier =+ state.serverReads.length > 1 ? state.serverReads.shift()! : state.serverReads[0]+ await json(200, supplier())+ return+ }+ if (method === 'PUT') {+ await json(200, reassignedConnector(serverName, new Date().toISOString()))+ return+ }+ }+ await json(404, {})+ }++ void page.route('**/api/v1/admin/**', route => void handle(route))+}++/** Authenticates AuthGate through the same localStorage contract the other+ * mocked specs use, before any page script runs. */+function authenticate(page: Page) {+ void page.addInitScript(() => {+ window.localStorage.setItem('controlUiAdminToken', 'e2e-mock-only-token')+ })+}++function evidencePath(fileName: string): string {+ fs.mkdirSync(EVIDENCE_DIR, { recursive: true })+ return path.join(EVIDENCE_DIR, fileName)+}++async function requestsFor(state: Mocks, method: string, suffix: string) {+ await expect+ .poll(+ () => state.requests.filter(r => r.method === method && r.pathname.endsWith(suffix)).length,+ { message: `expected a ${method} request ending in ${suffix}` }+ )+ .toBeGreaterThan(0)+ return state.requests.filter(r => r.method === method && r.pathname.endsWith(suffix))+}++/** The app's own alert region, excluding Next.js's route announcer, which+ * also exposes role="alert". */+function appAlert(page: Page) {+ return page.locator('[role="alert"]:not([id="__next-route-announcer__"])')+}++/** Drives the three-step create flow to a connector referencing the shared+ * Secret, with no credential value ever typed. */+async function createConnectorUsingSharedSecret(page: Page, name: string) {+ await page.goto('/connectors/new')+ await page.getByPlaceholder('my-mcp-server').fill(name)+ await page+ .getByPlaceholder('us-central1-docker.pkg.dev/my-project/repo/mcp-server:latest')+ .fill('ghcr.io/example/brave-mcp:1.0')+ await page.getByRole('button', { name: 'Continue' }).click()+ await page.getByRole('button', { name: 'Continue' }).click()++ await page.getByRole('radio', { name: 'Use an existing Secret' }).click()+ const selector = page.getByLabel(/^Existing Secret/)+ await expect(selector).toBeEnabled()+ // Write-only contract at the browser level: no value input ever renders.+ await expect(page.locator('input[type="password"]')).toHaveCount(0)++ await selector.selectOption(SHARED_SECRET.name)+ await expect(page.getByLabel(/^Environment variable 1/)).toHaveValue('API_KEY')++ await page.getByRole('button', { name: 'Create connector' }).click()+ await expect(page.getByText('Connector created successfully.')).toBeVisible({ timeout: 15_000 })+}++test.describe('Connector Secret reuse — creation', () => {+ test('creates a connector from an existing Secret without value re-entry or Secret writes', async ({+ page,+ }) => {+ authenticate(page)+ const state = mocks(() => connectorResource('brave-search', undefined))+ installApiMocks(page, state, 'brave-search')++ await createConnectorUsingSharedSecret(page, 'brave-search')+ await page.screenshot({+ path: evidencePath('pw-01-create-existing-secret.png'),+ fullPage: true,+ })++ const creations = await requestsFor(state, 'POST', 'api/v1/admin/mcp-servers')+ expect(creations).toHaveLength(1)+ // Every stored key is mapped to a suggested destination; no values exist+ // anywhere in the request.+ expect(creations[0]?.body).toMatchObject({+ metadata: { name: 'brave-search' },+ spec: {+ envSecret: {+ name: SHARED_SECRET.name,+ keys: [+ { secretKey: 'api-key', envVar: 'API_KEY' },+ { secretKey: 'workspace-id', envVar: 'WORKSPACE_ID' },+ ],+ },+ },+ })+ // Reference-only: the shared Secret was listed, never written or deleted.+ expect(state.secretWrites).toEqual([])+ })++ test('reuses one Secret across multiple connectors without Secret POST/PUT/DELETE', async ({+ page,+ }) => {+ authenticate(page)+ const state = mocks(() => connectorResource('brave-search', undefined))+ installApiMocks(page, state, 'brave-search')++ await createConnectorUsingSharedSecret(page, 'brave-search')+ await createConnectorUsingSharedSecret(page, 'jira-bridge')++ const creations = await requestsFor(state, 'POST', 'api/v1/admin/mcp-servers')+ expect(creations).toHaveLength(2)+ for (const creation of creations) {+ expect(creation.body).toMatchObject({+ spec: { envSecret: { name: SHARED_SECRET.name } },+ })+ }+ const listed = state.requests.filter(+ r => r.method === 'GET' && r.pathname.endsWith('api/v1/admin/mcp-secrets')+ )+ expect(listed.length).toBeGreaterThanOrEqual(2)+ expect(state.secretWrites).toEqual([])+ })++ test('surfaces listing failures with retry and an empty catalog without value prompts', async ({+ page,+ }) => {+ authenticate(page)+ const state = mocks(() => connectorResource('brave-search', undefined))+ state.listSecretsFailures = 1+ installApiMocks(page, state, 'brave-search')++ await page.goto('/connectors/new')+ await page.getByPlaceholder('my-mcp-server').fill('brave-search')+ await page+ .getByPlaceholder('us-central1-docker.pkg.dev/my-project/repo/mcp-server:latest')+ .fill('ghcr.io/example/brave-mcp:1.0')+ await page.getByRole('button', { name: 'Continue' }).click()+ await page.getByRole('button', { name: 'Continue' }).click()+ await page.getByRole('radio', { name: 'Use an existing Secret' }).click()++ const alert = appAlert(page)+ await expect(alert).toContainText('Could not load Secrets')+ await expect(page.getByRole('button', { name: 'Create connector' })).toBeDisabled()+ await expect(page.locator('input[type="password"]')).toHaveCount(0)++ await page.getByRole('button', { name: 'Retry loading Secrets' }).click()+ await expect(page.getByLabel(/^Existing Secret/)).toBeEnabled()+ await page.getByLabel(/^Existing Secret/).selectOption(SHARED_SECRET.name)+ await expect(page.getByLabel(/^Environment variable 1/)).toHaveValue('API_KEY')+ })+})++test.describe('Connector Secret reuse — change assigned Secret', () => {+ function prepare(page: Page, serverReads: Array<() => unknown>) {+ authenticate(page)+ const state = mocks(serverReads[0]!)+ state.serverReads = serverReads+ installApiMocks(page, state, 'linear')+ return state+ }++ async function openChangeFlow(page: Page) {+ await page.goto('/connectors/linear/edit/credentials')+ await expect(page.getByRole('button', { name: 'Rotate credentials' })).toBeVisible()+ await expect(page.getByText(`Current Secret: ${LEGACY_SECRET.name}`)).toBeVisible()+ await page.getByRole('button', { name: 'Change assigned Secret' }).click()+ await expect(page.getByLabel(/^Existing Secret/)).toBeEnabled()+ await expect(page.locator('input[type="password"]')).toHaveCount(0)+ }++ test('saves only envSecret with CAS identity, preserves destinations, and observes readiness', async ({+ page,+ }) => {+ // Reads: page mount, the change flow's two verification reads, then the+ // post-save poll sees the freshly Ready reassigned connector.+ const current = () => connectorResource('linear', LINEAR_REFERENCE)+ const state = prepare(page, [+ current,+ current,+ current,+ () => reassignedConnector('linear', new Date().toISOString()),+ ])++ await openChangeFlow(page)+ await page.getByLabel(/^Existing Secret/).selectOption(SHARED_SECRET.name)+ // Destinations are the connector's contract and survive the reassignment.+ await expect(page.getByLabel(/^Environment variable 1/)).toHaveValue('LINEAR_API_KEY')+ await expect(page.getByLabel(/^Environment variable 2/)).toHaveValue('LINEAR_WORKSPACE')++ await page.getByRole('button', { name: 'Assign and restart' }).click()+ const dialog = page.getByRole('alertdialog')+ await expect(dialog).toBeVisible()+ await expect(dialog).toContainText(LEGACY_SECRET.name)+ await expect(dialog).toContainText(SHARED_SECRET.name)+ await page.screenshot({ path: evidencePath('pw-02-change-confirm.png') })+ await dialog.getByRole('button', { name: 'Assign and restart' }).click()++ const saves = await requestsFor(state, 'PUT', 'api/v1/admin/mcp-servers/linear')+ expect(saves).toHaveLength(1)+ expect(saves[0]?.body).toMatchObject({+ metadata: { uid: 'linear-uid', resourceVersion: '10' },+ spec: {+ image: 'ghcr.io/example/linear-mcp:1.4.0',+ envSecret: {+ name: SHARED_SECRET.name,+ keys: [+ { secretKey: 'api-key', envVar: 'LINEAR_API_KEY' },+ { secretKey: 'workspace-id', envVar: 'LINEAR_WORKSPACE' },+ ],+ },+ },+ })+ expect(state.secretWrites).toEqual([])++ // Correlated readiness: a fresh True resolves the wait without any+ // operator action, then the page reloads the new assignment.+ await expect(page.getByText(/Assigned Secret changed/)).toBeVisible({ timeout: 20_000 })+ await expect(page.getByText(`Current Secret: ${SHARED_SECRET.name}`)).toBeVisible({+ timeout: 20_000,+ })+ await expect(page.getByRole('button', { name: 'Change assigned Secret' })).toBeFocused()+ await page.screenshot({+ path: evidencePath('pw-03-change-assigned-ready.png'),+ fullPage: true,+ })+ })++ test('keeps saved distinct from ready, locks navigation, and leaves cleanly', async ({+ page,+ }) => {+ // Reads 1-3 return the pre-change snapshot. From the first post-save poll+ // onward the connector reports the new reference WITHOUT a fresh Ready+ // condition: saved but never observed ready inside this journey.+ const stale = () => connectorResource('linear', LINEAR_REFERENCE)+ const savedNotReady = () => {+ const saved = reassignedConnector('linear', new Date().toISOString())+ return { ...saved, status: { conditions: [] } }+ }+ const state = prepare(page, [stale, stale, stale, savedNotReady])++ await openChangeFlow(page)+ await page.getByLabel(/^Existing Secret/).selectOption(SHARED_SECRET.name)+ await page.getByRole('button', { name: 'Assign and restart' }).click()+ await page.getByRole('alertdialog').getByRole('button', { name: 'Assign and restart' }).click()++ await requestsFor(state, 'PUT', 'api/v1/admin/mcp-servers/linear')+ await expect(page.getByText(/Assignment saved/)).toBeVisible()+ // Saved is not ready: tabs stay locked while the rollout is observed.+ for (const tab of await page.getByRole('tab').all()) {+ await expect(tab).toBeDisabled()+ }+ await page.screenshot({ path: evidencePath('pw-04-saved-not-ready.png'), fullPage: true })++ await page.getByRole('button', { name: 'Check credentials later' }).click()+ await expect(page.getByText(`Current Secret: ${SHARED_SECRET.name}`)).toBeVisible()+ await expect(page.getByRole('button', { name: 'Change assigned Secret' })).toBeFocused()+ })++ test('refuses a concurrent connector change before writing and locks until refresh', async ({+ page,+ }) => {+ const current = () => connectorResource('linear', LINEAR_REFERENCE)+ const concurrentlyEdited = () =>+ connectorResource('linear', LINEAR_REFERENCE, { image: 'ghcr.io/example/linear-mcp:1.5.0' })+ const state = prepare(page, [current, current, concurrentlyEdited])++ await openChangeFlow(page)+ await page.getByLabel(/^Existing Secret/).selectOption(SHARED_SECRET.name)+ await page.getByRole('button', { name: 'Assign and restart' }).click()+ await page.getByRole('alertdialog').getByRole('button', { name: 'Assign and restart' }).click()++ await expect(appAlert(page)).toContainText('connector changed')+ expect(state.requests.some(r => r.method === 'PUT')).toBe(false)+ expect(state.secretWrites).toEqual([])+ await page.getByRole('button', { name: 'Refresh credentials' }).click()+ await expect(page.getByText(`Current Secret: ${LEGACY_SECRET.name}`)).toBeVisible()+ })++ test('supports no-op refusal, explicit replacement keys, and cancel with focus return', async ({+ page,+ }) => {+ const current = () => connectorResource('linear', LINEAR_REFERENCE)+ const state = prepare(page, [current])++ await openChangeFlow(page)++ // No-op: re-choosing the currently assigned Secret disables assignment.+ await page.getByLabel(/^Existing Secret/).selectOption(LEGACY_SECRET.name)+ await expect(page.getByRole('button', { name: 'Assign and restart' })).toBeDisabled()++ // A Secret missing one stored key demands an explicit replacement key and+ // never a credential value.+ await page.getByLabel(/^Existing Secret/).selectOption(PARTIAL_SECRET.name)+ await expect(page.getByLabel(/^Secret key 2/)).toHaveValue('')+ await expect(page.getByLabel(/^Environment variable 2/)).toHaveValue('LINEAR_WORKSPACE')+ await expect(appAlert(page)).toContainText('Choose a stored key')+ await expect(page.getByRole('button', { name: 'Assign and restart' })).toBeDisabled()+ await expect(page.locator('input[type="password"]')).toHaveCount(0)+ await page.screenshot({ path: evidencePath('pw-05-missing-key-choice.png'), fullPage: true })++ // Cancel returns to the credentials view with focus on the trigger and+ // nothing written.+ await page.getByRole('button', { name: 'Cancel assignment change' }).click()+ await expect(page.getByText(`Current Secret: ${LEGACY_SECRET.name}`)).toBeVisible()+ await expect(page.getByRole('button', { name: 'Change assigned Secret' })).toBeFocused()+ expect(state.requests.some(r => r.method === 'PUT')).toBe(false)+ expect(state.secretWrites).toEqual([])+ })++ test('recovers from a Secret listing outage inside the change flow', async ({ page }) => {+ const current = () => connectorResource('linear', LINEAR_REFERENCE)+ const state = prepare(page, [current])+ state.listSecretsFailures = 1++ await page.goto('/connectors/linear/edit/credentials')+ await page.getByRole('button', { name: 'Change assigned Secret' }).click()+ await expect(appAlert(page)).toContainText('Could not load Secrets')+ await expect(page.getByRole('button', { name: 'Assign and restart' })).toBeDisabled()++ await page.getByRole('button', { name: 'Retry loading Secrets' }).click()+ await expect(page.getByLabel(/^Existing Secret/)).toBeEnabled()+ await page.getByLabel(/^Existing Secret/).selectOption(SHARED_SECRET.name)+ await expect(page.getByRole('button', { name: 'Assign and restart' })).toBeEnabled()+ })+})++test.describe('Connector Secret reuse — narrow viewport', () => {+ test.use({ viewport: { width: 390, height: 844 } })++ test('reuse and reassignment stay usable at 390px without horizontal overflow', async ({+ page,+ }) => {+ authenticate(page)+ const current = () => connectorResource('linear', LINEAR_REFERENCE)+ const state = mocks(current)+ installApiMocks(page, state, 'linear')++ await page.goto('/connectors/linear/edit/credentials')+ await page.getByRole('button', { name: 'Change assigned Secret' }).click()+ await expect(page.getByLabel(/^Existing Secret/)).toBeEnabled()+ await page.getByLabel(/^Existing Secret/).selectOption(SHARED_SECRET.name)+ await expect(page.getByLabel(/^Environment variable 1/)).toHaveValue('LINEAR_API_KEY')+ expect(await page.evaluate(() => document.documentElement.scrollWidth)).toBeLessThanOrEqual(+ await page.evaluate(() => window.innerWidth)+ )+ await page.screenshot({ path: evidencePath('pw-06-narrow-change-flow.png'), fullPage: true })+ })+})diff --git a/control-ui/package-lock.json b/control-ui/package-lock.jsonindex 6f440d7b7..b9d124208 100644--- a/control-ui/package-lock.json+++ b/control-ui/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-control-ui",- "version": "0.1.753",+ "version": "0.1.754","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-control-ui",- "version": "0.1.753",+ "version": "0.1.754","dependencies": {"@clerum/display-field": "file:../packages/display-field","@clerum/frontend-components": "file:../packages/frontend-components",diff --git a/control-ui/package.json b/control-ui/package.jsonindex 06c900a86..3617a97a6 100644--- a/control-ui/package.json+++ b/control-ui/package.json@@ -1,6 +1,6 @@{"name": "clerum-control-ui",- "version": "0.1.753",+ "version": "0.1.754","private": true,"scripts": {"dev": "NEXT_IGNORE_INCORRECT_LOCKFILE=1 next dev",