/
server.go
541 lines (461 loc) · 14.4 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
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
package server
import (
"context"
"crypto/tls"
"errors"
"fmt"
"net"
"net/http"
"path/filepath"
"regexp"
"strconv"
"sync"
"sync/atomic"
"time"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
"github.com/italypaleale/revaulter/pkg/config"
"github.com/italypaleale/revaulter/pkg/keyvault"
"github.com/italypaleale/revaulter/pkg/metrics"
"github.com/italypaleale/revaulter/pkg/utils/broker"
"github.com/italypaleale/revaulter/pkg/utils/webhook"
)
// Server is the server based on Gin
type Server struct {
appRouter *gin.Engine
httpClient *http.Client
states map[string]*requestState
lock sync.RWMutex
webhook webhook.Webhook
metrics metrics.RevaulterMetrics
// Subscribers that receive public events
pubsub *broker.Broker[*requestStatePublic]
// Subscriptions to watch for state changes
// Each state can only have one subscription
// If another call tries to subscribe to the same state, it will evict the first call
subs map[string]chan *requestState
// Servers
appSrv *http.Server
metricsSrv *http.Server
// Method that forces a reload of TLS certificates from disk
tlsCertWatchFn tlsCertWatchFn
// TLS configuration for the app server
tlsConfig *tls.Config
running atomic.Bool
wg sync.WaitGroup
// Listeners for the app and metrics servers
// These can be used for testing without having to start an actual TCP listener
appListener net.Listener
metricsListener net.Listener
// Factory for keyvault.Client objects
// This is defined as a property to allow for mocking
kvClientFactory keyvault.ClientFactory
}
// NewServer creates a new Server object and initializes it
func NewServer(log *zerolog.Logger, webhook webhook.Webhook) (*Server, error) {
s := &Server{
states: map[string]*requestState{},
subs: map[string]chan *requestState{},
pubsub: broker.NewBroker[*requestStatePublic](),
webhook: webhook,
httpClient: &http.Client{
Timeout: 15 * time.Second,
},
kvClientFactory: keyvault.NewClient,
}
// Init the object
err := s.init(log)
if err != nil {
return nil, err
}
return s, nil
}
// Init the Server object and create a Gin server
func (s *Server) init(log *zerolog.Logger) error {
// Init the Prometheus metrics
s.metrics.Init()
// Init the app server
err := s.initAppServer(log)
if err != nil {
return err
}
return nil
}
func (s *Server) initAppServer(log *zerolog.Logger) (err error) {
// Load the TLS configuration
s.tlsConfig, s.tlsCertWatchFn, err = s.loadTLSConfig(log)
if err != nil {
return fmt.Errorf("failed to load TLS configuration: %w", err)
}
// Set the baseURL in the webhook
s.webhook.SetBaseURL(s.getBaseURL())
// Create the Gin router and add various middlewares
s.appRouter = gin.New()
s.appRouter.Use(gin.Recovery())
s.appRouter.Use(s.RequestIdMiddleware)
s.appRouter.Use(s.LoggerMiddleware(log))
// CORS configuration
corsConfig := cors.Config{
AllowMethods: []string{
http.MethodGet,
http.MethodPost,
http.MethodHead,
},
AllowHeaders: []string{
"Authorization",
"Origin",
"Content-Length",
"Content-Type",
},
ExposeHeaders: []string{
"Retry-After",
"Content-Type",
},
AllowCredentials: false,
MaxAge: 12 * time.Hour,
}
// Check if we are restricting the origins for CORS
origins := config.Get().Origins
switch {
case len(origins) == 0 || (len(origins) == 1 && origins[0] == ""):
// Default is baseUrl
corsConfig.AllowOrigins = []string{s.getBaseURL()}
case len(origins) == 1 && origins[0] == "*":
corsConfig.AllowAllOrigins = true
default:
corsConfig.AllowAllOrigins = false
corsConfig.AllowOrigins = origins
}
s.appRouter.Use(cors.New(corsConfig))
// Logger middleware that removes the auth code from the URL
codeFilterLogMw := s.LoggerMaskMiddleware(regexp.MustCompile(`(\?|&)(code|state|session_state)=([^&]*)`), "$1$2***")
// Middleware to allow certain IPs
allowIpMw, err := s.AllowIpMiddleware()
if err != nil {
return err
}
// Add routes
// Start with the healthz route
s.appRouter.GET("/healthz", gin.WrapF(s.RouteHealthzHandler))
// Requests - these share the /request prefix and all use the allow IP middleware
requestRouteGroup := s.appRouter.Group("/request", allowIpMw, s.RequestKeyMiddleware())
requestRouteGroup.GET("/result/:state", s.RouteRequestResult)
requestRouteGroup.POST("/encrypt", s.RouteRequestOperations(OperationEncrypt))
requestRouteGroup.POST("/decrypt", s.RouteRequestOperations(OperationDecrypt))
requestRouteGroup.POST("/sign", s.RouteRequestOperations(OperationSign))
requestRouteGroup.POST("/verify", s.RouteRequestOperations(OperationVerify))
requestRouteGroup.POST("/wrapkey", s.RouteRequestOperations(OperationWrapKey))
requestRouteGroup.POST("/unwrapkey", s.RouteRequestOperations(OperationUnwrapKey))
// API routes - these share the /api prefix
apiRouteGroup := s.appRouter.Group("/api")
apiRouteGroup.GET("/list",
s.AccessTokenMiddleware(AccessTokenMiddlewareOpts{Required: true}),
s.RouteApiListGet,
)
apiRouteGroup.POST("/confirm",
s.AccessTokenMiddleware(AccessTokenMiddlewareOpts{Required: true, AllowAccessTokenInHeader: true}),
s.RouteApiConfirmPost,
)
// Auth routes - these share the /auth prefix
authRouteGroup := s.appRouter.Group("/auth")
authRouteGroup.GET("/signin", s.RouteAuthSignin)
authRouteGroup.GET("/confirm", codeFilterLogMw, s.RouteAuthConfirm)
// Static files as fallback
s.appRouter.NoRoute(s.serveClient())
return nil
}
func (s *Server) getBaseURL() string {
cfg := config.Get()
// If there's an explicit value in the configuration, use that
if cfg.BaseUrl != "" {
return cfg.BaseUrl
}
// Build our own
if s.tlsConfig != nil {
return "https://localhost:" + strconv.Itoa(cfg.Port)
} else {
return "http://localhost:" + strconv.Itoa(cfg.Port)
}
}
// Run the web server
// Note this function is blocking, and will return only when the servers are shut down via context cancellation.
func (s *Server) Run(ctx context.Context) error {
if !s.running.CompareAndSwap(false, true) {
return errors.New("server is already running")
}
defer s.running.Store(false)
defer s.wg.Wait()
cfg := config.Get()
// App server
s.wg.Add(1)
err := s.startAppServer(ctx)
if err != nil {
return fmt.Errorf("failed to start app server: %w", err)
}
defer func() {
// Handle graceful shutdown
defer s.wg.Done()
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 5*time.Second)
err := s.appSrv.Shutdown(shutdownCtx)
shutdownCancel()
if err != nil {
// Log the error only (could be context canceled)
zerolog.Ctx(ctx).Warn().
Err(err).
Msg("App server shutdown error")
}
}()
defer s.pubsub.Shutdown()
// Metrics server
if cfg.EnableMetrics {
s.wg.Add(1)
err = s.startMetricsServer(ctx)
if err != nil {
return fmt.Errorf("failed to start metrics server: %w", err)
}
defer func() {
// Handle graceful shutdown
defer s.wg.Done()
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
err := s.metricsSrv.Shutdown(shutdownCtx)
shutdownCancel()
if err != nil {
// Log the error only (could be context canceled)
zerolog.Ctx(ctx).Warn().
Err(err).
Msg("Metrics server shutdown error")
}
}()
}
// If we have a tlsCertWatchFn, invoke that
if s.tlsCertWatchFn != nil {
err = s.tlsCertWatchFn(ctx)
if err != nil {
return fmt.Errorf("failed to watch for TLS certificates: %w", err)
}
}
// Block until the context is canceled
<-ctx.Done()
// Servers are stopped with deferred calls
return nil
}
func (s *Server) startAppServer(ctx context.Context) error {
cfg := config.Get()
log := zerolog.Ctx(ctx)
// Create the HTTP(S) server
s.appSrv = &http.Server{
Addr: net.JoinHostPort(cfg.Bind, strconv.Itoa(cfg.Port)),
MaxHeaderBytes: 1 << 20,
ReadHeaderTimeout: 10 * time.Second,
}
if s.tlsConfig != nil {
// Using TLS
s.appSrv.Handler = s.appRouter
s.appSrv.TLSConfig = s.tlsConfig
} else {
// Not using TLS
// Here we also need to enable HTTP/2 Cleartext
log.Warn().Msg("Starting app server without TLS - this is not recommended unless Revaulter is exposed through a proxy that offers TLS termination")
h2s := &http2.Server{}
s.appSrv.Handler = h2c.NewHandler(s.appRouter, h2s)
}
// Create the listener if we don't have one already
if s.appListener == nil {
var err error
s.appListener, err = net.Listen("tcp", s.appSrv.Addr)
if err != nil {
return fmt.Errorf("failed to create TCP listener: %w", err)
}
}
// Start the HTTP(S) server in a background goroutine
log.Info().
Str("bind", cfg.Bind).
Int("port", cfg.Port).
Bool("tls", s.tlsConfig != nil).
Str("url", s.getBaseURL()).
Msg("App server started")
go func() {
defer s.appListener.Close()
// Next call blocks until the server is shut down
var srvErr error
if s.tlsConfig != nil {
srvErr = s.appSrv.ServeTLS(s.appListener, "", "")
} else {
srvErr = s.appSrv.Serve(s.appListener)
}
if srvErr != http.ErrServerClosed {
log.Fatal().Err(srvErr).Msgf("Error starting app server")
}
}()
return nil
}
func (s *Server) startMetricsServer(ctx context.Context) error {
cfg := config.Get()
log := zerolog.Ctx(ctx)
// Handler
mux := http.NewServeMux()
mux.HandleFunc("/healthz", s.RouteHealthzHandler)
mux.Handle("/metrics", s.metrics.HTTPHandler())
// Create the HTTP server
s.metricsSrv = &http.Server{
Addr: net.JoinHostPort(cfg.MetricsBind, strconv.Itoa(cfg.MetricsPort)),
Handler: mux,
MaxHeaderBytes: 1 << 20,
ReadHeaderTimeout: 10 * time.Second,
}
// Create the listener if we don't have one already
if s.metricsListener == nil {
var err error
s.metricsListener, err = net.Listen("tcp", s.metricsSrv.Addr)
if err != nil {
return fmt.Errorf("failed to create TCP listener: %w", err)
}
}
// Start the HTTPS server in a background goroutine
log.Info().
Str("bind", cfg.MetricsBind).
Int("port", cfg.MetricsPort).
Msg("Metrics server started")
go func() {
defer s.metricsListener.Close()
// Next call blocks until the server is shut down
srvErr := s.metricsSrv.Serve(s.metricsListener)
if srvErr != http.ErrServerClosed {
log.Fatal().Err(srvErr).Msgf("Error starting metrics server")
}
}()
return nil
}
// Adds a subscription to a state by key
// If another subscription to the same key exists, evicts that first
// Important: invocations must be wrapped in s.lock being locked
func (s *Server) subscribeToState(stateId string) chan *requestState {
ch := s.subs[stateId]
if ch != nil {
// Close the previous subscription
close(ch)
}
// Create a new subscription
ch = make(chan *requestState, 1)
s.subs[stateId] = ch
return ch
}
// Removes a subscription to a state by key, only if the channel matches the given one
// Important: invocations must be wrapped in s.lock being locked
func (s *Server) unsubscribeToState(stateId string, watch chan *requestState) {
ch := s.subs[stateId]
if ch != nil && ch == watch {
close(ch)
delete(s.subs, stateId)
}
}
// Sends a notification to a state subscriber, if any
// The channel is then closed right after
// Important: invocations must be wrapped in s.lock being locked
func (s *Server) notifySubscriber(stateId string, state *requestState) {
ch := s.subs[stateId]
if ch == nil {
return
}
// Send the notification
ch <- state
// Close the channel and remove it from the subscribers
close(ch)
delete(s.subs, stateId)
}
// This method makes a pending request expire after the given time interval
// It should be invoked in a background goroutine
func (s *Server) expireRequest(ctx context.Context, stateId string, validity time.Duration) {
// Wait until the request is expired
time.Sleep(validity)
// Acquire a lock to ensure consistency
s.lock.Lock()
defer s.lock.Unlock()
// Check if the request still exists
req := s.states[stateId]
if req == nil {
return
}
zerolog.Ctx(ctx).Info().Msg("Removing expired operation " + stateId)
// Set the request as canceled
req.Status = StatusCanceled
// If there's a subscription, send a notification
ch, ok := s.subs[stateId]
if ok {
if ch != nil {
ch <- req
close(ch)
}
delete(s.subs, stateId)
}
// Delete the state object
delete(s.states, stateId)
// Publish a message that the request has been removed
go s.pubsub.Publish(&requestStatePublic{
State: stateId,
Status: StatusRemoved.String(),
})
// Record the result
s.metrics.RecordResult("expired")
}
// Loads the TLS configuration
func (s *Server) loadTLSConfig(log *zerolog.Logger) (tlsConfig *tls.Config, watchFn tlsCertWatchFn, err error) {
cfg := config.Get()
tlsConfig = &tls.Config{
MinVersion: minTLSVersion,
}
// First, check if we have actual keys
tlsCert := cfg.TLSCertPEM
tlsKey := cfg.TLSKeyPEM
// If we don't have actual keys, then we need to load from file and reload when the files change
if tlsCert == "" && tlsKey == "" {
// If "tlsPath" is empty, use the folder where the config file is located
tlsPath := cfg.TLSPath
if tlsPath == "" {
file := cfg.GetLoadedConfigPath()
if file != "" {
tlsPath = filepath.Dir(file)
}
}
if tlsPath == "" {
// No config file loaded, so don't attempt to load TLS certs
return nil, nil, nil
}
var provider *tlsCertProvider
provider, err = newTLSCertProvider(tlsPath)
if err != nil {
return nil, nil, fmt.Errorf("failed to load TLS certificates from path '%s': %w", tlsPath, err)
}
// If newTLSCertProvider returns nil, there are no TLS certificates, so disable TLS
if provider == nil {
return nil, nil, nil
}
log.Debug().
Str("path", tlsPath).
Msg("Loaded TLS certificates from disk")
tlsConfig.GetCertificate = provider.GetCertificateFn()
return tlsConfig, provider.Watch, nil
}
// Assume the values from the config file are PEM-encoded certs and key
if tlsCert == "" || tlsKey == "" {
// If tlsCert and/or tlsKey is empty, do not use TLS
return nil, nil, nil
}
cert, err := tls.X509KeyPair([]byte(tlsCert), []byte(tlsKey))
if err != nil {
return nil, nil, fmt.Errorf("failed to parse TLS certificate or key: %w", err)
}
tlsConfig.Certificates = []tls.Certificate{cert}
log.Debug().Msg("Loaded TLS certificates from PEM values")
return tlsConfig, nil, nil
}
type operationResponse struct {
State string `json:"state"`
Pending bool `json:"pending,omitempty"`
Done bool `json:"done,omitempty"`
Failed bool `json:"failed,omitempty"`
keyvault.KeyVaultResponse `json:"response,omitempty"`
}