@marcusrbrown/infra 0.4.9 → 0.4.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/__snapshots__/cli.test.ts.snap +32 -1
- package/src/cli.ts +2 -0
- package/src/commands/gateway/backup.test.ts +288 -0
- package/src/commands/gateway/backup.ts +188 -0
- package/src/commands/gateway/deploy.test.ts +297 -0
- package/src/commands/gateway/deploy.ts +148 -0
- package/src/commands/gateway/host.test.ts +73 -0
- package/src/commands/gateway/host.ts +31 -0
- package/src/commands/gateway/index.ts +17 -0
- package/src/commands/gateway/logs.test.ts +222 -0
- package/src/commands/gateway/logs.ts +158 -0
- package/src/commands/gateway/restore.test.ts +494 -0
- package/src/commands/gateway/restore.ts +297 -0
- package/src/commands/gateway/status.test.ts +354 -0
- package/src/commands/gateway/status.ts +226 -0
- package/src/commands/status.test.ts +25 -0
- package/src/commands/status.ts +13 -3
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import {afterEach, beforeEach, describe, expect, it} from 'bun:test'
|
|
2
|
+
|
|
3
|
+
import {VALID_SERVICES, validateService} from './logs'
|
|
4
|
+
|
|
5
|
+
// ─── validateService ─────────────────────────────────────────────────────────
|
|
6
|
+
|
|
7
|
+
describe('validateService', () => {
|
|
8
|
+
it('accepts gateway as a valid service', () => {
|
|
9
|
+
expect(() => validateService('gateway')).not.toThrow()
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
it('accepts workspace as a valid service', () => {
|
|
13
|
+
expect(() => validateService('workspace')).not.toThrow()
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
it('accepts mitmproxy as a valid service', () => {
|
|
17
|
+
expect(() => validateService('mitmproxy')).not.toThrow()
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
it('rejects an unknown service name with a message listing valid services', () => {
|
|
21
|
+
expect(() => validateService('frobnicator')).toThrow(VALID_SERVICES.join(', '))
|
|
22
|
+
})
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
// ─── streamGatewayLogs — host validation (SEC1) ───────────────────────────────
|
|
26
|
+
|
|
27
|
+
describe('streamGatewayLogs — host validation (SEC1)', () => {
|
|
28
|
+
it('rejects a leading-hyphen host and does not invoke ssh', async () => {
|
|
29
|
+
delete process.env.CI
|
|
30
|
+
|
|
31
|
+
const {streamGatewayLogs} = await import('./logs')
|
|
32
|
+
|
|
33
|
+
const neverSpawn: SpawnFn = () => {
|
|
34
|
+
throw new Error('spawn must not be called for an invalid host')
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
await expect(
|
|
38
|
+
streamGatewayLogs({host: '-oProxyCommand=evil', service: 'gateway', tail: 100, allowCi: false}, neverSpawn),
|
|
39
|
+
).rejects.toThrow('Invalid GATEWAY_HOST')
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
it('rejects a host with shell metacharacters and does not invoke ssh', async () => {
|
|
43
|
+
delete process.env.CI
|
|
44
|
+
|
|
45
|
+
const {streamGatewayLogs} = await import('./logs')
|
|
46
|
+
|
|
47
|
+
const neverSpawn: SpawnFn = () => {
|
|
48
|
+
throw new Error('spawn must not be called for an invalid host')
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
await expect(
|
|
52
|
+
streamGatewayLogs(
|
|
53
|
+
{host: 'gateway.example.com;rm -rf', service: 'gateway', tail: 100, allowCi: false},
|
|
54
|
+
neverSpawn,
|
|
55
|
+
),
|
|
56
|
+
).rejects.toThrow('Invalid GATEWAY_HOST')
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
it('rejects an empty host and does not invoke ssh', async () => {
|
|
60
|
+
delete process.env.CI
|
|
61
|
+
|
|
62
|
+
const {streamGatewayLogs} = await import('./logs')
|
|
63
|
+
|
|
64
|
+
const neverSpawn: SpawnFn = () => {
|
|
65
|
+
throw new Error('spawn must not be called for an invalid host')
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
await expect(
|
|
69
|
+
streamGatewayLogs({host: '', service: 'gateway', tail: 100, allowCi: false}, neverSpawn),
|
|
70
|
+
).rejects.toThrow('Invalid GATEWAY_HOST')
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
it('accepts a valid FQDN and invokes ssh normally', async () => {
|
|
74
|
+
delete process.env.CI
|
|
75
|
+
|
|
76
|
+
const {streamGatewayLogs} = await import('./logs')
|
|
77
|
+
|
|
78
|
+
const result = await streamGatewayLogs(
|
|
79
|
+
{host: 'gateway.example.com', service: 'gateway', tail: 100, allowCi: false},
|
|
80
|
+
makeSpawnOk(),
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
expect(result.refused).toBe(false)
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
it('accepts localhost as a valid host', async () => {
|
|
87
|
+
delete process.env.CI
|
|
88
|
+
|
|
89
|
+
const {streamGatewayLogs} = await import('./logs')
|
|
90
|
+
|
|
91
|
+
const result = await streamGatewayLogs(
|
|
92
|
+
{host: 'localhost', service: 'gateway', tail: 100, allowCi: false},
|
|
93
|
+
makeSpawnOk(),
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
expect(result.refused).toBe(false)
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
it('accepts an IPv4 address as a valid host', async () => {
|
|
100
|
+
delete process.env.CI
|
|
101
|
+
|
|
102
|
+
const {streamGatewayLogs} = await import('./logs')
|
|
103
|
+
|
|
104
|
+
const result = await streamGatewayLogs(
|
|
105
|
+
{host: '147.182.133.210', service: 'gateway', tail: 100, allowCi: false},
|
|
106
|
+
makeSpawnOk(),
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
expect(result.refused).toBe(false)
|
|
110
|
+
})
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
// ─── CI guard ────────────────────────────────────────────────────────────────
|
|
114
|
+
|
|
115
|
+
type SpawnFn = (
|
|
116
|
+
cmd: string[],
|
|
117
|
+
opts: {env: Record<string, string>; stdout: 'inherit'; stderr: 'inherit'},
|
|
118
|
+
) => {
|
|
119
|
+
exited: Promise<number>
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function makeNeverSpawn(): SpawnFn {
|
|
123
|
+
return () => {
|
|
124
|
+
throw new Error('spawn should not have been called')
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function makeSpawnOk(): SpawnFn {
|
|
129
|
+
return (_cmd, _opts) => ({exited: Promise.resolve(0)})
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
describe('CI guard', () => {
|
|
133
|
+
let originalCI: string | undefined
|
|
134
|
+
|
|
135
|
+
beforeEach(() => {
|
|
136
|
+
originalCI = process.env.CI
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
afterEach(() => {
|
|
140
|
+
if (originalCI === undefined) {
|
|
141
|
+
delete process.env.CI
|
|
142
|
+
} else {
|
|
143
|
+
process.env.CI = originalCI
|
|
144
|
+
}
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
it('refuses to stream logs in CI without --allow-ci', async () => {
|
|
148
|
+
process.env.CI = 'true'
|
|
149
|
+
|
|
150
|
+
const {streamGatewayLogs} = await import('./logs')
|
|
151
|
+
|
|
152
|
+
const messages: string[] = []
|
|
153
|
+
const result = await streamGatewayLogs(
|
|
154
|
+
{host: 'gateway.example.com', service: 'gateway', tail: 100, allowCi: false},
|
|
155
|
+
makeNeverSpawn(),
|
|
156
|
+
(msg: string) => {
|
|
157
|
+
messages.push(msg)
|
|
158
|
+
},
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
expect(result.refused).toBe(true)
|
|
162
|
+
expect(messages.some(m => m.includes('--allow-ci'))).toBe(true)
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
it('streams logs in CI when --allow-ci is set, printing stderr warning first', async () => {
|
|
166
|
+
process.env.CI = 'true'
|
|
167
|
+
|
|
168
|
+
const {streamGatewayLogs} = await import('./logs')
|
|
169
|
+
|
|
170
|
+
const warnings: string[] = []
|
|
171
|
+
const result = await streamGatewayLogs(
|
|
172
|
+
{host: 'gateway.example.com', service: 'gateway', tail: 100, allowCi: true},
|
|
173
|
+
makeSpawnOk(),
|
|
174
|
+
undefined,
|
|
175
|
+
(msg: string) => {
|
|
176
|
+
warnings.push(msg)
|
|
177
|
+
},
|
|
178
|
+
)
|
|
179
|
+
|
|
180
|
+
expect(result.refused).toBe(false)
|
|
181
|
+
expect(warnings.some(w => w.includes('Discord tokens'))).toBe(true)
|
|
182
|
+
})
|
|
183
|
+
|
|
184
|
+
it('streams logs in non-CI context, printing stderr warning first', async () => {
|
|
185
|
+
delete process.env.CI
|
|
186
|
+
|
|
187
|
+
const {streamGatewayLogs} = await import('./logs')
|
|
188
|
+
|
|
189
|
+
const warnings: string[] = []
|
|
190
|
+
const result = await streamGatewayLogs(
|
|
191
|
+
{host: 'gateway.example.com', service: 'gateway', tail: 100, allowCi: false},
|
|
192
|
+
makeSpawnOk(),
|
|
193
|
+
undefined,
|
|
194
|
+
(msg: string) => {
|
|
195
|
+
warnings.push(msg)
|
|
196
|
+
},
|
|
197
|
+
)
|
|
198
|
+
|
|
199
|
+
expect(result.refused).toBe(false)
|
|
200
|
+
expect(warnings.some(w => w.includes('Discord tokens'))).toBe(true)
|
|
201
|
+
})
|
|
202
|
+
})
|
|
203
|
+
|
|
204
|
+
// ─── --tail forwarding ───────────────────────────────────────────────────────
|
|
205
|
+
|
|
206
|
+
describe('tail forwarding', () => {
|
|
207
|
+
it('forwards --tail N to docker compose logs command', async () => {
|
|
208
|
+
delete process.env.CI
|
|
209
|
+
|
|
210
|
+
const {streamGatewayLogs} = await import('./logs')
|
|
211
|
+
|
|
212
|
+
let capturedCmd: string[] = []
|
|
213
|
+
const spawnCapture: SpawnFn = (cmd, _opts) => {
|
|
214
|
+
capturedCmd = cmd
|
|
215
|
+
return {exited: Promise.resolve(0)}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
await streamGatewayLogs({host: 'gateway.example.com', service: 'gateway', tail: 25, allowCi: false}, spawnCapture)
|
|
219
|
+
|
|
220
|
+
expect(capturedCmd.join(' ')).toContain('--tail=25')
|
|
221
|
+
})
|
|
222
|
+
})
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import type {goke} from 'goke'
|
|
2
|
+
|
|
3
|
+
import {z} from 'zod'
|
|
4
|
+
|
|
5
|
+
import {validateGatewayHost} from './host'
|
|
6
|
+
|
|
7
|
+
declare const process: {
|
|
8
|
+
env: Record<string, string | undefined>
|
|
9
|
+
exitCode?: number
|
|
10
|
+
stderr: {write: (msg: string) => void}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const COMPOSE_PROJECT_DIR = '/opt/gateway/deploy'
|
|
14
|
+
const SENSITIVE_WARNING =
|
|
15
|
+
'Warning: Logs may contain Discord tokens, S3 credentials, or user data. Treat output as sensitive; do not capture in shared logs or chat.'
|
|
16
|
+
|
|
17
|
+
export const VALID_SERVICES = ['gateway', 'workspace', 'mitmproxy'] as const
|
|
18
|
+
|
|
19
|
+
export type ValidService = (typeof VALID_SERVICES)[number]
|
|
20
|
+
|
|
21
|
+
// ─── Pure helpers ─────────────────────────────────────────────────────────────
|
|
22
|
+
|
|
23
|
+
export function validateService(service: string): asserts service is ValidService {
|
|
24
|
+
if (!VALID_SERVICES.includes(service as ValidService)) {
|
|
25
|
+
throw new Error(`Invalid service "${service}". Valid services: ${VALID_SERVICES.join(', ')}`)
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// ─── Injectable spawn type ────────────────────────────────────────────────────
|
|
30
|
+
|
|
31
|
+
export type LogsSpawnFn = (
|
|
32
|
+
cmd: string[],
|
|
33
|
+
opts: {env: Record<string, string>; stdout: 'inherit'; stderr: 'inherit'},
|
|
34
|
+
) => {
|
|
35
|
+
exited: Promise<number>
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface StreamLogsOpts {
|
|
39
|
+
host: string
|
|
40
|
+
service: string
|
|
41
|
+
tail: number
|
|
42
|
+
allowCi: boolean
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface StreamLogsResult {
|
|
46
|
+
refused: boolean
|
|
47
|
+
exitCode?: number
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function defaultLogsSpawn(
|
|
51
|
+
cmd: string[],
|
|
52
|
+
opts: {env: Record<string, string>; stdout: 'inherit'; stderr: 'inherit'},
|
|
53
|
+
): {exited: Promise<number>} {
|
|
54
|
+
return Bun.spawn(cmd, opts)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export async function streamGatewayLogs(
|
|
58
|
+
opts: StreamLogsOpts,
|
|
59
|
+
spawn: LogsSpawnFn = defaultLogsSpawn,
|
|
60
|
+
printOut?: (msg: string) => void,
|
|
61
|
+
printErr?: (msg: string) => void,
|
|
62
|
+
): Promise<StreamLogsResult> {
|
|
63
|
+
const isCI = process.env.CI === 'true'
|
|
64
|
+
|
|
65
|
+
if (isCI && !opts.allowCi) {
|
|
66
|
+
const msg = 'Refusing to stream logs in CI without --allow-ci. Logs may contain sensitive tokens or user data.'
|
|
67
|
+
if (printOut) {
|
|
68
|
+
printOut(msg)
|
|
69
|
+
} else {
|
|
70
|
+
console.log(msg)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return {refused: true}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Warn on every run — logs are sensitive regardless of context
|
|
77
|
+
if (printErr) {
|
|
78
|
+
printErr(SENSITIVE_WARNING)
|
|
79
|
+
} else {
|
|
80
|
+
console.error(SENSITIVE_WARNING)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
validateGatewayHost(opts.host)
|
|
84
|
+
|
|
85
|
+
const sshCmd = [
|
|
86
|
+
'ssh',
|
|
87
|
+
'-o',
|
|
88
|
+
'BatchMode=yes',
|
|
89
|
+
'-o',
|
|
90
|
+
'StrictHostKeyChecking=yes',
|
|
91
|
+
`root@${opts.host}`,
|
|
92
|
+
`docker compose --project-directory ${COMPOSE_PROJECT_DIR} logs --no-color --tail=${opts.tail} ${opts.service}`,
|
|
93
|
+
]
|
|
94
|
+
|
|
95
|
+
const env: Record<string, string> = {
|
|
96
|
+
PATH: process.env.PATH ?? '/usr/bin:/bin',
|
|
97
|
+
HOME: process.env.HOME ?? '/tmp',
|
|
98
|
+
...(process.env.SSH_AUTH_SOCK ? {SSH_AUTH_SOCK: process.env.SSH_AUTH_SOCK} : {}),
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const child = spawn(sshCmd, {env, stdout: 'inherit', stderr: 'inherit'})
|
|
102
|
+
const exitCode = await child.exited
|
|
103
|
+
|
|
104
|
+
return {refused: false, exitCode}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// ─── Command registration ─────────────────────────────────────────────────────
|
|
108
|
+
|
|
109
|
+
export function registerGatewayLogs(cli: ReturnType<typeof goke>): void {
|
|
110
|
+
cli
|
|
111
|
+
.command('gateway logs [service]', 'Stream logs from a gateway service via SSH and docker compose.')
|
|
112
|
+
.option('--tail [n]', z.number().default(100).describe('Number of log lines to tail from each service.'))
|
|
113
|
+
.option(
|
|
114
|
+
'--allow-ci',
|
|
115
|
+
z
|
|
116
|
+
.boolean()
|
|
117
|
+
.default(false)
|
|
118
|
+
.describe('Allow log streaming in CI environments. Logs may contain sensitive credentials.'),
|
|
119
|
+
)
|
|
120
|
+
.option(
|
|
121
|
+
'--key [key]',
|
|
122
|
+
z.string().describe('Environment variable name holding the SSH host. Falls back to GATEWAY_HOST when omitted.'),
|
|
123
|
+
)
|
|
124
|
+
.action(async (service, options) => {
|
|
125
|
+
const targetService = (service as string | undefined) ?? 'gateway'
|
|
126
|
+
|
|
127
|
+
try {
|
|
128
|
+
validateService(targetService)
|
|
129
|
+
} catch (error) {
|
|
130
|
+
console.error(error instanceof Error ? error.message : String(error))
|
|
131
|
+
process.exitCode = 1
|
|
132
|
+
return
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const hostEnvKey = options.key ?? 'GATEWAY_HOST'
|
|
136
|
+
const host = process.env[hostEnvKey]
|
|
137
|
+
|
|
138
|
+
if (!host) {
|
|
139
|
+
console.error(`Gateway host not set. Export ${hostEnvKey} or pass --key <env-name> pointing to a set variable.`)
|
|
140
|
+
process.exitCode = 1
|
|
141
|
+
return
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const tail = typeof options.tail === 'number' ? options.tail : 100
|
|
145
|
+
const allowCi = options.allowCi === true
|
|
146
|
+
|
|
147
|
+
const result = await streamGatewayLogs({host, service: targetService, tail, allowCi})
|
|
148
|
+
|
|
149
|
+
if (result.refused) {
|
|
150
|
+
process.exitCode = 1
|
|
151
|
+
return
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (result.exitCode !== 0) {
|
|
155
|
+
process.exitCode = result.exitCode ?? 1
|
|
156
|
+
}
|
|
157
|
+
})
|
|
158
|
+
}
|