mirror of
https://github.com/optim-enterprises-bv/vault.git
synced 2025-11-02 03:27:54 +00:00
vault: Validate lease values
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
34
vault/logical_backend_test.go
Normal file
34
vault/logical_backend_test.go
Normal file
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user