-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
main.go
337 lines (282 loc) · 10.8 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
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
package main
import (
"bytes"
"fmt"
"net/http"
"net/url"
"os"
"os/signal"
"path"
"strings"
"syscall"
"text/template"
"github.com/Masterminds/sprig/v3"
"github.com/flosch/pongo2"
"github.com/gorilla/context"
"github.com/gorilla/sessions"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
yaml "gopkg.in/yaml.v3"
"github.com/Luzifer/nginx-sso/plugins"
"github.com/Luzifer/rconfig/v2"
)
type mainConfig struct {
ACL acl `yaml:"acl"`
AuditLog auditLogger `yaml:"audit_log"`
Cookie plugins.CookieConfig `yaml:"cookie"`
Listen struct {
Addr string `yaml:"addr"`
Port int `yaml:"port"`
} `yaml:"listen"`
Login struct {
Title string `yaml:"title" json:"title"`
DefaultMethod string `yaml:"default_method" json:"default_method"`
DefaultRedirect string `yaml:"default_redirect" json:"default_redirect"`
HideMFAField bool `yaml:"hide_mfa_field" json:"hide_mfa_field"`
Names map[string]string `yaml:"names" json:"names"`
} `yaml:"login"`
Plugins struct {
Directory string `yaml:"directory"`
} `yaml:"plugins"`
}
var (
cfg = struct {
ConfigFile string `flag:"config,c" default:"config.yaml" env:"CONFIG" description:"Location of the configuration file"`
AuthKey string `flag:"authkey" env:"COOKIE_AUTHENTICATION_KEY" description:"Cookie authentication key"`
LogLevel string `flag:"log-level" default:"info" description:"Level of logs to display (debug, info, warn, error)"`
TemplateDir string `flag:"frontend-dir" default:"./frontend/" env:"FRONTEND_DIR" description:"Location of the directory containing the web assets"`
VersionAndExit bool `flag:"version" default:"false" description:"Prints current version and exits"`
}{}
mainCfg = mainConfig{}
cookieStore *sessions.CookieStore
version = "dev"
)
func init() {
rconfig.AutoEnv(true)
if err := rconfig.Parse(&cfg); err != nil {
log.WithError(err).Fatal("Unable to parse commandline options")
}
if l, err := log.ParseLevel(cfg.LogLevel); err != nil {
log.WithError(err).Fatal("Unable to parse log level")
} else {
log.SetLevel(l)
}
if cfg.VersionAndExit {
fmt.Printf("nginx-sso %s\n", version)
os.Exit(0)
}
// Set sane defaults for main configuration
mainCfg.Cookie = plugins.DefaultCookieConfig()
mainCfg.Listen.Addr = "127.0.0.1"
mainCfg.Listen.Port = 8082
mainCfg.Login.DefaultRedirect = "debug"
mainCfg.AuditLog.TrustedIPHeaders = []string{"X-Forwarded-For", "RemoteAddr", "X-Real-IP"}
mainCfg.AuditLog.Headers = []string{"x-origin-uri"}
}
func loadConfiguration() ([]byte, error) {
yamlSource, err := os.ReadFile(cfg.ConfigFile)
if err != nil {
return nil, errors.Wrap(err, "reading configuration file")
}
tpl, err := template.New("config").Funcs(sprig.FuncMap()).Parse(string(yamlSource))
if err != nil {
return nil, errors.Wrap(err, "parsing config as template")
}
buf := new(bytes.Buffer)
if err = tpl.Execute(buf, nil); err != nil {
return nil, errors.Wrap(err, "executing config as template")
}
if err = yaml.Unmarshal(buf.Bytes(), &mainCfg); err != nil {
return nil, errors.Wrap(err, "loading configuration file")
}
if cfg.AuthKey != "" {
mainCfg.Cookie.AuthKey = cfg.AuthKey
}
return buf.Bytes(), nil
}
func initializeModules(yamlSource []byte) error {
if mainCfg.Plugins.Directory != "" {
if err := loadPlugins(mainCfg.Plugins.Directory); err != nil {
return errors.Wrap(err, "Unable to load plugins")
}
}
if err := initializeAuthenticators(yamlSource); err != nil {
return fmt.Errorf("Unable to configure authentication: %s", err)
}
if err := initializeMFAProviders(yamlSource); err != nil {
log.WithError(err).Fatal("Unable to configure MFA providers")
}
return nil
}
func main() {
yamlSource, err := loadConfiguration()
if err != nil {
log.WithError(err).Fatal("Unable to load configuration")
}
cookieStore = sessions.NewCookieStore([]byte(mainCfg.Cookie.AuthKey))
registerModules()
if err = initializeModules(yamlSource); err != nil {
log.WithError(err).Fatal("Unable to initialize modules")
}
http.HandleFunc("/", handleRootRequest)
http.HandleFunc("/auth", handleAuthRequest)
http.HandleFunc("/debug", handleLoginDebug)
http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("OK")) })
http.HandleFunc("/login", handleLoginRequest)
http.HandleFunc("/logout", handleLogoutRequest)
go http.ListenAndServe(
fmt.Sprintf("%s:%d", mainCfg.Listen.Addr, mainCfg.Listen.Port),
context.ClearHandler(http.DefaultServeMux),
)
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGHUP)
for sig := range sigChan {
switch sig {
case syscall.SIGHUP:
if yamlSource, err = loadConfiguration(); err != nil {
log.WithError(err).Error("Unable to reload configuration")
continue
}
if err = initializeModules(yamlSource); err != nil {
log.WithError(err).Error("Unable to initialize modules")
}
default:
log.Fatalf("Received unexpected signal: %v", sig)
}
}
}
func handleRootRequest(res http.ResponseWriter, r *http.Request) {
// In case of a request to `/` redirect to login utilizing the default redirect
http.Redirect(res, r, "login", http.StatusFound)
}
func handleAuthRequest(res http.ResponseWriter, r *http.Request) {
user, groups, err := detectUser(res, r)
switch err {
case plugins.ErrNoValidUserFound:
// No valid user found, check whether special anonymous "user" has access
// Username is set to 0x0 character to prevent accidental whitelist-match
if mainCfg.ACL.HasAccess(string(byte(0x0)), nil, r) {
mainCfg.AuditLog.Log(auditEventValidate, r, map[string]string{"result": "anonymous access granted"}) // #nosec G104 - This is only logging
res.WriteHeader(http.StatusOK)
return
}
mainCfg.AuditLog.Log(auditEventValidate, r, map[string]string{"result": "no valid user found"}) // #nosec G104 - This is only logging
http.Error(res, "No valid user found", http.StatusUnauthorized)
case nil:
if !mainCfg.ACL.HasAccess(user, groups, r) {
mainCfg.AuditLog.Log(auditEventAccessDenied, r, map[string]string{"username": user}) // #nosec G104 - This is only logging
http.Error(res, "Access denied for this resource", http.StatusForbidden)
return
}
mainCfg.AuditLog.Log(auditEventValidate, r, map[string]string{"result": "valid user found", "username": user}) // #nosec G104 - This is only logging
res.Header().Set("X-Username", user)
res.WriteHeader(http.StatusOK)
default:
log.WithError(err).Error("Error while handling auth request")
http.Error(res, "Something went wrong", http.StatusInternalServerError)
}
}
func handleLoginRequest(res http.ResponseWriter, r *http.Request) {
redirURL, err := getRedirectURL(r, mainCfg.Login.DefaultRedirect)
if err != nil {
http.Error(res, "Invalid redirect URL specified", http.StatusBadRequest)
}
if _, _, err := detectUser(res, r); err == nil {
// There is already a valid user
http.Redirect(res, r, redirURL, http.StatusFound)
return
}
auditFields := map[string]string{
"go": redirURL,
}
if r.Method == "POST" || r.URL.Query().Get("code") != "" {
// Simple authentication
user, mfaCfgs, err := loginUser(res, r)
switch err {
case plugins.ErrNoValidUserFound:
auditFields["reason"] = "invalid credentials"
mainCfg.AuditLog.Log(auditEventLoginFailure, r, auditFields) // #nosec G104 - This is only logging
http.Redirect(res, r, "/login?go="+url.QueryEscape(redirURL), http.StatusFound)
return
case nil:
// Don't handle for now, MFA validation comes first
default:
auditFields["reason"] = "error"
auditFields["error"] = err.Error()
mainCfg.AuditLog.Log(auditEventLoginFailure, r, auditFields) // #nosec G104 - This is only logging
log.WithError(err).Error("Login failed with unexpected error")
http.Redirect(res, r, "/login?go="+url.QueryEscape(redirURL), http.StatusFound)
return
}
// MFA validation against configs from login
err = validateMFA(res, r, user, mfaCfgs)
switch err {
case plugins.ErrNoValidUserFound:
auditFields["reason"] = "invalid credentials"
mainCfg.AuditLog.Log(auditEventLoginFailure, r, auditFields) // #nosec G104 - This is only logging
res.Header().Del("Set-Cookie") // Remove login cookie
http.Redirect(res, r, "/login?go="+url.QueryEscape(redirURL), http.StatusFound)
return
case nil:
mainCfg.AuditLog.Log(auditEventLoginSuccess, r, auditFields) // #nosec G104 - This is only logging
http.Redirect(res, r, redirURL, http.StatusFound)
return
default:
auditFields["reason"] = "error"
auditFields["error"] = err.Error()
mainCfg.AuditLog.Log(auditEventLoginFailure, r, auditFields) // #nosec G104 - This is only logging
log.WithError(err).Error("Login failed with unexpected error")
res.Header().Del("Set-Cookie") // Remove login cookie
http.Redirect(res, r, "/login?go="+url.QueryEscape(redirURL), http.StatusFound)
return
}
}
// Store redirect URL in session (required for oAuth2 flows)
sess, _ := cookieStore.Get(r, strings.Join([]string{mainCfg.Cookie.Prefix, "main"}, "-")) // #nosec G104 - On error empty session is returned
sess.Options = mainCfg.Cookie.GetSessionOpts()
sess.Values["go"] = redirURL
if err := sess.Save(r, res); err != nil {
log.WithError(err).Error("Unable to save session")
http.Error(res, "Something went wrong", http.StatusInternalServerError)
}
// Render login page
tpl := pongo2.Must(pongo2.FromFile(path.Join(cfg.TemplateDir, "index.html")))
if err := tpl.ExecuteWriter(pongo2.Context{
"active_methods": getFrontendAuthenticators(),
"go": redirURL,
"login": mainCfg.Login,
}, res); err != nil {
log.WithError(err).Error("Unable to render template")
http.Error(res, "Something went wrong", http.StatusInternalServerError)
}
}
func handleLogoutRequest(res http.ResponseWriter, r *http.Request) {
redirURL, err := getRedirectURL(r, mainCfg.Login.DefaultRedirect)
if err != nil {
http.Error(res, "Invalid redirect URL specified", http.StatusBadRequest)
}
mainCfg.AuditLog.Log(auditEventLogout, r, nil) // #nosec G104 - This is only logging
if err := logoutUser(res, r); err != nil {
log.WithError(err).Error("Failed to logout user")
http.Error(res, "Something went wrong", http.StatusInternalServerError)
return
}
http.Redirect(res, r, redirURL, http.StatusFound)
}
func handleLoginDebug(w http.ResponseWriter, r *http.Request) {
user, groups, err := detectUser(w, r)
switch err {
case nil:
// All fine
case plugins.ErrNoValidUserFound:
http.Redirect(w, r, "login", http.StatusFound)
return
default:
log.WithError(err).Error("Failed to get user for login debug")
http.Error(w, "Something went wrong", http.StatusInternalServerError)
}
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, "Successfully logged in:")
fmt.Fprintf(w, "- Username: %s\n", user)
fmt.Fprintf(w, "- Groups: %s\n", strings.Join(groups, ","))
}