dsh-ssh-hub 0.0.0-stage → 0.2.0

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.
Files changed (133) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +271 -2
  3. package/cordis.patch.yml +16 -0
  4. package/icon.svg +3 -0
  5. package/lib/client.js +13975 -0
  6. package/lib/index.js +6908 -0
  7. package/lib/types/activity/history.d.ts +50 -0
  8. package/lib/types/alerting/notify.d.ts +89 -0
  9. package/lib/types/alerting/prompt-summary.d.ts +28 -0
  10. package/lib/types/alerting/rules.d.ts +135 -0
  11. package/lib/types/alerting/tools.d.ts +30 -0
  12. package/lib/types/audit/audit-log.d.ts +84 -0
  13. package/lib/types/audit/recorder.d.ts +50 -0
  14. package/lib/types/client/api.d.ts +222 -0
  15. package/lib/types/client/index.d.ts +45 -0
  16. package/lib/types/client/locales.d.ts +319 -0
  17. package/lib/types/client/native-panel.d.ts +70 -0
  18. package/lib/types/client/panel/ClusterTab.d.ts +7 -0
  19. package/lib/types/client/panel/DeployTab.d.ts +10 -0
  20. package/lib/types/client/panel/FleetTab.d.ts +10 -0
  21. package/lib/types/client/panel/HostFormDialog.d.ts +12 -0
  22. package/lib/types/client/panel/HostsTab.d.ts +25 -0
  23. package/lib/types/client/panel/LogsTab.d.ts +10 -0
  24. package/lib/types/client/panel/SshPanel.d.ts +14 -0
  25. package/lib/types/client/panel/TerminalTab.d.ts +27 -0
  26. package/lib/types/client/panel/TransferTab.d.ts +7 -0
  27. package/lib/types/client/panel/TunnelEditDialog.d.ts +15 -0
  28. package/lib/types/client/panel/TunnelsTab.d.ts +27 -0
  29. package/lib/types/client/panel/controller.d.ts +253 -0
  30. package/lib/types/client/panel/helpers.d.ts +45 -0
  31. package/lib/types/client/panel/tabs.d.ts +62 -0
  32. package/lib/types/client/panel/xterm.css.d.ts +2 -0
  33. package/lib/types/client/settings-binding.d.ts +71 -0
  34. package/lib/types/deploy/orchestrator.d.ts +158 -0
  35. package/lib/types/deploy/tools.d.ts +14 -0
  36. package/lib/types/dsh-home.d.ts +16 -0
  37. package/lib/types/engine/cluster.d.ts +19 -0
  38. package/lib/types/engine/connection-pool.d.ts +183 -0
  39. package/lib/types/engine/proxy-command.d.ts +36 -0
  40. package/lib/types/engine/pty.d.ts +32 -0
  41. package/lib/types/engine/sftp.d.ts +20 -0
  42. package/lib/types/engine/terminal-sessions.d.ts +67 -0
  43. package/lib/types/engine/tunnel-discovery.d.ts +89 -0
  44. package/lib/types/engine/tunnel-remarks.d.ts +49 -0
  45. package/lib/types/engine/tunnel.d.ts +76 -0
  46. package/lib/types/engine.d.ts +103 -0
  47. package/lib/types/http.d.ts +59 -0
  48. package/lib/types/index.d.ts +128 -0
  49. package/lib/types/inspect/checks.d.ts +118 -0
  50. package/lib/types/inspect/inspection.d.ts +111 -0
  51. package/lib/types/inspect/tools.d.ts +50 -0
  52. package/lib/types/logs/search.d.ts +131 -0
  53. package/lib/types/logs/tools.d.ts +6 -0
  54. package/lib/types/loopback.d.ts +64 -0
  55. package/lib/types/monitor/loop.d.ts +133 -0
  56. package/lib/types/monitor/metrics.d.ts +151 -0
  57. package/lib/types/mount-once.d.ts +55 -0
  58. package/lib/types/protocol.d.ts +524 -0
  59. package/lib/types/routes.d.ts +82 -0
  60. package/lib/types/security/known-hosts.d.ts +153 -0
  61. package/lib/types/security/master-secret.d.ts +157 -0
  62. package/lib/types/security/redact.d.ts +44 -0
  63. package/lib/types/security/secrets.d.ts +70 -0
  64. package/lib/types/ssh-config.d.ts +42 -0
  65. package/lib/types/store.d.ts +183 -0
  66. package/lib/types/tool-surface.d.ts +72 -0
  67. package/lib/types/tools.d.ts +56 -0
  68. package/locale/en.json +6 -0
  69. package/locale/zh.json +6 -0
  70. package/package.json +126 -6
  71. package/src/activity/history.ts +117 -0
  72. package/src/alerting/notify.ts +212 -0
  73. package/src/alerting/prompt-summary.ts +71 -0
  74. package/src/alerting/rules.ts +281 -0
  75. package/src/alerting/tools.ts +263 -0
  76. package/src/audit/audit-log.ts +199 -0
  77. package/src/audit/recorder.ts +73 -0
  78. package/src/client/api.ts +782 -0
  79. package/src/client/css-modules.d.ts +5 -0
  80. package/src/client/index.ts +150 -0
  81. package/src/client/locales.ts +654 -0
  82. package/src/client/native-panel.tsx +141 -0
  83. package/src/client/panel/ClusterTab.tsx +141 -0
  84. package/src/client/panel/DeployTab.tsx +314 -0
  85. package/src/client/panel/FleetTab.tsx +494 -0
  86. package/src/client/panel/HostFormDialog.tsx +289 -0
  87. package/src/client/panel/HostsTab.tsx +395 -0
  88. package/src/client/panel/LogsTab.tsx +208 -0
  89. package/src/client/panel/SshPanel.tsx +105 -0
  90. package/src/client/panel/TerminalTab.tsx +550 -0
  91. package/src/client/panel/TransferTab.tsx +238 -0
  92. package/src/client/panel/TunnelEditDialog.tsx +174 -0
  93. package/src/client/panel/TunnelsTab.tsx +328 -0
  94. package/src/client/panel/controller.ts +507 -0
  95. package/src/client/panel/helpers.ts +84 -0
  96. package/src/client/panel/panel.module.css +1734 -0
  97. package/src/client/panel/tabs.ts +52 -0
  98. package/src/client/panel/xterm.css.ts +2 -0
  99. package/src/client/settings-binding.ts +184 -0
  100. package/src/deploy/orchestrator.ts +326 -0
  101. package/src/deploy/tools.ts +225 -0
  102. package/src/dsh-home.ts +42 -0
  103. package/src/engine/cluster.ts +60 -0
  104. package/src/engine/connection-pool.ts +721 -0
  105. package/src/engine/proxy-command.ts +248 -0
  106. package/src/engine/pty.ts +78 -0
  107. package/src/engine/sftp.ts +229 -0
  108. package/src/engine/terminal-sessions.ts +280 -0
  109. package/src/engine/tunnel-discovery.ts +353 -0
  110. package/src/engine/tunnel-remarks.ts +120 -0
  111. package/src/engine/tunnel.ts +300 -0
  112. package/src/engine.ts +195 -0
  113. package/src/http.ts +127 -0
  114. package/src/index.ts +690 -0
  115. package/src/inspect/checks.ts +251 -0
  116. package/src/inspect/inspection.ts +321 -0
  117. package/src/inspect/tools.ts +366 -0
  118. package/src/logs/search.ts +251 -0
  119. package/src/logs/tools.ts +160 -0
  120. package/src/loopback.ts +119 -0
  121. package/src/monitor/loop.ts +265 -0
  122. package/src/monitor/metrics.ts +333 -0
  123. package/src/mount-once.ts +142 -0
  124. package/src/protocol.ts +534 -0
  125. package/src/routes.ts +998 -0
  126. package/src/security/known-hosts.ts +342 -0
  127. package/src/security/master-secret.ts +334 -0
  128. package/src/security/redact.ts +119 -0
  129. package/src/security/secrets.ts +152 -0
  130. package/src/ssh-config.ts +182 -0
  131. package/src/store.ts +757 -0
  132. package/src/tool-surface.ts +96 -0
  133. package/src/tools.ts +550 -0
@@ -0,0 +1,199 @@
1
+ /**
2
+ * Operation audit log.
3
+ *
4
+ * Every consequential action (exec, cluster, transfer, tunnel, deploy,
5
+ * credential change) is recorded: what happened, against which hosts, with
6
+ * which outcome, and who triggered it.
7
+ *
8
+ * The log is append-only JSON Lines — one record per line — so it can be
9
+ * tailed, grepped and shipped without a database. It lives beside the
10
+ * host store under `$DSH_HOME`, owner-readable only.
11
+ *
12
+ * Two rules the log must never break:
13
+ * 1. No secret ever reaches it. Commands are recorded, but a command that
14
+ * embeds a password is redacted before it is written.
15
+ * 2. A record is written AFTER the outcome is known, so the log says what
16
+ * happened rather than what was attempted.
17
+ */
18
+
19
+ import { appendFileSync, existsSync, mkdirSync, readFileSync } from 'node:fs'
20
+ import { dirname, join } from 'node:path'
21
+ import { dshHome } from '../dsh-home.ts'
22
+
23
+ /** What kind of action was performed. */
24
+ export type AuditAction =
25
+ | 'host.create'
26
+ | 'host.update'
27
+ | 'host.delete'
28
+ | 'host.test'
29
+ | 'exec'
30
+ | 'cluster'
31
+ | 'upload'
32
+ | 'download'
33
+ | 'tunnel.start'
34
+ | 'tunnel.stop'
35
+ | 'tunnel.update'
36
+ | 'deploy'
37
+ | 'rollback'
38
+ | 'inspect.run'
39
+ | 'credential.rotate'
40
+ | 'security.policy'
41
+
42
+ /** How the action was triggered. */
43
+ export type AuditActor = 'user' | 'agent' | 'system'
44
+
45
+ /** One audit record. */
46
+ export interface AuditRecord {
47
+ /** Epoch milliseconds. */
48
+ ts: number
49
+ /** What happened. */
50
+ action: AuditAction
51
+ /** Who triggered it. */
52
+ actor: AuditActor
53
+ /** The hosts the action touched. */
54
+ targets: string[]
55
+ /** Whether it succeeded. */
56
+ ok: boolean
57
+ /** A short, human-readable summary (never contains secrets). */
58
+ summary: string
59
+ /** Free-form details: exit codes, durations, counts. */
60
+ detail?: Record<string, unknown>
61
+ }
62
+
63
+ /** The audit log file path. */
64
+ export function auditLogPath(): string {
65
+ return join(dshHome(), 'dsh-ssh-hub', 'audit.log')
66
+ }
67
+
68
+ /**
69
+ * Patterns whose match must never be written to the log.
70
+ *
71
+ * These are the shapes a password takes in a shell command line — the
72
+ * only place a secret is likely to appear in an audit record.
73
+ */
74
+ const SECRET_PATTERNS: Array<[RegExp, string]> = [
75
+ // `password=secret`, `PASSWORD: secret`, `passwd=secret`
76
+ [/\b(pass(?:word|wd|phrase)?)\s*[=:]\s*("[^"]*"|'[^']*'|\S+)/gi, '$1=[REDACTED]'],
77
+ // `-p secret` / `--password secret` (mysql, psql style)
78
+ [/(--password[= ])(("[^"]*"|'[^']*'|\S+))/g, '$1[REDACTED]'],
79
+ // `echo secret | sudo -S`
80
+ [/(echo\s+)("[^"]*"|'[^']*'|\S+)(\s*\|\s*(sudo\s+-S|-S))/g, '$1[REDACTED]$3'],
81
+ // `Authorization: Bearer <token>`
82
+ [/(Bearer\s+)[A-Za-z0-9._~+/-]{8,}=*/g, '$1[REDACTED]'],
83
+ // A private key block pasted into a command.
84
+ [/-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?-----END [A-Z ]*PRIVATE KEY-----/g, '[REDACTED PRIVATE KEY]'],
85
+ ]
86
+
87
+ /**
88
+ * Redact secrets from a string before it is recorded.
89
+ *
90
+ * @param text - the text to redact.
91
+ * @returns the redacted text.
92
+ */
93
+ export function redact(text: string): string {
94
+ let result = text
95
+ for (const [pattern, replacement] of SECRET_PATTERNS) {
96
+ result = result.replace(pattern, replacement)
97
+ }
98
+ return result
99
+ }
100
+
101
+ /**
102
+ * The audit log writer.
103
+ *
104
+ * Writes are best-effort: a failure to audit must not fail the operation
105
+ * the user asked for (an unwritable log directory should not block a
106
+ * deployment), but the failure is reported so it is not silent.
107
+ */
108
+ export class AuditLog {
109
+ /** The file this log writes to. */
110
+ readonly path: string
111
+ /** The last write error, if any. */
112
+ lastError: string | undefined
113
+
114
+ constructor(path?: string) {
115
+ this.path = path ?? auditLogPath()
116
+ }
117
+
118
+ /**
119
+ * Append one record.
120
+ *
121
+ * @param record - the record to write.
122
+ */
123
+ append(record: AuditRecord): void {
124
+ const line = JSON.stringify({
125
+ ...record,
126
+ summary: redact(record.summary),
127
+ detail: record.detail === undefined ? undefined : redactDetail(record.detail),
128
+ }) + '\n'
129
+ try {
130
+ const dir = dirname(this.path)
131
+ if (!existsSync(dir)) mkdirSync(dir, { recursive: true, mode: 0o700 })
132
+ appendFileSync(this.path, line, { encoding: 'utf8', mode: 0o600 })
133
+ } catch (error) {
134
+ this.lastError = error instanceof Error ? error.message : String(error)
135
+ }
136
+ }
137
+
138
+ /**
139
+ * Read the most recent records, newest last.
140
+ *
141
+ * @param limit - the maximum number of records to return.
142
+ * @returns the records, oldest first.
143
+ */
144
+ read(limit = 100): AuditRecord[] {
145
+ if (!existsSync(this.path)) return []
146
+ let text: string
147
+ try {
148
+ text = readFileSync(this.path, 'utf8')
149
+ } catch {
150
+ return []
151
+ }
152
+ const lines = text.split('\n').filter(line => line.trim() !== '')
153
+ const slice = lines.slice(-limit)
154
+ const records: AuditRecord[] = []
155
+ for (const line of slice) {
156
+ try {
157
+ records.push(JSON.parse(line) as AuditRecord)
158
+ } catch {
159
+ // A partially written line must not break the whole read.
160
+ }
161
+ }
162
+ return records
163
+ }
164
+ }
165
+
166
+ /**
167
+ * Redact secrets inside a detail object, recursively.
168
+ *
169
+ * @param detail - the detail object.
170
+ * @returns the redacted detail.
171
+ */
172
+ function redactDetail(detail: Record<string, unknown>): Record<string, unknown> {
173
+ const result: Record<string, unknown> = {}
174
+ for (const [key, value] of Object.entries(detail)) {
175
+ if (typeof value === 'string') {
176
+ result[key] = redact(value)
177
+ } else if (Array.isArray(value)) {
178
+ result[key] = value.map(item => typeof item === 'string' ? redact(item) : item)
179
+ } else if (value !== null && typeof value === 'object') {
180
+ result[key] = redactDetail(value as Record<string, unknown>)
181
+ } else {
182
+ result[key] = value
183
+ }
184
+ }
185
+ return result
186
+ }
187
+
188
+ /**
189
+ * A short, stable summary of an exec/cluster outcome.
190
+ *
191
+ * @param alias - the host alias.
192
+ * @param command - the command (redacted by the writer).
193
+ * @param exitCode - the remote exit code.
194
+ * @param durationMs - how long it took.
195
+ * @returns the summary text.
196
+ */
197
+ export function execSummary(alias: string, command: string, exitCode: number | null, durationMs: number): string {
198
+ return `${alias}: ${command} -> exit ${exitCode ?? 'null'} in ${durationMs}ms`
199
+ }
@@ -0,0 +1,73 @@
1
+ /**
2
+ * Audit recording for the fleet tools.
3
+ *
4
+ * The audit log answers "who changed this machine, when, and what did they
5
+ * run". That answer is only complete if EVERY mutating operation is
6
+ * recorded — a log that covers some operations is worse than no log,
7
+ * because the operator trusts the gaps to mean "nothing happened".
8
+ *
9
+ * So the recorder wraps the mutating tools at the point where they are
10
+ * registered, and the read-only tools are deliberately not recorded: an
11
+ * audit log full of `ssh_list` calls buries the entries that matter.
12
+ */
13
+
14
+ import type { AuditAction, AuditActor, AuditLog, AuditRecord } from './audit-log.ts'
15
+
16
+ /** The outcome of a tool call, as the recorder sees it. */
17
+ export interface CallOutcome {
18
+ /** Whether the call succeeded. */
19
+ ok: boolean
20
+ /** The hosts the call touched. */
21
+ targets?: string[]
22
+ /** A short summary (secrets are redacted on write). */
23
+ summary?: string
24
+ /** Extra structured detail. */
25
+ detail?: Record<string, unknown>
26
+ }
27
+
28
+ /**
29
+ * A recorder bound to one audit log and one actor.
30
+ *
31
+ * The actor matters: an operator running a command from the panel and an
32
+ * agent running the same command are different actors, and an audit trail
33
+ * that cannot tell them apart does not answer "who did this".
34
+ */
35
+ export class AuditRecorder {
36
+ /** The log to write to. */
37
+ private readonly log: AuditLog | undefined
38
+ /** Who is performing the actions. */
39
+ private readonly actor: AuditActor
40
+
41
+ constructor(log: AuditLog | undefined, actor: AuditActor = 'agent') {
42
+ this.log = log
43
+ this.actor = actor
44
+ }
45
+
46
+ /**
47
+ * Record one action.
48
+ *
49
+ * A missing log means auditing is disabled, which is a configuration
50
+ * choice — the call is then a no-op, not an error.
51
+ *
52
+ * @param action - what happened.
53
+ * @param outcome - the outcome of the call.
54
+ */
55
+ record(action: AuditAction, outcome: CallOutcome): void {
56
+ if (this.log === undefined) return
57
+ const record: AuditRecord = {
58
+ ts: Date.now(),
59
+ action,
60
+ actor: this.actor,
61
+ targets: outcome.targets ?? [],
62
+ ok: outcome.ok,
63
+ summary: outcome.summary ?? action,
64
+ ...(outcome.detail !== undefined ? { detail: outcome.detail } : {}),
65
+ }
66
+ this.log.append(record)
67
+ }
68
+
69
+ /** Whether this recorder writes anything. */
70
+ get enabled(): boolean {
71
+ return this.log !== undefined
72
+ }
73
+ }