-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathserver_outbound_clients_test.go
More file actions
371 lines (315 loc) · 9.67 KB
/
Copy pathserver_outbound_clients_test.go
File metadata and controls
371 lines (315 loc) · 9.67 KB
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package waved
import (
"context"
"encoding/hex"
"net"
"net/http"
"net/http/httptest"
"path/filepath"
"sync"
"testing"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/schnorr"
"github.com/lightninglabs/wavelength/arkrpc"
mailboxpb "github.com/lightninglabs/wavelength/mailbox/pb"
"github.com/lightninglabs/wavelength/rpcauth"
"github.com/lightninglabs/wavelength/serverconn"
"github.com/lightningnetwork/lnd/keychain"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"google.golang.org/protobuf/encoding/protojson"
"gopkg.in/macaroon-bakery.v2/bakery"
)
// TestConnectOperatorClientsREST verifies the daemon can construct all
// operator-facing clients over the grpc-gateway transport.
func TestConnectOperatorClientsREST(t *testing.T) {
t.Parallel()
operatorKey, err := btcec.NewPrivateKey()
require.NoError(t, err)
operatorPubKey := operatorKey.PubKey().SerializeCompressed()
tempDir := t.TempDir()
macaroonPath := filepath.Join(tempDir, "operator.macaroon")
newTestMacaroonService(
t, macaroonPath, "lumosd",
map[string][]bakery.Op{
"/arkrpc.ArkService/GetInfo": {{
Entity: "lumosd",
Action: "client",
}},
},
)
macHex, err := rpcauth.HexFromFile(macaroonPath)
require.NoError(t, err)
// Record the mailbox auth header the REST edge sent, guarded because
// the handler runs on the test server's goroutine.
var (
authSigMu sync.Mutex
gotAuthSig string
)
pulledAuthSig := func() string {
authSigMu.Lock()
defer authSigMu.Unlock()
return gotAuthSig
}
server := httptest.NewServer(
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
require.Equal(
t, macHex, r.Header.Get(
rpcauth.MacaroonMetadataKey,
),
)
w.Header().Set(
"Content-Type", "application/json",
)
var (
msg []byte
marshalErr error
)
switch r.URL.Path {
case "/v1/ark/get-info":
msg, marshalErr = protojson.Marshal(
&arkrpc.GetInfoResponse{
Pubkey: operatorPubKey,
},
)
case "/v1/mailbox/pull":
authSigMu.Lock()
gotAuthSig = r.Header.Get(
serverconn.AuthHeaderKey,
)
authSigMu.Unlock()
msg, marshalErr = protojson.Marshal(
&mailboxpb.PullResponse{},
)
default:
http.NotFound(w, r)
return
}
require.NoError(t, marshalErr)
_, err = w.Write(msg)
require.NoError(t, err)
},
),
)
defer server.Close()
// The mailbox edge signs every call with the daemon identity key, so
// the fixture needs one. Priming the memo stands in for the wallet
// backend that would produce the signature in a running daemon.
clientKey, err := btcec.NewPrivateKey()
require.NoError(t, err)
clientMailbox := serverconn.CompoundMailboxID(
serverconn.PubKeyMailboxID(
operatorKey.PubKey(),
),
serverconn.PubKeyMailboxID(
clientKey.PubKey(),
),
)
authSig, err := serverconn.SignMailboxAuth(clientKey, clientMailbox)
require.NoError(t, err)
s := &Server{
cfg: &Config{
Server: &ServerConfig{
Host: server.URL,
Transport: RPCTransportREST,
Insecure: true,
MacaroonPath: macaroonPath,
},
},
clientKeyDesc: keychain.KeyDescriptor{
PubKey: clientKey.PubKey(),
},
mailboxAuthSigs: map[string]*schnorr.Signature{
clientMailbox: authSig,
},
}
clients, err := s.connectOperatorClients()
require.NoError(t, err)
require.NotNil(t, clients.ark)
require.NotNil(t, clients.mailbox)
require.NoError(t, clients.cleanup())
info, err := clients.ark.GetInfo(
t.Context(), &arkrpc.GetInfoRequest{},
)
require.NoError(t, err)
require.Equal(t, operatorPubKey, info.Pubkey)
_, err = clients.mailbox.Pull(
t.Context(), &mailboxpb.PullRequest{
MailboxId: clientMailbox,
},
)
require.NoError(t, err)
// The operator authorizes Pull from this header when it has no client
// certificate to bind the caller to a mailbox, so the REST edge has to
// carry it rather than only the gRPC one.
require.Equal(
t,
hex.EncodeToString(
authSig.Serialize(),
),
pulledAuthSig(),
)
}
// mailboxAuthCapture is a stub MailboxService that records the mailbox auth
// metadata each inbound RPC carried, standing in for the operator's mailbox
// edge. The mutex is load bearing: gRPC serves the handler on its own
// goroutine, so the assertion below reads what the handler wrote.
type mailboxAuthCapture struct {
mailboxpb.UnimplementedMailboxServiceServer
mu sync.Mutex
authSig string
}
// Pull records the x-mailbox-auth-sig header the caller sent.
func (m *mailboxAuthCapture) Pull(ctx context.Context,
_ *mailboxpb.PullRequest) (*mailboxpb.PullResponse, error) {
md, _ := metadata.FromIncomingContext(ctx)
m.mu.Lock()
defer m.mu.Unlock()
if values := md.Get(serverconn.AuthHeaderKey); len(values) > 0 {
m.authSig = values[0]
}
return &mailboxpb.PullResponse{}, nil
}
// pulledAuthSig returns the header recorded by the last Pull.
func (m *mailboxAuthCapture) pulledAuthSig() string {
m.mu.Lock()
defer m.mu.Unlock()
return m.authSig
}
// TestConnectOperatorClientsGRPC verifies the gRPC mailbox edge stamps
// x-mailbox-auth-sig on outbound RPCs, the same property the REST test asserts
// for the gateway transport. An operator that terminates TLS at a proxy has no
// client certificate to authorize Pull with, so the header is the only thing
// standing between this daemon and a rejected mailbox RPC.
func TestConnectOperatorClientsGRPC(t *testing.T) {
t.Parallel()
listener, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
capture := &mailboxAuthCapture{}
operator := grpc.NewServer()
mailboxpb.RegisterMailboxServiceServer(operator, capture)
go func() {
_ = operator.Serve(listener)
}()
t.Cleanup(operator.Stop)
// The mailbox edge signs every call with the daemon identity key, so
// the fixture needs one. Priming the memo stands in for the wallet
// backend that would produce the signature in a running daemon.
operatorKey, err := btcec.NewPrivateKey()
require.NoError(t, err)
clientKey, err := btcec.NewPrivateKey()
require.NoError(t, err)
clientMailbox := serverconn.CompoundMailboxID(
serverconn.PubKeyMailboxID(
operatorKey.PubKey(),
),
serverconn.PubKeyMailboxID(
clientKey.PubKey(),
),
)
authSig, err := serverconn.SignMailboxAuth(clientKey, clientMailbox)
require.NoError(t, err)
s := &Server{
cfg: &Config{
Server: &ServerConfig{
Host: listener.Addr().String(),
Transport: RPCTransportGRPC,
Insecure: true,
},
},
clientKeyDesc: keychain.KeyDescriptor{
PubKey: clientKey.PubKey(),
},
mailboxAuthSigs: map[string]*schnorr.Signature{
clientMailbox: authSig,
},
}
clients, err := s.connectOperatorClients()
require.NoError(t, err)
require.NotNil(t, clients.ark)
require.NotNil(t, clients.mailbox)
t.Cleanup(func() {
require.NoError(t, clients.cleanup())
})
_, err = clients.mailbox.Pull(
t.Context(), &mailboxpb.PullRequest{
MailboxId: clientMailbox,
},
)
require.NoError(t, err)
require.Equal(
t,
hex.EncodeToString(
authSig.Serialize(),
),
capture.pulledAuthSig(),
)
}
// TestConnectOperatorClientsUnknownTransport rejects typoed config early.
func TestConnectOperatorClientsUnknownTransport(t *testing.T) {
t.Parallel()
s := &Server{
cfg: &Config{
Server: &ServerConfig{
Host: "localhost:10010",
Transport: "webdav",
},
},
}
_, err := s.connectOperatorClients()
require.ErrorContains(t, err, "unknown server transport")
}
// TestMailboxAuthSigMemo covers the memo behaviour the two wiring tests above
// deliberately skip past. Both of those prime the map, so a memo hit is all
// they ever exercise; this drives the miss as well, which is the path every
// first Pull takes on a live daemon.
//
// There is no seam to inject a signer — signTaggedSchnorr dispatches on the
// concrete wallet backends — so a miss is observed through the error a Server
// with no backend returns. That is enough to prove which recipients hit and
// which miss, which is the property the compound-vs-plain split rests on.
func TestMailboxAuthSigMemo(t *testing.T) {
t.Parallel()
clientKey, err := btcec.NewPrivateKey()
require.NoError(t, err)
const (
plain = "02aa"
compound = "02bb:02aa"
)
primed, err := serverconn.SignMailboxAuth(clientKey, plain)
require.NoError(t, err)
s := &Server{
clientKeyDesc: keychain.KeyDescriptor{
PubKey: clientKey.PubKey(),
},
mailboxAuthSigs: map[string]*schnorr.Signature{
plain: primed,
},
}
// The primed recipient is served from memory. No wallet backend is
// configured, so reaching the signing path at all would error.
got, err := s.mailboxAuthSig(t.Context(), plain)
require.NoError(t, err)
require.Same(t, primed, got)
// A different recipient is a distinct digest and therefore a distinct
// entry: it misses and goes to the wallet rather than being answered
// with the first recipient's signature. Serving the wrong signature
// here would authorize the wrong mailbox at the operator.
_, err = s.mailboxAuthSig(t.Context(), compound)
require.ErrorContains(t, err, "no wallet backend available")
// A failed sign caches nothing. Caching it would pin the failure for
// the life of the process, so every later RPC to that mailbox would
// fail without ever retrying the wallet.
require.Len(t, s.mailboxAuthSigs, 1)
require.NotContains(t, s.mailboxAuthSigs, compound)
// The miss must not have disturbed the entry that was already there.
got, err = s.mailboxAuthSig(t.Context(), plain)
require.NoError(t, err)
require.Same(t, primed, got)
// An unset identity key is refused before any of that.
empty := &Server{}
_, err = empty.mailboxAuthSig(t.Context(), plain)
require.ErrorContains(t, err, "identity key not yet derived")
}