Skip to content

Commit

Permalink
review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
zoxpx committed Apr 15, 2024
1 parent 3aaff8a commit 148196b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
1 change: 1 addition & 0 deletions vault/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ var (

ErrAuthMethodUnknown = errors.New("unknown auth method")
ErrKubernetesRole = errors.New(AuthKubernetesRole + " not set")
ErrInCooldown = errors.New("vault client is in cooldown")
)

// IsValidAddr checks address has the correct format.
Expand Down
27 changes: 12 additions & 15 deletions vault/vault.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package vault

import (
"errors"
"fmt"
"path"
"strings"
Expand Down Expand Up @@ -63,7 +62,6 @@ type vaultSecrets struct {
var (
newVaultClient = api.NewClient
isKvV2 = isKvBackendV2
errInCooldown = errors.New("vault client is in cooldown")
confCooldownPeriod time.Duration
)

Expand Down Expand Up @@ -138,19 +136,18 @@ func New(
}
}

confCooldownPeriod = defaultCooldownPeriod
if cd := utils.GetVaultParam(secretConfig, VaultCooldownPeriod); cd != "" {
confCooldownPeriod, err = time.ParseDuration(cd)
if err == nil && confCooldownPeriod > time.Minute {
logrus.Infof("cooldown period is set to %s via %s=%s", confCooldownPeriod, VaultCooldownPeriod, cd)
} else {
// let's turn OFF the cooldown, if one passed invalid value (or "disabled")
logrus.WithError(err).Warnf("cooldown period is invalid (%s=%s) -- cooldowns turned OFF", VaultCooldownPeriod, cd)
if cd == "0" {
logrus.Warnf("cooldown period is disabled via %s=%s", VaultCooldownPeriod, cd)
confCooldownPeriod = 0
} else if confCooldownPeriod, err = time.ParseDuration(cd); err == nil && confCooldownPeriod > time.Minute {
logrus.Infof("cooldown period is set to %s", confCooldownPeriod)
} else {
return nil, fmt.Errorf("invalid cooldown period: %s=%s", VaultCooldownPeriod, cd)
}
} else {
confCooldownPeriod = defaultCooldownPeriod
logrus.Infof("cooldown period is set to %s", confCooldownPeriod)
}
logrus.Infof("cooldown period is set to %s", confCooldownPeriod)

return &vaultSecrets{
endpoint: config.Address,
Expand Down Expand Up @@ -284,7 +281,7 @@ func (v *vaultSecrets) ListSecrets() ([]string, error) {

func (v *vaultSecrets) read(path keyPath) (*api.Secret, error) {
if v.isInCooldown() {
return nil, errInCooldown
return nil, utils.ErrInCooldown
}
if v.autoAuth {
v.lockClientToken.Lock()
Expand All @@ -307,7 +304,7 @@ func (v *vaultSecrets) read(path keyPath) (*api.Secret, error) {

func (v *vaultSecrets) write(path keyPath, data map[string]interface{}) (*api.Secret, error) {
if v.isInCooldown() {
return nil, errInCooldown
return nil, utils.ErrInCooldown
}
if v.autoAuth {
v.lockClientToken.Lock()
Expand All @@ -330,7 +327,7 @@ func (v *vaultSecrets) write(path keyPath, data map[string]interface{}) (*api.Se

func (v *vaultSecrets) delete(path keyPath) (*api.Secret, error) {
if v.isInCooldown() {
return nil, errInCooldown
return nil, utils.ErrInCooldown
}
if v.autoAuth {
v.lockClientToken.Lock()
Expand Down Expand Up @@ -394,7 +391,7 @@ func (v *vaultSecrets) renewTokenWithCooldown(namespace string) error {
if confCooldownPeriod <= 0 { // cooldown is disabled, return immediately
return v.renewToken(namespace)
} else if v.isInCooldown() {
return errInCooldown
return utils.ErrInCooldown
}

err := v.renewToken(namespace)
Expand Down
4 changes: 2 additions & 2 deletions vault/vault_cooldowns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func TestVaultK8sHappyPath(t *testing.T) {
"VAULT_AUTH_KUBERNETES_ROLE": "portworx",
"VAULT_AUTH_METHOD": "kubernetes",
"VAULT_BACKEND_PATH": "static_secrets/",
"VAULT_COOLDOWN_PERIOD": "disabled",
"VAULT_COOLDOWN_PERIOD": "0",
})
require.NoError(t, err)
require.NotNil(t, v)
Expand Down Expand Up @@ -217,7 +217,7 @@ func TestVaultK8sDisabledCooldown(t *testing.T) {
"VAULT_AUTH_KUBERNETES_ROLE": "portworx",
"VAULT_AUTH_METHOD": "kubernetes",
"VAULT_BACKEND_PATH": "static_secrets/",
"VAULT_COOLDOWN_PERIOD": "disabled",
"VAULT_COOLDOWN_PERIOD": "0",
})
require.NoError(t, err)
require.NotNil(t, v)
Expand Down

0 comments on commit 148196b

Please sign in to comment.