From b095753f79ee4ada5ef045392419bcfee8c99e4e Mon Sep 17 00:00:00 2001 From: Thomas Beaugrand Date: Fri, 26 Jul 2024 16:35:18 +0200 Subject: [PATCH] fix: rework TTL remaining time method --- internal/libvault/vault.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/internal/libvault/vault.go b/internal/libvault/vault.go index 070c8cc..37929de 100644 --- a/internal/libvault/vault.go +++ b/internal/libvault/vault.go @@ -1,8 +1,11 @@ package libvault import ( + "encoding/json" + "fmt" "os" "path/filepath" + "strconv" "github.com/mitchellh/go-homedir" @@ -75,5 +78,16 @@ func GetTokenTTLLeft(client *vault.Client) (int, error) { return 0, errors.Wrap(err, "failed to lookup token") } - return int(secret.Data["ttl"].(float64)), nil + ttlRaw, ok := secret.Data["ttl"] + if !ok { + return 0, fmt.Errorf("token TTL not found in the response") + } + + ttlStr := ttlRaw.(json.Number).String() + ttl, err := strconv.ParseInt(ttlStr, 10, 64) + if err != nil { + return 0, fmt.Errorf("failed to parse token ttl: %v", err) + } + + return int(ttl), nil }