-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
161 lines (140 loc) · 4.08 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
package main
import (
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"encoding/xml"
"flag"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"runtime"
"time"
"github.com/crewjam/saml"
"github.com/crewjam/saml/samlsp"
log "github.com/sirupsen/logrus"
"github.com/vulcand/oxy/buffer"
"github.com/vulcand/oxy/forward"
"github.com/vulcand/oxy/roundrobin"
goji "goji.io"
"goji.io/pat"
)
type Config struct {
Target *string
ServiceRootURL *string
ServiceCertificate *string
ServiceKey *string
IdpMetadata *string
HttpsCertificate *string
HttpsKey *string
}
func readIdpMetadata(path *string) (*saml.Metadata, error) {
data, err := ioutil.ReadFile(*path)
if err != nil {
log.WithFields(log.Fields{"path": *path}).Fatal(err)
return nil, err
}
idpMetadata := &saml.Metadata{}
err = xml.Unmarshal(data, idpMetadata)
if err != nil {
log.Fatal(err)
return nil, err
}
return idpMetadata, nil
}
func main() {
var C Config
C.Target = flag.String("target", "", "Target for the reverse proxy")
C.ServiceRootURL = flag.String("service-root-url", "", "Root URL for this service")
C.ServiceCertificate = flag.String("service-certificate", "", "Path to a certificate for the SP")
C.ServiceKey = flag.String("service-key", "", "Path to a private key for the SP")
C.IdpMetadata = flag.String("idp-metadata", "", "Path to IdP metadata")
C.HttpsCertificate = flag.String("-https-certificate", "", "Optional path to https certificate")
C.HttpsKey = flag.String("-https-key", "", "Optional path to https key")
flag.Parse()
log.SetFormatter(&log.JSONFormatter{})
log.SetOutput(os.Stdout)
logLevel, err := log.ParseLevel("info")
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.ServiceCertificate, *C.ServiceKey)
if err != nil {
log.Fatal("service-key", err)
}
keyPair.Leaf, err = x509.ParseCertificate(keyPair.Certificate[0])
if err != nil {
log.Fatal("service-certificate", err)
}
rootURL, err := url.Parse(*C.ServiceRootURL)
if err != nil {
log.Fatal(err)
}
idpMetadata, err := readIdpMetadata(C.IdpMetadata)
samlSP, _ := samlsp.New(samlsp.Options{
URL: *rootURL,
Key: keyPair.PrivateKey.(*rsa.PrivateKey),
Certificate: keyPair.Leaf,
IDPMetadata: idpMetadata,
})
// reverse proxy layer
fwd, err := forward.New()
if err != nil {
log.Fatal(err)
}
// load balancing layer
lb, err := roundrobin.New(fwd)
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(lb, buffer.Retry(`IsNetworkError() && Attempts() < 3`))
if err != nil {
log.Fatal(err)
}
targetURL, err := url.Parse(*C.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()
// 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: ":8080",
Handler: router,
// This breaks streaming requests
ReadTimeout: 45 * time.Second,
// This breaks long downloads
WriteTimeout: 45 * time.Second,
IdleTimeout: 120 * time.Second,
}
if (*C.HttpsKey) == "" || (*C.HttpsCertificate) == "" {
log.Fatal(srv.ListenAndServe())
} else {
log.Fatal(srv.ListenAndServeTLS(*C.HttpsCertificate, *C.HttpsKey))
}
}