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

Core handling of TTLs #4230

Merged
merged 35 commits into from
Apr 3, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
9eab394
govet cleanup in token store
Mar 27, 2018
21bd6dc
adding general ttl handling to login requests
Mar 29, 2018
23b96b6
consolidating TTL calculation to system view
Mar 30, 2018
27049aa
deprecate LeaseExtend
Mar 30, 2018
b9180b6
deprecate LeaseExtend
Mar 30, 2018
b0ddd3e
set the increment to the correct value
Mar 30, 2018
411a8ed
Merge remote-tracking branch 'oss/master' into core-auth-ttl
Mar 30, 2018
9147126
move calculateTTL out of SystemView
Mar 31, 2018
19886fa
remove unused value
Mar 31, 2018
21ebd68
add back clearing of lease id
Mar 31, 2018
e567b4e
implement core ttl in some backends
Mar 31, 2018
dec302c
removing increment and issue time from lease options
Mar 31, 2018
0907f9d
adding ttl tests, fixing some compile issue
Mar 31, 2018
30c3a98
adding ttl tests
Mar 31, 2018
fb272c1
fixing some explicit max TTL logic
Mar 31, 2018
dfe18e9
fixing up some tests
Mar 31, 2018
6f73f99
removing unneeded test
Mar 31, 2018
4ad2303
off by one errors...
Apr 1, 2018
f1cf562
adding back some logic for bc
Apr 1, 2018
0596d4d
adding period to return on renewal
Apr 2, 2018
21d1f83
tweaking max ttl capping slightly
Apr 2, 2018
611b029
use the appropriate precision for ttl calculation
Apr 2, 2018
790b9c2
deprecate proto fields instead of delete
Apr 2, 2018
49af1f1
addressing feedback
Apr 2, 2018
fc3dba6
moving TTL handling for backends to core
Apr 2, 2018
6893d38
mongo is a secret backend not auth
Apr 2, 2018
6798312
adding estimated ttl for backends that also manage the expiration time
Apr 2, 2018
65e9e1b
set the estimate values before calling the renew request
Apr 3, 2018
be70406
moving calculate TTL to framework, revert removal of increment and is…
Apr 3, 2018
6b240b8
minor edits
Apr 3, 2018
0b465c4
Merge remote-tracking branch 'oss/master' into core-auth-ttl
Apr 3, 2018
2317f5e
addressing feedback
Apr 3, 2018
0d54d5c
address more feedback
Apr 3, 2018
042657c
Merge remote-tracking branch 'oss/master' into core-auth-ttl
Apr 3, 2018
84a35ac
Merge branch 'master' into core-auth-ttl
vishalnayak Apr 3, 2018
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
25 changes: 17 additions & 8 deletions builtin/logical/database/secret_creds.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package database
import (
"context"
"fmt"
"time"

"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/framework"
Expand Down Expand Up @@ -42,12 +43,6 @@ func (b *databaseBackend) secretCredsRenew() framework.OperationFunc {
return nil, fmt.Errorf("error during renew: could not find role with name %s", req.Secret.InternalData["role"])
}

f := framework.LeaseExtend(role.DefaultTTL, role.MaxTTL, b.System())
resp, err := f(ctx, req, data)
if err != nil {
return nil, err
}

// Get the Database object
db, err := b.GetConnection(ctx, req.Storage, role.DBName)
if err != nil {
Expand All @@ -57,14 +52,28 @@ func (b *databaseBackend) secretCredsRenew() framework.OperationFunc {
db.RLock()
defer db.RUnlock()

// Make sure we increase the VALID UNTIL endpoint for this user.
if expireTime := resp.Secret.ExpirationTime(); !expireTime.IsZero() {
// Make sure we increase the VALID UNTIL endpoint for this user. This value is estimated and does not
// take into account any backend specific values. These value will be calculated by core and will only
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/These value/This value.

// reduce the TTL based on any running max ttl. Since vault still manages the lease, it will still get
// revokes at the lesser time.
if req.Secret.EstimatedTTL > 0 {
ttl := req.Secret.EstimatedTTL
if role.DefaultTTL > 0 && role.DefaultTTL < ttl {
ttl = role.DefaultTTL
}
if role.MaxTTL > 0 && role.MaxTTL < ttl {
ttl = role.MaxTTL
}
expireTime := time.Now().Add(ttl)
err := db.RenewUser(ctx, role.Statements, username, expireTime)
if err != nil {
b.CloseIfShutdown(db, err)
return nil, err
}
}
resp := &logical.Response{Secret: req.Secret}
resp.Secret.TTL = role.DefaultTTL
resp.Secret.MaxTTL = role.MaxTTL
return resp, nil
}
}
Expand Down
20 changes: 13 additions & 7 deletions builtin/logical/postgresql/secret_creds.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql"
"fmt"
"strings"
"time"

"github.com/hashicorp/vault/helper/strutil"
"github.com/hashicorp/vault/logical"
Expand Down Expand Up @@ -59,14 +60,16 @@ func (b *backend) secretCredsRenew(ctx context.Context, req *logical.Request, d
lease = &configLease{}
}

f := framework.LeaseExtend(lease.Lease, lease.LeaseMax, b.System())
resp, err := f(ctx, req, d)
if err != nil {
return nil, err
}

// Make sure we increase the VALID UNTIL endpoint for this user.
if expireTime := resp.Secret.ExpirationTime(); !expireTime.IsZero() {
if req.Secret.EstimatedTTL > 0 {
ttl := req.Secret.EstimatedTTL
if lease.Lease > 0 && lease.Lease < ttl {
ttl = lease.Lease
}
if lease.LeaseMax > 0 && lease.LeaseMax < ttl {
ttl = lease.LeaseMax
}
expireTime := time.Now().Add(ttl)
expiration := expireTime.Format("2006-01-02 15:04:05-0700")

query := fmt.Sprintf(
Expand All @@ -83,6 +86,9 @@ func (b *backend) secretCredsRenew(ctx context.Context, req *logical.Request, d
}
}

resp := &logical.Response{Secret: req.Secret}
resp.Secret.TTL = lease.Lease
resp.Secret.MaxTTL = lease.LeaseMax
return resp, nil
}

Expand Down
5 changes: 5 additions & 0 deletions logical/lease.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ type LeaseOptions struct {
// MaxTTL is the maximum duration that this secret is valid for.
MaxTTL time.Duration `json:"max_ttl"`

// EstimatedTTL is passed to backends to provide an anticipated value
// to use for any renewal functions that are required since TTL is not
// known at renewal
EstimatedTTL time.Duration `json:"-"`

// Renewable, if true, means that this secret can be renewed.
Renewable bool `json:"renewable"`
}
Expand Down
Loading