-
Notifications
You must be signed in to change notification settings - Fork 458
/
plugin_test.go
808 lines (697 loc) · 22.4 KB
/
plugin_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
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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package plugin
import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/rpc"
"os"
"os/exec"
"testing"
"time"
"github.com/golang/protobuf/ptypes/empty"
"github.com/hashicorp/go-hclog"
grpctest "github.com/hashicorp/go-plugin/test/grpc"
"google.golang.org/grpc"
)
// Test that NetRPCUnsupportedPlugin implements the correct interfaces.
var _ Plugin = new(NetRPCUnsupportedPlugin)
var testHandshake = HandshakeConfig{
ProtocolVersion: 1,
MagicCookieKey: "TEST_MAGIC_COOKIE",
MagicCookieValue: "test",
}
var testVersionedHandshake = HandshakeConfig{
MagicCookieKey: "TEST_MAGIC_COOKIE",
MagicCookieValue: "test",
}
// testInterface is the test interface we use for plugins.
type testInterface interface {
Double(int) int
PrintKV(string, interface{})
Bidirectional() error
PrintStdio(stdout, stderr []byte)
}
// testStreamer is used to test the grpc streaming interface
type testStreamer interface {
Stream(int32, int32) ([]int32, error)
}
// testInterfacePlugin implements the Plugin interface
var _ Plugin = (*testInterfacePlugin)(nil)
// testInterfacePlugin is the implementation of Plugin to create
// RPC client/server implementations for testInterface.
type testInterfacePlugin struct {
Impl testInterface
}
func (p *testInterfacePlugin) Server(b *MuxBroker) (interface{}, error) {
return &testInterfaceServer{Impl: p.impl()}, nil
}
func (p *testInterfacePlugin) Client(b *MuxBroker, c *rpc.Client) (interface{}, error) {
return &testInterfaceClient{Client: c}, nil
}
func (p *testInterfacePlugin) impl() testInterface {
if p.Impl != nil {
return p.Impl
}
return &testInterfaceImpl{
logger: hclog.New(&hclog.LoggerOptions{
Level: hclog.Trace,
Output: os.Stderr,
JSONFormat: true,
}),
}
}
// testGRPCInterfacePlugin implements both Plugin and GRPCPlugin interfaces
var _ Plugin = (*testGRPCInterfacePlugin)(nil)
var _ GRPCPlugin = (*testGRPCInterfacePlugin)(nil)
// testGRPCInterfacePlugin is a test implementation of the GRPCPlugin interface
type testGRPCInterfacePlugin struct {
Impl testInterface
}
func (p *testGRPCInterfacePlugin) Server(b *MuxBroker) (interface{}, error) {
return &testInterfaceServer{Impl: p.impl()}, nil
}
func (p *testGRPCInterfacePlugin) Client(b *MuxBroker, c *rpc.Client) (interface{}, error) {
return &testInterfaceClient{Client: c}, nil
}
func (p *testGRPCInterfacePlugin) GRPCServer(b *GRPCBroker, s *grpc.Server) error {
grpctest.RegisterTestServer(s, &testGRPCServer{broker: b, Impl: p.impl()})
return nil
}
func (p *testGRPCInterfacePlugin) GRPCClient(doneCtx context.Context, b *GRPCBroker, c *grpc.ClientConn) (interface{}, error) {
return &testGRPCClient{broker: b, Client: grpctest.NewTestClient(c)}, nil
}
func (p *testGRPCInterfacePlugin) impl() testInterface {
if p.Impl != nil {
return p.Impl
}
return &testInterfaceImpl{
logger: hclog.New(&hclog.LoggerOptions{
Level: hclog.Trace,
Output: os.Stderr,
JSONFormat: true,
}),
}
}
// testInterfaceImpl implements testInterface concretely
var _ testInterface = (*testInterfaceImpl)(nil)
type testInterfaceImpl struct {
logger hclog.Logger
}
func (i *testInterfaceImpl) Double(v int) int { return v * 2 }
func (i *testInterfaceImpl) PrintKV(key string, value interface{}) {
i.logger.Info("PrintKV called", key, value)
}
func (i *testInterfaceImpl) Bidirectional() error {
return nil
}
func (i *testInterfaceImpl) PrintStdio(stdout, stderr []byte) {
if len(stdout) > 0 {
fmt.Fprint(os.Stdout, string(stdout))
os.Stdout.Sync()
}
if len(stderr) > 0 {
fmt.Fprint(os.Stderr, string(stderr))
os.Stderr.Sync()
}
}
// testInterfaceClient implements testInterface to communicate over RPC
type testInterfaceClient struct {
Client *rpc.Client
}
func (impl *testInterfaceClient) Double(v int) int {
var resp int
err := impl.Client.Call("Plugin.Double", v, &resp)
if err != nil {
panic(err)
}
return resp
}
func (impl *testInterfaceClient) PrintKV(key string, value interface{}) {
err := impl.Client.Call("Plugin.PrintKV", map[string]interface{}{
"key": key,
"value": value,
}, &struct{}{})
if err != nil {
panic(err)
}
}
func (impl *testInterfaceClient) Bidirectional() error {
return nil
}
func (impl *testInterfaceClient) PrintStdio(stdout, stderr []byte) {
// We don't implement this because we test stream syncing another
// way (see rpc_client_test.go). We probably should test this way
// but very few people use the net/rpc protocol nowadays so we didn'
// put in the effort.
return
}
// testInterfaceServer is the RPC server for testInterfaceClient
type testInterfaceServer struct {
Broker *MuxBroker
Impl testInterface
}
func (s *testInterfaceServer) Double(arg int, resp *int) error {
*resp = s.Impl.Double(arg)
return nil
}
func (s *testInterfaceServer) PrintKV(args map[string]interface{}, _ *struct{}) error {
s.Impl.PrintKV(args["key"].(string), args["value"])
return nil
}
// testPluginMap can be used for tests as a plugin map
var testPluginMap = map[string]Plugin{
"test": new(testInterfacePlugin),
}
// testGRPCPluginMap can be used for tests as that need a GRPC plugin
var testGRPCPluginMap = map[string]Plugin{
"test": new(testGRPCInterfacePlugin),
}
// testGRPCServer is the implementation of our GRPC service.
type testGRPCServer struct {
Impl testInterface
broker *GRPCBroker
}
func (s *testGRPCServer) Double(
ctx context.Context,
req *grpctest.TestRequest) (*grpctest.TestResponse, error) {
return &grpctest.TestResponse{
Output: int32(s.Impl.Double(int(req.Input))),
}, nil
}
func (s *testGRPCServer) PrintKV(
ctx context.Context,
req *grpctest.PrintKVRequest) (*grpctest.PrintKVResponse, error) {
var v interface{}
switch rv := req.Value.(type) {
case *grpctest.PrintKVRequest_ValueString:
v = rv.ValueString
case *grpctest.PrintKVRequest_ValueInt:
v = rv.ValueInt
default:
panic(fmt.Sprintf("unknown value: %#v", req.Value))
}
s.Impl.PrintKV(req.Key, v)
return &grpctest.PrintKVResponse{}, nil
}
func (s *testGRPCServer) Bidirectional(ctx context.Context, req *grpctest.BidirectionalRequest) (*grpctest.BidirectionalResponse, error) {
conn, err := s.broker.Dial(req.Id)
if err != nil {
return nil, fmt.Errorf("server dial error: %w", err)
}
pingPongClient := grpctest.NewPingPongClient(conn)
resp, err := pingPongClient.Ping(ctx, &grpctest.PingRequest{})
if err != nil {
return nil, fmt.Errorf("server Ping() error: %w", err)
}
if resp.Msg != "pong" {
return nil, errors.New("server Bad PingPong")
}
nextID := s.broker.NextId()
go s.broker.AcceptAndServe(nextID, func(opts []grpc.ServerOption) *grpc.Server {
s := grpc.NewServer(opts...)
grpctest.RegisterPingPongServer(s, &pingPongServer{})
return s
})
return &grpctest.BidirectionalResponse{
Id: nextID,
}, nil
}
func (s *testGRPCServer) PrintStdio(
ctx context.Context,
req *grpctest.PrintStdioRequest,
) (*empty.Empty, error) {
s.Impl.PrintStdio(req.Stdout, req.Stderr)
return &empty.Empty{}, nil
}
type pingPongServer struct{}
func (p *pingPongServer) Ping(ctx context.Context, req *grpctest.PingRequest) (*grpctest.PongResponse, error) {
return &grpctest.PongResponse{
Msg: "pong",
}, nil
}
func (s testGRPCServer) Stream(stream grpctest.Test_StreamServer) error {
for {
req, err := stream.Recv()
if err != nil {
if err != io.EOF {
return err
}
return nil
}
if err := stream.Send(&grpctest.TestResponse{Output: req.Input}); err != nil {
return err
}
}
return nil
}
// testGRPCClient is an implementation of TestInterface that communicates
// over gRPC.
type testGRPCClient struct {
Client grpctest.TestClient
broker *GRPCBroker
}
func (c *testGRPCClient) Double(v int) int {
resp, err := c.Client.Double(context.Background(), &grpctest.TestRequest{
Input: int32(v),
})
if err != nil {
panic(err)
}
return int(resp.Output)
}
func (c *testGRPCClient) PrintKV(key string, value interface{}) {
req := &grpctest.PrintKVRequest{Key: key}
switch v := value.(type) {
case string:
req.Value = &grpctest.PrintKVRequest_ValueString{
ValueString: v,
}
case int:
req.Value = &grpctest.PrintKVRequest_ValueInt{
ValueInt: int32(v),
}
default:
panic(fmt.Sprintf("unknown type: %T", value))
}
_, err := c.Client.PrintKV(context.Background(), req)
if err != nil {
panic(err)
}
}
func (c *testGRPCClient) Bidirectional() error {
nextID := c.broker.NextId()
go c.broker.AcceptAndServe(nextID, func(opts []grpc.ServerOption) *grpc.Server {
s := grpc.NewServer(opts...)
grpctest.RegisterPingPongServer(s, &pingPongServer{})
return s
})
resp, err := c.Client.Bidirectional(context.Background(), &grpctest.BidirectionalRequest{
Id: nextID,
})
if err != nil {
return fmt.Errorf("client Bidirectional() error: %w", err)
}
conn, err := c.broker.Dial(resp.Id)
if err != nil {
return fmt.Errorf("client dial error: %w", err)
}
pingPongClient := grpctest.NewPingPongClient(conn)
pResp, err := pingPongClient.Ping(context.Background(), &grpctest.PingRequest{})
if err != nil {
return fmt.Errorf("client Ping() error: %w", err)
}
if pResp.Msg != "pong" {
return errors.New("client Bad PingPong")
}
return nil
}
// Stream sends a series of requests from [start, stop) using a bidirectional
// streaming service, and returns the streamed responses.
func (impl *testGRPCClient) Stream(start, stop int32) ([]int32, error) {
if stop <= start {
return nil, fmt.Errorf("invalid range [%d, %d)", start, stop)
}
streamClient, err := impl.Client.Stream(context.Background())
if err != nil {
return nil, err
}
var resp []int32
for i := start; i < stop; i++ {
if err := streamClient.Send(&grpctest.TestRequest{Input: i}); err != nil {
return resp, err
}
out, err := streamClient.Recv()
if err != nil {
return resp, err
}
resp = append(resp, out.Output)
}
streamClient.CloseSend()
return resp, nil
}
func (c *testGRPCClient) PrintStdio(stdout, stderr []byte) {
_, err := c.Client.PrintStdio(context.Background(), &grpctest.PrintStdioRequest{
Stdout: stdout,
Stderr: stderr,
})
if err != nil {
panic(err)
}
}
func helperProcess(s ...string) *exec.Cmd {
cs := []string{"-test.run=TestHelperProcess", "--"}
cs = append(cs, s...)
env := []string{
"GO_WANT_HELPER_PROCESS=1",
}
cmd := exec.Command(os.Args[0], cs...)
cmd.Env = append(env, os.Environ()...)
return cmd
}
// This is not a real test. This is just a helper process kicked off by
// tests.
func TestHelperProcess(*testing.T) {
if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
return
}
defer os.Exit(0)
args := os.Args
for len(args) > 0 {
if args[0] == "--" {
args = args[1:]
break
}
args = args[1:]
}
if len(args) == 0 {
fmt.Fprintf(os.Stderr, "No command\n")
os.Exit(2)
}
// override testPluginMap with one that uses
// hclog logger on its implementation
pluginLogger := hclog.New(&hclog.LoggerOptions{
Level: hclog.Trace,
Output: os.Stderr,
JSONFormat: true,
})
testPlugin := &testInterfaceImpl{
logger: pluginLogger,
}
testPluginMap := map[string]Plugin{
"test": &testInterfacePlugin{Impl: testPlugin},
}
testGRPCPluginMap := map[string]Plugin{
"test": &testGRPCInterfacePlugin{Impl: testPlugin},
}
cmd, args := args[0], args[1:]
switch cmd {
case "bad-version":
// If we have an arg, we write there on start
if len(args) > 0 {
path := args[0]
err := ioutil.WriteFile(path, []byte("foo"), 0644)
if err != nil {
panic(err)
}
}
fmt.Printf("%d|%d1|tcp|:1234\n", CoreProtocolVersion, testHandshake.ProtocolVersion)
<-make(chan int)
case "invalid-rpc-address":
fmt.Println("lolinvalid")
case "mock":
fmt.Printf("%d|%d|tcp|:1234\n", CoreProtocolVersion, testHandshake.ProtocolVersion)
<-make(chan int)
case "start-timeout":
time.Sleep(1 * time.Minute)
os.Exit(1)
case "stderr":
fmt.Printf("%d|%d|tcp|:1234\n", CoreProtocolVersion, testHandshake.ProtocolVersion)
os.Stderr.WriteString("HELLO\n")
os.Stderr.WriteString("WORLD\n")
case "stderr-json":
// write values that might be JSON, but aren't KVs
fmt.Printf("%d|%d|tcp|:1234\n", CoreProtocolVersion, testHandshake.ProtocolVersion)
os.Stderr.WriteString("[\"HELLO\"]\n")
os.Stderr.WriteString("12345\n")
os.Stderr.WriteString("{\"a\":1}\n")
case "level-warn-text":
// write values that might be JSON, but aren't KVs
fmt.Printf("%d|%d|tcp|:1234\n", CoreProtocolVersion, testHandshake.ProtocolVersion)
os.Stderr.WriteString("[WARN] test line 98765\n")
case "stdin":
fmt.Printf("%d|%d|tcp|:1234\n", CoreProtocolVersion, testHandshake.ProtocolVersion)
data := make([]byte, 5)
if _, err := os.Stdin.Read(data); err != nil {
log.Printf("stdin read error: %s", err)
os.Exit(100)
}
if string(data) == "hello" {
os.Exit(0)
}
os.Exit(1)
case "cleanup":
// Create a defer to write the file. This tests that we get cleaned
// up properly versus just calling os.Exit
path := args[0]
defer func() {
err := ioutil.WriteFile(path, []byte("foo"), 0644)
if err != nil {
panic(err)
}
}()
Serve(&ServeConfig{
HandshakeConfig: testHandshake,
Plugins: testPluginMap,
})
// Exit
return
case "test-grpc":
Serve(&ServeConfig{
HandshakeConfig: testHandshake,
Plugins: testGRPCPluginMap,
GRPCServer: DefaultGRPCServer,
})
// Shouldn't reach here but make sure we exit anyways
os.Exit(0)
case "test-grpc-tls":
// Serve!
Serve(&ServeConfig{
HandshakeConfig: testHandshake,
Plugins: testGRPCPluginMap,
GRPCServer: DefaultGRPCServer,
TLSProvider: helperTLSProvider,
})
// Shouldn't reach here but make sure we exit anyways
os.Exit(0)
case "test-interface":
Serve(&ServeConfig{
HandshakeConfig: testHandshake,
Plugins: testPluginMap,
})
// Shouldn't reach here but make sure we exit anyways
os.Exit(0)
case "test-interface-logger-netrpc":
Serve(&ServeConfig{
HandshakeConfig: testHandshake,
Plugins: testPluginMap,
})
// Shouldn't reach here but make sure we exit anyways
os.Exit(0)
case "test-interface-logger-grpc":
Serve(&ServeConfig{
HandshakeConfig: testHandshake,
Plugins: testPluginMap,
GRPCServer: DefaultGRPCServer,
})
// Shouldn't reach here but make sure we exit anyways
os.Exit(0)
case "test-interface-daemon":
// Serve!
Serve(&ServeConfig{
HandshakeConfig: testHandshake,
Plugins: testPluginMap,
})
// Shouldn't reach here but make sure we exit anyways
os.Exit(0)
case "test-interface-tls":
// Serve!
Serve(&ServeConfig{
HandshakeConfig: testHandshake,
Plugins: testPluginMap,
TLSProvider: helperTLSProvider,
})
// Shouldn't reach here but make sure we exit anyways
os.Exit(0)
case "test-interface-mtls":
// Serve!
Serve(&ServeConfig{
HandshakeConfig: testVersionedHandshake,
Plugins: testPluginMap,
})
// Shouldn't reach here but make sure we exit anyways
os.Exit(0)
case "test-versioned-plugins":
// Serve!
Serve(&ServeConfig{
HandshakeConfig: testVersionedHandshake,
VersionedPlugins: map[int]PluginSet{
2: testGRPCPluginMap,
},
GRPCServer: DefaultGRPCServer,
TLSProvider: helperTLSProvider,
})
// Shouldn't reach here but make sure we exit anyways
os.Exit(0)
case "test-proto-upgraded-plugin":
// Serve 2 plugins over different protocols
Serve(&ServeConfig{
HandshakeConfig: testVersionedHandshake,
VersionedPlugins: map[int]PluginSet{
1: PluginSet{
"old": &testInterfacePlugin{Impl: testPlugin},
},
2: testGRPCPluginMap,
},
GRPCServer: DefaultGRPCServer,
TLSProvider: helperTLSProvider,
})
// Shouldn't reach here but make sure we exit anyways
os.Exit(0)
case "test-proto-upgraded-client":
// Serve 2 plugins over different protocols
Serve(&ServeConfig{
HandshakeConfig: HandshakeConfig{
ProtocolVersion: 2,
MagicCookieKey: "TEST_MAGIC_COOKIE",
MagicCookieValue: "test",
},
Plugins: testGRPCPluginMap,
GRPCServer: DefaultGRPCServer,
TLSProvider: helperTLSProvider,
})
// Shouldn't reach here but make sure we exit anyways
os.Exit(0)
case "test-mtls":
// Serve 2 plugins over different protocols
Serve(&ServeConfig{
HandshakeConfig: HandshakeConfig{
ProtocolVersion: 2,
MagicCookieKey: "TEST_MAGIC_COOKIE",
MagicCookieValue: "test",
},
Plugins: testGRPCPluginMap,
GRPCServer: DefaultGRPCServer,
})
// Shouldn't reach here but make sure we exit anyways
os.Exit(0)
case "test-skip-host-env-true":
fmt.Printf("%d|%d|tcp|:1234\n", CoreProtocolVersion, testHandshake.ProtocolVersion)
if os.Getenv("PLUGIN_TEST_SKIP_HOST_ENV") == "" {
os.Exit(0)
}
os.Exit(1)
case "test-skip-host-env-false":
fmt.Printf("%d|%d|tcp|:1234\n", CoreProtocolVersion, testHandshake.ProtocolVersion)
if os.Getenv("PLUGIN_TEST_SKIP_HOST_ENV") != "" {
os.Exit(0)
}
os.Exit(1)
case "mux-grpc-with-old-plugin":
// gRPC broker multiplexing requested, but plugin has no awareness of the
// feature, so just prints 6 segments in the protocol negotiation line.
// Client should fail with a helpful error.
if os.Getenv(envMultiplexGRPC) == "" {
fmt.Println("failed precondition for mux test")
os.Exit(1)
}
fmt.Printf("%d|%d|tcp|:1234|%s|\n", CoreProtocolVersion, testHandshake.ProtocolVersion, ProtocolGRPC)
<-make(chan int)
case "mux-grpc-with-unsupported-plugin":
// gRPC broker multiplexing requested, but plugin explicitly does not
// support it. Client should fail with a helpful error.
if os.Getenv(envMultiplexGRPC) == "" {
fmt.Println("failed precondition for mux test")
os.Exit(1)
}
fmt.Printf("%d|%d|tcp|:1234|%s||false\n", CoreProtocolVersion, testHandshake.ProtocolVersion, ProtocolGRPC)
<-make(chan int)
default:
fmt.Fprintf(os.Stderr, "Unknown command: %q\n", cmd)
os.Exit(2)
}
}
func helperTLSProvider() (*tls.Config, error) {
serverCert, err := tls.X509KeyPair([]byte(TestClusterServerCert), []byte(TestClusterServerKey))
if err != nil {
return nil, err
}
rootCAs := x509.NewCertPool()
rootCAs.AppendCertsFromPEM([]byte(TestClusterCACert))
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{serverCert},
RootCAs: rootCAs,
ClientCAs: rootCAs,
ClientAuth: tls.VerifyClientCertIfGiven,
ServerName: "127.0.0.1",
}
tlsConfig.BuildNameToCertificate()
return tlsConfig, nil
}
const (
TestClusterCACert = `-----BEGIN CERTIFICATE-----
MIIDPjCCAiagAwIBAgIUfIKsF2VPT7sdFcKOHJH2Ii6K4MwwDQYJKoZIhvcNAQEL
BQAwFjEUMBIGA1UEAxMLbXl2YXVsdC5jb20wIBcNMTYwNTAyMTYwNTQyWhgPMjA2
NjA0MjAxNjA2MTJaMBYxFDASBgNVBAMTC215dmF1bHQuY29tMIIBIjANBgkqhkiG
9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuOimEXawD2qBoLCFP3Skq5zi1XzzcMAJlfdS
xz9hfymuJb+cN8rB91HOdU9wQCwVKnkUtGWxUnMp0tT0uAZj5NzhNfyinf0JGAbP
67HDzVZhGBHlHTjPX0638yaiUx90cTnucX0N20SgCYct29dMSgcPl+W78D3Jw3xE
JsHQPYS9ASe2eONxG09F/qNw7w/RO5/6WYoV2EmdarMMxq52pPe2chtNMQdSyOUb
cCcIZyk4QVFZ1ZLl6jTnUPb+JoCx1uMxXvMek4NF/5IL0Wr9dw2gKXKVKoHDr6SY
WrCONRw61A5Zwx1V+kn73YX3USRlkufQv/ih6/xThYDAXDC9cwIDAQABo4GBMH8w
DgYDVR0PAQH/BAQDAgEGMA8GA1UdEwEB/wQFMAMBAf8wHQYDVR0OBBYEFOuKvPiU
G06iHkRXAOeMiUdBfHFyMB8GA1UdIwQYMBaAFOuKvPiUG06iHkRXAOeMiUdBfHFy
MBwGA1UdEQQVMBOCC215dmF1bHQuY29thwR/AAABMA0GCSqGSIb3DQEBCwUAA4IB
AQBcN/UdAMzc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