forked from louketo/louketo-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
498 lines (436 loc) · 14.2 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
/*
Copyright 2015 All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"runtime"
"strings"
"time"
httplog "log"
log "github.com/Sirupsen/logrus"
"github.com/armon/go-proxyproto"
"github.com/coreos/go-oidc/oidc"
"github.com/gambol99/goproxy"
"github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus"
)
type oauthProxy struct {
// the proxy configuration
config *Config
// the gin service
router http.Handler
// the opened client
client *oidc.Client
// the openid provider configuration
idp oidc.ProviderConfig
// the provider http client
idpClient *http.Client
// the proxy client
upstream reverseProxy
// the upstream endpoint url
endpoint *url.URL
// the store interface
store storage
// the prometheus handler
prometheusHandler http.Handler
}
func init() {
// step: ensure all time is in UTC
time.LoadLocation("UTC")
// step: set the core
runtime.GOMAXPROCS(runtime.NumCPU())
}
// newProxy create's a new proxy from configuration
func newProxy(config *Config) (*oauthProxy, error) {
var err error
// step: set the logger
httplog.SetOutput(ioutil.Discard)
// step: set the logging level
if config.LogJSONFormat {
log.SetFormatter(&log.JSONFormatter{})
}
// step: set the logging level
gin.SetMode(gin.ReleaseMode)
if config.Verbose {
log.SetLevel(log.DebugLevel)
gin.SetMode(gin.DebugMode)
httplog.SetOutput(os.Stderr)
}
log.Infof("starting %s, author: %s, version: %s, ", prog, author, version)
svc := &oauthProxy{
config: config,
prometheusHandler: prometheus.Handler(),
}
// step: parse the upstream endpoint
if svc.endpoint, err = url.Parse(config.Upstream); err != nil {
return nil, err
}
// step: initialize the store if any
if config.StoreURL != "" {
if svc.store, err = createStorage(config.StoreURL); err != nil {
return nil, err
}
}
// step: initialize the openid client
if !config.SkipTokenVerification {
if svc.client, svc.idp, svc.idpClient, err = newOpenIDClient(config); err != nil {
return nil, err
}
} else {
log.Warnf("TESTING ONLY CONFIG - the verification of the token have been disabled")
}
if config.ClientID == "" && config.ClientSecret == "" {
log.Warnf("Note: client credentials are not set, depending on provider (confidential|public) you might be unable to auth")
}
// step: are we running in forwarding more?
switch config.EnableForwarding {
case true:
if err := svc.createForwardingProxy(); err != nil {
return nil, err
}
default:
if err := svc.createReverseProxy(); err != nil {
return nil, err
}
}
return svc, nil
}
// createReverseProxy creates a reverse proxy
func (r *oauthProxy) createReverseProxy() error {
log.Infof("enabled reverse proxy mode, upstream url: %s", r.config.Upstream)
// step: display the protected resources
for _, resource := range r.config.Resources {
log.Infof("protecting resources under uri: %s", resource)
}
for name, value := range r.config.MatchClaims {
log.Infof("the token must container the claim: %s, required: %s", name, value)
}
if r.config.RedirectionURL == "" {
log.Warnf("no redirection url has been set, will use host headers")
}
// step: initialize the reverse http proxy
if err := r.createUpstreamProxy(r.endpoint); err != nil {
return err
}
// step: create the gin router
engine := gin.New()
engine.Use(gin.Recovery())
// step: custom filtering
engine.Use(r.filterMiddleware(), r.reverseProxyMiddleware())
// step: is profiling enabled?
if r.config.EnableProfiling {
log.Warn("Enabling the debug profiling on /debug/pprof")
engine.Any("/debug/pprof/:name", r.debugHandler)
}
// step: are we logging the traffic?
if r.config.LogRequests {
engine.Use(r.loggingMiddleware())
}
// step: enabling the metrics?
if r.config.EnableMetrics {
engine.Use(r.metricsMiddleware())
}
// step: enabling the security filter?
if r.config.EnableSecurityFilter {
engine.Use(r.securityMiddleware())
}
cors := Cors{
Origins: r.config.CorsOrigins,
Methods: r.config.CorsMethods,
Headers: r.config.CorsHeaders,
ExposedHeaders: r.config.CorsExposedHeaders,
Credentials: r.config.CorsCredentials,
MaxAge: r.config.CorsMaxAge,
}
// step: enabling globaling?
if r.config.EnableCorsGlobal {
log.Info("enabling CORs header injection globally")
engine.Use(r.corsMiddleware(cors))
}
// step: add the routing and cors middleware
oauth := engine.Group(oauthURL)
if !r.config.EnableCorsGlobal {
oauth.Use(r.corsMiddleware(cors))
}
oauth.GET(authorizationURL, r.oauthAuthorizationHandler)
oauth.GET(callbackURL, r.oauthCallbackHandler)
oauth.GET(healthURL, r.healthHandler)
oauth.GET(tokenURL, r.tokenHandler)
oauth.GET(expiredURL, r.expirationHandler)
oauth.GET(logoutURL, r.logoutHandler)
oauth.POST(loginURL, r.loginHandler)
// step: enable the metric page?
if r.config.EnableMetrics {
oauth.GET(metricsURL, r.metricsHandler)
}
// step: add the middleware
engine.Use(r.entrypointMiddleware(),
r.authenticationMiddleware(),
r.admissionMiddleware(),
r.headersMiddleware(r.config.AddClaims))
// step: set the handler
r.router = engine
// step: load the templates
if err := r.createTemplates(); err != nil {
return err
}
return nil
}
// createForwardingProxy creates a forwarding proxy
func (r *oauthProxy) createForwardingProxy() error {
log.Infof("enabling forward signing mode, listening on %s", r.config.Listen)
if r.config.SkipUpstreamTLSVerify {
log.Warnf("TLS verification switched off. In forward signing mode it's recommended you verify! (--skip-upstream-tls-verify=false)")
}
// step: initialize the reverse http proxy
if err := r.createUpstreamProxy(nil); err != nil {
return err
}
// step: setup and initialize the handler
forwardingHandler := r.forwardProxyHandler()
// step: set the http handler
proxy := r.upstream.(*goproxy.ProxyHttpServer)
r.router = proxy
// step: setup the tls configuration
if r.config.TLSCaCertificate != "" && r.config.TLSCaPrivateKey != "" {
ca, err := loadCA(r.config.TLSCaCertificate, r.config.TLSCaPrivateKey)
if err != nil {
return fmt.Errorf("unable to load certificate authority, error: %s", err)
}
// step: implement the goproxy connect method
proxy.OnRequest().HandleConnectFunc(
func(host string, ctx *goproxy.ProxyCtx) (*goproxy.ConnectAction, string) {
return &goproxy.ConnectAction{
Action: goproxy.ConnectMitm,
TLSConfig: goproxy.TLSConfigFromCA(ca),
}, host
},
)
} else {
// step: use the default certificate provided by goproxy
proxy.OnRequest().HandleConnect(goproxy.AlwaysMitm)
}
proxy.OnResponse().DoFunc(func(resp *http.Response, ctx *goproxy.ProxyCtx) *http.Response {
// @NOTES, somewhat annoying but goproxy hands back a nil response on proxy client errors
if resp != nil && r.config.LogRequests {
start := ctx.UserData.(time.Time)
latency := time.Now().Sub(start)
log.WithFields(log.Fields{
"method": resp.Request.Method,
"status": resp.StatusCode,
"bytes": resp.ContentLength,
"host": resp.Request.Host,
"path": resp.Request.URL.Path,
"latency": latency.String(),
}).Infof("[%d] |%s| |%10v| %-5s %s", resp.StatusCode, resp.Request.Host, latency, resp.Request.Method, resp.Request.URL.Path)
}
return resp
})
proxy.OnRequest().DoFunc(func(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
ctx.UserData = time.Now()
// step: forward into the handler
forwardingHandler(req, ctx.Resp)
return req, ctx.Resp
})
return nil
}
// Run starts the proxy service
func (r *oauthProxy) Run() error {
// step: create the service listener
listener, err := createHTTPListener(listenerConfig{
listen: r.config.Listen,
certificate: r.config.TLSCertificate,
privateKey: r.config.TLSPrivateKey,
ca: r.config.TLSCaCertificate,
clientCert: r.config.TLSClientCertificate,
proxyProtocol: r.config.EnableProxyProtocol,
})
if err != nil {
return err
}
// step: create the http server
server := &http.Server{
Addr: r.config.Listen,
Handler: r.router,
}
go func() {
log.Infof("keycloak proxy service starting on %s", r.config.Listen)
if err = server.Serve(listener); err != nil {
log.WithFields(log.Fields{
"error": err.Error(),
}).Fatalf("failed to start the http service")
}
}()
// step: are we running http service as well?
if r.config.ListenHTTP != "" {
log.Infof("keycloak proxy service starting on %s", r.config.ListenHTTP)
httpListener, err := createHTTPListener(listenerConfig{
listen: r.config.ListenHTTP,
proxyProtocol: r.config.EnableProxyProtocol,
})
if err != nil {
return err
}
httpsvc := &http.Server{
Addr: r.config.ListenHTTP,
Handler: r.router,
}
go func() {
if err := httpsvc.Serve(httpListener); err != nil {
log.WithFields(log.Fields{
"error": err.Error(),
}).Fatalf("failed to start the http redirect service")
}
}()
}
return nil
}
// listenerConfig encapsulate listener options
type listenerConfig struct {
listen string // the interface to bind the listener to
certificate string // the path to the certificate if any
privateKey string // the path to the private key if any
ca string // the path to a certificate authority
clientCert string // the path to a client certificate to use for mutual tls
proxyProtocol bool // whether to enable proxy protocol on the listen
}
// createHTTPListener is responsible for creating a listening socket
func createHTTPListener(config listenerConfig) (net.Listener, error) {
var listener net.Listener
var err error
// step: are we create a unix socket or tcp listener?
if strings.HasPrefix(config.listen, "unix://") {
socket := strings.Trim(config.listen, "unix://")
// step: delete the socket if it exists
if exists := fileExists(socket); exists {
if err = os.Remove(socket); err != nil {
return nil, err
}
}
log.Infof("listening on unix socket: %s", config.listen)
if listener, err = net.Listen("unix", socket); err != nil {
return nil, err
}
} else {
if listener, err = net.Listen("tcp", config.listen); err != nil {
return nil, err
}
}
// step: does the socket require TLS?
if config.certificate != "" && config.privateKey != "" {
log.Infof("tls enabled, certificate: %s, key: %s", config.certificate, config.privateKey)
// step: creating a certificate rotation
rotate, err := newCertificateRotator(config.certificate, config.privateKey)
if err != nil {
return nil, err
}
// step: start watching the files for changes
if err := rotate.watch(); err != nil {
return nil, err
}
tlsConfig := &tls.Config{
PreferServerCipherSuites: true,
GetCertificate: rotate.GetCertificate,
}
listener = tls.NewListener(listener, tlsConfig)
// step: are we doing mutual tls?
if config.clientCert != "" {
caCert, err := ioutil.ReadFile(config.clientCert)
if err != nil {
return nil, err
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
tlsConfig.ClientCAs = caCertPool
tlsConfig.ClientAuth = tls.RequireAndVerifyClientCert
}
}
// step: does it require proxy protocol?
if config.proxyProtocol {
log.Infof("enabling the proxy protocol on listener: %s", config.listen)
listener = &proxyproto.Listener{Listener: listener}
}
return listener, nil
}
// createUpstreamProxy create a reverse http proxy from the upstream
func (r *oauthProxy) createUpstreamProxy(upstream *url.URL) error {
// step: create the default dialer
dialer := (&net.Dialer{
KeepAlive: r.config.UpstreamKeepaliveTimeout,
Timeout: r.config.UpstreamTimeout,
}).Dial
// step: are we using a unix socket?
if upstream != nil && upstream.Scheme == "unix" {
log.Infof("using the unix domain socket: %s%s for upstream", upstream.Host, upstream.Path)
socketPath := fmt.Sprintf("%s%s", upstream.Host, upstream.Path)
dialer = func(network, address string) (net.Conn, error) {
return net.Dial("unix", socketPath)
}
upstream.Path = ""
upstream.Host = "domain-sock"
upstream.Scheme = "http"
}
// step: create the upstream tls configure
tlsConfig := &tls.Config{
InsecureSkipVerify: r.config.SkipUpstreamTLSVerify,
}
// step: are we using a client certificate
// @TODO provide a means of reload on the client certificate when it expires. I'm not sure if it's just a
// case of update the http transport settings - Also we to place this go-routine?
if r.config.TLSClientCertificate != "" {
cert, err := ioutil.ReadFile(r.config.TLSClientCertificate)
if err != nil {
log.Fatal(err)
}
pool := x509.NewCertPool()
pool.AppendCertsFromPEM(cert)
// step: update the upstream tls to use the client certificate
tlsConfig.ClientCAs = pool
tlsConfig.ClientAuth = tls.RequireAndVerifyClientCert
}
// step: create the forwarding proxy
proxy := goproxy.NewProxyHttpServer()
proxy.Logger = httplog.New(ioutil.Discard, "", 0)
r.upstream = proxy
// step: update the tls configuration of the reverse proxy
r.upstream.(*goproxy.ProxyHttpServer).Tr = &http.Transport{
Dial: dialer,
TLSClientConfig: tlsConfig,
DisableKeepAlives: !r.config.UpstreamKeepalives,
}
return nil
}
// createTemplates loads the custom template
func (r *oauthProxy) createTemplates() error {
var list []string
if r.config.SignInPage != "" {
log.Debugf("loading the custom sign in page: %s", r.config.SignInPage)
list = append(list, r.config.SignInPage)
}
if r.config.ForbiddenPage != "" {
log.Debugf("loading the custom sign forbidden page: %s", r.config.ForbiddenPage)
list = append(list, r.config.ForbiddenPage)
}
if len(list) > 0 {
log.Infof("loading the custom templates: %s", strings.Join(list, ","))
r.router.(*gin.Engine).LoadHTMLFiles(list...)
}
return nil
}