-
-
Notifications
You must be signed in to change notification settings - Fork 359
/
server.go
198 lines (169 loc) · 5.3 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
package server
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"net/http"
"net/http/httputil"
"sync"
negronilogrus "github.com/meatballhat/negroni-logrus"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/urfave/negroni"
"github.com/ory/viper"
"github.com/ory/x/healthx"
"github.com/ory/graceful"
"github.com/ory/x/corsx"
"github.com/ory/x/logrusx"
"github.com/ory/x/metricsx"
"github.com/ory/x/tlsx"
"github.com/ory/oathkeeper/api"
"github.com/ory/oathkeeper/driver"
"github.com/ory/oathkeeper/driver/configuration"
"github.com/ory/oathkeeper/x"
)
func runProxy(d driver.Driver, n *negroni.Negroni, logger *logrus.Logger) func() {
return func() {
proxy := d.Registry().Proxy()
handler := &httputil.ReverseProxy{
Director: proxy.Director,
Transport: proxy,
}
n.Use(negronilogrus.NewMiddlewareFromLogger(logger, "oathkeeper-proxy"))
n.UseHandler(handler)
h := corsx.Initialize(n, logger, "serve.proxy")
certs := cert("proxy", logger)
addr := d.Configuration().ProxyServeAddress()
server := graceful.WithDefaults(&http.Server{
Addr: addr,
Handler: h,
TLSConfig: &tls.Config{Certificates: certs},
ReadTimeout: d.Configuration().ProxyReadTimeout(),
WriteTimeout: d.Configuration().ProxyWriteTimeout(),
IdleTimeout: d.Configuration().ProxyIdleTimeout(),
})
if err := graceful.Graceful(func() error {
if certs != nil {
logger.Printf("Listening on https://%s", addr)
return server.ListenAndServeTLS("", "")
}
logger.Infof("Listening on http://%s", addr)
return server.ListenAndServe()
}, server.Shutdown); err != nil {
logger.Fatalf("Unable to gracefully shutdown HTTP(s) server because %v", err)
return
}
logger.Println("HTTP(s) server was shutdown gracefully")
}
}
func runAPI(d driver.Driver, n *negroni.Negroni, logger *logrus.Logger) func() {
return func() {
router := x.NewAPIRouter()
d.Registry().RuleHandler().SetRoutes(router)
d.Registry().HealthHandler().SetRoutes(router.Router, true)
d.Registry().CredentialHandler().SetRoutes(router)
n.Use(negronilogrus.NewMiddlewareFromLogger(logger, "oathkeeper-api"))
n.Use(d.Registry().DecisionHandler()) // This needs to be the last entry, otherwise the judge API won't work
n.UseHandler(router)
h := corsx.Initialize(n, logger, "serve.api")
certs := cert("api", logger)
addr := d.Configuration().APIServeAddress()
server := graceful.WithDefaults(&http.Server{
Addr: addr,
Handler: h,
TLSConfig: &tls.Config{Certificates: certs},
})
if err := graceful.Graceful(func() error {
if certs != nil {
logger.Printf("Listening on https://%s", addr)
return server.ListenAndServeTLS("", "")
}
logger.Infof("Listening on http://%s", addr)
return server.ListenAndServe()
}, server.Shutdown); err != nil {
logger.Fatalf("Unable to gracefully shutdown HTTP(s) server because %v", err)
return
}
logger.Println("HTTP server was shutdown gracefully")
}
}
func cert(daemon string, logger logrus.FieldLogger) []tls.Certificate {
cert, err := tlsx.Certificate(
viper.GetString("serve."+daemon+".tls.cert.base64"),
viper.GetString("serve."+daemon+".tls.key.base64"),
viper.GetString("serve."+daemon+".tls.cert.path"),
viper.GetString("serve."+daemon+".tls.key.path"),
)
if err == nil {
logger.Infof("Setting up HTTPS for %s", daemon)
return cert
} else if errors.Cause(err) != tlsx.ErrNoCertificatesConfigured {
logger.WithError(err).Fatalf("Unable to load HTTPS TLS Certificate")
}
logger.Infof("TLS has not been configured for %s, skipping", daemon)
return nil
}
func clusterID(c configuration.Provider) string {
var id bytes.Buffer
if err := json.NewEncoder(&id).Encode(viper.AllSettings()); err != nil {
for _, repo := range append(
append(c.AccessRuleRepositories(), *c.MutatorIDTokenJWKSURL(), *c.MutatorIDTokenIssuerURL()),
c.AuthenticatorJWTJWKSURIs()...,
) {
_, _ = id.WriteString(repo.String())
}
_, _ = id.WriteString(c.ProxyServeAddress())
_, _ = id.WriteString(c.APIServeAddress())
}
return id.String()
}
func isDevelopment(c configuration.Provider) bool {
return len(c.AccessRuleRepositories()) == 0
}
func RunServe(version, build, date string) func(cmd *cobra.Command, args []string) {
return func(cmd *cobra.Command, args []string) {
fmt.Println(banner(version))
logger := logrusx.New()
d := driver.NewDefaultDriver(logger, version, build, date, true)
d.Registry().Init()
adminmw := negroni.New()
publicmw := negroni.New()
metrics := metricsx.New(cmd, logger,
&metricsx.Options{
Service: "ory-oathkeeper",
ClusterID: clusterID(d.Configuration()),
IsDevelopment: isDevelopment(d.Configuration()),
WriteKey: "MSx9A6YQ1qodnkzEFOv22cxOmOCJXMFa",
WhitelistedPaths: []string{
"/",
api.CredentialsPath,
api.DecisionPath,
api.RulesPath,
healthx.VersionPath,
healthx.AliveCheckPath,
healthx.ReadyCheckPath,
},
BuildVersion: version,
BuildTime: build,
BuildHash: date,
},
)
adminmw.Use(metrics)
publicmw.Use(metrics)
var wg sync.WaitGroup
tasks := []func(){
runAPI(d, adminmw, logger),
runProxy(d, publicmw, logger),
}
wg.Add(len(tasks))
for _, t := range tasks {
go func(t func()) {
defer wg.Done()
t()
}(t)
}
wg.Wait()
}
}