From 3afcadae76cf8722b0b9c25bd8df1f3264dddd5d Mon Sep 17 00:00:00 2001 From: Joel Speed Date: Tue, 12 May 2020 00:13:46 +0100 Subject: [PATCH] Move logging options to a struct --- pkg/apis/options/logging.go | 24 +++++++++++++++ pkg/apis/options/options.go | 60 ++++++++++++++++--------------------- pkg/validation/options.go | 40 ++++++++++++------------- 3 files changed, 69 insertions(+), 55 deletions(-) create mode 100644 pkg/apis/options/logging.go diff --git a/pkg/apis/options/logging.go b/pkg/apis/options/logging.go new file mode 100644 index 00000000..3eb6ddc5 --- /dev/null +++ b/pkg/apis/options/logging.go @@ -0,0 +1,24 @@ +package options + +// Logging contains all options required for configuring the logging +type Logging struct { + AuthEnabled bool `flag:"auth-logging" cfg:"auth_logging"` + AuthFormat string `flag:"auth-logging-format" cfg:"auth_logging_format"` + RequestEnabled bool `flag:"request-logging" cfg:"request_logging"` + RequestFormat string `flag:"request-logging-format" cfg:"request_logging_format"` + StandardEnabled bool `flag:"standard-logging" cfg:"standard_logging"` + StandardFormat string `flag:"standard-logging-format" cfg:"standard_logging_format"` + ExcludePaths string `flag:"exclude-logging-paths" cfg:"exclude_logging_paths"` + LocalTime bool `flag:"logging-local-time" cfg:"logging_local_time"` + SilencePing bool `flag:"silence-ping-logging" cfg:"silence_ping_logging"` + File LogFileOptions `cfg:",squash"` +} + +// LogFileOptions contains options for configuring logging to a file +type LogFileOptions struct { + Filename string `flag:"logging-filename" cfg:"logging_filename"` + MaxSize int `flag:"logging-max-size" cfg:"logging_max_size"` + MaxAge int `flag:"logging-max-age" cfg:"logging_max_age"` + MaxBackups int `flag:"logging-max-backups" cfg:"logging_max_backups"` + Compress bool `flag:"logging-compress" cfg:"logging_compress"` +} diff --git a/pkg/apis/options/options.go b/pkg/apis/options/options.go index 27b0f66c..e5e509a3 100644 --- a/pkg/apis/options/options.go +++ b/pkg/apis/options/options.go @@ -61,6 +61,7 @@ type Options struct { Cookie CookieOptions `cfg:",squash"` Session SessionOptions `cfg:",squash"` + Logging Logging `cfg:",squash"` Upstreams []string `flag:"upstream" cfg:"upstreams"` SkipAuthRegex []string `flag:"skip-auth-regex" cfg:"skip_auth_regex"` @@ -101,27 +102,12 @@ type Options struct { ApprovalPrompt string `flag:"approval-prompt" cfg:"approval_prompt"` // Deprecated by OIDC 1.0 UserIDClaim string `flag:"user-id-claim" cfg:"user_id_claim"` - // Configuration values for logging - LoggingFilename string `flag:"logging-filename" cfg:"logging_filename"` - LoggingMaxSize int `flag:"logging-max-size" cfg:"logging_max_size"` - LoggingMaxAge int `flag:"logging-max-age" cfg:"logging_max_age"` - LoggingMaxBackups int `flag:"logging-max-backups" cfg:"logging_max_backups"` - LoggingLocalTime bool `flag:"logging-local-time" cfg:"logging_local_time"` - LoggingCompress bool `flag:"logging-compress" cfg:"logging_compress"` - StandardLogging bool `flag:"standard-logging" cfg:"standard_logging"` - StandardLoggingFormat string `flag:"standard-logging-format" cfg:"standard_logging_format"` - RequestLogging bool `flag:"request-logging" cfg:"request_logging"` - RequestLoggingFormat string `flag:"request-logging-format" cfg:"request_logging_format"` - ExcludeLoggingPaths string `flag:"exclude-logging-paths" cfg:"exclude_logging_paths"` - SilencePingLogging bool `flag:"silence-ping-logging" cfg:"silence_ping_logging"` - AuthLogging bool `flag:"auth-logging" cfg:"auth_logging"` - AuthLoggingFormat string `flag:"auth-logging-format" cfg:"auth_logging_format"` - SignatureKey string `flag:"signature-key" cfg:"signature_key"` - AcrValues string `flag:"acr-values" cfg:"acr_values"` - JWTKey string `flag:"jwt-key" cfg:"jwt_key"` - JWTKeyFile string `flag:"jwt-key-file" cfg:"jwt_key_file"` - PubJWKURL string `flag:"pubjwk-url" cfg:"pubjwk_url"` - GCPHealthChecks bool `flag:"gcp-healthchecks" cfg:"gcp_healthchecks"` + SignatureKey string `flag:"signature-key" cfg:"signature_key"` + AcrValues string `flag:"acr-values" cfg:"acr_values"` + JWTKey string `flag:"jwt-key" cfg:"jwt_key"` + JWTKeyFile string `flag:"jwt-key-file" cfg:"jwt_key_file"` + PubJWKURL string `flag:"pubjwk-url" cfg:"pubjwk_url"` + GCPHealthChecks bool `flag:"gcp-healthchecks" cfg:"gcp_healthchecks"` // internal values that are set after config validation redirectURL *url.URL @@ -197,20 +183,24 @@ func NewOptions() *Options { UserIDClaim: "email", InsecureOIDCAllowUnverifiedEmail: false, SkipOIDCDiscovery: false, - LoggingFilename: "", - LoggingMaxSize: 100, - LoggingMaxAge: 7, - LoggingMaxBackups: 0, - LoggingLocalTime: true, - LoggingCompress: false, - ExcludeLoggingPaths: "", - SilencePingLogging: false, - StandardLogging: true, - StandardLoggingFormat: logger.DefaultStandardLoggingFormat, - RequestLogging: true, - RequestLoggingFormat: logger.DefaultRequestLoggingFormat, - AuthLogging: true, - AuthLoggingFormat: logger.DefaultAuthLoggingFormat, + Logging: Logging{ + ExcludePaths: "", + LocalTime: true, + SilencePing: false, + AuthEnabled: true, + AuthFormat: logger.DefaultAuthLoggingFormat, + RequestEnabled: true, + RequestFormat: logger.DefaultRequestLoggingFormat, + StandardEnabled: true, + StandardFormat: logger.DefaultStandardLoggingFormat, + File: LogFileOptions{ + Filename: "", + MaxSize: 100, + MaxAge: 7, + MaxBackups: 0, + Compress: false, + }, + }, } } diff --git a/pkg/validation/options.go b/pkg/validation/options.go index 761d216d..ecf6fcc7 100644 --- a/pkg/validation/options.go +++ b/pkg/validation/options.go @@ -455,55 +455,55 @@ func validateCookieName(o *options.Options, msgs []string) []string { func setupLogger(o *options.Options, msgs []string) []string { // Setup the log file - if len(o.LoggingFilename) > 0 { + if len(o.Logging.File.Filename) > 0 { // Validate that the file/dir can be written - file, err := os.OpenFile(o.LoggingFilename, os.O_WRONLY|os.O_CREATE, 0666) + file, err := os.OpenFile(o.Logging.File.Filename, os.O_WRONLY|os.O_CREATE, 0666) if err != nil { if os.IsPermission(err) { - return append(msgs, "unable to write to log file: "+o.LoggingFilename) + return append(msgs, "unable to write to log file: "+o.Logging.File.Filename) } } file.Close() - logger.Printf("Redirecting logging to file: %s", o.LoggingFilename) + logger.Printf("Redirecting logging to file: %s", o.Logging.File.Filename) logWriter := &lumberjack.Logger{ - Filename: o.LoggingFilename, - MaxSize: o.LoggingMaxSize, // megabytes - MaxAge: o.LoggingMaxAge, // days - MaxBackups: o.LoggingMaxBackups, - LocalTime: o.LoggingLocalTime, - Compress: o.LoggingCompress, + Filename: o.Logging.File.Filename, + MaxSize: o.Logging.File.MaxSize, // megabytes + MaxAge: o.Logging.File.MaxAge, // days + MaxBackups: o.Logging.File.MaxBackups, + LocalTime: o.Logging.LocalTime, + Compress: o.Logging.File.Compress, } logger.SetOutput(logWriter) } // Supply a sanity warning to the logger if all logging is disabled - if !o.StandardLogging && !o.AuthLogging && !o.RequestLogging { + if !o.Logging.StandardEnabled && !o.Logging.AuthEnabled && !o.Logging.RequestEnabled { logger.Print("Warning: Logging disabled. No further logs will be shown.") } // Pass configuration values to the standard logger - logger.SetStandardEnabled(o.StandardLogging) - logger.SetAuthEnabled(o.AuthLogging) - logger.SetReqEnabled(o.RequestLogging) - logger.SetStandardTemplate(o.StandardLoggingFormat) - logger.SetAuthTemplate(o.AuthLoggingFormat) - logger.SetReqTemplate(o.RequestLoggingFormat) + logger.SetStandardEnabled(o.Logging.StandardEnabled) + logger.SetAuthEnabled(o.Logging.AuthEnabled) + logger.SetReqEnabled(o.Logging.RequestEnabled) + logger.SetStandardTemplate(o.Logging.StandardFormat) + logger.SetAuthTemplate(o.Logging.AuthFormat) + logger.SetReqTemplate(o.Logging.RequestFormat) logger.SetGetClientFunc(func(r *http.Request) string { return ip.GetClientString(o.GetRealClientIPParser(), r, false) }) excludePaths := make([]string, 0) - excludePaths = append(excludePaths, strings.Split(o.ExcludeLoggingPaths, ",")...) - if o.SilencePingLogging { + excludePaths = append(excludePaths, strings.Split(o.Logging.ExcludePaths, ",")...) + if o.Logging.SilencePing { excludePaths = append(excludePaths, o.PingPath) } logger.SetExcludePaths(excludePaths) - if !o.LoggingLocalTime { + if !o.Logging.LocalTime { logger.SetFlags(logger.Flags() | logger.LUTC) }