git.iko.soy/typeable/oauth2-proxy
on master help sign in
Move configuration of logger to separate file
commit3cbac6122dbd88137b2ac578e898248ca4cdd5ab authorJoel Speed <joel.speed@hotmail.co.uk> dateTue 12 May 2020, 00:32 (6 years ago) parent3afcada
2 files changed+71−59
pkg/validation/logging.go+65 pkg/validation/options.go+6−59
pkg/validation/logging.gonew file+65
@@ -0,0 +1,65 @@
1+package validation
2+
3+import (
4+ "os"
5+ "strings"
6+
7+ "github.com/oauth2-proxy/oauth2-proxy/pkg/apis/options"
8+ "github.com/oauth2-proxy/oauth2-proxy/pkg/logger"
9+ "gopkg.in/natefinch/lumberjack.v2"
10+)
11+
12+// configureLogger is responsible for configuring the logger based on the options given
13+func configureLogger(o options.Logging, pingPath string, msgs []string) []string {
14+ // Setup the log file
15+ if len(o.File.Filename) > 0 {
16+ // Validate that the file/dir can be written
17+ file, err := os.OpenFile(o.File.Filename, os.O_WRONLY|os.O_CREATE, 0666)
18+ if err != nil {
19+ if os.IsPermission(err) {
20+ return append(msgs, "unable to write to log file: "+o.File.Filename)
21+ }
22+ }
23+ file.Close()
24+
25+ logger.Printf("Redirecting logging to file: %s", o.File.Filename)
26+
27+ logWriter := &lumberjack.Logger{
28+ Filename: o.File.Filename,
29+ MaxSize: o.File.MaxSize, // megabytes
30+ MaxAge: o.File.MaxAge, // days
31+ MaxBackups: o.File.MaxBackups,
32+ LocalTime: o.LocalTime,
33+ Compress: o.File.Compress,
34+ }
35+
36+ logger.SetOutput(logWriter)
37+ }
38+
39+ // Supply a sanity warning to the logger if all logging is disabled
40+ if !o.StandardEnabled && !o.AuthEnabled && !o.RequestEnabled {
41+ logger.Print("Warning: Logging disabled. No further logs will be shown.")
42+ }
43+
44+ // Pass configuration values to the standard logger
45+ logger.SetStandardEnabled(o.StandardEnabled)
46+ logger.SetAuthEnabled(o.AuthEnabled)
47+ logger.SetReqEnabled(o.RequestEnabled)
48+ logger.SetStandardTemplate(o.StandardFormat)
49+ logger.SetAuthTemplate(o.AuthFormat)
50+ logger.SetReqTemplate(o.RequestFormat)
51+
52+ excludePaths := make([]string, 0)
53+ excludePaths = append(excludePaths, strings.Split(o.ExcludePaths, ",")...)
54+ if o.SilencePing {
55+ excludePaths = append(excludePaths, pingPath)
56+ }
57+
58+ logger.SetExcludePaths(excludePaths)
59+
60+ if !o.LocalTime {
61+ logger.SetFlags(logger.Flags() | logger.LUTC)
62+ }
63+
64+ return msgs
65+}
pkg/validation/options.go+6−59
@@ -23,7 +23,6 @@ import (
2323 "github.com/oauth2-proxy/oauth2-proxy/pkg/requests"
2424 "github.com/oauth2-proxy/oauth2-proxy/pkg/sessions"
2525 "github.com/oauth2-proxy/oauth2-proxy/providers"
26− "gopkg.in/natefinch/lumberjack.v2"
2726)
2827
2928// Validate checks that required options are set and validates those that they
@@ -265,7 +264,7 @@ func Validate(o *options.Options) error {
265264
266265 msgs = parseSignatureKey(o, msgs)
267266 msgs = validateCookieName(o, msgs)
268− msgs = setupLogger(o, msgs)
267+ msgs = configureLogger(o.Logging, o.PingPath, msgs)
269268
270269 if o.ReverseProxy {
271270 parser, err := ip.GetRealClientIPParser(o.RealClientIPHeader)
@@ -273,6 +272,11 @@ func Validate(o *options.Options) error {
273272 msgs = append(msgs, fmt.Sprintf("real_client_ip_header (%s) not accepted parameter value: %v", o.RealClientIPHeader, err))
274273 }
275274 o.SetRealClientIPParser(parser)
275+
276+ // Allow the logger to get client IPs
277+ logger.SetGetClientFunc(func(r *http.Request) string {
278+ return ip.GetClientString(o.GetRealClientIPParser(), r, false)
279+ })
276280 }
277281
278282 if len(msgs) != 0 {
@@ -453,63 +457,6 @@ func validateCookieName(o *options.Options, msgs []string) []string {
453457 return msgs
454458}
455459
456−func setupLogger(o *options.Options, msgs []string) []string {
457− // Setup the log file
458− if len(o.Logging.File.Filename) > 0 {
459− // Validate that the file/dir can be written
460− file, err := os.OpenFile(o.Logging.File.Filename, os.O_WRONLY|os.O_CREATE, 0666)
461− if err != nil {
462− if os.IsPermission(err) {
463− return append(msgs, "unable to write to log file: "+o.Logging.File.Filename)
464− }
465− }
466− file.Close()
467−
468− logger.Printf("Redirecting logging to file: %s", o.Logging.File.Filename)
469−
470− logWriter := &lumberjack.Logger{
471− Filename: o.Logging.File.Filename,
472− MaxSize: o.Logging.File.MaxSize, // megabytes
473− MaxAge: o.Logging.File.MaxAge, // days
474− MaxBackups: o.Logging.File.MaxBackups,
475− LocalTime: o.Logging.LocalTime,
476− Compress: o.Logging.File.Compress,
477− }
478−
479− logger.SetOutput(logWriter)
480− }
481−
482− // Supply a sanity warning to the logger if all logging is disabled
483− if !o.Logging.StandardEnabled && !o.Logging.AuthEnabled && !o.Logging.RequestEnabled {
484− logger.Print("Warning: Logging disabled. No further logs will be shown.")
485− }
486−
487− // Pass configuration values to the standard logger
488− logger.SetStandardEnabled(o.Logging.StandardEnabled)
489− logger.SetAuthEnabled(o.Logging.AuthEnabled)
490− logger.SetReqEnabled(o.Logging.RequestEnabled)
491− logger.SetStandardTemplate(o.Logging.StandardFormat)
492− logger.SetAuthTemplate(o.Logging.AuthFormat)
493− logger.SetReqTemplate(o.Logging.RequestFormat)
494− logger.SetGetClientFunc(func(r *http.Request) string {
495− return ip.GetClientString(o.GetRealClientIPParser(), r, false)
496− })
497−
498− excludePaths := make([]string, 0)
499− excludePaths = append(excludePaths, strings.Split(o.Logging.ExcludePaths, ",")...)
500− if o.Logging.SilencePing {
501− excludePaths = append(excludePaths, o.PingPath)
502− }
503−
504− logger.SetExcludePaths(excludePaths)
505−
506− if !o.Logging.LocalTime {
507− logger.SetFlags(logger.Flags() | logger.LUTC)
508− }
509−
510− return msgs
511−}
512−
513460// jwtIssuer hold parsed JWT issuer info that's used to construct a verifier.
514461type jwtIssuer struct {
515462 issuerURI string