feat: introduce structured, field logging (#87)

* feat: introduce structured, field logging

The logging is currently minimal and only for initial message of listening and user login attempts

* chore: major version upgarde

there was a breaking API change in 8cd6d0a585 for which the git tag was v4.0.0 but the Go SIV was missed. This commit updates the SIV and retracts the defective version of v4.1.0.
This commit is contained in:
Mohammed Al Sahaf
2021-10-19 17:45:54 +00:00
committed by GitHub
parent 5701cbb5b8
commit 8a8650d9b2
8 changed files with 64 additions and 89 deletions
-31
View File
@@ -1,31 +0,0 @@
package lib
import (
"context"
"log"
"time"
)
// map[reqHost:username]lastRequestSuccessTime
var authorizedSource = make(map[string]time.Time, 1)
func LastRequestLogIndex(ctx context.Context) {
updateInterval := 5 * time.Second
ticker := time.NewTicker(updateInterval)
for {
ticker.Reset(updateInterval)
select {
case <-ctx.Done():
log.Println("received a signal to cancel the service")
return
case <-ticker.C:
for v, k := range authorizedSource {
// no response for 2 minutes log again
if k.Unix()+(60*2) < time.Now().Unix() {
log.Printf("%s cache will be clean\n", v)
delete(authorizedSource, v)
}
}
}
}
}
+10 -20
View File
@@ -2,11 +2,10 @@ package lib
import (
"context"
"fmt"
"log"
"net/http"
"strings"
"time"
"go.uber.org/zap"
)
// CorsCfg is the CORS config.
@@ -22,10 +21,11 @@ type CorsCfg struct {
// Config is the configuration of a WebDAV instance.
type Config struct {
*User
Auth bool
NoSniff bool
Cors CorsCfg
Users map[string]*User
Auth bool
NoSniff bool
Cors CorsCfg
Users map[string]*User
LogFormat string
}
// ServeHTTP determines if the request is for this plugin, and if all prerequisites are met.
@@ -74,16 +74,7 @@ func (c *Config) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Gets the correct user for this request.
username, password, ok := r.BasicAuth()
reqHost := func() string {
hArr := strings.Split(r.RemoteAddr, ":")
return hArr[0]
}
reqMark := fmt.Sprintf("%s:%s", reqHost(), username)
if _, found := authorizedSource[reqMark]; !found {
log.Printf("%s tried to verify account , username is [%s]", r.RemoteAddr, username)
}
zap.L().Info("login attempt", zap.String("username", username), zap.String("remote_address", r.RemoteAddr))
if !ok {
http.Error(w, "Not authorized", 401)
return
@@ -96,14 +87,13 @@ func (c *Config) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
if !checkPassword(user.Password, password) {
log.Println("Wrong Password for user", username)
zap.L().Info("invalid password", zap.String("username", username), zap.String("remote_address", r.RemoteAddr))
http.Error(w, "Not authorized", 401)
return
} else {
authorizedSource[reqMark] = time.Now()
}
u = user
zap.L().Info("user authorized", zap.String("username", username))
} else {
// Even if Auth is disabled, we might want to get
// the user from the Basic Auth header. Useful for Caddy