refactor(server): narrow the SSH session seam and type its connection state - #6938
Open
otavio wants to merge 1 commit into
Open
refactor(server): narrow the SSH session seam and type its connection state#6938otavio wants to merge 1 commit into
otavio wants to merge 1 commit into
Conversation
… 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
Code Review CompleteThe automated review ran but did not post an updated summary — this usually means no new issues were found since the previous review. If you've pushed changes and want a fresh pass, comment |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.

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.