Skip to content

Commit

Permalink
Merge pull request #4956 from hashicorp/b-vault-client-tweaks-followup
Browse files Browse the repository at this point in the history
server/vault: Lock Vault expiration tracking
  • Loading branch information
notnoop authored Dec 5, 2018
2 parents 615c525 + 2f8b1eb commit 661dc4b
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions nomad/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ type VaultStats struct {
// TokenTTL is the time-to-live duration for the current token
TokenTTL time.Duration

// TokenExpiry Time is the recoreded expiry time of the current token
// TokenExpiry is the recorded expiry time of the current token
TokenExpiry time.Time
}

Expand Down Expand Up @@ -216,7 +216,8 @@ type vaultClient struct {
childTTL string

// currentExpiration is the time the current token lease expires
currentExpiration time.Time
currentExpiration time.Time
currentExpirationLock sync.Mutex

tomb *tomb.Tomb
logger log.Logger
Expand Down Expand Up @@ -488,7 +489,9 @@ func (v *vaultClient) renewalLoop() {
case <-authRenewTimer.C:
// Renew the token and determine the new expiration
recoverable, err := v.renew()
v.currentExpirationLock.Lock()
currentExpiration := v.currentExpiration
v.currentExpirationLock.Unlock()

// Successfully renewed
if err == nil {
Expand Down Expand Up @@ -602,7 +605,7 @@ func (v *vaultClient) renew() (bool, error) {
return true, fmt.Errorf("renewal successful but no lease duration returned")
}

v.currentExpiration = time.Now().Add(time.Duration(auth.LeaseDuration) * time.Second)
v.extendExpiration(auth.LeaseDuration)

v.logger.Debug("successfully renewed server token")
return true, nil
Expand Down Expand Up @@ -650,7 +653,7 @@ func (v *vaultClient) parseSelfToken() error {
}
data.Root = root
v.tokenData = &data
v.currentExpiration = time.Now().Add(time.Duration(data.TTL) * time.Second)
v.extendExpiration(data.TTL)

// The criteria that must be met for the token to be valid are as follows:
// 1) If token is non-root or is but has a creation ttl
Expand Down Expand Up @@ -1274,7 +1277,10 @@ func (v *vaultClient) stats() *VaultStats {
stats.TrackedForRevoke = len(v.revoking)
v.revLock.Unlock()

v.currentExpirationLock.Lock()
stats.TokenExpiry = v.currentExpiration
v.currentExpirationLock.Unlock()

if !stats.TokenExpiry.IsZero() {
stats.TokenTTL = time.Until(stats.TokenExpiry)
}
Expand All @@ -1296,3 +1302,10 @@ func (v *vaultClient) EmitStats(period time.Duration, stopCh chan struct{}) {
}
}
}

// extendExpiration sets the current auth token expiration record to ttLSeconds seconds from now
func (v *vaultClient) extendExpiration(ttlSeconds int) {
v.currentExpirationLock.Lock()
v.currentExpiration = time.Now().Add(time.Duration(ttlSeconds) * time.Second)
v.currentExpirationLock.Unlock()
}

0 comments on commit 661dc4b

Please sign in to comment.