Skip to content

Commit

Permalink
avoid use of sync.Once
Browse files Browse the repository at this point in the history
  • Loading branch information
pschultz committed Jun 29, 2017
1 parent c151ca2 commit f6e2463
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions cert/vault_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ type VaultSource struct {
// This value is advisory and the Vault server may ignore or silently
// change it.
renewTTL int

once sync.Once
}
}

Expand All @@ -71,13 +69,10 @@ func (s *VaultSource) client() (*api.Client, error) {

func (s *VaultSource) setAuth(c *api.Client) error {
s.mu.Lock()
defer func() {
c.SetToken(s.auth.token)
s.auth.once.Do(func() { s.checkRenewal(c) })
s.mu.Unlock()
}()
defer s.mu.Unlock()

if s.auth.token != "" {
c.SetToken(s.auth.token)
return nil
}

Expand All @@ -87,17 +82,20 @@ func (s *VaultSource) setAuth(c *api.Client) error {

// did we get a wrapped token?
resp, err := c.Logical().Unwrap(s.vaultToken)
if err != nil {
// not a wrapped token?
if strings.HasPrefix(err.Error(), "no value found at") {
s.auth.token = s.vaultToken
return nil
}
switch {
case err == nil:
log.Printf("[INFO] vault: Unwrapped token %s", s.vaultToken)
s.auth.token = resp.Auth.ClientToken
case strings.HasPrefix(err.Error(), "no value found at"):
// not a wrapped token
s.auth.token = s.vaultToken
default:
return err
}
log.Printf("[INFO] vault: Unwrapped token %s", s.vaultToken)

s.auth.token = resp.Auth.ClientToken
c.SetToken(s.auth.token)
s.checkRenewal(c)

return nil
}

Expand Down

0 comments on commit f6e2463

Please sign in to comment.