Skip to content

Commit

Permalink
feat: Add method to get ttl time remaining (#31)
Browse files Browse the repository at this point in the history
* feat: Add method to get ttl time remaining

* lint
  • Loading branch information
tbeaugrand authored Jul 26, 2024
1 parent d524ee7 commit b609555
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions internal/libvault/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
9 changes: 9 additions & 0 deletions vaultv1/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions vaultv2/v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit b609555

Please sign in to comment.