diff --git a/vault/logical_backend.go b/vault/logical_backend.go index b698c26080..ccafe1a36a 100644 --- a/vault/logical_backend.go +++ b/vault/logical_backend.go @@ -112,6 +112,23 @@ type Lease struct { MaxIncrement time.Duration // Maximum increment to lease duration } +// Validate is used to sanity check a lease +func (l *Lease) Validate() error { + if l.Duration <= 0 { + return fmt.Errorf("lease duration must be greater than zero") + } + if l.MaxDuration <= 0 { + return fmt.Errorf("maximum lease duration must be greater than zero") + } + if l.Duration > l.MaxDuration { + return fmt.Errorf("lease duration cannot be greater than maximum lease duration") + } + if l.MaxIncrement < 0 { + return fmt.Errorf("maximum lease increment cannot be negative") + } + return nil +} + // Factory is the factory function to create a logical backend. type Factory func(map[string]string) (LogicalBackend, error) diff --git a/vault/logical_backend_test.go b/vault/logical_backend_test.go new file mode 100644 index 0000000000..1a7e8bab88 --- /dev/null +++ b/vault/logical_backend_test.go @@ -0,0 +1,34 @@ +package vault + +import ( + "testing" + "time" +) + +func TestLease_Validate(t *testing.T) { + l := &Lease{} + if err := l.Validate(); err.Error() != "lease duration must be greater than zero" { + t.Fatalf("err: %v", err) + } + + l.Duration = time.Minute + if err := l.Validate(); err.Error() != "maximum lease duration must be greater than zero" { + t.Fatalf("err: %v", err) + } + + l.MaxDuration = time.Second + if err := l.Validate(); err.Error() != "lease duration cannot be greater than maximum lease duration" { + t.Fatalf("err: %v", err) + } + + l.MaxDuration = time.Minute + l.MaxIncrement = -1 * time.Second + if err := l.Validate(); err.Error() != "maximum lease increment cannot be negative" { + t.Fatalf("err: %v", err) + } + + l.MaxIncrement = time.Second + if err := l.Validate(); err != nil { + t.Fatalf("err: %v", err) + } +}