Files

136 lines
3.5 KiB
Go

// Package freshness bounds how long a process may use OpenBao-rendered credentials.
package freshness
import (
"context"
"errors"
"io"
"os"
"strings"
"syscall"
"time"
)
const MaxBytes int64 = 128
var (
ErrInvalid = errors.New("secret freshness timestamp invalid")
ErrFuture = errors.New("secret freshness timestamp is in the future")
ErrStale = errors.New("secret freshness expired")
ErrUnreadable = errors.New("secret freshness unavailable")
ErrConfiguration = errors.New("secret freshness configuration invalid")
)
type Reader func(context.Context, int64) ([]byte, error)
type Ticker struct {
C <-chan time.Time
Stop func()
}
type Ports struct {
Read Reader
Now func() time.Time
NewTicker func(time.Duration) Ticker
}
type Watcher struct {
ports Ports
maxAge, interval time.Duration
}
// New requires explicit ports so tests cannot accidentally read live credentials or control processes.
func New(ports Ports, maxAge, interval time.Duration) (*Watcher, error) {
if ports.Read == nil || ports.Now == nil || ports.NewTicker == nil || maxAge <= 0 || interval <= 0 || interval > 5*time.Second || interval > maxAge {
return nil, ErrConfiguration
}
return &Watcher{ports: ports, maxAge: maxAge, interval: interval}, nil
}
func (w *Watcher) Check(ctx context.Context) error {
if err := ctx.Err(); err != nil {
return err
}
raw, err := w.ports.Read(ctx, MaxBytes+1)
if contextErr := ctx.Err(); contextErr != nil {
return contextErr
}
if err != nil {
return ErrUnreadable
}
if len(raw) == 0 || int64(len(raw)) > MaxBytes {
return ErrInvalid
}
issued, err := time.Parse(time.RFC3339Nano, strings.TrimSpace(string(raw)))
if err != nil {
return ErrInvalid
}
now := w.ports.Now()
if issued.After(now) {
return ErrFuture
}
if now.Sub(issued) > w.maxAge {
return ErrStale
}
return nil
}
// Run validates startup before allocating its ticker and returns the first failure.
// The composition root owns graceful shutdown; this package never exits or signals a process.
func (w *Watcher) Run(ctx context.Context) error {
if err := w.Check(ctx); err != nil {
return err
}
ticker := w.ports.NewTicker(w.interval)
if ticker.Stop == nil {
return ErrConfiguration
}
defer ticker.Stop()
if ticker.C == nil {
return ErrConfiguration
}
for {
select {
case <-ctx.Done():
return ctx.Err()
case _, ok := <-ticker.C:
if !ok {
return ErrConfiguration
}
if err := w.Check(ctx); err != nil {
return err
}
}
}
}
// FileReader opens only the named file, never reads more than the requested bound,
// and deliberately hides path/system error detail from the runtime failure signal.
func FileReader(path string) Reader {
return func(ctx context.Context, limit int64) ([]byte, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
if limit < 1 || limit > MaxBytes+1 {
return nil, ErrConfiguration
}
file, err := os.OpenFile(path, os.O_RDONLY|syscall.O_NONBLOCK|syscall.O_NOFOLLOW, 0)
if err != nil {
return nil, ErrUnreadable
}
defer func() { _ = file.Close() }()
info, err := file.Stat()
if err != nil || !info.Mode().IsRegular() {
return nil, ErrUnreadable
}
raw, err := io.ReadAll(io.LimitReader(file, limit))
if err != nil {
return nil, ErrUnreadable
}
if err = ctx.Err(); err != nil {
return nil, err
}
return raw, nil
}
}
func SystemTicker(interval time.Duration) Ticker {
ticker := time.NewTicker(interval)
return Ticker{C: ticker.C, Stop: ticker.Stop}
}