refactor(server): narrow the SSH session seam and type its connection state - #6938
refactor(server): narrow the SSH session seam and type its connection state#6938otavio wants to merge 1 commit into
Conversation
|
/review |
|
Claude finished @otavio's task in 6m 49s —— View job Code Review CompleteReviewed 21 files across code quality, security, testing, language patterns, and architecture. 1 inline issue posted (1 medium). The refactor is clean and self-contained. Verified along the way: the typed The one finding is a test-effectiveness bug: the To request another review round, comment |
… state The channel handlers reached through sess.Agent.Client into another module's struct and read the device version inside the byte-copy loop, so neither handler could run without a real handshake and a live agent. They now take an interface declared in the channels package, and the session's agent, client and seat collection are unexported, which makes that compile-enforced rather than a convention. Per-connection state moves behind typed context keys and named predicates, and the two auth handlers share one reject-and-close path. They had none before, which is why the auth package carried no tests at all. Changes that are not pure refactors: - direct-tcpip logged origin_port and origin_addr transposed, in all seven copies of the block; collapsing them into one field set corrects it - the channel path logs session where the session handler logged uid, agreeing with the guard log both handlers already emitted - direct-tcpip logs sshid as user@namespace.device rather than namespace.device - forward dials abort when the SSH context is cancelled (Dial -> DialContext) - CloseAgentWrite on a seat with no agent channel reports ErrSeatNotFound instead of closing; the only caller discards it
dc98801 to
942ea01
Compare
|
/review |
|
Claude finished @otavio's task in 5m 59s —— View job Code Review CompleteReviewed 21 files across code quality, security, testing, language patterns, and architecture — no new issues found. The code looks good as-is. Verified this round: the prior finding is resolved — To request another review round, comment |
What
Narrows what the
channelspackage needs from an SSH session down to one interface declared at theconsumer, and moves per-connection state behind typed context keys and named predicates. Both exist
to make the SSH path testable:
DefaultSessionHandler,DefaultDirectTCPIPHandlerand the wholeserver/ssh/server/authpackage had no tests before this.Why
Sessionexposed 17 fields and 14 methods, and the channel handlers used 19 distinct paths into it.Two of those reached through
sess.Agent.Client— two levels into another module's struct — and thedata-copy loop read
sess.Device.Info.Versionto choose betweenCloseandCloseWrite, putting adevice-compatibility rule inside a byte pump. Reaching either handler required a real SSH handshake
plus a live agent, so neither could be tested.
Per-connection state hung off
gliderssh.Contextunder the string keys"snap"and"conn", withprogress tracked as an untyped int compared using
<in four files. Both auth handlers separatelyreimplemented "read the connection out of the context and close it", and
password.godid not reusepublickey.go's helper — which is why that package had no test seam at all.Comes from the C1 and C2 candidates of an architecture review of the SSH path.
Changes
Sessioninterface declared in the consuming package, listing the 14 operations thehandlers actually use. Each handler splits into a thin adapter that obtains the session and a core
that takes the interface, following the
newBannerHandlerWithDepspattern already inserver.go.A fake is the second adapter, which is what makes the seam real rather than hypothetical.
agent,clientand the seat collection are unexported, so the reach-through is nowa compile error rather than a convention.
OpenAgentForwards,DialAgentandCloseAgentWritereplace the paths that went through them;
CloseAgentWriteowns the agent-version rule.Statebecomes a real type withEvaluated(),Established()andString(). Typedcontext keys replace
"snap"and"conn".advance()replaces four inlinegetSnapshot(ctx).save(...)calls, and stays unexported so only the step doing the work candeclare it done.
AuthenticableSessionOrDrop. The name states the side effect,because the function closes the socket on the false branch.
auth drop-guard, the agent-close policy and the connection-state module.
pipe_test.gonow drivesthe fake, which let its edition and namespace setup go away.
Testing
The interesting risk here is log output, not behaviour. Four changes a reviewer should agree with:
direct-tcpiploggedorigin_portandorigin_addrtransposed, in all seven copies. Mergingthem corrected it. This was a live bug, not a refactor.
sessionwhere the session handler loggeduid. Both handler guardsalready logged
"session": ctx.SessionID(), so one session now reads under one key. The tree issplit 23/24 on this pre-existing inconsistency; the rest is untouched.
direct-tcpiplogssshidasuser@namespace.device(SSHID) rather thannamespace.device(
Target.Data).DialbecameDialContextper the Goconventions. A cancelled dial's error text reaches the client in the channel rejection.
Also worth a look:
CloseAgentWriteon a seat with no agent channel returnsErrSeatNotFoundinstead of closing. The only caller discards the error, so there is no behavioural difference today,
but it is a deliberate change from the old silent
CloseWrite.Full server suite passes (28 packages),
golangci-lint run ./...reports no issues, andgo mod tidyleaves the tree clean.cloud/does not import either package, so nothing there needs to move.