Skip to content

Commit

Permalink
Config+Auth: Add flags to log unauthorized requests
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
networkException committed Sep 11, 2021
1 parent 1172d7e commit 30b3069
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ Flags:
--append-only enable append only mode
--cpu-profile string write CPU profile to file
--debug output debug messages
--header-for-ip string use a header to obtain the ip for unauthorized request logging
-h, --help help for rest-server
--listen string listen address (default ":8000")
--log string log HTTP requests in the combined log format
--log-auth-failure log the ip address of unauthorized requests
--max-size int the maximum size of the repository in bytes
--no-auth disable .htpasswd authentication
--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
Expand Down
11 changes: 11 additions & 0 deletions changelog/unreleased/pull-167
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Feature: Logging of unauthorized requests

Two new command line flags have been added 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.

https://github.com/restic/rest-server/pull/167
https://forum.restic.net/t/rest-server-and-fail2ban/2569
2 changes: 2 additions & 0 deletions cmd/rest-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ func init() {
flags := cmdRoot.Flags()
flags.StringVar(&cpuProfile, "cpu-profile", cpuProfile, "write CPU profile to file")
flags.BoolVar(&server.Debug, "debug", server.Debug, "output debug messages")
flags.BoolVar(&server.LogAuthFailure, "log-auth-failure", server.LogAuthFailure, "log the ip address of unauthorized requests")
flags.StringVar(&server.HeaderForIP, "header-for-ip", server.HeaderForIP, "use a header to obtain the ip for unauthorized request logging")
flags.StringVar(&server.Listen, "listen", server.Listen, "listen address")
flags.StringVar(&server.Log, "log", server.Log, "log HTTP requests in the combined log format")
flags.Int64Var(&server.MaxRepoSize, "max-size", server.MaxRepoSize, "the maximum size of the repository in bytes")
Expand Down
2 changes: 2 additions & 0 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type Server struct {
Prometheus bool
PrometheusNoAuth bool
Debug bool
LogAuthFailure bool
HeaderForIP string
MaxRepoSize int64
PanicOnError bool
NoVerifyUpload bool
Expand Down
8 changes: 8 additions & 0 deletions mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ func (s *Server) checkAuth(r *http.Request) (username string, ok bool) {
var password string
username, password, ok = r.BasicAuth()
if !ok || !s.htpasswdFile.Validate(username, password) {
if s.LogAuthFailure {
if s.HeaderForIP != "" {
log.Printf("unauthorized: %s", r.Header.Get(s.HeaderForIP))
} else {
log.Printf("unauthorized: %s", r.RemoteAddr)
}
}

return "", false
}
return username, true
Expand Down

0 comments on commit 30b3069

Please sign in to comment.