mirror of
https://github.com/hashicorp/vault.git
synced 2026-05-28 04:10:44 -04:00
Factor out parsing duration second type and use it for parsing tune values too
This commit is contained in:
parent
24b89dd452
commit
984641af21
4 changed files with 64 additions and 25 deletions
28
helper/duration/duration.go
Normal file
28
helper/duration/duration.go
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
package duration
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func ParseDurationSecond(inp string) (int, error) {
|
||||
var result int
|
||||
// Look for a suffix otherwise its a plain second value
|
||||
if strings.HasSuffix(inp, "s") || strings.HasSuffix(inp, "m") || strings.HasSuffix(inp, "h") {
|
||||
dur, err := time.ParseDuration(inp)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
result = int(dur.Seconds())
|
||||
} else {
|
||||
// Plain integer
|
||||
val, err := strconv.ParseInt(inp, 10, 64)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
result = int(val)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
20
helper/duration/duration_test.go
Normal file
20
helper/duration/duration_test.go
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
package duration
|
||||
|
||||
import "testing"
|
||||
|
||||
func Test_ParseDurationSecond(t *testing.T) {
|
||||
outp, err := ParseDurationSecond("9876s")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if outp != 9876 {
|
||||
t.Fatal("not equivalent")
|
||||
}
|
||||
outp, err = ParseDurationSecond("9876")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if outp != 9876 {
|
||||
t.Fatal("not equivalent")
|
||||
}
|
||||
}
|
||||
|
|
@ -2,10 +2,8 @@ package framework
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/vault/helper/duration"
|
||||
"github.com/mitchellh/mapstructure"
|
||||
)
|
||||
|
||||
|
|
@ -152,6 +150,7 @@ func (d *FieldData) getPrimitive(
|
|||
|
||||
case TypeDurationSecond:
|
||||
var result int
|
||||
var err error
|
||||
switch inp := raw.(type) {
|
||||
case nil:
|
||||
return nil, false, nil
|
||||
|
|
@ -162,20 +161,9 @@ func (d *FieldData) getPrimitive(
|
|||
case float64:
|
||||
result = int(inp)
|
||||
case string:
|
||||
// Look for a suffix otherwise its a plain second value
|
||||
if strings.HasSuffix(inp, "s") || strings.HasSuffix(inp, "m") || strings.HasSuffix(inp, "h") {
|
||||
dur, err := time.ParseDuration(inp)
|
||||
if err != nil {
|
||||
return nil, true, err
|
||||
}
|
||||
result = int(dur.Seconds())
|
||||
} else {
|
||||
// Plain integer
|
||||
val, err := strconv.ParseInt(inp, 10, 64)
|
||||
if err != nil {
|
||||
return nil, true, err
|
||||
}
|
||||
result = int(val)
|
||||
result, err = duration.ParseDurationSecond(inp)
|
||||
if err != nil {
|
||||
return nil, true, err
|
||||
}
|
||||
|
||||
default:
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import (
|
|||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/vault/helper/duration"
|
||||
"github.com/hashicorp/vault/logical"
|
||||
"github.com/hashicorp/vault/logical/framework"
|
||||
"github.com/mitchellh/mapstructure"
|
||||
|
|
@ -702,26 +703,26 @@ func (b *SystemBackend) handleMount(
|
|||
case "":
|
||||
case "system":
|
||||
default:
|
||||
tmpDef, err := time.ParseDuration(apiConfig.DefaultLeaseTTL)
|
||||
tmpDef, err := duration.ParseDurationSecond(apiConfig.DefaultLeaseTTL)
|
||||
if err != nil {
|
||||
return logical.ErrorResponse(fmt.Sprintf(
|
||||
"unable to parse default TTL of %s: %s", apiConfig.DefaultLeaseTTL, err)),
|
||||
logical.ErrInvalidRequest
|
||||
}
|
||||
config.DefaultLeaseTTL = tmpDef
|
||||
config.DefaultLeaseTTL = time.Duration(tmpDef) * time.Second
|
||||
}
|
||||
|
||||
switch apiConfig.MaxLeaseTTL {
|
||||
case "":
|
||||
case "system":
|
||||
default:
|
||||
tmpMax, err := time.ParseDuration(apiConfig.MaxLeaseTTL)
|
||||
tmpMax, err := duration.ParseDurationSecond(apiConfig.MaxLeaseTTL)
|
||||
if err != nil {
|
||||
return logical.ErrorResponse(fmt.Sprintf(
|
||||
"unable to parse max TTL of %s: %s", apiConfig.MaxLeaseTTL, err)),
|
||||
logical.ErrInvalidRequest
|
||||
}
|
||||
config.MaxLeaseTTL = tmpMax
|
||||
config.MaxLeaseTTL = time.Duration(tmpMax) * time.Second
|
||||
}
|
||||
|
||||
if config.MaxLeaseTTL != 0 && config.DefaultLeaseTTL > config.MaxLeaseTTL {
|
||||
|
|
@ -927,11 +928,12 @@ func (b *SystemBackend) handleTuneWriteCommon(
|
|||
tmpDef := time.Duration(0)
|
||||
newDefault = &tmpDef
|
||||
default:
|
||||
tmpDef, err := time.ParseDuration(defTTL)
|
||||
tmpDef, err := duration.ParseDurationSecond(defTTL)
|
||||
if err != nil {
|
||||
return handleError(err)
|
||||
}
|
||||
newDefault = &tmpDef
|
||||
tmpDurDef := time.Duration(tmpDef) * time.Second
|
||||
newDefault = &tmpDurDef
|
||||
}
|
||||
|
||||
maxTTL := data.Get("max_lease_ttl").(string)
|
||||
|
|
@ -941,11 +943,12 @@ func (b *SystemBackend) handleTuneWriteCommon(
|
|||
tmpMax := time.Duration(0)
|
||||
newMax = &tmpMax
|
||||
default:
|
||||
tmpMax, err := time.ParseDuration(maxTTL)
|
||||
tmpMax, err := duration.ParseDurationSecond(maxTTL)
|
||||
if err != nil {
|
||||
return handleError(err)
|
||||
}
|
||||
newMax = &tmpMax
|
||||
tmpDurMax := time.Duration(tmpMax) * time.Second
|
||||
newMax = &tmpDurMax
|
||||
}
|
||||
|
||||
if newDefault != nil || newMax != nil {
|
||||
|
|
|
|||
Loading…
Reference in a new issue