forked from bitly/oauth2_proxy
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request bitly#423 from Jimdo/configure_accesslog_format
Make Request Logging Format Configurable
- Loading branch information
Showing
5 changed files
with
126 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -210,6 +210,7 @@ Usage of oauth2_proxy: | |
-redeem-url string: Token redemption endpoint | ||
-redirect-url string: the OAuth Redirect URL. ie: "https://internalapp.yourcompany.com/oauth2/callback" | ||
-request-logging: Log requests to stdout (default true) | ||
-request-logging-format: Template for request log lines (see "Logging Format" paragraph below) | ||
-resource string: The resource that is protected (Azure AD only) | ||
-scope string: OAuth scope specification | ||
-set-xauthrequest: set X-Auth-Request-User and X-Auth-Request-Email response headers (useful in Nginx auth_request mode) | ||
|
@@ -347,12 +348,21 @@ following: | |
|
||
## Logging Format | ||
|
||
OAuth2 Proxy logs requests to stdout in a format similar to Apache Combined Log. | ||
By default, OAuth2 Proxy logs requests to stdout in a format similar to Apache Combined Log. | ||
|
||
``` | ||
<REMOTE_ADDRESS> - <[email protected]> [19/Mar/2015:17:20:19 -0400] <HOST_HEADER> GET <UPSTREAM_HOST> "/path/" HTTP/1.1 "<USER_AGENT>" <RESPONSE_CODE> <RESPONSE_BYTES> <REQUEST_DURATION> | ||
``` | ||
|
||
If you require a different format than that, you can configure it with the `-request-logging-format` flag. | ||
The default format is configured as follows: | ||
|
||
``` | ||
{{.Client}} - {{.Username}} [{{.Timestamp}}] {{.Host}} {{.RequestMethod}} {{.Upstream}} {{.RequestURI}} {{.Protocol}} {{.UserAgent}} {{.StatusCode}} {{.ResponseSize}} {{.RequestDuration}} | ||
``` | ||
|
||
[See `logMessageData` in `logging_handler.go`](./logging_handler.go) for all available variables. | ||
|
||
## Adding a new Provider | ||
|
||
Follow the examples in the [`providers` package](providers/) to define a new | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestLoggingHandler_ServeHTTP(t *testing.T) { | ||
ts := time.Now() | ||
|
||
tests := []struct { | ||
Format, | ||
ExpectedLogMessage string | ||
}{ | ||
{defaultRequestLoggingFormat, fmt.Sprintf("127.0.0.1 - - [%s] test-server GET - \"/foo/bar\" HTTP/1.1 \"\" 200 4 0.000\n", ts.Format("02/Jan/2006:15:04:05 -0700"))}, | ||
{"{{.RequestMethod}}", "GET\n"}, | ||
} | ||
|
||
for _, test := range tests { | ||
buf := bytes.NewBuffer(nil) | ||
handler := func(w http.ResponseWriter, req *http.Request) { | ||
w.Write([]byte("test")) | ||
} | ||
|
||
h := LoggingHandler(buf, http.HandlerFunc(handler), true, test.Format) | ||
|
||
r, _ := http.NewRequest("GET", "/foo/bar", nil) | ||
r.RemoteAddr = "127.0.0.1" | ||
r.Host = "test-server" | ||
|
||
h.ServeHTTP(httptest.NewRecorder(), r) | ||
|
||
actual := buf.String() | ||
if actual != test.ExpectedLogMessage { | ||
t.Errorf("Log message was\n%s\ninstead of expected \n%s", actual, test.ExpectedLogMessage) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters