This repository has been archived by the owner on Sep 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
110 lines (91 loc) · 3.01 KB
/
server.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
package go_translator
import (
"fmt"
"net"
"os"
"os/signal"
"syscall"
"github.com/WirePact/go-translator/internal"
"github.com/WirePact/go-translator/pki"
auth "github.com/envoyproxy/go-control-plane/envoy/service/auth/v3"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
)
// Translator acts as the server for translation and communicating with envoy.
type Translator struct {
close chan bool
config *TranslatorConfig
ingressServer *grpc.Server
ingressListen *net.Listener
egressServer *grpc.Server
egressListen *net.Listener
}
// NewTranslator creates a new translator that adheres to the given config.
func NewTranslator(config *TranslatorConfig) (*Translator, error) {
var ingressOpts []grpc.ServerOption
ingressServer := grpc.NewServer(ingressOpts...)
auth.RegisterAuthorizationServer(ingressServer, &internal.IngressServer{
IngressTranslator: config.IngressTranslator,
})
ingressListen, err := net.Listen("tcp", fmt.Sprintf(":%v", config.IngressPort))
if err != nil {
logrus.WithError(err).Errorf("Could not listen on ingress port %v", config.IngressPort)
return nil, err
}
var egressOpts []grpc.ServerOption
egressServer := grpc.NewServer(egressOpts...)
auth.RegisterAuthorizationServer(egressServer, &internal.EgressServer{
EgressTranslator: config.EgressTranslator,
JWTConfig: &config.JWTConfig,
})
egressListen, err := net.Listen("tcp", fmt.Sprintf(":%v", config.EgressPort))
if err != nil {
logrus.WithError(err).Errorf("Could not listen on egress port %v", config.EgressPort)
return nil, err
}
return &Translator{
config: config,
ingressServer: ingressServer,
ingressListen: &ingressListen,
egressServer: egressServer,
egressListen: &egressListen,
}, nil
}
// Start runs the server by ensuring the PKI key material and then starting the grpc servers.
// When a system interrupt is received the server stops.
func (translator *Translator) Start() {
err := pki.EnsureKeyMaterial(&translator.config.Config)
if err != nil {
logrus.WithError(err).Fatal("Could not ensure key material.")
}
translator.close = make(chan bool)
go func() {
logrus.Info("Serving Ingress")
err := translator.ingressServer.Serve(*translator.ingressListen)
if err != nil {
logrus.WithError(err).Fatal("Could not serve ingress.")
}
}()
go func() {
logrus.Info("Serving Egress")
err := translator.egressServer.Serve(*translator.egressListen)
if err != nil {
logrus.WithError(err).Fatal("Could not serve egress.")
}
}()
go func() {
signalChannel := make(chan os.Signal, 1)
signal.Notify(signalChannel, syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL)
<-signalChannel
logrus.Infoln("Graceful shutdown signal received. Closing translator.")
translator.close <- true
}()
<-translator.close
translator.ingressServer.GracefulStop()
translator.egressServer.GracefulStop()
}
// Stop closes the server and returns the "start" function.
func (translator *Translator) Stop() {
logrus.Infoln("Stop function called. Closing translator.")
translator.close <- true
}