git.iko.soy/typeable/oauth2-proxy
on master help sign in
Move logging options to a struct
commit3afcadae76cf8722b0b9c25bd8df1f3264dddd5d authorJoel Speed <joel.speed@hotmail.co.uk> dateTue 12 May 2020, 00:13 (6 years ago) parentf7b28cb
3 files changed+69−55
pkg/apis/options/logging.go+24 pkg/apis/options/options.go+25−35 pkg/validation/options.go+20−20
pkg/apis/options/logging.gonew file+24
@@ -0,0 +1,24 @@
1+package options
2+
3+// Logging contains all options required for configuring the logging
4+type Logging struct {
5+ AuthEnabled bool `flag:"auth-logging" cfg:"auth_logging"`
6+ AuthFormat string `flag:"auth-logging-format" cfg:"auth_logging_format"`
7+ RequestEnabled bool `flag:"request-logging" cfg:"request_logging"`
8+ RequestFormat string `flag:"request-logging-format" cfg:"request_logging_format"`
9+ StandardEnabled bool `flag:"standard-logging" cfg:"standard_logging"`
10+ StandardFormat string `flag:"standard-logging-format" cfg:"standard_logging_format"`
11+ ExcludePaths string `flag:"exclude-logging-paths" cfg:"exclude_logging_paths"`
12+ LocalTime bool `flag:"logging-local-time" cfg:"logging_local_time"`
13+ SilencePing bool `flag:"silence-ping-logging" cfg:"silence_ping_logging"`
14+ File LogFileOptions `cfg:",squash"`
15+}
16+
17+// LogFileOptions contains options for configuring logging to a file
18+type LogFileOptions struct {
19+ Filename string `flag:"logging-filename" cfg:"logging_filename"`
20+ MaxSize int `flag:"logging-max-size" cfg:"logging_max_size"`
21+ MaxAge int `flag:"logging-max-age" cfg:"logging_max_age"`
22+ MaxBackups int `flag:"logging-max-backups" cfg:"logging_max_backups"`
23+ Compress bool `flag:"logging-compress" cfg:"logging_compress"`
24+}
pkg/apis/options/options.go+25−35
@@ -61,6 +61,7 @@ type Options struct {
6161
6262 Cookie CookieOptions `cfg:",squash"`
6363 Session SessionOptions `cfg:",squash"`
64+ Logging Logging `cfg:",squash"`
6465
6566 Upstreams []string `flag:"upstream" cfg:"upstreams"`
6667 SkipAuthRegex []string `flag:"skip-auth-regex" cfg:"skip_auth_regex"`
@@ -101,27 +102,12 @@ type Options struct {
101102 ApprovalPrompt string `flag:"approval-prompt" cfg:"approval_prompt"` // Deprecated by OIDC 1.0
102103 UserIDClaim string `flag:"user-id-claim" cfg:"user_id_claim"`
103104
104− // Configuration values for logging
105− LoggingFilename string `flag:"logging-filename" cfg:"logging_filename"`
106− LoggingMaxSize int `flag:"logging-max-size" cfg:"logging_max_size"`
107− LoggingMaxAge int `flag:"logging-max-age" cfg:"logging_max_age"`
108− LoggingMaxBackups int `flag:"logging-max-backups" cfg:"logging_max_backups"`
109− LoggingLocalTime bool `flag:"logging-local-time" cfg:"logging_local_time"`
110− LoggingCompress bool `flag:"logging-compress" cfg:"logging_compress"`
111− StandardLogging bool `flag:"standard-logging" cfg:"standard_logging"`
112− StandardLoggingFormat string `flag:"standard-logging-format" cfg:"standard_logging_format"`
113− RequestLogging bool `flag:"request-logging" cfg:"request_logging"`
114− RequestLoggingFormat string `flag:"request-logging-format" cfg:"request_logging_format"`
115− ExcludeLoggingPaths string `flag:"exclude-logging-paths" cfg:"exclude_logging_paths"`
116− SilencePingLogging bool `flag:"silence-ping-logging" cfg:"silence_ping_logging"`
117− AuthLogging bool `flag:"auth-logging" cfg:"auth_logging"`
118− AuthLoggingFormat string `flag:"auth-logging-format" cfg:"auth_logging_format"`
119− SignatureKey string `flag:"signature-key" cfg:"signature_key"`
120− AcrValues string `flag:"acr-values" cfg:"acr_values"`
121− JWTKey string `flag:"jwt-key" cfg:"jwt_key"`
122− JWTKeyFile string `flag:"jwt-key-file" cfg:"jwt_key_file"`
123− PubJWKURL string `flag:"pubjwk-url" cfg:"pubjwk_url"`
124− GCPHealthChecks bool `flag:"gcp-healthchecks" cfg:"gcp_healthchecks"`
105+ SignatureKey string `flag:"signature-key" cfg:"signature_key"`
106+ AcrValues string `flag:"acr-values" cfg:"acr_values"`
107+ JWTKey string `flag:"jwt-key" cfg:"jwt_key"`
108+ JWTKeyFile string `flag:"jwt-key-file" cfg:"jwt_key_file"`
109+ PubJWKURL string `flag:"pubjwk-url" cfg:"pubjwk_url"`
110+ GCPHealthChecks bool `flag:"gcp-healthchecks" cfg:"gcp_healthchecks"`
125111
126112 // internal values that are set after config validation
127113 redirectURL *url.URL
@@ -197,20 +183,24 @@ func NewOptions() *Options {
197183 UserIDClaim: "email",
198184 InsecureOIDCAllowUnverifiedEmail: false,
199185 SkipOIDCDiscovery: false,
200− LoggingFilename: "",
201− LoggingMaxSize: 100,
202− LoggingMaxAge: 7,
203− LoggingMaxBackups: 0,
204− LoggingLocalTime: true,
205− LoggingCompress: false,
206− ExcludeLoggingPaths: "",
207− SilencePingLogging: false,
208− StandardLogging: true,
209− StandardLoggingFormat: logger.DefaultStandardLoggingFormat,
210− RequestLogging: true,
211− RequestLoggingFormat: logger.DefaultRequestLoggingFormat,
212− AuthLogging: true,
213− AuthLoggingFormat: logger.DefaultAuthLoggingFormat,
186+ Logging: Logging{
187+ ExcludePaths: "",
188+ LocalTime: true,
189+ SilencePing: false,
190+ AuthEnabled: true,
191+ AuthFormat: logger.DefaultAuthLoggingFormat,
192+ RequestEnabled: true,
193+ RequestFormat: logger.DefaultRequestLoggingFormat,
194+ StandardEnabled: true,
195+ StandardFormat: logger.DefaultStandardLoggingFormat,
196+ File: LogFileOptions{
197+ Filename: "",
198+ MaxSize: 100,
199+ MaxAge: 7,
200+ MaxBackups: 0,
201+ Compress: false,
202+ },
203+ },
214204 }
215205}
216206
pkg/validation/options.go+20−20
@@ -455,55 +455,55 @@ func validateCookieName(o *options.Options, msgs []string) []string {
455455
456456func setupLogger(o *options.Options, msgs []string) []string {
457457 // Setup the log file
458− if len(o.LoggingFilename) > 0 {
458+ if len(o.Logging.File.Filename) > 0 {
459459 // Validate that the file/dir can be written
460− file, err := os.OpenFile(o.LoggingFilename, os.O_WRONLY|os.O_CREATE, 0666)
460+ file, err := os.OpenFile(o.Logging.File.Filename, os.O_WRONLY|os.O_CREATE, 0666)
461461 if err != nil {
462462 if os.IsPermission(err) {
463− return append(msgs, "unable to write to log file: "+o.LoggingFilename)
463+ return append(msgs, "unable to write to log file: "+o.Logging.File.Filename)
464464 }
465465 }
466466 file.Close()
467467
468− logger.Printf("Redirecting logging to file: %s", o.LoggingFilename)
468+ logger.Printf("Redirecting logging to file: %s", o.Logging.File.Filename)
469469
470470 logWriter := &lumberjack.Logger{
471− Filename: o.LoggingFilename,
472− MaxSize: o.LoggingMaxSize, // megabytes
473− MaxAge: o.LoggingMaxAge, // days
474− MaxBackups: o.LoggingMaxBackups,
475− LocalTime: o.LoggingLocalTime,
476− Compress: o.LoggingCompress,
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,
477477 }
478478
479479 logger.SetOutput(logWriter)
480480 }
481481
482482 // Supply a sanity warning to the logger if all logging is disabled
483− if !o.StandardLogging && !o.AuthLogging && !o.RequestLogging {
483+ if !o.Logging.StandardEnabled && !o.Logging.AuthEnabled && !o.Logging.RequestEnabled {
484484 logger.Print("Warning: Logging disabled. No further logs will be shown.")
485485 }
486486
487487 // Pass configuration values to the standard logger
488− logger.SetStandardEnabled(o.StandardLogging)
489− logger.SetAuthEnabled(o.AuthLogging)
490− logger.SetReqEnabled(o.RequestLogging)
491− logger.SetStandardTemplate(o.StandardLoggingFormat)
492− logger.SetAuthTemplate(o.AuthLoggingFormat)
493− logger.SetReqTemplate(o.RequestLoggingFormat)
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)
494494 logger.SetGetClientFunc(func(r *http.Request) string {
495495 return ip.GetClientString(o.GetRealClientIPParser(), r, false)
496496 })
497497
498498 excludePaths := make([]string, 0)
499− excludePaths = append(excludePaths, strings.Split(o.ExcludeLoggingPaths, ",")...)
500− if o.SilencePingLogging {
499+ excludePaths = append(excludePaths, strings.Split(o.Logging.ExcludePaths, ",")...)
500+ if o.Logging.SilencePing {
501501 excludePaths = append(excludePaths, o.PingPath)
502502 }
503503
504504 logger.SetExcludePaths(excludePaths)
505505
506− if !o.LoggingLocalTime {
506+ if !o.Logging.LocalTime {
507507 logger.SetFlags(logger.Flags() | logger.LUTC)
508508 }
509509