Permalink
Browse files

Honor sessionsDir setting in session task, fix panic on error

The walker wasn't checking for an error and then panicing in
some circumstances when the FileInfo was nil.
  • Loading branch information...
lfkeitel committed Jan 3, 2018
1 parent bab8ffe commit ee1435ad2c055b3bfcb97db5553b3c8a60645150
Showing with 40 additions and 8 deletions.
  1. +17 −8 src/tasks/oldSession.go
  2. +23 −0 src/tasks/oldSessions_test.go
@@ -13,8 +13,6 @@ import (
"github.com/packet-guardian/packet-guardian/src/common"
)
const sessionsDir string = "sessions"
var sessionExpiration = time.Duration(-24) * time.Hour
func init() {
@@ -32,21 +30,32 @@ func cleanUpExpiredSessions(e *common.Environment) (string, error) {
}
func cleanFileSystemSessions(e *common.Environment) (string, error) {
w := &sessionWalker{n: time.Now().Add(sessionExpiration)}
err := filepath.Walk(sessionsDir, w.sessionDirWalker)
if err != nil {
w := &sessionWalker{
n: time.Now().Add(sessionExpiration),
sessionsDir: e.Config.Webserver.SessionsDir,
}
if err := w.walk(); err != nil {
return "", err
}
return fmt.Sprintf("Deleted %d sessions", w.c), nil
}
type sessionWalker struct {
n time.Time
c int
n time.Time
c int
sessionsDir string
}
func (s *sessionWalker) walk() error {
return filepath.Walk(s.sessionsDir, s.sessionDirWalker)
}
func (s *sessionWalker) sessionDirWalker(path string, info os.FileInfo, err error) error {
if info.IsDir() && path != sessionsDir {
if err != nil {
return err
}
if info.IsDir() && path != s.sessionsDir {
return filepath.SkipDir
}
if info.ModTime().Before(s.n) {
@@ -0,0 +1,23 @@
package tasks
import (
"os"
"testing"
"github.com/packet-guardian/packet-guardian/src/common"
)
func TestFilesystemSessionTaskEmptyDir(t *testing.T) {
fakeEnv := common.NewEnvironment(common.EnvTesting)
fakeEnv.Config = common.NewEmptyConfig()
fakeEnv.Config.Webserver.SessionStore = "filesystem"
fakeEnv.Config.Webserver.SessionsDir = "sessions"
os.MkdirAll("sessions", 0755)
defer os.RemoveAll("sessions")
_, err := cleanFileSystemSessions(fakeEnv)
if err != nil {
t.Fatal(err)
}
}

0 comments on commit ee1435a

Please sign in to comment.