-
Notifications
You must be signed in to change notification settings - Fork 20
/
grpc.go
237 lines (202 loc) · 8.5 KB
/
grpc.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
// Copyright 2022 Fraunhofer AISEC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// $$\ $$\ $$\ $$\
// $$ | $$ |\__| $$ |
// $$$$$$$\ $$ | $$$$$$\ $$\ $$\ $$$$$$$ |$$\ $$$$$$\ $$$$$$\ $$$$$$\
// $$ _____|$$ |$$ __$$\ $$ | $$ |$$ __$$ |$$ |\_$$ _| $$ __$$\ $$ __$$\
// $$ / $$ |$$ / $$ |$$ | $$ |$$ / $$ |$$ | $$ | $$ / $$ |$$ | \__|
// $$ | $$ |$$ | $$ |$$ | $$ |$$ | $$ |$$ | $$ |$$\ $$ | $$ |$$ |
// \$$$$$$\ $$ |\$$$$$ |\$$$$$ |\$$$$$$ |$$ | \$$$ |\$$$$$ |$$ |
// \_______|\__| \______/ \______/ \_______|\__| \____/ \______/ \__|
//
// This file is part of Clouditor Community Edition.
package server
import (
"context"
"fmt"
"net"
"clouditor.io/clouditor/api/assessment"
"clouditor.io/clouditor/api/discovery"
"clouditor.io/clouditor/api/evaluation"
"clouditor.io/clouditor/api/evidence"
"clouditor.io/clouditor/api/orchestrator"
"clouditor.io/clouditor/logging/formatter"
grpc_auth "github.com/grpc-ecosystem/go-grpc-middleware/auth"
grpc_logrus "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus"
grpc_ctxtags "github.com/grpc-ecosystem/go-grpc-middleware/tags"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)
// Server is a typealias for [grpc.Server] so that users of this package do not need to import the grpc packages
// directly.
type Server = grpc.Server
// StartGRPCServerOption is a type for functional style options that can configure the [StartGRPCServer] function.
type StartGRPCServerOption func(c *config)
// config contains additional server configurations
type config struct {
grpcOpts []grpc.ServerOption
services map[*grpc.ServiceDesc]any
publicEndpoints []string
ac AuthConfig
reflection bool
}
// WithOrchestrator is an option for [StartGRPCServer] to register a [orchestrator.OrchestratorServer] at start.
func WithOrchestrator(svc orchestrator.OrchestratorServer) StartGRPCServerOption {
return func(c *config) {
c.services[&orchestrator.Orchestrator_ServiceDesc] = svc
}
}
// WithAssessment is an option for [StartGRPCServer] to register a [assessment.AssessmentServer] at start.
func WithAssessment(svc assessment.AssessmentServer) StartGRPCServerOption {
return func(c *config) {
c.services[&assessment.Assessment_ServiceDesc] = svc
}
}
// WithEvidenceStore is an option for [StartGRPCServer] to register a [evidence.EvidenceStoreServer] at start.
func WithEvidenceStore(svc evidence.EvidenceStoreServer) StartGRPCServerOption {
return func(c *config) {
c.services[&evidence.EvidenceStore_ServiceDesc] = svc
}
}
// WithDiscovery is an option for [StartGRPCServer] to register a [discovery.DiscoveryServer] at start.
func WithDiscovery(svc discovery.DiscoveryServer) StartGRPCServerOption {
return func(c *config) {
c.services[&discovery.Discovery_ServiceDesc] = svc
}
}
// WithEvaluation is an option for [StartGRPCServer] to register a [evaluation.EvaluationServer] at start.
func WithEvaluation(svc evaluation.EvaluationServer) StartGRPCServerOption {
return func(c *config) {
c.services[&evaluation.Evaluation_ServiceDesc] = svc
}
}
// WithReflection is an option for [StartGRPCServer] to enable gRPC reflection.
func WithReflection() StartGRPCServerOption {
return func(c *config) {
c.reflection = true
}
}
// WithReflection is an option for [StartGRPCServer] to enable gRPC reflection.
func WithPublicEndpoints(endpoints []string) StartGRPCServerOption {
return func(c *config) {
c.publicEndpoints = endpoints
}
}
// WithAdditionalGRPCOpts is an option to add an additional gRPC dial options in the REST server communication to the
// backend.
func WithAdditionalGRPCOpts(opts []grpc.ServerOption) StartGRPCServerOption {
return func(c *config) {
c.grpcOpts = append(c.grpcOpts, opts...)
}
}
// StartGRPCServer starts a gRPC server listening on the given address. The server can be configured using the supplied
// opts, e.g., to register various Clouditor services. The server itself is started in a separate Go routine, therefore
// this function will NOT block.
func StartGRPCServer(addr string, opts ...StartGRPCServerOption) (sock net.Listener, srv *Server, err error) {
// create a new socket for gRPC communication
sock, err = net.Listen("tcp", addr)
if err != nil {
return nil, nil, fmt.Errorf("could not listen: %w", err)
}
var c config
grpcLogger := logrus.New()
grpcLogger.Formatter = &formatter.GRPCFormatter{TextFormatter: logrus.TextFormatter{ForceColors: true}}
grpcLoggerEntry := grpcLogger.WithField("component", "grpc")
c.grpcOpts = []grpc.ServerOption{
grpc.ChainUnaryInterceptor(
grpc_ctxtags.UnaryServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)),
grpc_logrus.UnaryServerInterceptor(grpcLoggerEntry),
UnaryServerInterceptorWithFilter(&c, grpc_auth.UnaryServerInterceptor(c.ac.AuthFunc()), UnaryReflectionFilter, UnaryPublicEndpointFilter),
),
grpc.ChainStreamInterceptor(
grpc_ctxtags.StreamServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)),
grpc_logrus.StreamServerInterceptor(grpcLoggerEntry),
StreamServerInterceptorWithFilter(&c, grpc_auth.StreamServerInterceptor(c.ac.AuthFunc()), StreamReflectionFilter, StreamPublicEndpointFilter),
),
}
c.services = map[*grpc.ServiceDesc]any{}
for _, o := range opts {
o(&c)
}
srv = grpc.NewServer(
c.grpcOpts...,
)
// Register services
for sd, svc := range c.services {
srv.RegisterService(sd, svc)
}
// Enable reflection
if c.reflection {
reflection.Register(srv)
}
go func() {
// serve the gRPC socket
_ = srv.Serve(sock)
}()
return sock, srv, nil
}
// UnaryServerInterceptorWithFilter wraps a grpc.UnaryServerInterceptor and only invokes the interceptor, if the filter
// function does not return true.
func UnaryServerInterceptorWithFilter(c *config, in grpc.UnaryServerInterceptor, filter ...func(c *config, info *grpc.UnaryServerInfo) bool) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
// If the filter evaluates to true, we directly return the handler and ignore the interceptor
for _, f := range filter {
if f(c, info) {
return handler(ctx, req)
}
}
return in(ctx, req, info, handler)
}
}
// StreamServerInterceptorWithFilter wraps a grpc.StreamServerInterceptor and only invokes the interceptor, if the
// filter function does not return true.
func StreamServerInterceptorWithFilter(c *config, in grpc.StreamServerInterceptor, filter ...func(c *config, info *grpc.StreamServerInfo) bool) grpc.StreamServerInterceptor {
return func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
// If the filter evaluates to true, we directly return the handler and ignore the interceptor
for _, f := range filter {
if f(c, info) {
return handler(srv, ss)
}
}
return in(srv, ss, info, handler)
}
}
// UnaryReflectionFilter is a filter that ignores calls to the reflection endpoint
func UnaryReflectionFilter(_ *config, info *grpc.UnaryServerInfo) bool {
return info.FullMethod == "/grpc.reflection.v1.ServerReflection/ServerReflectionInfo"
}
// StreamReflectionFilter is a filter that ignores calls to the reflection endpoint
func StreamReflectionFilter(_ *config, info *grpc.StreamServerInfo) bool {
return info.FullMethod == "/grpc.reflection.v1.ServerReflection/ServerReflectionInfo"
}
// UnaryPublicEndpointFilter is a filter that ignores calls to the public endpoints
func UnaryPublicEndpointFilter(c *config, info *grpc.UnaryServerInfo) bool {
for _, e := range c.publicEndpoints {
if info.FullMethod == e {
return true
}
}
return false
}
// StreamPublicEndpointFilter is a filter that ignores calls to the public endpoints
func StreamPublicEndpointFilter(c *config, info *grpc.StreamServerInfo) bool {
for _, e := range c.publicEndpoints {
if info.FullMethod == e {
return true
}
}
return false
}