-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathgit_test.go
243 lines (205 loc) · 7.57 KB
/
git_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package git
import (
"fmt"
"net"
"os/exec"
"path/filepath"
"runtime"
"sync"
"testing"
"github.com/charmbracelet/keygen"
"github.com/charmbracelet/ssh"
"github.com/charmbracelet/wish"
)
func TestGitMiddleware(t *testing.T) {
pubkey, pkPath := createKeyPair(t)
hkPath := filepath.Join(t.TempDir(), "id_ed25519")
l, err := net.Listen("tcp", "127.0.0.1:0")
requireNoError(t, err)
remote := "ssh://" + l.Addr().String()
repoDir := t.TempDir()
hooks := &testHooks{
pushes: []action{},
fetches: []action{},
access: []accessDetails{
{pubkey, "repo1", AdminAccess},
{pubkey, "repo2", AdminAccess},
{pubkey, "repo3", AdminAccess},
{pubkey, "repo4", AdminAccess},
{pubkey, "repo5", NoAccess},
{pubkey, "repo6", ReadOnlyAccess},
{pubkey, "repo7", AdminAccess},
{pubkey, "abc/repo1", AdminAccess},
{pubkey, "abc/def/repo1", AdminAccess},
},
}
srv, err := wish.NewServer(
wish.WithHostKeyPath(hkPath),
wish.WithMiddleware(Middleware(repoDir, hooks)),
wish.WithPublicKeyAuth(func(ctx ssh.Context, key ssh.PublicKey) bool {
return true
}),
)
requireNoError(t, err)
go func() { srv.Serve(l) }()
t.Cleanup(func() { _ = srv.Close() })
t.Run("create repo on master", func(t *testing.T) {
cwd := t.TempDir()
requireNoError(t, runGitHelper(t, pkPath, cwd, "init", "-b", "master"))
requireNoError(t, runGitHelper(t, pkPath, cwd, "remote", "add", "origin", remote+"/repo1"))
requireNoError(t, runGitHelper(t, pkPath, cwd, "commit", "--allow-empty", "-m", "initial commit"))
requireNoError(t, runGitHelper(t, pkPath, cwd, "push", "origin", "master"))
requireHasAction(t, hooks.pushes, pubkey, "repo1")
})
t.Run("create repo on main", func(t *testing.T) {
cwd := t.TempDir()
requireNoError(t, runGitHelper(t, pkPath, cwd, "init", "-b", "main"))
requireNoError(t, runGitHelper(t, pkPath, cwd, "remote", "add", "origin", remote+"/repo2"))
requireNoError(t, runGitHelper(t, pkPath, cwd, "commit", "--allow-empty", "-m", "initial commit"))
requireNoError(t, runGitHelper(t, pkPath, cwd, "push", "origin", "main"))
requireHasAction(t, hooks.pushes, pubkey, "repo2")
})
t.Run("create repo in subdir", func(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("permission issues")
}
cwd := t.TempDir()
requireNoError(t, runGitHelper(t, pkPath, cwd, "init", "-b", "main"))
requireNoError(t, runGitHelper(t, pkPath, cwd, "remote", "add", "origin", remote+"/abc/repo1"))
requireNoError(t, runGitHelper(t, pkPath, cwd, "commit", "--allow-empty", "-m", "initial commit"))
requireNoError(t, runGitHelper(t, pkPath, cwd, "push", "origin", "main"))
requireHasAction(t, hooks.pushes, pubkey, "abc/repo1")
})
t.Run("create wrong repo", func(t *testing.T) {
cwd := t.TempDir()
requireNoError(t, runGitHelper(t, pkPath, cwd, "init", "-b", "main"))
requireNoError(t, runGitHelper(t, pkPath, cwd, "remote", "add", "origin", remote+"//../../repo1"))
requireNoError(t, runGitHelper(t, pkPath, cwd, "commit", "--allow-empty", "-m", "initial commit"))
requireError(t, runGitHelper(t, pkPath, cwd, "push", "origin", "main"))
})
t.Run("create wrong repo in subdir", func(t *testing.T) {
cwd := t.TempDir()
requireNoError(t, runGitHelper(t, pkPath, cwd, "init", "-b", "main"))
requireNoError(t, runGitHelper(t, pkPath, cwd, "remote", "add", "origin", remote+"/abc/def/repo1"))
requireNoError(t, runGitHelper(t, pkPath, cwd, "commit", "--allow-empty", "-m", "initial commit"))
requireError(t, runGitHelper(t, pkPath, cwd, "push", "origin", "main"))
})
t.Run("create and clone repo", func(t *testing.T) {
cwd := t.TempDir()
requireNoError(t, runGitHelper(t, pkPath, cwd, "init", "-b", "main"))
requireNoError(t, runGitHelper(t, pkPath, cwd, "remote", "add", "origin", remote+"/repo3"))
requireNoError(t, runGitHelper(t, pkPath, cwd, "commit", "--allow-empty", "-m", "initial commit"))
requireNoError(t, runGitHelper(t, pkPath, cwd, "push", "origin", "main"))
cwd = t.TempDir()
requireNoError(t, runGitHelper(t, pkPath, cwd, "clone", remote+"/repo3"))
requireHasAction(t, hooks.pushes, pubkey, "repo3")
requireHasAction(t, hooks.fetches, pubkey, "repo3")
})
t.Run("clone repo that doesn't exist", func(t *testing.T) {
cwd := t.TempDir()
requireError(t, runGitHelper(t, pkPath, cwd, "clone", remote+"/repo4"))
})
t.Run("clone repo with no access", func(t *testing.T) {
cwd := t.TempDir()
requireError(t, runGitHelper(t, pkPath, cwd, "clone", remote+"/repo5"))
})
t.Run("push repo with with readonly", func(t *testing.T) {
cwd := t.TempDir()
requireNoError(t, runGitHelper(t, pkPath, cwd, "init", "-b", "main"))
requireNoError(t, runGitHelper(t, pkPath, cwd, "remote", "add", "origin", remote+"/repo6"))
requireNoError(t, runGitHelper(t, pkPath, cwd, "commit", "--allow-empty", "-m", "initial commit"))
requireError(t, runGitHelper(t, pkPath, cwd, "push", "origin", "main"))
})
t.Run("create and clone repo on weird branch", func(t *testing.T) {
cwd := t.TempDir()
requireNoError(t, runGitHelper(t, pkPath, cwd, "init", "-b", "a-weird-branch-name"))
requireNoError(t, runGitHelper(t, pkPath, cwd, "remote", "add", "origin", remote+"/repo7"))
requireNoError(t, runGitHelper(t, pkPath, cwd, "commit", "--allow-empty", "-m", "initial commit"))
requireNoError(t, runGitHelper(t, pkPath, cwd, "push", "origin", "a-weird-branch-name"))
cwd = t.TempDir()
requireNoError(t, runGitHelper(t, pkPath, cwd, "clone", remote+"/repo7"))
requireHasAction(t, hooks.pushes, pubkey, "repo7")
requireHasAction(t, hooks.fetches, pubkey, "repo7")
})
}
func runGitHelper(t *testing.T, pk, cwd string, args ...string) error {
t.Helper()
allArgs := []string{
"-c", "user.name='wish'",
"-c", "user.email='test@wish'",
"-c", "commit.gpgSign=false",
"-c", "tag.gpgSign=false",
"-c", "log.showSignature=false",
"-c", "ssh.variant=ssh",
}
allArgs = append(allArgs, args...)
cmd := exec.Command("git", allArgs...)
cmd.Dir = cwd
cmd.Env = []string{fmt.Sprintf(`GIT_SSH_COMMAND=ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i "%s" -F /dev/null`, pk)}
out, err := cmd.CombinedOutput()
if err != nil {
t.Log("git out:", string(out))
}
return err
}
func requireNoError(t *testing.T, err error) {
t.Helper()
if err != nil {
t.Fatalf("expected no error, got %q", err.Error())
}
}
func requireError(t *testing.T, err error) {
t.Helper()
if err == nil {
t.Fatalf("expected an error, got nil")
}
}
func requireHasAction(t *testing.T, actions []action, key ssh.PublicKey, repo string) {
t.Helper()
for _, action := range actions {
if repo == action.repo && ssh.KeysEqual(key, action.key) {
return
}
}
t.Fatalf("expected action for %q, got none", repo)
}
func createKeyPair(t *testing.T) (ssh.PublicKey, string) {
t.Helper()
pk := filepath.Join(t.TempDir(), "id_ed25519")
kp, err := keygen.New(pk, keygen.WithKeyType(keygen.Ed25519), keygen.WithWrite())
requireNoError(t, err)
return kp.PublicKey(), pk
}
type accessDetails struct {
key ssh.PublicKey
repo string
level AccessLevel
}
type action struct {
key ssh.PublicKey
repo string
}
type testHooks struct {
sync.Mutex
pushes []action
fetches []action
access []accessDetails
}
func (h *testHooks) AuthRepo(repo string, key ssh.PublicKey) AccessLevel {
for _, dets := range h.access {
if dets.repo == repo && ssh.KeysEqual(key, dets.key) {
return dets.level
}
}
return NoAccess
}
func (h *testHooks) Push(repo string, key ssh.PublicKey) {
h.Lock()
defer h.Unlock()
h.pushes = append(h.pushes, action{key, repo})
}
func (h *testHooks) Fetch(repo string, key ssh.PublicKey) {
h.Lock()
defer h.Unlock()
h.fetches = append(h.fetches, action{key, repo})
}