forked from louketo/louketo-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
441 lines (375 loc) · 12 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
/*
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/elazarl/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
provider oidc.ProviderConfig
// 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)
service := &oauthProxy{
config: config,
prometheusHandler: prometheus.Handler(),
}
// step: parse the upstream endpoint
if service.endpoint, err = url.Parse(config.Upstream); err != nil {
return nil, err
}
// step: initialize the store if any
if config.StoreURL != "" {
if service.store, err = createStorage(config.StoreURL); err != nil {
return nil, err
}
}
// step: initialize the openid client
if !config.SkipTokenVerification {
if service.client, service.provider, err = createOpenIDClient(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 := service.createForwardingProxy(); err != nil {
return nil, err
}
default:
if err := service.createReverseProxy(); err != nil {
return nil, err
}
}
return service, 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)
}
// 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: 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())
}
// step: add the routing
oauth := engine.Group(oauthURL).Use(r.corsMiddleware(r.config.CrossOrigin))
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)
// step: is the login hanler enabled?
if r.config.EnableLoginHandler {
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), r.reverseProxyMiddleware())
// 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 != "" {
// step: read in the ca
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 {
tlsConfig := &tls.Config{}
// step: are we doing mutual tls?
if r.config.TLSCaCertificate != "" {
log.Infof("enabling mutual tls, reading in the signing ca: %s", r.config.TLSCaCertificate)
caCert, err := ioutil.ReadFile(r.config.TLSCaCertificate)
if err != nil {
return err
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
tlsConfig.ClientCAs = caCertPool
tlsConfig.ClientAuth = tls.RequireAndVerifyClientCert
}
server := &http.Server{
Addr: r.config.Listen,
Handler: r.router,
}
// step: create the listener
var listener net.Listener
var err error
switch strings.HasPrefix(r.config.Listen, "unix://") {
case true:
socket := strings.Trim(r.config.Listen, "unix://")
// step: delete the socket if it exists
if exists := fileExists(socket); exists {
if err = os.Remove(socket); err != nil {
return err
}
}
log.Infof("listening on unix socket: %s", r.config.Listen)
if listener, err = net.Listen("unix", socket); err != nil {
return err
}
default:
listener, err = net.Listen("tcp", r.config.Listen)
if err != nil {
return err
}
}
// step: configure tls
if r.config.TLSCertificate != "" && r.config.TLSPrivateKey != "" {
server.TLSConfig = tlsConfig
if tlsConfig.NextProtos == nil {
tlsConfig.NextProtos = []string{"http/1.1"}
}
if len(tlsConfig.Certificates) == 0 || r.config.TLSCertificate != "" || r.config.TLSPrivateKey != "" {
tlsConfig.Certificates = make([]tls.Certificate, 1)
if tlsConfig.Certificates[0], err = tls.LoadX509KeyPair(r.config.TLSCertificate, r.config.TLSPrivateKey); err != nil {
return err
}
}
log.Infof("tls enabled, certificate: %s, key: %s", r.config.TLSCertificate, r.config.TLSPrivateKey)
listener = tls.NewListener(listener, tlsConfig)
}
// step: wrap the listen in a proxy protocol
if r.config.EnableProxyProtocol {
log.Infof("enabling the proxy protocol on listener: %s", r.config.Listen)
listener = &proxyproto.Listener{Listener: listener}
}
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 service")
}
}()
return 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
}