Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
feat(control-api): add a names-only GET for channel credentials
Jose Ramirez93a7e74c6f8e
diff --git a/control-api/package-lock.json b/control-api/package-lock.jsonindex e580496e1..7a68d2d8d 100644--- a/control-api/package-lock.json+++ b/control-api/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-control-api",- "version": "0.1.439",+ "version": "0.1.440","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-control-api",- "version": "0.1.439",+ "version": "0.1.440","dependencies": {"@clerum/image-policy": "file:../packages/image-policy","@clerum/llm-providers": "file:../packages/llm-providers",diff --git a/control-api/package.json b/control-api/package.jsonindex cde104553..caddbdcb8 100644--- a/control-api/package.json+++ b/control-api/package.json@@ -1,6 +1,6 @@{"name": "clerum-control-api",- "version": "0.1.439",+ "version": "0.1.440","description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets","main": "dist/main.js","engines": {diff --git a/control-api/src/routes/admin/communicationChannelCredentials.ts b/control-api/src/routes/admin/communicationChannelCredentials.tsindex 401b22cab..306638f2d 100644--- a/control-api/src/routes/admin/communicationChannelCredentials.ts+++ b/control-api/src/routes/admin/communicationChannelCredentials.ts@@ -3,6 +3,7 @@ import { config } from '../../config.js'import { asyncHandler } from '../../http/asyncHandler.js'import { enforceNamespace } from '../../http/namespaceAudit.js'import type { K8sGateway } from '../../k8s.js'+import { secretKeyNames } from '../../services/secretKeyNames.js'function ccCredentialsSecretName(ccName: string): string {return `cc-${ccName}-credentials`@@ -48,6 +49,40 @@ export function registerCommunicationChannelCredentialsRoutes(router: Router,gateway: K8sGateway): void {+ /**+ * Which credential keys this channel actually holds — names only, never values.+ *+ * The credentials panel used to infer presence from `credentialsSecretRef`+ * alone, so a Telegram-only channel rendered a populated Slack Signing Secret+ * field. Per-key names are the smallest answer that stops that lie.+ *+ * An absent Secret is 200 with `keys: []`, not 404: "no credentials yet" is a+ * normal state the panel renders, and a 404 would make every caller handle+ * two shapes for one meaning. An unreadable Secret (RBAC, apiserver failure)+ * propagates instead, so a denial is never laundered into "not configured".+ */+ router.get(+ '/admin/communication-channels/:name/credentials',+ enforceNamespace(config.communicationChannelsNamespace),+ asyncHandler(async (req, res) => {+ const ns = config.communicationChannelsNamespace+ const name = req.params.name++ const cc = (await gateway.getResource('communicationchannels', name, ns)) as {+ spec?: { credentialsSecretRef?: { name?: string } }+ }+ // Same resolution as the PUT: an operator-supplied ref wins, otherwise the+ // conventional name the PUT would create. Reading the ref matters — a+ // channel pointing at a custom Secret would otherwise report the keys of a+ // `cc-<name>-credentials` that does not exist, reintroducing the lie.+ const secretName = cc.spec?.credentialsSecretRef?.name || ccCredentialsSecretName(name)++ const keys = await secretKeyNames(gateway, secretName, ns)++ res.status(200).json({ name, secretName, namespace: ns, keys })+ })+ )+router.put('/admin/communication-channels/:name/credentials',enforceNamespace(config.communicationChannelsNamespace),diff --git a/control-api/test/routes.communicationChannelCredentialsGet.test.ts b/control-api/test/routes.communicationChannelCredentialsGet.test.tsnew file mode 100644index 000000000..d15f18f08--- /dev/null+++ b/control-api/test/routes.communicationChannelCredentialsGet.test.ts@@ -0,0 +1,152 @@+import { beforeEach, describe, expect, it, vi } from 'vitest'+import express from 'express'+import { ApiException } from '@kubernetes/client-node'+import request from 'supertest'+import { createApp } from '../src/app.js'+import type { K8sGateway } from '../src/k8s.js'+import { createAdminResourcesRouter } from '../src/routes/admin/resources.js'+import { MockGateway } from './mockGateway.js'++// Same hand-crafted gateway double as routes.adminCommunicationChannelCredentials.test.ts:+// only the K8sGateway methods this cascade touches.+const gatewayMock = {+ createResource: vi.fn(),+ deleteResource: vi.fn(),+ getResource: vi.fn(),+ updateResource: vi.fn(),+ createSecret: vi.fn(),+ mergeSecret: vi.fn(),+ removeSecretKey: vi.fn(),+ deleteSecret: vi.fn(),+ getSecret: vi.fn(),+}++function makeApp(): express.Express {+ const app = express()+ app.use(express.json())+ app.use(createAdminResourcesRouter(gatewayMock as unknown as K8sGateway))+ return app+}++// The wire form of a Secret value: base64. Planting the raw string instead+// would let a leak that ships `.data` verbatim slip past the sentinel checks.+const SENTINEL_RAW = 'hunter2'+const SENTINEL_B64 = Buffer.from(SENTINEL_RAW).toString('base64') // 'aHVudGVyMg=='++describe('GET /admin/communication-channels/:name/credentials — names only', () => {+ beforeEach(() => {+ Object.values(gatewayMock).forEach(fn => fn.mockReset())+ })++ it('returns exactly { name, secretName, namespace, keys } and nothing else', async () => {+ gatewayMock.getResource.mockResolvedValue({+ metadata: { name: 'foo', namespace: 'channels' },+ spec: { hostRef: 'h1', credentialsSecretRef: { name: 'cc-foo-credentials' } },+ })+ gatewayMock.getSecret.mockResolvedValue({+ metadata: { name: 'cc-foo-credentials', namespace: 'channels' },+ data: {+ 'telegram-bot-token': Buffer.from('tg-token').toString('base64'),+ 'slack-signing-secret': SENTINEL_B64,+ },+ })++ const res = await request(makeApp()).get('/admin/communication-channels/foo/credentials')++ expect(res.status).toBe(200)+ // toEqual on the WHOLE body, never objectContaining: the panel only needs+ // presence, so any extra property is a leak surface and must fail here.+ expect(res.body).toEqual({+ name: 'foo',+ secretName: 'cc-foo-credentials',+ namespace: 'channels',+ keys: ['slack-signing-secret', 'telegram-bot-token'],+ })+ })++ it('returns 200 with keys: [] when the channel has no Secret yet', async () => {+ gatewayMock.getResource.mockResolvedValue({+ metadata: { name: 'foo', namespace: 'channels' },+ spec: { hostRef: 'h1' },+ })+ gatewayMock.getSecret.mockRejectedValue(+ new ApiException(404, 'Not Found', { message: 'secrets "cc-foo-credentials" not found' }, {})+ )++ const res = await request(makeApp()).get('/admin/communication-channels/foo/credentials')++ // "No credentials yet" is a normal state the panel renders, not an error.+ // A 404 here would force every caller to handle two shapes for one meaning.+ expect(res.status).toBe(200)+ expect(res.body).toEqual({+ name: 'foo',+ secretName: 'cc-foo-credentials',+ namespace: 'channels',+ keys: [],+ })+ })++ it('never leaks a Secret value, raw or base64', async () => {+ gatewayMock.getResource.mockResolvedValue({+ metadata: { name: 'foo', namespace: 'channels' },+ spec: { hostRef: 'h1', credentialsSecretRef: { name: 'cc-foo-credentials' } },+ })+ gatewayMock.getSecret.mockResolvedValue({+ metadata: { name: 'cc-foo-credentials', namespace: 'channels' },+ data: { 'slack-signing-secret': SENTINEL_B64 },+ })++ const res = await request(makeApp()).get('/admin/communication-channels/foo/credentials')++ expect(res.status).toBe(200)+ const serialized = JSON.stringify(res.body)+ // Checking only the raw string is how the existing PUT's `result: merged`+ // leak survived a green suite: `.data` ships every value base64-INTACT,+ // so a raw-only assertion stays green while the whole Secret goes out.+ expect(serialized).not.toContain(SENTINEL_RAW)+ expect(serialized).not.toContain(SENTINEL_B64)+ expect(res.text).not.toContain(SENTINEL_RAW)+ expect(res.text).not.toContain(SENTINEL_B64)+ })++ it('fails closed on an unreadable Secret without echoing key names or values', async () => {+ gatewayMock.getResource.mockResolvedValue({+ metadata: { name: 'foo', namespace: 'channels' },+ spec: { hostRef: 'h1', credentialsSecretRef: { name: 'cc-foo-credentials' } },+ })+ // A REAL ApiException, not a hand-rolled { statusCode } object: the client+ // sets `.code`, and a fabricated shape would let a status check that can+ // never match in production pass here.+ gatewayMock.getSecret.mockRejectedValue(+ new ApiException(403, 'Forbidden', { message: 'secrets is forbidden' }, {})+ )++ const res = await request(makeApp()).get('/admin/communication-channels/foo/credentials')++ // A 403 must not be laundered into "no credentials": the panel would then+ // tell the operator a configured channel is empty.+ expect(res.status).toBeGreaterThanOrEqual(500)+ const serialized = JSON.stringify(res.body)+ expect(serialized).not.toContain('slack-signing-secret')+ expect(serialized).not.toContain(SENTINEL_RAW)+ expect(serialized).not.toContain(SENTINEL_B64)+ expect(res.text).not.toContain(SENTINEL_RAW)+ expect(res.text).not.toContain(SENTINEL_B64)+ })++ it('rejects an unauthenticated caller exactly like the sibling PUT', async () => {+ const app = createApp(new MockGateway('channels') as never)++ const getRes = await request(app).get('/api/v1/admin/communication-channels/foo/credentials')+ const putRes = await request(app)+ .put('/api/v1/admin/communication-channels/foo/credentials')+ .send({ 'telegram-bot-token': 'tok' })++ // Same gate, same answer: the read is no more public than the write that+ // sets the values. The control-UI gate is app-level middleware over the+ // whole /admin prefix, so this pins the route to that prefix rather than+ // re-testing the middleware (middleware.controlUIAuth.test.ts owns that).+ expect(getRes.status).toBe(401)+ expect(getRes.status).toBe(putRes.status)+ })+})