Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server/vault: Lock Vault expiration tracking #4956

Merged
merged 1 commit into from
Dec 5, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}