This repository has been archived by the owner on May 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
175 lines (145 loc) · 4.56 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
package main
import (
"context"
"crypto/tls"
"flag"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"os/signal"
"time"
"github.com/cyakimov/helios/authentication"
"github.com/cyakimov/helios/authentication/providers"
"github.com/cyakimov/helios/authentication/providers/auth0"
"github.com/cyakimov/helios/authentication/providers/azuread"
"github.com/cyakimov/helios/authentication/providers/google"
"github.com/cyakimov/helios/authorization"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)
var (
configPath string
config *Config
tlsConfig *tls.Config
debugMode bool
)
func init() {
// Enable TLS 1.3
_ = os.Setenv("GODEBUG", os.Getenv("GODEBUG")+",tls13=1")
flag.StringVar(&configPath, "config", "default.yaml", "Configuration file path")
flag.BoolVar(&debugMode, "verbose", false, "DEBUG level logging")
}
func router() *mux.Router {
router := mux.NewRouter()
upstreams := make(map[string]http.Handler, len(config.Upstreams))
oauth2conf := providers.OAuth2Config{
ClientID: config.Identity.ClientID,
ClientSecret: config.Identity.ClientSecret,
AuthURL: config.Identity.OAuth2.AuthURL,
TokenURL: config.Identity.OAuth2.TokenURL,
ProfileURL: config.Identity.OAuth2.ProfileURL,
}
var provider providers.OAuth2Provider
switch config.Identity.Provider {
case "aad":
provider = azuread.NewAzureADProvider(oauth2conf)
case "auth0":
provider = auth0.NewAuth0Provider(oauth2conf)
case "google":
provider = google.NewGoogleProvider(oauth2conf)
default:
log.Fatalf("%q provider is not supported", config.Identity.Provider)
}
authN := authentication.NewHeliosAuthentication(provider, config.JWT.Secret, config.JWT.Expires)
router.PathPrefix("/.well-known/callback").HandlerFunc(authN.CallbackHandler)
router.PathPrefix("/.well-known/logout").HandlerFunc(authN.Logout)
for _, up := range config.Upstreams {
upstreamURL, err := url.Parse(up.URL)
if err != nil {
log.Fatalf("Cannot parse upstream %q URL: %v", up.Name, err)
}
conf := ReverseProxyConfig{
ConnectTimeout: up.ConnectTimeout,
IdleTimeout: config.Server.IdleTimeout,
Timeout: config.Server.Timeout,
}
proxy := NewSingleHostReverseProxy(upstreamURL, conf)
upstreams[up.Name] = proxy
}
for _, route := range config.Routes {
h := router.Host(route.Host).Subrouter()
for _, path := range route.HTTP.Paths {
upstream := upstreams[path.Upstream]
if upstream == nil {
log.Fatalf("Upstream %q for route %q not found", path.Upstream, route.Host)
break
}
authZ := authorization.NewAuthorization(route.Rules)
if path.Authentication {
h.PathPrefix(path.Path).Handler(authN.Middleware(authZ.Middleware(upstream)))
} else {
h.PathPrefix(path.Path).Handler(authZ.Middleware(upstream))
}
}
}
return router
}
func main() {
flag.Parse()
if debugMode {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}
cb, err := ioutil.ReadFile(configPath)
if err != nil {
log.Fatalf("Error loading configuration: %v", err)
}
if err = yaml.Unmarshal(cb, &config); err != nil {
log.Fatalf("Error parsing configuration: %v", err)
}
var wait time.Duration
tlsConfig = &tls.Config{
MinVersion: tls.VersionTLS12,
MaxVersion: tls.VersionTLS13,
}
address := fmt.Sprintf("%s:%d", config.Server.ListenIP, config.Server.ListenPort)
srv := &http.Server{
Addr: address,
WriteTimeout: config.Server.Timeout,
ReadTimeout: config.Server.Timeout,
IdleTimeout: config.Server.IdleTimeout,
TLSConfig: tlsConfig,
MaxHeaderBytes: 1 << 20, // 1mb
Handler: router(),
}
// Run our server in a goroutine so that it doesn't block.
go func() {
if err := srv.ListenAndServeTLS(config.Server.TLSContext.CertificatePath, config.Server.TLSContext.PrivateKeyPath); err != nil {
log.Fatal(err)
}
}()
log.Infof("Listening on %s", address)
c := make(chan os.Signal, 1)
// We'll accept graceful shutdowns when quit via SIGINT (Ctrl+C)
// SIGKILL, SIGQUIT or SIGTERM (Ctrl+/) will not be caught.
signal.Notify(c, os.Interrupt)
// Block until we receive our signal.
<-c
// Create a deadline to wait for.
ctx, cancel := context.WithTimeout(context.Background(), wait)
defer cancel()
// Doesn't block if no connections, but will otherwise wait
// until the timeout deadline.
if err = srv.Shutdown(ctx); err != nil {
log.Error(err)
}
// Optionally, you could run srv.Shutdown in a goroutine and block on
// <-ctx.Done() if your application should wait for other services
// to finalize based on context cancellation.
log.Info("shutting down")
os.Exit(0)
}