Skip to content

Commit

Permalink
command/token-lookup: better output for the key expiration left duration
Browse files Browse the repository at this point in the history
Make the expiration time left more readable.
Example: the previous output "767h55m35s" is now displayed as
         "4 weeks 3 days 23 hours 55 minutes 35 seconds".

Signed-off-by: Davide Madrisan <[email protected]>
  • Loading branch information
madrisan committed Sep 11, 2019
1 parent c30e8e2 commit 16b5aa0
Show file tree
Hide file tree
Showing 11 changed files with 420 additions and 5 deletions.
9 changes: 9 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
name = "github.com/go-test/deep"
version = "v1.0.3"

[[constraint]]
name = "github.com/hako/durafmt"
version = "1.0.0"

[prune]
go-tests = true
unused-packages = true
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,10 @@ As usual, add `-output=nagios` to get an output compliant with the Nagios specif
###### Example of output

# default output message
The token will expire on Mon, 07 Oct 2019 14:25:06 UTC (767h55m35s left)
This (renewable) token will expire on Mon, 07 Oct 2019 14:25:06 UTC (4 weeks 3 days 23 hours 55 minutes 35 seconds left)

# with the '-output=nagios' switch
vault OK - The token will expire on Mon, 07 Oct 2019 14:25:06 UTC (767h55m35s left)
vault OK - This (renewable) token will expire on Mon, 07 Oct 2019 14:25:06 UTC (4 weeks 3 days 23 hours 55 minutes 35 seconds left)

Note that you should replace `39d2c7...` with the generated *Root token* from
your output.
Expand Down
18 changes: 15 additions & 3 deletions command/token-lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"flag"
"fmt"
"time"

"github.com/hako/durafmt"
)

// Thresholds in hours for warning and critical status.
Expand Down Expand Up @@ -97,6 +99,7 @@ func (c *TokenLookupCommand) Run(args []string) int {
cmdFlags := flag.NewFlagSet("token-lookup", flag.ContinueOnError)
cmdFlags.Usage = func() { c.UI.Output(c.Help()) }
cmdFlags.StringVar(&c.Address, "address", addressDefault, addressDescr)
cmdFlags.StringVar(&c.Token, "token", tokenDefault, tokenDescr)
cmdFlags.StringVar(&c.OutputFormat, "output", "default", outputFormatDescr)
cmdFlags.StringVar(&c.WarningThreshold, "warning",
DefaultWarningTokenExpiration,
Expand Down Expand Up @@ -157,13 +160,22 @@ func (c *TokenLookupCommand) Run(args []string) int {
t, _ := time.Parse(time.RFC3339Nano, expireTimeStr)
delta := time.Until(t)

pluginMessage := ""
retCode := StateOk

if delta > 0 {
pluginMessage = fmt.Sprintf("The token will expire on %s (%s left)",
t.Format(time.RFC1123),
var pluginMessage, renewable string

if tokenIsRenewable, _ := s.TokenIsRenewable(); tokenIsRenewable {
renewable = "(renewable) "
}

left, _ := durafmt.ParseString(
delta.Truncate(time.Second).String())
pluginMessage = fmt.Sprintf("This %stoken will expire on %s (%s left)",
renewable,
t.Format(time.RFC1123),
left)

if delta < criticalThreshold {
out.Critical(pluginMessage)
retCode = StateCritical
Expand Down
24 changes: 24 additions & 0 deletions vendor/github.com/hako/durafmt/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions vendor/github.com/hako/durafmt/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions vendor/github.com/hako/durafmt/CODE_OF_CONDUCT.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions vendor/github.com/hako/durafmt/CONTRIBUTING.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions vendor/github.com/hako/durafmt/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

122 changes: 122 additions & 0 deletions vendor/github.com/hako/durafmt/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 16b5aa0

Please sign in to comment.