rework password expiration cleaning

This commit is contained in:
1e99 2024-10-30 11:47:17 +01:00
parent e3805b1a04
commit aaa1715f32
4 changed files with 69 additions and 90 deletions

15
main.go
View file

@ -22,7 +22,20 @@ func run() error {
return err return err
} }
defer storage.Close() go func() {
ticker := time.Tick(20 * time.Second)
for {
<-ticker
err := storage.ClearExpired()
if err != nil {
log.Printf("Failed to clear expired passwords: %s", err)
continue
}
log.Printf("Cleared expired passwords.")
}
}()
mux := http.NewServeMux() mux := http.NewServeMux()
mux.Handle("GET /", routes.ServeFiles(embedFS, "static")) mux.Handle("GET /", routes.ServeFiles(embedFS, "static"))

View file

@ -8,23 +8,16 @@ import (
"time" "time"
) )
func NewDirStore(clearInterval time.Duration, path string) Store { func NewDirStore(path string) Store {
store := &dir{ store := &dir{
clearInterval: clearInterval,
timeLayout: time.RFC3339Nano,
path: path, path: path,
close: make(chan bool),
} }
go store.clearExpired()
return store return store
} }
type dir struct { type dir struct {
clearInterval time.Duration
timeLayout string
path string path string
close chan bool
} }
func (store *dir) Create(password []byte, expiresAt time.Time) (string, error) { func (store *dir) Create(password []byte, expiresAt time.Time) (string, error) {
@ -98,26 +91,12 @@ func (store *dir) Delete(id string) error {
return nil return nil
} }
func (store *dir) Close() error { func (store *dir) ClearExpired() error {
store.close <- true
return nil
}
func (store *dir) clearExpired() error {
ticker := time.NewTicker(store.clearInterval)
defer ticker.Stop()
for {
select {
case <-store.close:
return nil
case <-ticker.C:
// TODO: Error handling?
now := time.Now() now := time.Now()
entries, err := os.ReadDir(store.path) entries, err := os.ReadDir(store.path)
if err != nil { if err != nil {
continue return err
} }
for _, file := range entries { for _, file := range entries {
@ -129,7 +108,7 @@ func (store *dir) clearExpired() error {
0, 0,
) )
if err != nil { if err != nil {
continue return err
} }
defer file.Close() defer file.Close()
@ -137,17 +116,20 @@ func (store *dir) clearExpired() error {
var entry entry var entry entry
err = gob.NewDecoder(file).Decode(&entry) err = gob.NewDecoder(file).Decode(&entry)
if err != nil { if err != nil {
continue return err
} }
if now.After(entry.ExpiresAt) { if now.After(entry.ExpiresAt) {
// Close file early as we need to delete it // Close file early as we need to delete it
file.Close() file.Close()
store.Delete(id) err = store.Delete(id)
} if err != nil {
return err
} }
} }
} }
return nil
} }
func (store *dir) getPath(id string) string { func (store *dir) getPath(id string) string {

View file

@ -5,23 +5,18 @@ import (
"time" "time"
) )
func NewRamStore(clearInterval time.Duration) Store { func NewRamStore() Store {
store := &ram{ store := &ram{
clearInterval: clearInterval,
passwords: make(map[string]entry), passwords: make(map[string]entry),
lock: sync.Mutex{}, lock: sync.Mutex{},
close: make(chan bool),
} }
go store.clearExpired()
return store return store
} }
type ram struct { type ram struct {
clearInterval time.Duration
passwords map[string]entry passwords map[string]entry
lock sync.Mutex lock sync.Mutex
close chan bool
} }
func (store *ram) Create(password []byte, expiresAt time.Time) (string, error) { func (store *ram) Create(password []byte, expiresAt time.Time) (string, error) {
@ -63,30 +58,19 @@ func (store *ram) Delete(id string) error {
return nil return nil
} }
func (store *ram) Close() error { func (store *ram) ClearExpired() error {
store.close <- true
return nil
}
func (store *ram) clearExpired() error {
ticker := time.NewTicker(store.clearInterval)
defer ticker.Stop()
for {
select {
case <-store.close:
return nil
case <-ticker.C:
store.lock.Lock() store.lock.Lock()
defer store.lock.Unlock()
time := time.Now() time := time.Now()
for id, password := range store.passwords { for id, password := range store.passwords {
if time.After(password.ExpiresAt) { if time.After(password.ExpiresAt) {
store.Delete(id) err := store.Delete(id)
if err != nil {
return err
}
} }
} }
store.lock.Unlock() return nil
}
}
} }

View file

@ -23,7 +23,7 @@ type Store interface {
Create(password []byte, expiresAt time.Time) (string, error) Create(password []byte, expiresAt time.Time) (string, error)
Get(id string) ([]byte, error) Get(id string) ([]byte, error)
Delete(id string) error Delete(id string) error
Close() error ClearExpired() error
} }
func NewStore() (Store, error) { func NewStore() (Store, error) {
@ -32,7 +32,7 @@ func NewStore() (Store, error) {
switch storeType { switch storeType {
case "ram": case "ram":
return NewRamStore(20 * time.Second), nil return NewRamStore(), nil
case "dir": case "dir":
path := os.Getenv("PASSED_STORE_DIR_PATH") path := os.Getenv("PASSED_STORE_DIR_PATH")
if path == "" { if path == "" {
@ -40,11 +40,11 @@ func NewStore() (Store, error) {
path = "passwords" path = "passwords"
} }
return NewDirStore(60*time.Second, path), nil return NewDirStore(path), nil
default: default:
log.Printf("No PASSED_STORE_TYPE provided, defaulting to memory store.") log.Printf("No PASSED_STORE_TYPE provided, defaulting to memory store.")
return NewRamStore(20 * time.Second), nil return NewRamStore(), nil
} }
} }