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
}
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.Handle("GET /", routes.ServeFiles(embedFS, "static"))

View file

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

View file

@ -5,23 +5,18 @@ import (
"time"
)
func NewRamStore(clearInterval time.Duration) Store {
func NewRamStore() Store {
store := &ram{
clearInterval: clearInterval,
passwords: make(map[string]entry),
lock: sync.Mutex{},
close: make(chan bool),
}
go store.clearExpired()
return store
}
type ram struct {
clearInterval time.Duration
passwords map[string]entry
lock sync.Mutex
close chan bool
}
func (store *ram) Create(password []byte, expiresAt time.Time) (string, error) {
@ -63,30 +58,19 @@ func (store *ram) Delete(id string) error {
return nil
}
func (store *ram) Close() 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:
func (store *ram) ClearExpired() error {
store.lock.Lock()
defer store.lock.Unlock()
time := time.Now()
for id, password := range store.passwords {
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)
Get(id string) ([]byte, error)
Delete(id string) error
Close() error
ClearExpired() error
}
func NewStore() (Store, error) {
@ -32,7 +32,7 @@ func NewStore() (Store, error) {
switch storeType {
case "ram":
return NewRamStore(20 * time.Second), nil
return NewRamStore(), nil
case "dir":
path := os.Getenv("PASSED_STORE_DIR_PATH")
if path == "" {
@ -40,11 +40,11 @@ func NewStore() (Store, error) {
path = "passwords"
}
return NewDirStore(60*time.Second, path), nil
return NewDirStore(path), nil
default:
log.Printf("No PASSED_STORE_TYPE provided, defaulting to memory store.")
return NewRamStore(20 * time.Second), nil
return NewRamStore(), nil
}
}