-
Notifications
You must be signed in to change notification settings - Fork 14
/
mino.go
620 lines (515 loc) · 15.3 KB
/
mino.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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
package fake
import (
"bytes"
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/tls"
"crypto/x509"
"encoding/binary"
"fmt"
"io"
"math/big"
"net"
"testing"
"time"
"github.com/stretchr/testify/require"
"go.dedis.ch/dela/crypto"
"go.dedis.ch/dela/mino"
"go.dedis.ch/dela/serde"
)
// Address is a fake implementation of an address.
//
// - implements mino.Address
type Address struct {
mino.Address
index int
err error
}
// NewAddress returns a fake address with the given index.
func NewAddress(index int) Address {
return Address{index: index}
}
// NewBadAddress returns a fake address that returns an error when appropriate.
func NewBadAddress() Address {
return Address{err: fakeErr}
}
// Equal implements mino.Address.
func (a Address) Equal(o mino.Address) bool {
other, ok := o.(Address)
return ok && other.index == a.index
}
// MarshalText implements encoding.TextMarshaler.
func (a Address) MarshalText() ([]byte, error) {
buffer := make([]byte, 4)
binary.LittleEndian.PutUint32(buffer, uint32(a.index))
return buffer, a.err
}
// String implements fmt.Stringer.
func (a Address) String() string {
return fmt.Sprintf("fake.Address[%d]", a.index)
}
// ConnectionType always returns ACTgRPCS
func (a Address) ConnectionType() mino.AddressConnectionType {
return mino.ACTgRPC
}
// AddressFactory is a fake implementation of an address factory.
//
// - implements mino.AddressFactory
type AddressFactory struct {
mino.AddressFactory
}
// FromText implements mino.AddressFactory.
func (f AddressFactory) FromText(text []byte) mino.Address {
if len(text) >= 4 {
index := binary.LittleEndian.Uint32(text)
return Address{index: int(index)}
}
return Address{}
}
// AddressIterator is a fake implementation an address iterator.
//
// - implements mino.AddressIterator
type AddressIterator struct {
mino.AddressIterator
addrs []mino.Address
index int
}
// NewAddressIterator returns a new address iterator
func NewAddressIterator(addrs []mino.Address) *AddressIterator {
return &AddressIterator{
addrs: addrs,
}
}
// Seek implements mino.AddressIterator.
func (i *AddressIterator) Seek(index int) {
i.index = index
}
// HasNext implements mino.AddressIterator.
func (i *AddressIterator) HasNext() bool {
return i.index < len(i.addrs)
}
// GetNext implements mino.AddressIterator.
func (i *AddressIterator) GetNext() mino.Address {
res := i.addrs[i.index]
i.index++
return res
}
// PublicKeyIterator is a fake implementation of a public key iterator.
//
// - implements crypto.PublicKeyIterator
type PublicKeyIterator struct {
signers []crypto.Signer
index int
}
// NewPublicKeyIterator returns a new address iterator
func NewPublicKeyIterator(signers []crypto.Signer) *PublicKeyIterator {
return &PublicKeyIterator{
signers: signers,
}
}
// Seek implements crypto.PublicKeyIterator.
func (i *PublicKeyIterator) Seek(index int) {
i.index = index
}
// HasNext implements crypto.PublicKeyIterator.
func (i *PublicKeyIterator) HasNext() bool {
return i.index < len(i.signers)
}
// GetNext implements crypto.PublicKeyIterator.
func (i *PublicKeyIterator) GetNext() crypto.PublicKey {
if i.HasNext() {
res := i.signers[i.index]
i.index++
return res.GetPublicKey()
}
return nil
}
// CollectiveAuthority is a fake implementation of a collective authority.
//
// - implements crypto.CollectiveAuthority
type CollectiveAuthority struct {
crypto.CollectiveAuthority
addrs []mino.Address
signers []crypto.Signer
Call *Call
PubkeyNotFound bool
}
// GenSigner is a function to generate a signer.
type GenSigner func() crypto.Signer
// NewAuthority returns a new collective authority of n members with new signers
// generated by g.
func NewAuthority(n int, g GenSigner) CollectiveAuthority {
return NewAuthorityWithBase(0, n, g)
}
// NewAuthorityWithBase returns a new fake collective authority of size n with
// a given starting base index.
func NewAuthorityWithBase(base int, n int, g GenSigner) CollectiveAuthority {
signers := make([]crypto.Signer, n)
for i := range signers {
signers[i] = g()
}
addrs := make([]mino.Address, n)
for i := range addrs {
addrs[i] = Address{index: i + base}
}
return CollectiveAuthority{
signers: signers,
addrs: addrs,
}
}
// NewAuthorityFromMino returns a new fake collective authority using
// the addresses of the Mino instances.
func NewAuthorityFromMino(g GenSigner, instances ...mino.Mino) CollectiveAuthority {
signers := make([]crypto.Signer, len(instances))
for i := range signers {
signers[i] = g()
}
addrs := make([]mino.Address, len(instances))
for i, instance := range instances {
addrs[i] = instance.GetAddress()
}
return CollectiveAuthority{
signers: signers,
addrs: addrs,
}
}
// GetAddress returns the address at the provided index.
func (ca CollectiveAuthority) GetAddress(index int) mino.Address {
return ca.addrs[index]
}
// GetSigner returns the signer at the provided index.
func (ca CollectiveAuthority) GetSigner(index int) crypto.Signer {
return ca.signers[index]
}
// GetPublicKey implements crypto.CollectiveAuthority.
func (ca CollectiveAuthority) GetPublicKey(addr mino.Address) (crypto.PublicKey, int) {
if ca.PubkeyNotFound {
return nil, -1
}
for i, address := range ca.addrs {
if address.Equal(addr) {
return ca.signers[i].GetPublicKey(), i
}
}
return nil, -1
}
// Take implements mino.Players.
func (ca CollectiveAuthority) Take(updaters ...mino.FilterUpdater) mino.Players {
filter := mino.ApplyFilters(updaters)
newCA := CollectiveAuthority{
Call: ca.Call,
addrs: make([]mino.Address, len(filter.Indices)),
signers: make([]crypto.Signer, len(filter.Indices)),
}
for i, k := range filter.Indices {
newCA.addrs[i] = ca.addrs[k]
newCA.signers[i] = ca.signers[k]
}
return newCA
}
// Len implements mino.Players.
func (ca CollectiveAuthority) Len() int {
return len(ca.signers)
}
// AddressIterator implements mino.Players.
func (ca CollectiveAuthority) AddressIterator() mino.AddressIterator {
return &AddressIterator{addrs: ca.addrs}
}
// PublicKeyIterator implements crypto.CollectiveAuthority.
func (ca CollectiveAuthority) PublicKeyIterator() crypto.PublicKeyIterator {
return &PublicKeyIterator{signers: ca.signers}
}
// ReceiverMessage is the combination of an address and a message that is
// returned by the receiver.
type ReceiverMessage struct {
Address mino.Address
Message serde.Message
}
// NewRecvMsg creates a new receiver message.
func NewRecvMsg(addr mino.Address, msg serde.Message) ReceiverMessage {
return ReceiverMessage{
Address: addr,
Message: msg,
}
}
// Receiver is a fake RPC stream receiver. It will return the consecutive
// messages stored in the Msg slice.
//
// - implements mino.Receiver
type Receiver struct {
mino.Receiver
err error
Msgs []ReceiverMessage
index int
blocking bool
}
// NewReceiver returns a new receiver
func NewReceiver(msgs ...ReceiverMessage) *Receiver {
return &Receiver{
Msgs: msgs,
err: io.EOF,
}
}
// NewBlockingReceiver returns a new fake receiver that is blocking until the
// context is done.
func NewBlockingReceiver() *Receiver {
return &Receiver{
blocking: true,
}
}
// NewBadReceiver returns a new receiver that returns an error.
func NewBadReceiver(msg ...ReceiverMessage) *Receiver {
return &Receiver{Msgs: msg, err: fakeErr}
}
// Recv implements mino.Receiver.
func (r *Receiver) Recv(ctx context.Context) (mino.Address, serde.Message, error) {
if r.blocking {
<-ctx.Done()
return nil, nil, ctx.Err()
}
if len(r.Msgs) == 0 {
return nil, nil, r.err
}
// In the case there are no more messages to read we return nil.
if r.index >= len(r.Msgs) {
return nil, nil, r.err
}
defer func() {
r.index++
}()
m := r.Msgs[r.index]
return m.Address, m.Message, nil
}
// Sender is a fake RPC stream sender.
//
// - implements mino.Sender
type Sender struct {
mino.Sender
err error
}
// NewBadSender returns a sender that always returns an error.
func NewBadSender() Sender {
return Sender{err: fakeErr}
}
// Send implements mino.Sender.
func (s Sender) Send(serde.Message, ...mino.Address) <-chan error {
errs := make(chan error, 1)
if s.err != nil {
errs <- s.err
}
close(errs)
return errs
}
// RPC is a fake implementation of an RPC.
//
// - implements mino.RPC
type RPC struct {
mino.RPC
Calls *Call
msgs chan mino.Response
receiver *Receiver
sender Sender
err error
}
// NewRPC returns a fake rpc.
func NewRPC() *RPC {
rpc := &RPC{}
rpc.Reset()
return rpc
}
// NewStreamRPC returns a fake rpc with specific stream options.
func NewStreamRPC(r *Receiver, s Sender) *RPC {
rpc := &RPC{
receiver: r,
sender: s,
}
rpc.Reset()
return rpc
}
// NewBadRPC returns a fake rpc that returns an error when appropriate.
func NewBadRPC() *RPC {
rpc := &RPC{
err: fakeErr,
}
rpc.Reset()
return rpc
}
// SendResponse fills the rpc with a message.
func (rpc *RPC) SendResponse(from mino.Address, msg serde.Message) {
rpc.msgs <- mino.NewResponse(from, msg)
}
// SendResponseWithError fills the rpc with an error.
func (rpc *RPC) SendResponseWithError(from mino.Address, err error) {
rpc.msgs <- mino.NewResponseWithError(from, err)
}
// Done closes the response channel.
func (rpc *RPC) Done() {
close(rpc.msgs)
}
// Call implements mino.RPC.
func (rpc *RPC) Call(ctx context.Context,
m serde.Message, p mino.Players) (<-chan mino.Response, error) {
rpc.Calls.Add(ctx, m, p)
return rpc.msgs, rpc.err
}
// Stream implements mino.RPC.
func (rpc *RPC) Stream(ctx context.Context, p mino.Players) (mino.Sender, mino.Receiver, error) {
rpc.Calls.Add(ctx, p)
return rpc.sender, rpc.receiver, rpc.err
}
// Reset resets the channels.
func (rpc *RPC) Reset() {
rpc.Calls = &Call{}
rpc.msgs = make(chan mino.Response, 100)
}
// Mino is a fake implementation of mino.
//
// - implements mino.Mino
type Mino struct {
mino.Mino
err error
}
// NewBadMino returns a Mino instance that returns an error when appropriate.
func NewBadMino() Mino {
return Mino{err: fakeErr}
}
// GetAddress implements mino.Mino.
func (m Mino) GetAddress() mino.Address {
if m.err != nil {
return NewBadAddress()
}
return Address{}
}
// GetAddressFactory implements mino.Mino.
func (m Mino) GetAddressFactory() mino.AddressFactory {
return AddressFactory{}
}
// WithSegment implements mino.Mino.
func (m Mino) WithSegment(segment string) mino.Mino {
return m
}
// CreateRPC implements mino.Mino.
func (m Mino) CreateRPC(string, mino.Handler, serde.Factory) (mino.RPC, error) {
return NewRPC(), nil
}
// MakeCertificate generates a valid certificate for the localhost address and
// for an hour. It outputs only its byte representation.
func MakeCertificate(t *testing.T, ips ...net.IP) []byte {
priv, err := ecdsa.GenerateKey(elliptic.P521(), rand.Reader)
require.NoError(t, err)
tmpl := &x509.Certificate{
SerialNumber: big.NewInt(1),
IPAddresses: ips,
NotBefore: time.Now(),
NotAfter: time.Now().Add(time.Hour),
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
IsCA: true,
MaxPathLen: 1,
}
buf, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &priv.PublicKey, priv)
require.NoError(t, err)
return buf
}
// MakeFullCertificate generates a valid certificate for the localhost address
// and for an hour. it outputs the TLS certificate and its byte representation.
func MakeFullCertificate(t *testing.T) (*tls.Certificate, []byte) {
priv, err := ecdsa.GenerateKey(elliptic.P521(), rand.Reader)
require.NoError(t, err)
tmpl := &x509.Certificate{
SerialNumber: big.NewInt(1),
IPAddresses: []net.IP{},
NotBefore: time.Now(),
NotAfter: time.Now().Add(time.Hour),
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
IsCA: true,
MaxPathLen: 1,
}
buf, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &priv.PublicKey, priv)
require.NoError(t, err)
cert, err := x509.ParseCertificate(buf)
require.NoError(t, err)
return &tls.Certificate{
Certificate: [][]byte{buf},
PrivateKey: priv,
Leaf: cert,
}, buf
}
// MakeCertificateChain creates a valid certificate chain with an intermediary
// certificate.
func MakeCertificateChain(t *testing.T) []byte {
root, pk := makeRootCertificate(t)
intermediary, pk2 := makeIntermediaryCertificate(t, root, pk)
server, _ := makeServerCertificate(t, intermediary, pk2)
chain := bytes.Buffer{}
chain.Write(server.Raw)
chain.Write(intermediary.Raw)
chain.Write(root.Raw)
return chain.Bytes()
}
func genCert(t *testing.T, template, parent *x509.Certificate,
publicKey *ecdsa.PublicKey, privateKey *ecdsa.PrivateKey) *x509.Certificate {
certBytes, err := x509.CreateCertificate(rand.Reader, template, parent, publicKey, privateKey)
require.NoError(t, err)
cert, err := x509.ParseCertificate(certBytes)
require.NoError(t, err)
return cert
}
func makeRootCertificate(t *testing.T) (*x509.Certificate, *ecdsa.PrivateKey) {
var template = x509.Certificate{
SerialNumber: big.NewInt(1),
NotBefore: time.Now(),
NotAfter: time.Now().Add(time.Hour),
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
IsCA: true,
MaxPathLen: 2,
IPAddresses: []net.IP{net.ParseIP("127.0.0.1")},
}
priv, err := ecdsa.GenerateKey(elliptic.P521(), rand.Reader)
require.NoError(t, err)
rootCert := genCert(t, &template, &template, &priv.PublicKey, priv)
return rootCert, priv
}
func makeIntermediaryCertificate(t *testing.T, rootCert *x509.Certificate,
rootKey *ecdsa.PrivateKey) (*x509.Certificate, *ecdsa.PrivateKey) {
priv, err := ecdsa.GenerateKey(elliptic.P521(), rand.Reader)
require.NoError(t, err)
var template = x509.Certificate{
SerialNumber: big.NewInt(1),
NotBefore: time.Now(),
NotAfter: time.Now().Add(time.Hour),
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
IsCA: true,
MaxPathLenZero: false,
MaxPathLen: 1,
IPAddresses: []net.IP{net.ParseIP("127.0.0.1")},
}
intermediary := genCert(t, &template, rootCert, &priv.PublicKey, rootKey)
return intermediary, priv
}
func makeServerCertificate(t *testing.T, intermediaryCert *x509.Certificate,
intermediaryKey *ecdsa.PrivateKey) (*x509.Certificate, *ecdsa.PrivateKey) {
priv, err := ecdsa.GenerateKey(elliptic.P521(), rand.Reader)
require.NoError(t, err)
var template = x509.Certificate{
SerialNumber: big.NewInt(1),
NotBefore: time.Now().Add(-10 * time.Second),
NotAfter: time.Now().AddDate(10, 0, 0),
KeyUsage: x509.KeyUsageCRLSign,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
IsCA: false,
MaxPathLenZero: true,
IPAddresses: []net.IP{net.ParseIP("127.0.0.1")},
}
ServerCert := genCert(t, &template, intermediaryCert, &priv.PublicKey, intermediaryKey)
return ServerCert, priv
}