Debian Codegraph @

server/server.go

raw symbols SCIP
1 //
2 // Copyright (c) SAS Institute Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 package server
18
19 import (
20 "fmt"
21 "net/http"
22 "time"
23
24 "github.com/go-chi/chi/v5"
25 "github.com/sassoftware/relic/v7/config"
26 "github.com/sassoftware/relic/v7/internal/authmodel"
27 "github.com/sassoftware/relic/v7/internal/realip"
28 "github.com/sassoftware/relic/v7/internal/zhttp"
29 "github.com/sassoftware/relic/v7/lib/compresshttp"
30 "github.com/sassoftware/relic/v7/token"
31 "github.com/sassoftware/relic/v7/token/open"
32 "github.com/sassoftware/relic/v7/token/tokencache"
33 "github.com/sassoftware/relic/v7/token/worker"
34 )
35
36 type Server struct {
37 Config *config.Config
38 Closed <-chan bool
39 closeCh chan<- bool
40 tokens map[string]token.Token
41 auth authmodel.Authenticator
42 realIP func(http.Handler) http.Handler
43 }
44
45 func (s *Server) Handler() http.Handler {
46 r := chi.NewRouter()
47 r.Use(s.realIP)
48 r.Use(zhttp.LoggingMiddleware())
49 r.Use(zhttp.RecoveryMiddleware)
50 r.Use(compresshttp.Middleware)
51 // unauthenticated methods
52 r.Get("/health", s.serveHealth)
53 r.Get("/directory", handleFunc(s.serveDirectory))
54 // authenticated methods
55 a := r.With(authmodel.Middleware(s.auth))
56 a.Get("/", handleFunc(s.serveHome))
57 a.Get("/list_keys", handleFunc(s.serveListKeys))
58 a.Get("/keys/{key}", handleFunc(s.serveGetKey))
59 a.Post("/sign", handleFunc(s.serveSign))
60 return r
61 }
62
63 func (s *Server) Close() error {
64 if s.closeCh != nil {
65 close(s.closeCh)
66 s.closeCh = nil
67 }
68 for _, t := range s.tokens {
69 t.Close()
70 }
71 return nil
72 }
73
74 func New(config *config.Config) (*Server, error) {
75 closed := make(chan bool)
76 auth, err := authmodel.New(config)
77 if err != nil {
78 return nil, fmt.Errorf("configuration authentication: %w", err)
79 }
80 realIP, err := realip.Middleware(config.Server.TrustedProxies)
81 if err != nil {
82 return nil, err
83 }
84 s := &Server{
85 Config: config,
86 Closed: closed,
87 closeCh: closed,
88 auth: auth,
89 realIP: realIP,
90 tokens: make(map[string]token.Token),
91 }
92 if err := s.openTokens(); err != nil {
93 for _, t := range s.tokens {
94 t.Close()
95 }
96 return nil, err
97 }
98 if err := s.startHealthCheck(); err != nil {
99 return nil, err
100 }
101 return s, nil
102 }
103
104 // Open each token used by any key. pkcs11 tokens get a worker, while other
105 // types are used in-process via a cache.
106 func (s *Server) openTokens() error {
107 expiry := time.Second * time.Duration(s.Config.Server.TokenCacheSeconds)
108 for _, name := range s.Config.ListServedTokens() {
109 tconf, err := s.Config.GetToken(name)
110 if err != nil {
111 return err
112 }
113 var tok token.Token
114 switch tconf.Type {
115 case "pkcs11":
116 // worker is responsible for metrics and caching
117 tok, err = worker.New(s.Config, name)
118 default:
119 tok, err = open.Token(s.Config, name, nil)
120 if err == nil {
121 // instrument token with metrics and caching
122 tok = tokencache.Metrics{Token: tok}
123 if tconf.RateLimit != 0 {
124 tok = tokencache.NewLimiter(tok, tconf.RateLimit, tconf.RateBurst)
125 }
126 tok = tokencache.New(tok, expiry)
127 }
128 }
129 if err != nil {
130 return fmt.Errorf("configuring token %q: %w", name, err)
131 }
132 s.tokens[name] = tok
133 }
134 return nil
135 }