forked from dustin-decker/saml-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
207 lines (182 loc) · 5.41 KB
/
main.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
package main
import (
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"path"
"runtime"
"time"
yaml "gopkg.in/yaml.v2"
"net/http/pprof"
log "github.com/Sirupsen/logrus"
"github.com/crewjam/saml/samlsp"
"github.com/vulcand/oxy/buffer"
"github.com/vulcand/oxy/cbreaker"
"github.com/vulcand/oxy/forward"
"github.com/vulcand/oxy/ratelimit"
"github.com/vulcand/oxy/roundrobin"
"github.com/vulcand/oxy/trace"
"github.com/vulcand/oxy/utils"
goji "goji.io"
"goji.io/pat"
)
// Config for reverse proxy settings and RBAC users and groups
// Unmarshalled from config on disk
type Config struct {
ListenInterface string `yaml:"listen_interface"`
ListenPort int `yaml:"listen_port"`
Targets []string
IdpMetadataURL string `yaml:"idp_metadata_url"`
ServiceRootURL string `yaml:"service_root_url"`
Cert string
Key string
RateLimitAvgMinute int64 `yaml:"rate_limit_avg_minute"`
RateLimitBurstSecond int64 `yaml:"rate_limit_burst_second"`
TraceRequestHeaders []string `yaml:"trace_request_headers"`
CookieMaxAge time.Duration `yaml:"cookie_max_age"`
LogLevel string `yaml:"log_level"`
}
func (C *Config) getConf() *Config {
pwd, _ := os.Getwd()
yamlFile, err := ioutil.ReadFile(path.Join(pwd, os.Args[1]))
if err != nil {
log.Error(err)
}
err = yaml.Unmarshal(yamlFile, C)
if err != nil {
log.Error(err)
}
return C
}
func attachProfiler(router *goji.Mux) {
router.HandleFunc(pat.New("/debug/pprof/"), pprof.Index)
router.HandleFunc(pat.New("/debug/pprof/cmdline"), pprof.Cmdline)
router.HandleFunc(pat.New("/debug/pprof/profile"), pprof.Profile)
router.HandleFunc(pat.New("/debug/pprof/symbol"), pprof.Symbol)
// Manually add support for paths linked to by index page at /debug/pprof/
router.Handle(pat.New("/debug/pprof/goroutine"), pprof.Handler("goroutine"))
router.Handle(pat.New("/debug/pprof/heap"), pprof.Handler("heap"))
router.Handle(pat.New("/debug/pprof/threadcreate"), pprof.Handler("threadcreate"))
router.Handle(pat.New("/debug/pprof/block"), pprof.Handler("block"))
}
func main() {
var C Config
C.getConf()
log.SetFormatter(&log.JSONFormatter{})
log.SetOutput(os.Stdout)
logLevel, err := log.ParseLevel(C.LogLevel)
if err != nil {
log.Fatal(err)
}
log.SetLevel(logLevel)
go func() {
for {
var m runtime.MemStats
runtime.ReadMemStats(&m)
log.WithFields(log.Fields{
"alloc": fmt.Sprintf("%v", m.Alloc),
"total-alloc": fmt.Sprintf("%v", m.TotalAlloc/1024),
"sys": fmt.Sprintf("%v", m.Sys/1024),
"num-gc": fmt.Sprintf("%v", m.NumGC),
"goroutines": fmt.Sprintf("%v", runtime.NumGoroutine()),
"stop-pause-nanosec": fmt.Sprintf("%v", m.PauseTotalNs),
}).Warn("Process stats")
time.Sleep(60 * time.Second)
}
}()
keyPair, err := tls.LoadX509KeyPair(C.Cert, C.Key)
if err != nil {
log.Fatal(err)
}
keyPair.Leaf, err = x509.ParseCertificate(keyPair.Certificate[0])
if err != nil {
log.Fatal(err)
}
idpMetadataURL, err := url.Parse(C.IdpMetadataURL)
if err != nil {
log.Fatal(err)
}
rootURL, err := url.Parse(C.ServiceRootURL)
if err != nil {
log.Fatal(err)
}
samlSP, _ := samlsp.New(samlsp.Options{
URL: *rootURL,
Key: keyPair.PrivateKey.(*rsa.PrivateKey),
Certificate: keyPair.Leaf,
IDPMetadataURL: idpMetadataURL,
CookieMaxAge: C.CookieMaxAge,
})
// reverse proxy layer
fwd, err := forward.New()
if err != nil {
log.Fatal(err)
}
// rate-limiting layers
extractor, err := utils.NewExtractor("client.ip")
if err != nil {
log.Fatal(err)
}
rates := ratelimit.NewRateSet()
rates.Add(time.Second, C.RateLimitAvgMinute*60, C.RateLimitBurstSecond)
rm, err := ratelimit.New(fwd, extractor, rates)
if err != nil {
log.Fatal(err)
}
// circuit-breaker layer
const triggerNetRatio = `NetworkErrorRatio() > 0.5`
cb, err := cbreaker.New(rm, triggerNetRatio)
if err != nil {
log.Fatal(err)
}
// load balancing layer
lb, err := roundrobin.New(cb)
if err != nil {
log.Fatal(err)
}
// trace layer
trace, err := trace.New(lb, io.Writer(os.Stdout),
trace.Option(trace.RequestHeaders(C.TraceRequestHeaders...)))
if err != nil {
log.Fatal(err)
}
// buffer will read the request body and will replay the request again in case if forward returned status
// corresponding to nework error (e.g. Gateway Timeout)
buffer, err := buffer.New(trace, buffer.Retry(`IsNetworkError() && Attempts() < 3`))
if err != nil {
log.Fatal(err)
}
for _, target := range C.Targets {
targetURL, err := url.Parse(target)
if err != nil {
log.Fatal(err)
}
// add target to the load balancer
lb.UpsertServer(targetURL)
}
// Use mux for explicit paths and so no other routes are accidently exposed
router := goji.NewMux()
if logLevel == log.DebugLevel || logLevel == log.InfoLevel {
attachProfiler(router)
}
// This endpoint handles SAML auth flow
router.Handle(pat.New("/saml/*"), samlSP)
// These endpoints require valid session cookie
router.Handle(pat.New("/*"), samlSP.RequireAccount(buffer))
srv := &http.Server{
Addr: fmt.Sprintf("%s:%d", C.ListenInterface, C.ListenPort),
Handler: router,
// This breaks streaming requests
ReadTimeout: 45 * time.Second,
// This breaks long downloads
WriteTimeout: 45 * time.Second,
IdleTimeout: 120 * time.Second,
}
log.Fatal(srv.ListenAndServe())
}