Skip to content

Commit 86c20b5

Browse files
Users: Log unauthorized requests
This patch adds a new configuration option to the web.config.file which makes it possible to enable logging of unauthorized requests. If "log_unauthorized" at config file's top level is set to true, any unauthorized request will have the ip as well as the X-Forwarded-For header logged. This way, a program that might parse the logs can determent for itself if the X-Forwarded-For header can be trusted.
1 parent 7cd0e90 commit 86c20b5

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

web/tls_config.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ var (
3434
)
3535

3636
type Config struct {
37-
TLSConfig TLSStruct `yaml:"tls_server_config"`
38-
HTTPConfig HTTPStruct `yaml:"http_server_config"`
39-
Users map[string]config_util.Secret `yaml:"basic_auth_users"`
37+
TLSConfig TLSStruct `yaml:"tls_server_config"`
38+
HTTPConfig HTTPStruct `yaml:"http_server_config"`
39+
Users map[string]config_util.Secret `yaml:"basic_auth_users"`
40+
LogUnauthorized bool `yaml:"log_unauthorized"`
4041
}
4142

4243
type TLSStruct struct {
@@ -73,7 +74,8 @@ func getConfig(configPath string) (*Config, error) {
7374
MaxVersion: tls.VersionTLS13,
7475
PreferServerCipherSuites: true,
7576
},
76-
HTTPConfig: HTTPStruct{HTTP2: true},
77+
HTTPConfig: HTTPStruct{HTTP2: true},
78+
LogUnauthorized: false,
7779
}
7880
err = yaml.UnmarshalStrict(content, c)
7981
c.TLSConfig.SetDirectory(filepath.Dir(configPath))

web/users.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"sync"
2222

2323
"github.com/go-kit/log"
24+
"github.com/go-kit/log/level"
2425
"golang.org/x/crypto/bcrypt"
2526
)
2627

@@ -93,6 +94,10 @@ func (u *userAuthRoundtrip) ServeHTTP(w http.ResponseWriter, r *http.Request) {
9394
}
9495
}
9596

97+
if c.LogUnauthorized {
98+
level.Info(u.logger).Log("msg", "Unauthorized", "ip", r.RemoteAddr, "forwarded", r.Header.Get("X-Forwarded-For"))
99+
}
100+
96101
w.Header().Set("WWW-Authenticate", "Basic")
97102
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
98103
}

0 commit comments

Comments
 (0)