2015-04-09 19:14:04 +00:00
|
|
|
package logical
|
|
|
|
|
2017-10-23 20:42:56 +00:00
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
2015-04-09 19:14:04 +00:00
|
|
|
|
|
|
|
// LeaseOptions is an embeddable struct to capture common lease
|
|
|
|
// settings between a Secret and Auth
|
|
|
|
type LeaseOptions struct {
|
2018-04-03 16:20:20 +00:00
|
|
|
// TTL is the duration that this secret is valid for. Vault
|
2016-02-01 00:33:16 +00:00
|
|
|
// will automatically revoke it after the duration.
|
|
|
|
TTL time.Duration `json:"lease"`
|
2015-04-09 19:14:04 +00:00
|
|
|
|
2018-04-03 16:20:20 +00:00
|
|
|
// MaxTTL is the maximum duration that this secret is valid for.
|
|
|
|
MaxTTL time.Duration `json:"max_ttl"`
|
|
|
|
|
2015-04-09 19:14:04 +00:00
|
|
|
// Renewable, if true, means that this secret can be renewed.
|
|
|
|
Renewable bool `json:"renewable"`
|
|
|
|
|
2015-08-21 05:27:01 +00:00
|
|
|
// Increment will be the lease increment that the user requested.
|
2015-04-09 19:14:04 +00:00
|
|
|
// This is only available on a Renew operation and has no effect
|
|
|
|
// when returning a response.
|
2015-08-21 05:27:01 +00:00
|
|
|
Increment time.Duration `json:"-"`
|
2015-04-09 19:14:04 +00:00
|
|
|
|
2015-08-21 05:27:01 +00:00
|
|
|
// IssueTime is the time of issue for the original lease. This is
|
logical/aws: Harden WAL entry creation (#5202)
* logical/aws: Harden WAL entry creation
If AWS IAM user creation failed in any way, the WAL corresponding to the
IAM user would get left around and Vault would try to roll it back.
However, because the user never existed, the rollback failed. Thus, the
WAL would essentially get "stuck" and Vault would continually attempt to
roll it back, failing every time. A similar situation could arise if the
IAM user that Vault created got deleted out of band, or if Vault deleted
it but was unable to write the lease revocation back to storage (e.g., a
storage failure).
This attempts to harden it in two ways. One is by deleting the WAL log
entry if the IAM user creation fails. However, the WAL deletion could
still fail, and this wouldn't help where the user is deleted out of
band, so second, consider the user rolled back if the user just doesn't
exist, under certain circumstances.
Fixes #5190
* Fix segfault in expiration unit tests
TestExpiration_Tidy was passing in a leaseEntry that had a nil Secret,
which then caused a segfault as the changes to revokeEntry didn't check
whether Secret was nil; this is probably unlikely to occur in real life,
but good to be extra cautious.
* Fix potential segfault
Missed the else...
* Respond to PR feedback
2018-09-27 14:54:59 +00:00
|
|
|
// only available on Renew and Revoke operations and has no effect when returning
|
2015-04-09 19:14:04 +00:00
|
|
|
// a response. It can be used to enforce maximum lease periods by
|
2016-07-07 21:44:14 +00:00
|
|
|
// a logical backend.
|
2015-08-21 05:27:01 +00:00
|
|
|
IssueTime time.Time `json:"-"`
|
2015-04-09 19:14:04 +00:00
|
|
|
}
|
2015-04-09 19:29:13 +00:00
|
|
|
|
|
|
|
// LeaseEnabled checks if leasing is enabled
|
|
|
|
func (l *LeaseOptions) LeaseEnabled() bool {
|
2015-08-21 00:47:17 +00:00
|
|
|
return l.TTL > 0
|
2015-04-09 19:29:13 +00:00
|
|
|
}
|
|
|
|
|
2016-02-01 00:33:16 +00:00
|
|
|
// LeaseTotal is the lease duration with a guard against a negative TTL
|
2015-04-09 19:29:13 +00:00
|
|
|
func (l *LeaseOptions) LeaseTotal() time.Duration {
|
2015-08-21 00:47:17 +00:00
|
|
|
if l.TTL <= 0 {
|
2015-04-09 19:29:13 +00:00
|
|
|
return 0
|
|
|
|
}
|
2015-04-11 04:29:03 +00:00
|
|
|
|
2016-02-01 00:33:16 +00:00
|
|
|
return l.TTL
|
2015-04-09 19:29:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ExpirationTime computes the time until expiration including the grace period
|
|
|
|
func (l *LeaseOptions) ExpirationTime() time.Time {
|
|
|
|
var expireTime time.Time
|
2015-06-17 20:59:09 +00:00
|
|
|
if l.LeaseEnabled() {
|
2016-07-07 21:44:14 +00:00
|
|
|
expireTime = time.Now().Add(l.LeaseTotal())
|
2015-04-09 19:29:13 +00:00
|
|
|
}
|
|
|
|
return expireTime
|
|
|
|
}
|