From 984641af213529bd83faefab2311f70c58995e87 Mon Sep 17 00:00:00 2001 From: Jeff Mitchell Date: Mon, 11 Jul 2016 17:46:23 +0000 Subject: [PATCH] Factor out parsing duration second type and use it for parsing tune values too --- helper/duration/duration.go | 28 ++++++++++++++++++++++++++++ helper/duration/duration_test.go | 20 ++++++++++++++++++++ logical/framework/field_data.go | 22 +++++----------------- vault/logical_system.go | 19 +++++++++++-------- 4 files changed, 64 insertions(+), 25 deletions(-) create mode 100644 helper/duration/duration.go create mode 100644 helper/duration/duration_test.go diff --git a/helper/duration/duration.go b/helper/duration/duration.go new file mode 100644 index 0000000000..e3c5b683c8 --- /dev/null +++ b/helper/duration/duration.go @@ -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 +} diff --git a/helper/duration/duration_test.go b/helper/duration/duration_test.go new file mode 100644 index 0000000000..9d3124ff45 --- /dev/null +++ b/helper/duration/duration_test.go @@ -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") + } +} diff --git a/logical/framework/field_data.go b/logical/framework/field_data.go index 4edeac1e90..00e0f3122a 100644 --- a/logical/framework/field_data.go +++ b/logical/framework/field_data.go @@ -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: diff --git a/vault/logical_system.go b/vault/logical_system.go index 1263d2c2da..c3caaad8df 100644 --- a/vault/logical_system.go +++ b/vault/logical_system.go @@ -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 {