git.iko.soy/typeable/oauth2-proxy
on master help sign in
logger.go ExcludedPaths changed to slice of paths.

- `logger.go` convert slice of paths to map for quicker lookup - `options.go` combines csv paths and pingpath into slice

commit289dfce28ad8863bbf65023f15302ee273a20803 authorKarl Skewes <karl.skewes@gmail.com> dateSat 22 Jun 2019, 09:39 (7 years ago) parent4e10cc7
5 files changed+41−27
docs/configuration/configuration.md+3−2 logging_handler_test.go+18−10 main.go+1−1 options.go+7−5 pkg/logger/logger.go+12−9
docs/configuration/configuration.md+3−2
@@ -41,8 +41,9 @@ Usage of oauth2_proxy:
4141 -custom-templates-dir string: path to custom html templates
4242 -display-htpasswd-form: display username / password login form if an htpasswd file is provided (default true)
4343 -email-domain value: authenticate emails with the specified domain (may be given multiple times). Use * to authenticate any email
44+<<<<<<< HEAD
4445 -extra-jwt-issuers: if -skip-jwt-bearer-tokens is set, a list of extra JWT issuer=audience pairs (where the issuer URL has a .well-known/openid-configuration or a .well-known/jwks.json)
45− -exclude-logging-path: don't log requests to this path, eg: /ping (default "" = no paths excluded)
46+ -exclude-logging-paths: comma separated list of paths to exclude from logging, eg: "/ping,/path2" (default "" = no paths excluded)
4647 -flush-interval: period between flushing response buffers when streaming responses (default "1s")
4748 -banner string: custom banner string. Use "-" to disable default banner.
4849 -footer string: custom footer string. Use "-" to disable default footer.
@@ -142,7 +143,7 @@ There are three different types of logging: standard, authentication, and HTTP r
142143
143144Each type of logging has their own configurable format and variables. By default these formats are similar to the Apache Combined Log.
144145
145−Logging of requests to the `/ping` endpoint can be disabled with `-silence-ping-logging` reducing log volume. This flag sets the `-exclude-logging-path` value to the `-ping-path` and takes precedence over any other value `-exclude-logging-path` may have been set to directly.
146+Logging of requests to the `/ping` endpoint can be disabled with `-silence-ping-logging` reducing log volume. This flag appends the `-ping-path` to `-exclude-logging-paths`.
146147
147148### Auth Log Format
148149Authentication logs are logs which are guaranteed to contain a username or email address of a user attempting to authenticate. These logs are output by default in the below format:
logging_handler_test.go+18−10
@@ -19,16 +19,21 @@ func TestLoggingHandler_ServeHTTP(t *testing.T) {
1919 Format,
2020 ExpectedLogMessage,
2121 Path string
22− ExcludePath string
22+ ExcludePaths []string
23+ SilencePingLogging bool
2324 }{
24− {logger.DefaultRequestLoggingFormat, fmt.Sprintf("127.0.0.1 - - [%s] test-server GET - \"/foo/bar\" HTTP/1.1 \"\" 200 4 0.000\n", logger.FormatTimestamp(ts)), "/foo/bar", ""},
25− {logger.DefaultRequestLoggingFormat, fmt.Sprintf("127.0.0.1 - - [%s] test-server GET - \"/foo/bar\" HTTP/1.1 \"\" 200 4 0.000\n", logger.FormatTimestamp(ts)), "/foo/bar", "/ping"},
26− {logger.DefaultRequestLoggingFormat, fmt.Sprintf("127.0.0.1 - - [%s] test-server GET - \"/ping\" HTTP/1.1 \"\" 200 4 0.000\n", logger.FormatTimestamp(ts)), "/ping", ""},
27− {logger.DefaultRequestLoggingFormat, "", "/ping", "/ping"},
28− {"{{.RequestMethod}}", "GET\n", "/foo/bar", ""},
29− {"{{.RequestMethod}}", "GET\n", "/foo/bar", "/ping"},
30− {"{{.RequestMethod}}", "GET\n", "/ping", ""},
31− {"{{.RequestMethod}}", "", "/ping", "/ping"},
25+ {logger.DefaultRequestLoggingFormat, fmt.Sprintf("127.0.0.1 - - [%s] test-server GET - \"/foo/bar\" HTTP/1.1 \"\" 200 4 0.000\n", logger.FormatTimestamp(ts)), "/foo/bar", []string{}, false},
26+ {logger.DefaultRequestLoggingFormat, fmt.Sprintf("127.0.0.1 - - [%s] test-server GET - \"/foo/bar\" HTTP/1.1 \"\" 200 4 0.000\n", logger.FormatTimestamp(ts)), "/foo/bar", []string{}, true},
27+ {logger.DefaultRequestLoggingFormat, fmt.Sprintf("127.0.0.1 - - [%s] test-server GET - \"/foo/bar\" HTTP/1.1 \"\" 200 4 0.000\n", logger.FormatTimestamp(ts)), "/foo/bar", []string{"/ping"}, false},
28+ {logger.DefaultRequestLoggingFormat, "", "/foo/bar", []string{"/foo/bar"}, false},
29+ {logger.DefaultRequestLoggingFormat, "", "/ping", []string{}, true},
30+ {logger.DefaultRequestLoggingFormat, "", "/ping", []string{"/ping"}, false},
31+ {logger.DefaultRequestLoggingFormat, "", "/ping", []string{"/ping"}, true},
32+ {logger.DefaultRequestLoggingFormat, "", "/ping", []string{"/foo/bar", "/ping"}, false},
33+ {"{{.RequestMethod}}", "GET\n", "/foo/bar", []string{}, true},
34+ {"{{.RequestMethod}}", "GET\n", "/foo/bar", []string{"/ping"}, false},
35+ {"{{.RequestMethod}}", "GET\n", "/ping", []string{}, false},
36+ {"{{.RequestMethod}}", "", "/ping", []string{"/ping"}, true},
3237 }
3338
3439 for _, test := range tests {
@@ -44,7 +49,10 @@ func TestLoggingHandler_ServeHTTP(t *testing.T) {
4449
4550 logger.SetOutput(buf)
4651 logger.SetReqTemplate(test.Format)
47− logger.SetExcludePath(test.ExcludePath)
52+ if test.SilencePingLogging {
53+ test.ExcludePaths = append(test.ExcludePaths, "/ping")
54+ }
55+ logger.SetExcludePaths(test.ExcludePaths)
4856 h := LoggingHandler(http.HandlerFunc(handler))
4957
5058 r, _ := http.NewRequest("GET", test.Path, nil)
main.go+1−1
@@ -99,7 +99,7 @@ func main() {
9999
100100 flagSet.Bool("request-logging", true, "Log HTTP requests")
101101 flagSet.String("request-logging-format", logger.DefaultRequestLoggingFormat, "Template for HTTP request log lines")
102− flagSet.String("exclude-logging-path", "", "Exclude logging requests to path (eg: /ping)")
102+ flagSet.String("exclude-logging-paths", "", "Exclude logging requests to paths (eg: '/path1,/path2,/path3')")
103103 flagSet.Bool("silence-ping-logging", false, "Disable logging of requests to ping endpoint")
104104
105105 flagSet.Bool("auth-logging", true, "Log authentication attempts")
options.go+7−5
@@ -104,7 +104,7 @@ type Options struct {
104104 StandardLoggingFormat string `flag:"standard-logging-format" cfg:"standard_logging_format" env:"OAUTH2_PROXY_STANDARD_LOGGING_FORMAT"`
105105 RequestLogging bool `flag:"request-logging" cfg:"request_logging" env:"OAUTH2_PROXY_REQUEST_LOGGING"`
106106 RequestLoggingFormat string `flag:"request-logging-format" cfg:"request_logging_format" env:"OAUTH2_PROXY_REQUEST_LOGGING_FORMAT"`
107− ExcludeLoggingPath string `flag:"exclude-logging-path" cfg:"exclude_logging_path" env:"OAUTH2_PROXY_EXCLUDE_LOGGING_PATH"`
107+ ExcludeLoggingPaths string `flag:"exclude-logging-paths" cfg:"exclude_logging_paths" env:"OAUTH2_EXCLUDE_LOGGING_PATHS"`
108108 SilencePingLogging bool `flag:"silence-ping-logging" cfg:"silence_ping_logging" env:"OAUTH2_PROXY_SILENCE_PING_LOGGING"`
109109 AuthLogging bool `flag:"auth-logging" cfg:"auth_logging" env:"OAUTH2_PROXY_LOGGING_AUTH_LOGGING"`
110110 AuthLoggingFormat string `flag:"auth-logging-format" cfg:"auth_logging_format" env:"OAUTH2_PROXY_AUTH_LOGGING_FORMAT"`
@@ -168,7 +168,7 @@ func NewOptions() *Options {
168168 LoggingMaxBackups: 0,
169169 LoggingLocalTime: true,
170170 LoggingCompress: false,
171− ExcludeLoggingPath: "",
171+ ExcludeLoggingPaths: "",
172172 SilencePingLogging: false,
173173 StandardLogging: true,
174174 StandardLoggingFormat: logger.DefaultStandardLoggingFormat,
@@ -576,12 +576,14 @@ func setupLogger(o *Options, msgs []string) []string {
576576 logger.SetAuthTemplate(o.AuthLoggingFormat)
577577 logger.SetReqTemplate(o.RequestLoggingFormat)
578578
579+ excludePaths := make([]string, 0)
580+ excludePaths = append(excludePaths, strings.Split(o.ExcludeLoggingPaths, ",")...)
579581 if o.SilencePingLogging {
580− logger.SetExcludePath(o.PingPath)
581− } else {
582− logger.SetExcludePath(o.ExcludeLoggingPath)
582+ excludePaths = append(excludePaths, o.PingPath)
583583 }
584584
585+ logger.SetExcludePaths(excludePaths)
586+
585587 if !o.LoggingLocalTime {
586588 logger.SetFlags(logger.Flags() | logger.LUTC)
587589 }
pkg/logger/logger.go+12−9
@@ -88,7 +88,7 @@ type Logger struct {
8888 stdEnabled bool
8989 authEnabled bool
9090 reqEnabled bool
91− excludePath string
91+ excludePaths map[string]struct{}
9292 stdLogTemplate *template.Template
9393 authTemplate *template.Template
9494 reqTemplate *template.Template
@@ -102,7 +102,7 @@ func New(flag int) *Logger {
102102 stdEnabled: true,
103103 authEnabled: true,
104104 reqEnabled: true,
105− excludePath: "",
105+ excludePaths: nil,
106106 stdLogTemplate: template.Must(template.New("std-log").Parse(DefaultStandardLoggingFormat)),
107107 authTemplate: template.Must(template.New("auth-log").Parse(DefaultAuthLoggingFormat)),
108108 reqTemplate: template.Must(template.New("req-log").Parse(DefaultRequestLoggingFormat)),
@@ -179,7 +179,7 @@ func (l *Logger) PrintReq(username, upstream string, req *http.Request, url url.
179179 return
180180 }
181181
182− if url.Path == l.excludePath {
182+ if _, excludedPath := l.excludePaths[url.Path]; excludedPath {
183183 return
184184 }
185185
@@ -308,11 +308,14 @@ func (l *Logger) SetReqEnabled(e bool) {
308308 l.reqEnabled = e
309309}
310310
311−// SetExcludePath sets the path to exclude from logging.
312−func (l *Logger) SetExcludePath(s string) {
311+// SetExcludePaths sets the paths to exclude from logging.
312+func (l *Logger) SetExcludePaths(s []string) {
313313 l.mu.Lock()
314314 defer l.mu.Unlock()
315− l.excludePath = s
315+ l.excludePaths = make(map[string]struct{})
316+ for _, p := range s {
317+ l.excludePaths[p] = struct{}{}
318+ }
316319}
317320
318321// SetStandardTemplate sets the template for standard logging.
@@ -378,9 +381,9 @@ func SetReqEnabled(e bool) {
378381 std.SetReqEnabled(e)
379382}
380383
381−// SetExcludePath sets the path to exclude from logging, eg: health checks
382−func SetExcludePath(s string) {
383− std.SetExcludePath(s)
384+// SetExcludePaths sets the path to exclude from logging, eg: health checks
385+func SetExcludePaths(s []string) {
386+ std.SetExcludePaths(s)
384387}
385388
386389// SetStandardTemplate sets the template for standard logging for