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.

Code

527 commit(s) on main

Put job execution behind an interface Keenan Nemetz · 2h ago de3eec16
history blame raw 3.4 KiB
1package sshwire
2
3import (
4 "encoding/json"
5 "errors"
6 "fmt"
7 "io"
8 "net"
9)
10
11// stdinChunk is how much of the client's input one frame carries. It is well
12// under MaxFrame so a frame's header costs little on a stream of small writes.
13const stdinChunk = 32 << 10
14
15// Exec sends one request to the service's Unix socket and runs the session it
16// answers. The session's output is written to stdout and its diagnostics to
17// stderr as they arrive, the caller's input is streamed from stdin, and the
18// result is the command's own exit status.
19//
20// An error means no session ran: the socket could not be reached, the service
21// refused the token, or the connection ended without a status. The caller turns
22// that into its own failure; a session that ran and failed is the second return
23// value, not an error.
24func Exec(socket string, hello Hello, stdin io.Reader, stdout, stderr io.Writer) (int, error) {
25 if socket == "" {
26 return 0, errors.New("no front door socket is configured")
27 }
28 conn, err := net.Dial("unix", socket)
29 if err != nil {
30 return 0, fmt.Errorf("cannot reach the forge at %s: %w", socket, err)
31 }
32 defer conn.Close()
33
34 if err := WriteJSON(conn, KindHello, hello); err != nil {
35 return 0, fmt.Errorf("cannot send the request: %w", err)
36 }
37 kind, payload, err := ReadFrame(conn)
38 if err != nil {
39 return 0, fmt.Errorf("the forge did not answer: %w", err)
40 }
41 if kind != KindAccept {
42 return 0, fmt.Errorf("the forge answered with an unexpected %q frame", string(rune(kind)))
43 }
44 var accept Accept
45 if err := json.Unmarshal(payload, &accept); err != nil {
46 return 0, fmt.Errorf("the forge's answer is not readable: %w", err)
47 }
48 if !accept.OK {
49 if accept.Error == "" {
50 accept.Error = "the session was refused"
51 }
52 return 0, errors.New(accept.Error)
53 }
54
55 // Input goes out in the background: the service does not answer until the
56 // command has run, and a command that writes before it reads must not wait for
57 // input that may never come. Closing the connection when Exec returns is what
58 // ends this goroutine if the command exits first.
59 go pumpStdin(conn, stdin)
60
61 for {
62 kind, payload, err := ReadFrame(conn)
63 if err != nil {
64 if errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) {
65 return 0, errors.New("the session ended without an exit status")
66 }
67 return 0, fmt.Errorf("the session was cut short: %w", err)
68 }
69 switch kind {
70 case KindStdout:
71 if _, err := stdout.Write(payload); err != nil {
72 return 0, err
73 }
74 case KindStderr:
75 if _, err := stderr.Write(payload); err != nil {
76 return 0, err
77 }
78 case KindExit:
79 var exit Exit
80 if err := json.Unmarshal(payload, &exit); err != nil {
81 return 0, fmt.Errorf("the exit status is not readable: %w", err)
82 }
83 return exit.Code, nil
84 }
85 // An unknown frame is ignored rather than fatal: this side cannot know
86 // what a newer service might add, and the exit frame still ends the
87 // session.
88 }
89}
90
91// pumpStdin streams the caller's input to the session and then says it ended. A
92// read error is reported as end-of-input: the only thing the service can do with
93// a client whose input failed is finish the command.
94func pumpStdin(w io.Writer, stdin io.Reader) {
95 if stdin == nil {
96 _ = WriteFrame(w, KindStdinEOF, nil)
97 return
98 }
99 buf := make([]byte, stdinChunk)
100 for {
101 n, err := stdin.Read(buf)
102 if n > 0 {
103 if err := WriteFrame(w, KindStdin, buf[:n]); err != nil {
104 return
105 }
106 }
107 if err != nil {
108 _ = WriteFrame(w, KindStdinEOF, nil)
109 return
110 }
111 }
112}
113