Skip to content

Commit 30b3069

Browse files
Config+Auth: Add flags to log unauthorized requests
This patch adds new command line flags in order to support logging of unauthorized requests to the server. The flag `--log-auth-failure` enables the logging and uses the remote address of the request as the default for the logged ip. If the server is used behind a reverse proxy for, `--header-for-ip` can be used to specify a header like "X-Forwarded-For" to be used for logging the ip.
1 parent 1172d7e commit 30b3069

File tree

5 files changed

+25
-0
lines changed

5 files changed

+25
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,11 @@ Flags:
5757
--append-only enable append only mode
5858
--cpu-profile string write CPU profile to file
5959
--debug output debug messages
60+
--header-for-ip string use a header to obtain the ip for unauthorized request logging
6061
-h, --help help for rest-server
6162
--listen string listen address (default ":8000")
6263
--log string log HTTP requests in the combined log format
64+
--log-auth-failure log the ip address of unauthorized requests
6365
--max-size int the maximum size of the repository in bytes
6466
--no-auth disable .htpasswd authentication
6567
--no-verify-upload do not verify the integrity of uploaded data. DO NOT enable unless the rest-server runs on a very low-power device

changelog/unreleased/pull-167

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Feature: Logging of unauthorized requests
2+
3+
Two new command line flags have been added in order to support logging of
4+
unauthorized requests to the server. The flag `--log-auth-failure` enables
5+
the logging and uses the remote address of the request as the default for
6+
the logged ip. If the server is used behind a reverse proxy for, `--header-for-ip`
7+
can be used to specify a header like "X-Forwarded-For" to be used for logging
8+
the ip.
9+
10+
https://github.com/restic/rest-server/pull/167
11+
https://forum.restic.net/t/rest-server-and-fail2ban/2569

cmd/rest-server/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ func init() {
3939
flags := cmdRoot.Flags()
4040
flags.StringVar(&cpuProfile, "cpu-profile", cpuProfile, "write CPU profile to file")
4141
flags.BoolVar(&server.Debug, "debug", server.Debug, "output debug messages")
42+
flags.BoolVar(&server.LogAuthFailure, "log-auth-failure", server.LogAuthFailure, "log the ip address of unauthorized requests")
43+
flags.StringVar(&server.HeaderForIP, "header-for-ip", server.HeaderForIP, "use a header to obtain the ip for unauthorized request logging")
4244
flags.StringVar(&server.Listen, "listen", server.Listen, "listen address")
4345
flags.StringVar(&server.Log, "log", server.Log, "log HTTP requests in the combined log format")
4446
flags.Int64Var(&server.MaxRepoSize, "max-size", server.MaxRepoSize, "the maximum size of the repository in bytes")

handlers.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ type Server struct {
2727
Prometheus bool
2828
PrometheusNoAuth bool
2929
Debug bool
30+
LogAuthFailure bool
31+
HeaderForIP string
3032
MaxRepoSize int64
3133
PanicOnError bool
3234
NoVerifyUpload bool

mux.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ func (s *Server) checkAuth(r *http.Request) (username string, ok bool) {
3636
var password string
3737
username, password, ok = r.BasicAuth()
3838
if !ok || !s.htpasswdFile.Validate(username, password) {
39+
if s.LogAuthFailure {
40+
if s.HeaderForIP != "" {
41+
log.Printf("unauthorized: %s", r.Header.Get(s.HeaderForIP))
42+
} else {
43+
log.Printf("unauthorized: %s", r.RemoteAddr)
44+
}
45+
}
46+
3947
return "", false
4048
}
4149
return username, true

0 commit comments

Comments
 (0)