Rejig Lease terminology internally; also, put a few JSON names back to their original values

This commit is contained in:
Jeff Mitchell
2015-08-20 22:27:01 -07:00
parent 97112665e8
commit e7f2a54720
10 changed files with 45 additions and 58 deletions

View File

@@ -15,7 +15,7 @@ func TestCopy_auth(t *testing.T) {
expected := logical.Auth{
LeaseOptions: logical.LeaseOptions{
TTL: 1 * time.Hour,
LeaseIssue: time.Now().UTC(),
IssueTime: time.Now().UTC(),
},
ClientToken: "foo",
@@ -122,7 +122,7 @@ func TestHash(t *testing.T) {
&logical.Auth{
LeaseOptions: logical.LeaseOptions{
TTL: 1 * time.Hour,
LeaseIssue: now,
IssueTime: now,
},
ClientToken: "foo",
@@ -130,7 +130,7 @@ func TestHash(t *testing.T) {
&logical.Auth{
LeaseOptions: logical.LeaseOptions{
TTL: 1 * time.Hour,
LeaseIssue: now,
IssueTime: now,
},
ClientToken: "sha1:0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33",

View File

@@ -258,8 +258,8 @@ func TestBackendHandleRequest_renewExtend(t *testing.T) {
}
req := logical.RenewRequest("/foo", secret.Response(nil, nil).Secret, nil)
req.Secret.LeaseIssue = time.Now().UTC()
req.Secret.LeaseIncrement = 1 * time.Hour
req.Secret.IssueTime = time.Now().UTC()
req.Secret.Increment = 1 * time.Hour
resp, err := b.HandleRequest(req)
if err != nil {
t.Fatalf("err: %s", err)

View File

@@ -20,26 +20,26 @@ import (
// lease duration.
func LeaseExtend(max, maxSession time.Duration, maxFromLease bool) OperationFunc {
return func(req *logical.Request, data *FieldData) (*logical.Response, error) {
lease := detectLease(req)
if lease == nil {
leaseOpts := detectLease(req)
if leaseOpts == nil {
return nil, fmt.Errorf("no lease options for request")
}
// Check if we should limit max
if maxFromLease {
max = lease.TTL
max = leaseOpts.TTL
}
// Sanity check the desired increment
switch {
// Protect against negative leases
case lease.LeaseIncrement < 0:
case leaseOpts.Increment < 0:
return logical.ErrorResponse(
"increment must be greater than 0"), logical.ErrInvalidRequest
// If no lease increment, or too large of an increment, use the max
case max > 0 && lease.LeaseIncrement == 0, max > 0 && lease.LeaseIncrement > max:
lease.LeaseIncrement = max
case max > 0 && leaseOpts.Increment == 0, max > 0 && leaseOpts.Increment > max:
leaseOpts.Increment = max
}
// Get the current time
@@ -48,7 +48,7 @@ func LeaseExtend(max, maxSession time.Duration, maxFromLease bool) OperationFunc
// Check if we're passed the issue limit
var maxSessionTime time.Time
if maxSession > 0 {
maxSessionTime = lease.LeaseIssue.Add(maxSession)
maxSessionTime = leaseOpts.IssueTime.Add(maxSession)
if maxSessionTime.Before(now) {
return logical.ErrorResponse(fmt.Sprintf(
"lease can only be renewed up to %s past original issue",
@@ -56,9 +56,9 @@ func LeaseExtend(max, maxSession time.Duration, maxFromLease bool) OperationFunc
}
}
// The new lease is the minimum of the requested LeaseIncrement
// The new lease is the minimum of the requested Increment
// or the maxSessionTime
requestedLease := now.Add(lease.LeaseIncrement)
requestedLease := now.Add(leaseOpts.Increment)
if !maxSessionTime.IsZero() && requestedLease.After(maxSessionTime) {
requestedLease = maxSessionTime
}
@@ -67,11 +67,7 @@ func LeaseExtend(max, maxSession time.Duration, maxFromLease bool) OperationFunc
newLeaseDuration := requestedLease.Sub(now)
// Set the lease
lease.TTL = newLeaseDuration
var zeroDur time.Duration
if lease.Lease != zeroDur {
lease.Lease = newLeaseDuration
}
leaseOpts.TTL = newLeaseDuration
return &logical.Response{Auth: req.Auth, Secret: req.Secret}, nil
}

View File

@@ -76,8 +76,8 @@ func TestLeaseExtend(t *testing.T) {
Auth: &logical.Auth{
LeaseOptions: logical.LeaseOptions{
TTL: 1 * time.Hour,
LeaseIssue: now,
LeaseIncrement: tc.Request,
IssueTime: now,
Increment: tc.Request,
},
},
}

View File

@@ -7,23 +7,22 @@ import "time"
type LeaseOptions struct {
// Lease is the duration that this secret is valid for. Vault
// will automatically revoke it after the duration + grace period.
Lease time.Duration `json:"lease,omitempty"`
TTL time.Duration `json:"ttl,omitempty"`
GracePeriod time.Duration `json:"grace_period"`
TTL time.Duration `json:"lease"`
GracePeriod time.Duration `json:"lease_grace_period"`
// Renewable, if true, means that this secret can be renewed.
Renewable bool `json:"renewable"`
// LeaseIncrement will be the lease increment that the user requested.
// Increment will be the lease increment that the user requested.
// This is only available on a Renew operation and has no effect
// when returning a response.
LeaseIncrement time.Duration `json:"-"`
Increment time.Duration `json:"-"`
// LeaseIssue is the time of issue for the original lease. This is
// IssueTime is the time of issue for the original lease. This is
// only available on a Renew operation and has no effect when returning
// a response. It can be used to enforce maximum lease periods by
// a logical backend. This time will always be in UTC.
LeaseIssue time.Time `json:"-"`
IssueTime time.Time `json:"-"`
}
// LeaseEnabled checks if leasing is enabled

View File

@@ -337,7 +337,7 @@ func (m *ExpirationManager) RenewToken(source string, token string,
// Attach the ClientToken
resp.Auth.ClientToken = token
resp.Auth.LeaseIncrement = 0
resp.Auth.Increment = 0
// Update the lease entry
le.Auth = resp.Auth
@@ -492,8 +492,8 @@ func (m *ExpirationManager) revokeEntry(le *leaseEntry) error {
// renewEntry is used to attempt renew of an internal entry
func (m *ExpirationManager) renewEntry(le *leaseEntry, increment time.Duration) (*logical.Response, error) {
secret := *le.Secret
secret.LeaseIssue = le.IssueTime
secret.LeaseIncrement = increment
secret.IssueTime = le.IssueTime
secret.Increment = increment
secret.LeaseID = ""
req := logical.RenewRequest(le.Path, &secret, le.Data)
@@ -507,8 +507,8 @@ func (m *ExpirationManager) renewEntry(le *leaseEntry, increment time.Duration)
// renewAuthEntry is used to attempt renew of an auth entry
func (m *ExpirationManager) renewAuthEntry(le *leaseEntry, increment time.Duration) (*logical.Response, error) {
auth := *le.Auth
auth.LeaseIssue = le.IssueTime
auth.LeaseIncrement = increment
auth.IssueTime = le.IssueTime
auth.Increment = increment
auth.ClientToken = ""
req := logical.RenewAuthRequest(le.Path, &auth, nil)

View File

@@ -741,10 +741,10 @@ func TestExpiration_renewEntry(t *testing.T) {
if !reflect.DeepEqual(req.Data, le.Data) {
t.Fatalf("Bad: %v", req)
}
if req.Secret.LeaseIncrement != time.Second {
if req.Secret.Increment != time.Second {
t.Fatalf("Bad: %v", req)
}
if req.Secret.LeaseIssue.IsZero() {
if req.Secret.IssueTime.IsZero() {
t.Fatalf("Bad: %v", req)
}
}
@@ -801,10 +801,10 @@ func TestExpiration_renewAuthEntry(t *testing.T) {
if req.Path != "login" {
t.Fatalf("Bad: %v", req)
}
if req.Auth.LeaseIncrement != time.Second {
if req.Auth.Increment != time.Second {
t.Fatalf("Bad: %v", req)
}
if req.Auth.LeaseIssue.IsZero() {
if req.Auth.IssueTime.IsZero() {
t.Fatalf("Bad: %v", req)
}
if req.Auth.InternalData["MySecret"] != "secret" {

View File

@@ -93,21 +93,15 @@ func (b *PassthroughBackend) handleRead(
resp := b.Secret("generic").Response(rawData, nil)
resp.Secret.Renewable = false
// Check if there is a lease key
leaseVal, ok := rawData["lease"].(string)
if ok {
leaseDuration, err := time.ParseDuration(leaseVal)
if err == nil {
resp.Secret.Renewable = true
resp.Secret.Lease = leaseDuration
resp.Secret.TTL = leaseDuration
}
// Check if there is a ttl key
var ttl string
ttl, _ = rawData["lease"].(string)
if len(ttl) == 0 {
ttl, _ = rawData["ttl"].(string)
}
// Check if there is a ttl key
ttlVal, ok := rawData["ttl"].(string)
if ok {
ttlDuration, err := time.ParseDuration(ttlVal)
if len(ttl) != 0 {
ttlDuration, err := time.ParseDuration(ttl)
if err == nil {
resp.Secret.Renewable = true
resp.Secret.TTL = ttlDuration

View File

@@ -61,7 +61,6 @@ func TestPassthroughBackend_Read_Lease(t *testing.T) {
Secret: &logical.Secret{
LeaseOptions: logical.LeaseOptions{
Renewable: true,
Lease: time.Hour,
TTL: time.Hour,
},
},

View File

@@ -32,8 +32,7 @@ Also note that setting `ttl` does not actually expire the data; it is
informational only.
N.B.: Prior to version 0.3, the `ttl` parameter was called `lease`. Both will
work for 0.3, but in 0.4 `lease` will be removed. When providing a `lease` value
in 0.3, both `lease` and `ttl` will be returned with the same data.
work for 0.3, but in 0.4 `lease` will be removed.
As an example, we can write a new key "foo" to the generic backend
mounted at "secret/" by default: