-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.go
58 lines (49 loc) · 1.38 KB
/
connection.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
package gw
import (
"context"
"crypto/tls"
"crypto/x509"
"os"
"github.com/lab5e/spangw/pkg/pb/gateway/v1"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
// Create creates a new gateway process, connects to the Span service and launches the command
// processing. The handler implements the actual gateway
func Create(config Parameters, handler CommandHandler) (*GatewayProcess, error) {
creds, err := loadCertificates(config.CertFile, config.KeyFile)
if err != nil {
return nil, err
}
cc, err := grpc.Dial(
config.SpanEndpoint,
grpc.WithTransportCredentials(creds))
if err != nil {
return nil, err
}
demoGW := gateway.NewUserGatewayServiceClient(cc)
stream, err := demoGW.ControlStream(context.Background())
if err != nil {
return nil, err
}
return NewGatewayProcess(config.StateFile, stream, handler), nil
}
func loadCertificates(certFile, keyFile string) (credentials.TransportCredentials, error) {
certs, err := os.ReadFile(certFile)
if err != nil {
return nil, err
}
certPool := x509.NewCertPool()
certPool.AppendCertsFromPEM(certs)
cCert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
return nil, err
}
return credentials.NewTLS(&tls.Config{
Certificates: []tls.Certificate{cCert},
GetClientCertificate: func(*tls.CertificateRequestInfo) (*tls.Certificate, error) {
return &cCert, nil
},
RootCAs: certPool,
}), nil
}