-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathverify.go
99 lines (85 loc) · 2.15 KB
/
verify.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
package verify
import (
"context"
"crypto/x509"
"net"
"net/http"
"os"
"cloud.google.com/go/firestore"
"github.com/go-chi/chi"
"github.com/rs/zerolog/log"
"golang.org/x/sync/errgroup"
"github.com/pomerium/verify/internal/storage"
)
// Server is the verify server backend.
type Server struct {
cfg *config
http *http.Server
router chi.Router
storage storage.Backend
tlsVerifier *tlsVerifier
}
// New creates a new Server.
func New(options ...Option) *Server {
cfg := getConfig(options...)
var verifierOpts tlsVerifierOptions
if len(cfg.extraCACerts) > 0 {
pool, err := x509.SystemCertPool()
if err != nil {
log.Fatal().Err(err).Msg("failed to load system CA certs")
}
for _, certPath := range cfg.extraCACerts {
cert, err := os.ReadFile(certPath)
if err != nil {
log.Fatal().Err(err).Str("path", certPath).Msg("failed to read CA cert")
} else {
log.Info().Str("path", certPath).Msg("adding extra CA cert")
}
ok := pool.AppendCertsFromPEM(cert)
if !ok {
log.Warn().Str("path", certPath).Msg("no CA certs found in file")
}
}
verifierOpts.rootCAs = pool
}
return &Server{
cfg: cfg,
tlsVerifier: newTLSVerifier(verifierOpts),
}
}
// Run runs the server.
func (srv *Server) Run(ctx context.Context) error {
eg, ctx := errgroup.WithContext(ctx)
err := srv.init(ctx)
if err != nil {
return err
}
eg.Go(func() error {
log.Info().
Str("bind-addr", srv.cfg.bindAddress).
Msg("starting http server")
return srv.http.ListenAndServe()
})
return eg.Wait()
}
func (srv *Server) init(ctx context.Context) error {
log.Info().
Str("project-id", srv.cfg.firestoreProjectID).
Msg("connecting to firestore")
client, err := firestore.NewClient(ctx, srv.cfg.firestoreProjectID)
if err == nil {
srv.storage = storage.NewFirestoreBackend(client)
} else {
log.Error().Err(err).Msg("failed to create firestore client, falling back to in-memory storage")
srv.storage = storage.NewInMemoryBackend()
}
srv.initRouter()
srv.http = &http.Server{
Addr: srv.cfg.bindAddress,
BaseContext: func(l net.Listener) context.Context {
return ctx
},
Handler: srv.router,
}
return nil
}