loginsrv

Unnamed repository; edit this file 'description' to name the repository.
git clone git@jamesshield.xyz:repos/loginsrv.git
Log | Files | Refs | README | LICENSE

commit 8e70a3a3333be1c03f6a83c51fd0d52bb8561f93
parent 5ac72cf332a3bca1770dda16c48b93fb5bea06a5
Author: Sebastian Mancke <sebastian.mancke@snabble.io>
Date:   Wed, 26 Dec 2018 23:02:47 +0100

reworked the update handling of password filed, inspired by PR #74

Diffstat:
Mhtpasswd/auth.go | 13++++++++-----
Mhtpasswd/backend_test.go | 16+++++++++++++---
2 files changed, 21 insertions(+), 8 deletions(-)

diff --git a/htpasswd/auth.go b/htpasswd/auth.go @@ -41,23 +41,25 @@ func NewAuth(filenames []string) (*Auth, error) { a := &Auth{ filenames: htpasswdFiles, } - return a, a.parse(htpasswdFiles) + return a, a.parse() } -func (a *Auth) parse(filenames []File) error { +func (a *Auth) parse() error { tmpUserHash := map[string]string{} + tmpFilenames := a.filenames - for _, filename := range a.filenames { + for i, filename := range a.filenames { r, err := os.Open(filename.name) if err != nil { return err } + defer r.Close() fileInfo, err := os.Stat(filename.name) if err != nil { return err } - filename.modTime = fileInfo.ModTime() + tmpFilenames[i].modTime = fileInfo.ModTime() cr := csv.NewReader(r) cr.Comma = ':' @@ -84,6 +86,7 @@ func (a *Auth) parse(filenames []File) error { } a.muUserHash.Lock() a.userHash = tmpUserHash + a.filenames = tmpFilenames a.muUserHash.Unlock() return nil @@ -122,7 +125,7 @@ func reloadIfChanged(a *Auth) { } currentmodTime := fileInfo.ModTime() if currentmodTime != file.modTime { - a.parse(a.filenames) + a.parse() return } } diff --git a/htpasswd/backend_test.go b/htpasswd/backend_test.go @@ -3,8 +3,10 @@ package htpasswd import ( . "github.com/stretchr/testify/assert" "github.com/tarent/loginsrv/login" + "os" "strings" "testing" + "time" ) func TestSetupOneFile(t *testing.T) { @@ -19,7 +21,7 @@ func TestSetupOneFile(t *testing.T) { NoError(t, err) Equal(t, - []File{File{name: files[0]}}, + []File{File{files[0], modTime(files[0])}}, backend.(*Backend).auth.filenames) } @@ -32,7 +34,7 @@ func TestSetupTwoFiles(t *testing.T) { var morphed []File for _, curFile := range filenames { - morphed = append(morphed, File{name: curFile}) + morphed = append(morphed, File{curFile, modTime(curFile)}) } backend, err := p(map[string]string{ "file": strings.Join(filenames, ";"), @@ -54,7 +56,7 @@ func TestSetupTwoConfigs(t *testing.T) { var morphed []File for _, curFile := range append(configFiles, configFile...) { - morphed = append(morphed, File{name: curFile}) + morphed = append(morphed, File{curFile, modTime(curFile)}) } backend, err := p(map[string]string{ @@ -96,3 +98,11 @@ func TestSimpleBackend_Authenticate(t *testing.T) { Equal(t, "", userInfo.Sub) NoError(t, err) } + +func modTime(f string) time.Time { + fileInfo, err := os.Stat(f) + if err != nil { + panic(err) + } + return fileInfo.ModTime() +}