Skip to content

Commit abb4627

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 abb4627

File tree

5 files changed

+42
-17
lines changed

5 files changed

+42
-17
lines changed

README.md

+19-17
Original file line numberDiff line numberDiff line change
@@ -54,23 +54,25 @@ Usage:
5454
rest-server [flags]
5555

5656
Flags:
57-
--append-only enable append only mode
58-
--cpu-profile string write CPU profile to file
59-
--debug output debug messages
60-
-h, --help help for rest-server
61-
--listen string listen address (default ":8000")
62-
--log string log HTTP requests in the combined log format
63-
--max-size int the maximum size of the repository in bytes
64-
--no-auth disable .htpasswd authentication
65-
--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
66-
--path string data directory (default "/tmp/restic")
67-
--private-repos users can only access their private repo
68-
--prometheus enable Prometheus metrics
69-
--prometheus-no-auth disable auth for Prometheus /metrics endpoint
70-
--tls turn on TLS support
71-
--tls-cert string TLS certificate path
72-
--tls-key string TLS key path
73-
-v, --version version for rest-server
57+
--append-only enable append only mode
58+
--cpu-profile string write CPU profile to file
59+
--debug output debug messages
60+
--header-for-ip string use a header to obtain the ip for unauthorized request logging
61+
-h, --help help for rest-server
62+
--listen string listen address (default ":8000")
63+
--log string log HTTP requests in the combined log format
64+
--log-auth-failure log the ip address of unauthorized requests
65+
--max-size int the maximum size of the repository in bytes
66+
--no-auth disable .htpasswd authentication
67+
--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
68+
--path string data directory (default "/tmp/restic")
69+
--private-repos users can only access their private repo
70+
--prometheus enable Prometheus metrics
71+
--prometheus-no-auth disable auth for Prometheus /metrics endpoint
72+
--tls turn on TLS support
73+
--tls-cert string TLS certificate path
74+
--tls-key string TLS key path
75+
-v, --version version for rest-server
7476
```
7577

7678
By default the server persists backup data in `/tmp/restic`. To start the server with a custom persistence directory and with authentication disabled:

changelog/unreleased/pull-167

+11
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

+2
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

+2
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

+8
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)