Debian Codegraph @

t/cmd/lfs-ssh-echo.go

raw symbols SCIP
1 //go:build testtools
2 // +build testtools
3
4 package main
5
6 import (
7 "encoding/json"
8 "fmt"
9 "os"
10 "os/exec"
11 "runtime"
12 "strings"
13 "syscall"
14 "time"
15 )
16
17 type sshResponse struct {
18 Href string `json:"href"`
19 Header map[string]string `json:"header"`
20 ExpiresAt time.Time `json:"expires_at,omitempty"`
21 ExpiresIn int `json:"expires_in,omitempty"`
22 }
23
24 func shell() string {
25 if runtime.GOOS == "windows" {
26 return "bash"
27 }
28 return "sh"
29 }
30
31 func spawnCommand(command string) error {
32 cmd := exec.Command(shell(), "-c", command)
33 cmd.Stdin = os.Stdin
34 cmd.Stdout = os.Stdout
35 cmd.Stderr = os.Stderr
36 err := cmd.Run()
37 if e, ok := err.(*exec.ExitError); ok {
38 var ws syscall.WaitStatus
39 ws, ok = e.ProcessState.Sys().(syscall.WaitStatus)
40 if ok {
41 os.Exit(ws.ExitStatus())
42 }
43 }
44 return err
45 }
46
47 func checkSufficientArgs(offset int) {
48 if len(os.Args) < offset+2 {
49 fmt.Fprintf(os.Stderr, "got %d args: %v", len(os.Args), os.Args)
50 os.Exit(1)
51 }
52 }
53
54 func main() {
55 // expect args:
56 // lfs-ssh-echo [-p PORT [--]] [email protected][.invalid] "git-lfs-authenticate REPO OPERATION"
57 // lfs-ssh-echo [-p PORT [--]] [email protected][.invalid] "git-lfs-transfer REPO OPERATION"
58 // lfs-ssh-echo [email protected] "git-upload-pack REPO"
59 // lfs-ssh-echo [email protected] "git-receive-pack REPO"
60 offset := 1
61
62 checkSufficientArgs(offset)
63 if masterArg, found := strings.CutPrefix(os.Args[offset], "-oControlMaster="); found {
64 var master bool
65 switch masterArg {
66 case "yes":
67 master = true
68 case "no":
69 master = false
70 default:
71 fmt.Fprintf(os.Stderr, "expected \"-oControlMaster=yes\" or \"-oControlMaster=no\", got %q", os.Args[offset])
72 os.Exit(1)
73 }
74 if pathArg, found := strings.CutPrefix(os.Args[offset+1], "-oControlPath="); found {
75 if master {
76 if file, err := os.OpenFile(pathArg, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0666); err != nil {
77 if os.IsExist(err) {
78 fmt.Fprintf(os.Stderr, "expected %q to not exist", pathArg)
79 } else {
80 fmt.Fprintf(os.Stderr, "unable to create %q: %s", pathArg, err)
81 }
82 os.Exit(1)
83 } else {
84 file.Close()
85 defer os.Remove(pathArg)
86 }
87 } else {
88 if file, err := os.OpenFile(pathArg, os.O_RDONLY, 0); err != nil {
89 if os.IsNotExist(err) {
90 fmt.Fprintf(os.Stderr, "expected %q to exist", pathArg)
91 } else {
92 fmt.Fprintf(os.Stderr, "unable to open %q: %s", pathArg, err)
93 }
94 os.Exit(1)
95 } else {
96 file.Close()
97 }
98 }
99 } else {
100 fmt.Fprintf(os.Stderr, "expected \"-oControlPath\"")
101 os.Exit(1)
102 }
103 offset += 2
104 }
105
106 checkSufficientArgs(offset)
107 if os.Args[offset] == "-p" {
108 offset += 2
109 }
110
111 checkSufficientArgs(offset)
112 if os.Args[offset] == "--" {
113 offset += 1
114 }
115
116 checkSufficientArgs(offset)
117 if os.Args[offset] != "[email protected]" && os.Args[offset] != "[email protected]" {
118 fmt.Fprintf(os.Stderr, "expected \"[email protected]\" or \"[email protected]\", got %q", os.Args[offset])
119 os.Exit(1)
120 }
121
122 // just "git-lfs-(authenticate|transfer) REPO OPERATION" or "git-(upload|receive)-pack REPO"
123 remoteCmd := strings.Split(os.Args[offset+1], " ")
124 if len(remoteCmd) < 2 {
125 fmt.Fprintf(os.Stderr, "bad command line: %s\nargs: %v", remoteCmd, os.Args)
126 os.Exit(1)
127 }
128
129 if remoteCmd[0] == "git-lfs-transfer" || remoteCmd[0] == "git-upload-pack" || remoteCmd[0] == "git-receive-pack" {
130 err := spawnCommand(os.Args[offset+1])
131 if err != nil {
132 fmt.Fprintf(os.Stderr, "error running command %q: %v", remoteCmd[0], err)
133 os.Exit(1)
134 }
135 return
136 }
137
138 repo := remoteCmd[1]
139
140 switch repo {
141 case "/ssh-unavailable":
142 os.Exit(127)
143 case "/ssh-unavailable-message":
144 // Simulate the error message returned by a third-party utility
145 // which does not support the "git-lfs-authenticate" command.
146 fmt.Fprintf(os.Stderr, "fatal: Gerrit Code Review: git-lfs-authenticate: not found")
147 os.Exit(1)
148 }
149
150 r := &sshResponse{
151 Href: fmt.Sprintf("http://127.0.0.1:%s/%s.git/info/lfs", os.Args[2], repo),
152 }
153 switch repo {
154 case "/ssh-expired-absolute":
155 r.ExpiresAt = time.Now().Add(-5 * time.Minute)
156 case "/ssh-expired-relative":
157 r.ExpiresIn = -5
158 case "/ssh-expired-both":
159 r.ExpiresAt = time.Now().Add(-5 * time.Minute)
160 r.ExpiresIn = -5
161 }
162
163 json.NewEncoder(os.Stdout).Encode(r)
164 }