From 2b7be14d7ae9bfcb15f7c05aaccb6ec8d8395292 Mon Sep 17 00:00:00 2001 From: Thomas Beaugrand Date: Fri, 26 Jul 2024 14:54:05 +0200 Subject: [PATCH 1/2] feat: Add method to get ttl time remaining --- internal/libvault/vault.go | 9 +++++++++ vaultv1/v1.go | 9 +++++++++ vaultv2/v2.go | 9 +++++++++ 3 files changed, 27 insertions(+) diff --git a/internal/libvault/vault.go b/internal/libvault/vault.go index 1bfb5ab..b529ba0 100644 --- a/internal/libvault/vault.go +++ b/internal/libvault/vault.go @@ -68,3 +68,12 @@ func CreateClientWithAppRole(roleID, secretID string) (*vault.Client, error) { client.SetToken(resp.Auth.ClientToken) return client, nil } + +func GetTokenTtlLeft(client *vault.Client) (int, error) { + secret, err := client.Auth().Token().LookupSelf() + if err != nil { + return 0, errors.Wrap(err, "failed to lookup token") + } + + return int(secret.Data["ttl"].(float64)), nil +} diff --git a/vaultv1/v1.go b/vaultv1/v1.go index 7b1b272..42dc64a 100644 --- a/vaultv1/v1.go +++ b/vaultv1/v1.go @@ -34,6 +34,15 @@ func CreateClientWithAppRole(roleID, secretID string) (*Client, error) { }, nil } +func GetTokenTtlLeft(client *Client) (int, error) { + timeLeft, err := libvault.GetTokenTtlLeft(client.Client) + if err != nil { + return 0, errors.Wrapf(err, "") + } + + return timeLeft, nil +} + func (vc *Client) ListSecretPath(path string) ([]string, error) { s, err := vc.Client.Logical().List(path) if err != nil { diff --git a/vaultv2/v2.go b/vaultv2/v2.go index 12fd3c7..9d7f404 100644 --- a/vaultv2/v2.go +++ b/vaultv2/v2.go @@ -35,6 +35,15 @@ func CreateClientWithAppRole(roleID, secretID string) (*Client, error) { }, nil } +func GetTokenTtlLeft(client *Client) (int, error) { + timeLeft, err := libvault.GetTokenTtlLeft(client.Client) + if err != nil { + return 0, errors.Wrapf(err, "") + } + + return timeLeft, nil +} + func (vc *Client) ReadSecret(path string, field string) (string, error) { secret, err := vc.GetSecret(path) if err != nil { From e7b834bfea8b74158a7d336e603c8eaf50565d1e Mon Sep 17 00:00:00 2001 From: Thomas Beaugrand Date: Fri, 26 Jul 2024 14:56:53 +0200 Subject: [PATCH 2/2] lint --- internal/libvault/vault.go | 2 +- vaultv1/v1.go | 4 ++-- vaultv2/v2.go | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/internal/libvault/vault.go b/internal/libvault/vault.go index b529ba0..070c8cc 100644 --- a/internal/libvault/vault.go +++ b/internal/libvault/vault.go @@ -69,7 +69,7 @@ func CreateClientWithAppRole(roleID, secretID string) (*vault.Client, error) { return client, nil } -func GetTokenTtlLeft(client *vault.Client) (int, error) { +func GetTokenTTLLeft(client *vault.Client) (int, error) { secret, err := client.Auth().Token().LookupSelf() if err != nil { return 0, errors.Wrap(err, "failed to lookup token") diff --git a/vaultv1/v1.go b/vaultv1/v1.go index 42dc64a..c33aeed 100644 --- a/vaultv1/v1.go +++ b/vaultv1/v1.go @@ -34,8 +34,8 @@ func CreateClientWithAppRole(roleID, secretID string) (*Client, error) { }, nil } -func GetTokenTtlLeft(client *Client) (int, error) { - timeLeft, err := libvault.GetTokenTtlLeft(client.Client) +func GetTokenTTLLeft(client *Client) (int, error) { + timeLeft, err := libvault.GetTokenTTLLeft(client.Client) if err != nil { return 0, errors.Wrapf(err, "") } diff --git a/vaultv2/v2.go b/vaultv2/v2.go index 9d7f404..7b53efd 100644 --- a/vaultv2/v2.go +++ b/vaultv2/v2.go @@ -35,8 +35,8 @@ func CreateClientWithAppRole(roleID, secretID string) (*Client, error) { }, nil } -func GetTokenTtlLeft(client *Client) (int, error) { - timeLeft, err := libvault.GetTokenTtlLeft(client.Client) +func GetTokenTTLLeft(client *Client) (int, error) { + timeLeft, err := libvault.GetTokenTTLLeft(client.Client) if err != nil { return 0, errors.Wrapf(err, "") }