-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.go
368 lines (317 loc) · 10.8 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
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
package server
import (
"context"
"crypto/tls"
"errors"
"fmt"
"log/slog"
"net"
"net/http"
"net/netip"
"strings"
"time"
"github.com/bufbuild/protovalidate-go"
"github.com/go-chi/cors"
protovalidate_middleware "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/protovalidate"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/realip"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/opentdf/platform/service/internal/auth"
"github.com/opentdf/platform/service/internal/logger/audit"
"github.com/opentdf/platform/service/internal/security"
"github.com/valyala/fasthttp/fasthttputil"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
healthpb "google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/reflection"
)
const (
writeTimeoutSeconds = 5
readTimeoutSeconds = 10
shutdownTimeout = 5
maxAge = 300
)
type Error string
func (e Error) Error() string {
return string(e)
}
// Configurations for the server
type Config struct {
Auth auth.Config `yaml:"auth"`
GRPC GRPCConfig `yaml:"grpc"`
CryptoProvider security.Config `yaml:"cryptoProvider"`
TLS TLSConfig `yaml:"tls"`
CORS CORSConfig `yaml:"cors"`
WellKnownConfigRegister func(namespace string, config any) error
// Port to listen on
Port int `yaml:"port" default:"8080"`
Host string `yaml:"host,omitempty"`
}
// GRPC Server specific configurations
type GRPCConfig struct {
// Enable reflection for grpc server (default: true)
ReflectionEnabled bool `yaml:"reflectionEnabled" default:"true"`
}
// TLS Configuration for the server
type TLSConfig struct {
// Enable TLS for the server (default: false)
Enabled bool `yaml:"enabled" default:"false"`
// Path to the certificate file
Cert string `yaml:"cert"`
// Path to the key file
Key string `yaml:"key"`
}
// CORS Configuration for the server
type CORSConfig struct {
// Enable CORS for the server (default: true)
Enabled bool `yaml:"enabled" default:"true"`
AllowedOrigins []string `yaml:"allowedorigins"`
AllowedMethods []string `yaml:"allowedmethods"`
AllowedHeaders []string `yaml:"allowedheaders"`
ExposedHeaders []string `yaml:"exposedheaders"`
AllowCredentials bool `yaml:"allowcredentials" default:"true"`
MaxAge int `yaml:"maxage" default:"3600"`
}
type OpenTDFServer struct {
Mux *runtime.ServeMux
HTTPServer *http.Server
GRPCServer *grpc.Server
GRPCInProcess *inProcessServer
CryptoProvider security.CryptoProvider
}
/*
Still need to flush this out for internal communication. Would like to leverage grpc
as mechanism for internal communication. Hopefully making it easier to define service boundaries.
https://github.com/heroku/x/blob/master/grpc/grpcserver/inprocess.go
https://github.com/valyala/fasthttp/blob/master/fasthttputil/inmemory_listener.go
*/
type inProcessServer struct {
ln *fasthttputil.InmemoryListener
srv *grpc.Server
}
func NewOpenTDFServer(config Config) (*OpenTDFServer, error) {
var (
authN *auth.Authentication
err error
)
// Add authN interceptor
// TODO Remove this conditional once we move to the hardening phase (https://github.com/opentdf/platform/issues/381)
if config.Auth.Enabled {
authN, err = auth.NewAuthenticator(
context.Background(),
config.Auth,
)
if err != nil {
return nil, fmt.Errorf("failed to create authentication interceptor: %w", err)
}
slog.Debug("authentication interceptor enabled")
} else {
slog.Warn("disabling authentication. this is deprecated and will be removed. if you are using an IdP without DPoP set `enforceDPoP = false`")
}
// Try an register oidc issuer to wellknown service but don't return an error if it fails
if err := config.WellKnownConfigRegister("platform_issuer", config.Auth.Issuer); err != nil {
slog.Warn("failed to register platform issuer", slog.String("error", err.Error()))
}
// Create grpc server and in process grpc server
grpcServer, err := newGrpcServer(config, authN)
if err != nil {
return nil, fmt.Errorf("failed to create grpc server: %w", err)
}
grpcIPCServer := &inProcessServer{
ln: fasthttputil.NewInmemoryListener(),
srv: grpc.NewServer(),
}
// Create http server
mux := runtime.NewServeMux(
runtime.WithHealthzEndpoint(healthpb.NewHealthClient(grpcIPCServer.Conn())),
)
httpServer, err := newHTTPServer(config, mux, authN, grpcServer)
if err != nil {
return nil, fmt.Errorf("failed to create http server: %w", err)
}
o := OpenTDFServer{
Mux: mux,
HTTPServer: httpServer,
GRPCServer: grpcServer,
GRPCInProcess: grpcIPCServer,
}
// Create crypto provider
slog.Info("creating crypto provider", slog.String("type", config.CryptoProvider.Type))
o.CryptoProvider, err = security.NewCryptoProvider(config.CryptoProvider)
if err != nil {
return nil, fmt.Errorf("security.NewCryptoProvider: %w", err)
}
return &o, nil
}
// newHTTPServer creates a new http server with the given handler and grpc server
func newHTTPServer(c Config, h http.Handler, a *auth.Authentication, g *grpc.Server) (*http.Server, error) {
var err error
var tc *tls.Config
// Add authN interceptor
// This is needed because we are leveraging RegisterXServiceHandlerServer instead of RegisterXServiceHandlerFromEndpoint
if c.Auth.Enabled {
h = a.MuxHandler(h)
} else {
slog.Error("disabling authentication. this is deprecated and will be removed. if you are using an IdP without DPoP set `enforceDPoP = false`")
}
// CORS
if c.CORS.Enabled {
h = cors.New(cors.Options{
AllowOriginFunc: func(_ *http.Request, origin string) bool {
for _, allowedOrigin := range c.CORS.AllowedOrigins {
if allowedOrigin == "*" {
return true
}
if strings.EqualFold(origin, allowedOrigin) {
return true
}
}
return false
},
AllowedMethods: c.CORS.AllowedMethods,
AllowedHeaders: c.CORS.AllowedHeaders,
ExposedHeaders: c.CORS.ExposedHeaders,
AllowCredentials: c.CORS.AllowCredentials,
MaxAge: c.CORS.MaxAge,
}).Handler(h)
}
// Add grpc handler
h2 := httpGrpcHandlerFunc(h, g)
if !c.TLS.Enabled {
h2 = h2c.NewHandler(h2, &http2.Server{})
} else {
tc, err = loadTLSConfig(c.TLS)
if err != nil {
return nil, fmt.Errorf("failed to load tls config: %w", err)
}
}
return &http.Server{
Addr: fmt.Sprintf("%s:%d", c.Host, c.Port),
WriteTimeout: writeTimeoutSeconds * time.Second,
ReadTimeout: readTimeoutSeconds * time.Second,
Handler: h2,
TLSConfig: tc,
}, nil
}
// httpGrpcHandlerFunc returns a http.Handler that delegates to the grpc server if the request is a grpc request
func httpGrpcHandlerFunc(h http.Handler, g *grpc.Server) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
slog.Debug("grpc handler func", slog.Int("proto_major", r.ProtoMajor), slog.String("content_type", r.Header.Get("Content-Type")))
if r.ProtoMajor == 2 && strings.Contains(r.Header.Get("Content-Type"), "application/grpc") {
g.ServeHTTP(w, r)
} else {
h.ServeHTTP(w, r)
}
})
}
// newGrpcServer creates a new grpc server with the given config and authN interceptor
func newGrpcServer(c Config, a *auth.Authentication) (*grpc.Server, error) {
var i []grpc.UnaryServerInterceptor
var o []grpc.ServerOption
// Enable proto validation
validator, err := protovalidate.New()
if err != nil {
slog.Warn("failed to create proto validator", slog.String("error", err.Error()))
}
// Add Audit Unary Server Interceptor
i = append(i, audit.UnaryServerInterceptor)
if c.Auth.Enabled {
i = append(i, a.UnaryServerInterceptor)
} else {
slog.Error("disabling authentication. this is deprecated and will be removed. if you are using an IdP without DPoP you can set `enforceDpop = false`")
}
// Add tls creds if tls is not nil
if c.TLS.Enabled {
c, err := loadTLSConfig(c.TLS)
if err != nil {
return nil, fmt.Errorf("failed to load tls config: %w", err)
}
o = append(o, grpc.Creds(credentials.NewTLS(c)))
}
// Add proto validation interceptor
i = append(i, protovalidate_middleware.UnaryServerInterceptor(validator))
o = append(o, grpc.ChainUnaryInterceptor(
i...,
))
// Chain relaip interceptor
trustedPeers := []netip.Prefix{} // TODO: add this as a config option?
headers := []string{realip.XForwardedFor, realip.XRealIp}
o = append(o, grpc.ChainUnaryInterceptor(
realip.UnaryServerInterceptor(trustedPeers, headers),
))
s := grpc.NewServer(o...)
// Enable grpc reflection
if c.GRPC.ReflectionEnabled {
reflection.Register(s)
}
return s, nil
}
func (s OpenTDFServer) Start() {
// // Start Http Server
go s.startHTTPServer()
// Start In Process Grpc Server
go s.startInProcessGrpcServer()
}
func (s OpenTDFServer) Stop() {
slog.Info("shutting down http server")
ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout*time.Second)
defer cancel()
if err := s.HTTPServer.Shutdown(ctx); err != nil {
slog.Error("failed to shutdown http server", slog.String("error", err.Error()))
return
}
slog.Info("shutting down in process grpc server")
s.GRPCInProcess.srv.GracefulStop()
slog.Info("shutdown complete")
}
func (s inProcessServer) GetGrpcServer() *grpc.Server {
return s.srv
}
func (s inProcessServer) Conn() *grpc.ClientConn {
defaultOptions := []grpc.DialOption{
grpc.WithContextDialer(func(_ context.Context, _ string) (net.Conn, error) {
conn, err := s.ln.Dial()
if err != nil {
return nil, fmt.Errorf("failed to dial in process grpc server: %w", err)
}
return conn, nil
}),
grpc.WithTransportCredentials(insecure.NewCredentials()),
}
conn, _ := grpc.Dial("", defaultOptions...)
return conn
}
func (s OpenTDFServer) startInProcessGrpcServer() {
slog.Info("starting in process grpc server")
if err := s.GRPCInProcess.srv.Serve(s.GRPCInProcess.ln); err != nil {
slog.Error("failed to serve in process grpc", slog.String("error", err.Error()))
panic(err)
}
}
func (s OpenTDFServer) startHTTPServer() {
var err error
if s.HTTPServer.TLSConfig != nil {
slog.Info("starting https server", "address", s.HTTPServer.Addr)
err = s.HTTPServer.ListenAndServeTLS("", "")
} else {
slog.Info("starting http server", "address", s.HTTPServer.Addr)
err = s.HTTPServer.ListenAndServe()
}
if err != nil && !errors.Is(err, http.ErrServerClosed) {
slog.Error("failed to serve http", slog.String("error", err.Error()))
return
}
}
func loadTLSConfig(config TLSConfig) (*tls.Config, error) {
cert, err := tls.LoadX509KeyPair(config.Cert, config.Key)
if err != nil {
return nil, fmt.Errorf("failed to load tls cert: %w", err)
}
return &tls.Config{
Certificates: []tls.Certificate{cert},
MinVersion: tls.VersionTLS12,
NextProtos: []string{"h2", "http/1.1"},
}, nil
}