Sign in

Search issue titles and bodies, change requests and comments. Private repositories you cannot read are not included.

vole
Site
Clone

Read only, no credentials:

https://rodent.software/vole.git

Write over SSH with a registered key:

git clone ssh://git@rodent.software/vole.git

Self-hosted forge that lives in your Git repo.

Blame · server/ssh/session.go

Line attribution on main · 67 line(s)

5ff8dfc9
Keenan Nemetz
16d ago
1 package ssh
2
3 // A session is the transport a verb runs on.
4 //
5 // Behind the exec verb are git, the admin API, secrets, runs, channels, LFS and
6 // the runner lease. They all need the same three things from the connection:
7 // stdin, stdout, a separate error stream, and two control messages — confirm the
8 // exec request, and report an exit status. None of that is specific to SSH.
9 //
10 // So the handlers take this interface rather than *xssh.Channel. sshSession is
11 // the only implementation that knows what an SSH channel is, which is what keeps
12 // a second transport from becoming a second copy of the access rules.
13
14 import (
15 "io"
16
17 xssh "golang.org/x/crypto/ssh"
18 )
19
20 // Session is one exec session: the streams a verb reads and writes, and the
21 // control messages it sends back.
22 type Session interface {
23 io.Reader
24 io.Writer
25 // Stderr is the error stream, kept apart from stdout so a verb's output can be
26 // redirected or piped without its diagnostics mixed in.
27 Stderr() io.Writer
28 // Accept confirms the exec request. A session built only to report a refusal
29 // has no request to answer and returns nil.
30 Accept() error
31 // CloseWrite ends the output stream while leaving the input open, which is how
32 // a client is told the command has finished writing.
33 CloseWrite() error
34 // Exit reports the command's exit status.
35 Exit(code int)
36 // Close ends the session.
37 Close() error
38 }
39
40 // sshSession is the SSH transport: one session channel, and the exec request
41 // that opened it when there was one.
42 type sshSession struct {
43 ch xssh.Channel
44 req *xssh.Request
45 }
46
47 func (s *sshSession) Read(p []byte) (int, error) { return s.ch.Read(p) }
48 func (s *sshSession) Write(p []byte) (int, error) { return s.ch.Write(p) }
49 func (s *sshSession) Stderr() io.Writer { return s.ch.Stderr() }
50 func (s *sshSession) CloseWrite() error { return s.ch.CloseWrite() }
51 func (s *sshSession) Close() error { return s.ch.Close() }
52
53 // Accept answers the exec request. sshd's client is the only party that reads
54 // the answer; a transport without a request does not have one to send.
55 func (s *sshSession) Accept() error {
56 if s.req == nil {
57 return nil
58 }
59 return s.req.Reply(true, nil)
60 }
61
62 // Exit sends the status an ssh client turns into its own exit code. It is
63 // best-effort: the channel may already be gone, and a caller reporting an exit
64 // status has nothing better to do about that than return.
65 func (s *sshSession) Exit(code int) {
66 s.ch.SendRequest("exit-status", false, xssh.Marshal(struct{ Status uint32 }{uint32(code)}))
67 }