@devries-cameron20 / composio Mirror · read-only
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 04:45 UTC

next
ts 60 lines 1.71 KB
Raw
1 /**
2 * A throwaway certificate for the fake Dashboard.
3 *
4 * The CLI sends a user API key to the Dashboard only over `https:` (or to
5 * loopback), so a fake the container reaches by host name has to speak TLS.
6 */
7
8 import { execFileSync } from 'node:child_process';
9 import { mkdtempSync, readFileSync, rmSync } from 'node:fs';
10 import { tmpdir } from 'node:os';
11 import { join } from 'node:path';
12
13 export interface TlsMaterial {
14 readonly key: string;
15 readonly cert: string;
16 }
17
18 /** Host names the binary under test may reach the fake by. */
19 export const FAKE_HOSTNAMES = ['host.docker.internal', 'localhost'] as const;
20
21 let cached: TlsMaterial | undefined;
22
23 /** Self-signed, generated once per test process with the host's `openssl`. */
24 export const tlsMaterial = (): TlsMaterial => {
25 if (cached) return cached;
26 const directory = mkdtempSync(join(tmpdir(), 'composio-e2e-tls-'));
27 const keyPath = join(directory, 'key.pem');
28 const certPath = join(directory, 'cert.pem');
29 try {
30 execFileSync(
31 'openssl',
32 [
33 'req',
34 '-x509',
35 '-newkey',
36 'rsa:2048',
37 '-nodes',
38 '-days',
39 '2',
40 '-keyout',
41 keyPath,
42 '-out',
43 certPath,
44 '-subj',
45 `/CN=${FAKE_HOSTNAMES[0]}`,
46 '-addext',
47 `subjectAltName=${FAKE_HOSTNAMES.map(name => `DNS:${name}`).join(',')},IP:127.0.0.1`,
48 ],
49 { stdio: 'pipe' }
50 );
51 cached = { key: readFileSync(keyPath, 'utf8'), cert: readFileSync(certPath, 'utf8') };
52 return cached;
53 } catch (cause) {
54 throw new Error('The fake Dashboard needs `openssl` on PATH to create its test certificate.', {
55 cause,
56 });
57 } finally {
58 rmSync(directory, { recursive: true, force: true });
59 }
60 };