-
Notifications
You must be signed in to change notification settings - Fork 528
fix(serial): auto-login on serial consoles with saved host credentials #3418
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
97a8018
fix(#3417): automated AI fix
netcatty-bot f861952
fix: address Codex review on PR #3418
netcatty-bot 91a69e2
fix: address Codex review on PR #3418
netcatty-bot b1f05be
fix: address Codex review on PR #3418
netcatty-bot 283d15c
fix: address Codex review on PR #3418
netcatty-bot 07fa516
fix: address Codex review on PR #3418
netcatty-bot d8abc38
fix: address Codex review on PR #3418
netcatty-bot 908e626
fix: address Codex review on PR #3418
netcatty-bot ac4b43c
fix: address Codex review on PR #3418
netcatty-bot 58fdb45
fix: address Codex review on PR #3418
netcatty-bot d240af4
fix: guard serial quick login and reconnect startup commands
binaricat 5573304
fix: preserve serial startup while tabs sleep
binaricat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
240 changes: 240 additions & 0 deletions
240
components/terminal/runtime/createTerminalSessionStarters.serial.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,240 @@ | ||
| import test from "node:test"; | ||
| import assert from "node:assert/strict"; | ||
|
|
||
| import { createTerminalSessionStarters } from "./createTerminalSessionStarters"; | ||
|
|
||
| const noop = () => undefined; | ||
| const ENCRYPTED_CREDENTIAL_PLACEHOLDER = "enc:v1:djEwdGVzdAAAAAAAAAAAAAAAAA=="; | ||
|
|
||
| const buildBackend = (overrides: Partial<Record<string, unknown>> = {}) => ({ | ||
| backendAvailable: () => true, | ||
| telnetAvailable: () => true, | ||
| moshAvailable: () => true, | ||
| localAvailable: () => true, | ||
| serialAvailable: () => true, | ||
| execAvailable: () => true, | ||
| startSSHSession: async () => "ssh-session", | ||
| startTelnetSession: async () => "telnet-session", | ||
| startMoshSession: async () => "mosh-session", | ||
| startLocalSession: async () => "local-session", | ||
| startSerialSession: async () => "serial-session", | ||
| execCommand: async () => ({}), | ||
| onSessionData: () => noop, | ||
| onSessionExit: () => noop, | ||
| onChainProgress: () => noop, | ||
| writeToSession: noop, | ||
| resizeSession: noop, | ||
| ...overrides, | ||
| }); | ||
|
|
||
| const buildCtx = (backend: unknown, extra: Record<string, unknown> = {}) => ({ | ||
| host: { | ||
| id: "serial-1", | ||
| label: "Serial: ttyUSB0", | ||
| hostname: "/dev/ttyUSB0", | ||
| protocol: "serial", | ||
| charset: "UTF-8", | ||
| }, | ||
| keys: [], | ||
| identities: [], | ||
| sessionId: "session-1", | ||
| serialConfig: { | ||
| path: "/dev/ttyUSB0", | ||
| baudRate: 115200, | ||
| dataBits: 8, | ||
| stopBits: 1, | ||
| parity: "none", | ||
| flowControl: "none", | ||
| }, | ||
| terminalSettings: {}, | ||
| terminalBackend: backend, | ||
| sessionRef: { current: null }, | ||
| hasConnectedRef: { current: false }, | ||
| hasRunStartupCommandRef: { current: false }, | ||
| disposeDataRef: { current: null }, | ||
| disposeExitRef: { current: null }, | ||
| fitAddonRef: { current: null }, | ||
| serializeAddonRef: { current: null }, | ||
| pendingAuthRef: { current: null }, | ||
| bootEpochRef: { current: 0 }, | ||
| updateStatus: noop, | ||
| setStatus: noop, | ||
| setError: noop, | ||
| setNeedsAuth: noop, | ||
| setAuthRetryMessage: noop, | ||
| setAuthPassword: noop, | ||
| setProgressLogs: noop, | ||
| setProgressValue: noop, | ||
| ...extra, | ||
| }); | ||
|
|
||
| const term = { | ||
| cols: 120, | ||
| rows: 32, | ||
| write: noop, | ||
| writeln: noop, | ||
| scrollToBottom: noop, | ||
| }; | ||
|
|
||
| test("startSerial passes saved host credentials for auto-login", async () => { | ||
| let capturedOptions: Record<string, unknown> | null = null; | ||
| const backend = buildBackend({ | ||
| startSerialSession: async (options: Record<string, unknown>) => { | ||
| capturedOptions = options; | ||
| return "serial-session"; | ||
| }, | ||
| }); | ||
|
|
||
| await createTerminalSessionStarters(buildCtx(backend, { | ||
| host: { | ||
| id: "serial-1", | ||
| label: "Serial: ttyUSB0", | ||
| hostname: "/dev/ttyUSB0", | ||
| protocol: "serial", | ||
| username: "admin", | ||
| password: "secret", | ||
| }, | ||
| }) as never).startSerial(term as never); | ||
|
|
||
| assert.ok(capturedOptions); | ||
| assert.equal(capturedOptions.username, "admin"); | ||
| assert.equal(capturedOptions.password, "secret"); | ||
| }); | ||
|
|
||
| test("startSerial omits credentials when none are saved", async () => { | ||
| let capturedOptions: Record<string, unknown> | null = null; | ||
| const backend = buildBackend({ | ||
| startSerialSession: async (options: Record<string, unknown>) => { | ||
| capturedOptions = options; | ||
| return "serial-session"; | ||
| }, | ||
| }); | ||
|
|
||
| await createTerminalSessionStarters(buildCtx(backend) as never).startSerial(term as never); | ||
|
|
||
| assert.ok(capturedOptions); | ||
| assert.equal(capturedOptions.username, undefined); | ||
| assert.equal("password" in capturedOptions, false); | ||
| }); | ||
|
|
||
| test("startSerial skips an undecryptable saved password", async () => { | ||
| let capturedOptions: Record<string, unknown> | null = null; | ||
| const backend = buildBackend({ | ||
| startSerialSession: async (options: Record<string, unknown>) => { | ||
| capturedOptions = options; | ||
| return "serial-session"; | ||
| }, | ||
| }); | ||
|
|
||
| await createTerminalSessionStarters(buildCtx(backend, { | ||
| host: { | ||
| id: "serial-1", | ||
| hostname: "/dev/ttyUSB0", | ||
| protocol: "serial", | ||
| username: "", | ||
| password: ENCRYPTED_CREDENTIAL_PLACEHOLDER, | ||
| }, | ||
| }) as never).startSerial(term as never); | ||
|
|
||
| assert.ok(capturedOptions); | ||
| assert.equal("username" in capturedOptions, false); | ||
| assert.equal("password" in capturedOptions, false); | ||
| }); | ||
|
|
||
| test("startSerial waits for auto-login before running the startup command", async () => { | ||
| const writtenCommands: string[] = []; | ||
| const executedCommands: string[] = []; | ||
| let autoLoginComplete: ((evt: { sessionId: string }) => void) | null = null; | ||
| let resolveCommand: (() => void) | null = null; | ||
| const commandWritten = new Promise<void>((resolve) => { | ||
| resolveCommand = resolve; | ||
| }); | ||
|
|
||
| const backend = buildBackend({ | ||
| startSerialSession: async () => "serial-session", | ||
| onTelnetAutoLoginComplete: ( | ||
| _sessionId: string, | ||
| cb: (evt: { sessionId: string }) => void, | ||
| ) => { | ||
| autoLoginComplete = cb; | ||
| return noop; | ||
| }, | ||
| onTelnetAutoLoginCancelled: () => noop, | ||
| writeToSession: (_sessionId: string, data: string) => { | ||
| writtenCommands.push(data); | ||
| resolveCommand?.(); | ||
| }, | ||
| }); | ||
|
|
||
| const ctx = buildCtx(backend, { | ||
| host: { | ||
| id: "serial-1", | ||
| hostname: "/dev/ttyUSB0", | ||
| protocol: "serial", | ||
| username: "admin", | ||
| password: "secret", | ||
| startupCommand: "show version", | ||
| }, | ||
| onCommandExecuted: (command: string) => { | ||
| executedCommands.push(command); | ||
| }, | ||
| }); | ||
|
|
||
| await createTerminalSessionStarters(ctx as never).startSerial(term as never); | ||
| assert.ok(autoLoginComplete); | ||
|
|
||
| // The startup command must not fire on the default 600ms delay while the | ||
| // main-process auto-login is still answering prompts. | ||
| await new Promise((resolve) => setTimeout(resolve, 700)); | ||
| assert.deepEqual(writtenCommands, []); | ||
| assert.deepEqual(executedCommands, []); | ||
|
|
||
| autoLoginComplete?.({ sessionId: "session-1" }); | ||
|
|
||
| await Promise.race([ | ||
| commandWritten, | ||
| new Promise((_, reject) => | ||
| setTimeout(() => reject(new Error("Timed out waiting for startup command")), 1000), | ||
| ), | ||
| ]); | ||
| assert.deepEqual(writtenCommands, ["show version\r"]); | ||
| assert.deepEqual(executedCommands, ["show version"]); | ||
| }); | ||
|
|
||
| test("startSerial runs the startup command without waiting when no credentials are saved", async () => { | ||
| const writtenCommands: string[] = []; | ||
| const backend = buildBackend({ | ||
| startSerialSession: async () => "serial-session", | ||
| writeToSession: (_sessionId: string, data: string) => { | ||
| writtenCommands.push(data); | ||
| }, | ||
| }); | ||
|
|
||
| const ctx = buildCtx(backend, { | ||
| host: { | ||
| id: "serial-1", | ||
| hostname: "/dev/ttyUSB0", | ||
| protocol: "serial", | ||
| startupCommand: "show version", | ||
| }, | ||
| }); | ||
|
|
||
| await createTerminalSessionStarters(ctx as never).startSerial(term as never); | ||
|
|
||
| await Promise.race([ | ||
| new Promise<void>((resolve) => { | ||
| const tick = () => { | ||
| if (writtenCommands.length > 0) { | ||
| resolve(); | ||
| return; | ||
| } | ||
| setTimeout(tick, 20); | ||
| }; | ||
| tick(); | ||
| }), | ||
| new Promise((_, reject) => | ||
| setTimeout(() => reject(new Error("Timed out waiting for startup command")), 2000), | ||
| ), | ||
| ]); | ||
| assert.deepEqual(writtenCommands, ["show version\r"]); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.