Output human duration in TTL warnings (#7901)

This commit is contained in:
Seth Vargo
2019-11-22 17:38:46 +00:00
committed by Brian Kassouf
parent d5b188d6e2
commit 4798202103

View File

@@ -3,6 +3,7 @@ package framework
import (
"context"
"fmt"
"strings"
"time"
"github.com/hashicorp/vault/sdk/logical"
@@ -61,7 +62,8 @@ func CalculateTTL(sysView logical.SystemView, increment, backendTTL, period, bac
// Cap the period value to the sys max_ttl value
if period > maxTTL {
warnings = append(warnings,
fmt.Sprintf("period of %q exceeded the effective max_ttl of %q; period value is capped accordingly", period, maxTTL))
fmt.Sprintf("period of %q exceeded the effective max_ttl of %q; period value is capped accordingly",
humanDuration(period), humanDuration(maxTTL)))
period = maxTTL
}
ttl = period
@@ -97,10 +99,27 @@ func CalculateTTL(sysView logical.SystemView, increment, backendTTL, period, bac
// cap the increment to whatever is left
if maxValidTTL-ttl < 0 {
warnings = append(warnings,
fmt.Sprintf("TTL of %q exceeded the effective max_ttl of %q; TTL value is capped accordingly", ttl, maxValidTTL))
fmt.Sprintf("TTL of %q exceeded the effective max_ttl of %q; TTL value is capped accordingly",
humanDuration(ttl), humanDuration(maxValidTTL)))
ttl = maxValidTTL
}
}
return ttl, warnings, nil
}
// humanDuration prints the time duration without zero elements.
func humanDuration(d time.Duration) string {
if d == 0 {
return "0s"
}
s := d.String()
if strings.HasSuffix(s, "m0s") {
s = s[:len(s)-2]
}
if idx := strings.Index(s, "h0m"); idx > 0 {
s = s[:idx+1] + s[idx+3:]
}
return s
}