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
|
package gerrit
import (
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/subtle"
"fmt"
"net"
"testing"
"time"
"code.tvl.fyi/fun/clbot/gerrit/gerritevents"
log "github.com/golang/glog"
"github.com/google/go-cmp/cmp"
"golang.org/x/crypto/ssh"
)
var (
sshServerSigner, sshServerPublicKey = mustNewKey()
sshClientSigner, sshClientPublicKey = mustNewKey()
)
func mustNewKey() (ssh.Signer, ssh.PublicKey) {
key, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
if err != nil {
panic(err)
}
signer, err := ssh.NewSignerFromKey(key)
if err != nil {
panic(err)
}
publicKey, err := ssh.NewPublicKey(key.Public())
if err != nil {
panic(err)
}
return signer, publicKey
}
func newSSHServer(lines string) (addr string, cleanup func(), err error) {
config := &ssh.ServerConfig{
PublicKeyCallback: func(c ssh.ConnMetadata, pubKey ssh.PublicKey) (*ssh.Permissions, error) {
pkBytes := pubKey.Marshal()
wantPKBytes := sshClientPublicKey.Marshal()
if subtle.ConstantTimeCompare(pkBytes, wantPKBytes) == 0 {
return nil, fmt.Errorf("unauthorized")
}
return &ssh.Permissions{}, nil
},
}
config.AddHostKey(sshServerSigner)
ln, err := net.Listen("tcp", ":0")
if err != nil {
log.Fatalf("Listen on tcp/:0: %v", err)
}
handle := func(conn net.Conn) {
defer conn.Close()
sc, newchch, newreqch, err := ssh.NewServerConn(conn, config)
if err != nil {
log.Fatalf("NewServerConn: %v", err)
}
go ssh.DiscardRequests(newreqch)
for newCh := range newchch {
if newCh.ChannelType() != "session" {
newCh.Reject(ssh.UnknownChannelType, "unknown channel type")
continue
}
channel, reqs, err := newCh.Accept()
if err != nil {
log.Fatalf("Could not accept channel: %v", err)
}
go func(in <-chan *ssh.Request) {
for req := range in {
req.Reply(req.Type == "exec", nil)
}
}(reqs)
channel.Write([]byte(lines))
sc.SendRequest("goaway", false, nil)
}
}
go func() {
for {
conn, err := ln.Accept()
if err != nil {
return
}
go handle(conn)
}
}()
cleanup = func() {
ln.Close()
}
return ln.Addr().String(), cleanup, err
}
func ts(s string) gerritevents.Time {
t, err := time.Parse("2006-01-02 15:04:05 -0700 MST", s)
if err != nil {
panic(err)
}
return gerritevents.Time{t}
}
func optStr(s string) *string { return &s }
func TestWatcher(t *testing.T) {
tcs := []struct {
name string
lines string
want []gerritevents.Event
}{{
name: "no events",
}, {
name: "single test event",
lines: `{"author":{"name":"tazjin","email":"mail@tazj.in","username":"tazjin"},"approvals":[{"type":"Code-Review","description":"Code-Review","value":"2","oldValue":"0"}],"comment":"Patch Set 3: Code-Review+2","patchSet":{"number":3,"revision":"6fe272d3f82c6efdfe1167fab98bf918efc03fe5","parents":["d984b6018cf68c7e8b7169b475d90134fbcee767"],"ref":"refs/changes/44/244/3","uploader":{"name":"tazjin","email":"mail@tazj.in","username":"tazjin"},"createdOn":1592081910,"author":{"name":"tazjin","email":"mail@tazj.in","username":"tazjin"},"kind":"REWORK","sizeInsertions":83,"sizeDeletions":-156},"change":{"project":"depot","branch":"master","id":"I546c701145fa204b7ba7518a8a56a783588629e0","number":244,"subject":"refactor(ops/nixos): Move my NixOS configurations to //users/tazjin","owner":{"name":"tazjin","email":"mail@tazj.in","username":"tazjin"},"url":"https://cl.tvl.fyi/c/depot/+/244","commitMessage":"refactor(ops/nixos): Move my NixOS configurations to //users/tazjin\n\nNixOS modules move one level up because it\u0027s unlikely that //ops/nixos\nwill contain actual systems at this point (they\u0027re user-specific).\n\nThis is the first users folder, so it is also added to the root\nreadTree invocation for the repository.\n\nChange-Id: I546c701145fa204b7ba7518a8a56a783588629e0\n","createdOn":1592081577,"status":"NEW"},"project":"depot","refName":"refs/heads/master","changeKey":{"id":"I546c701145fa204b7ba7518a8a56a783588629e0"},"type":"comment-added","eventCreatedOn":1592081929}
`,
want: []gerritevents.Event{
&gerritevents.CommentAdded{
Type: "comment-added",
Change: gerritevents.Change{
Project: "depot",
Branch: "master",
ID: "I546c701145fa204b7ba7518a8a56a783588629e0",
Number: 244,
Subject: "refactor(ops/nixos): Move my NixOS configurations to //users/tazjin",
Owner: gerritevents.Account{Name: "tazjin", Email: "mail@tazj.in", Username: "tazjin"},
URL: "https://cl.tvl.fyi/c/depot/+/244",
CommitMessage: "refactor(ops/nixos): Move my NixOS configurations to //users/tazjin\n\nNixOS modules move one level up because it's unlikely that //ops/nixos\nwill contain actual systems at this point (they're user-specific).\n\nThis is the first users folder, so it is also added to the root\nreadTree invocation for the repository.\n\nChange-Id: I546c701145fa204b7ba7518a8a56a783588629e0\n",
CreatedOn: ts("2020-06-13 21:52:57 +0100 BST"),
Status: "NEW",
},
PatchSet: gerritevents.PatchSet{
Number: 3,
Revision: "6fe272d3f82c6efdfe1167fab98bf918efc03fe5",
Parents: []string{"d984b6018cf68c7e8b7169b475d90134fbcee767"},
Ref: "refs/changes/44/244/3",
Uploader: gerritevents.Account{Name: "tazjin", Email: "mail@tazj.in", Username: "tazjin"},
Author: gerritevents.Account{Name: "tazjin", Email: "mail@tazj.in", Username: "tazjin"},
CreatedOn: ts("2020-06-13 21:58:30 +0100 BST"),
Kind: "REWORK",
SizeInsertions: 83,
SizeDeletions: -156,
},
Author: gerritevents.Account{Name: "tazjin", Email: "mail@tazj.in", Username: "tazjin"},
Approvals: []gerritevents.Approval{{Type: "Code-Review", Description: "Code-Review", Value: "2", OldValue: optStr("0")}},
Comment: "Patch Set 3: Code-Review+2",
EventCreatedOn: ts("2020-06-13 21:58:49 +0100 BST"),
},
},
}}
for _, tc := range tcs {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
serverAddr, cleanup, err := newSSHServer(tc.lines)
if err != nil {
t.Fatalf("newSSHServer: %v", err)
}
t.Cleanup(cleanup)
config := &ssh.ClientConfig{
User: "bert",
Auth: []ssh.AuthMethod{ssh.PublicKeys(sshClientSigner)},
HostKeyCallback: ssh.FixedHostKey(sshServerPublicKey),
Timeout: 10 * time.Millisecond,
}
w, err := New(ctx, "tcp", serverAddr, config)
if err != nil {
t.Fatalf("New: %v", err)
}
var gotEvents []gerritevents.Event
for ev := range w.Events() {
gotEvents = append(gotEvents, ev)
}
if diff := cmp.Diff(gotEvents, tc.want); diff != "" {
t.Errorf("got events != want events: diff:\n%v", diff)
}
})
}
}
|