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

Core handling of TTLs #4230

merged 35 commits into from
Apr 3, 2018

Conversation

chrishoffman
Copy link
Contributor

@chrishoffman chrishoffman commented Mar 31, 2018

This PR moves the responsibility of TTL handling from the backends into core. This should ensure a standard way for calculating and validating TTL values. The backends only have to pass back the specific request configured TTL and period and core will determine the TTL to put on the lease.

LeaseExtend has also been converted to work in a backwards compatible way. Additionally, an auth backend (AppRole) and secret backend (AWS) have been implemented using the new method. Once the core logic is reviewed, the rest of the backends and plugins will get updated although the LeaseExtend semantics will still work.

@jefferai jefferai added this to the 0.10 milestone Apr 1, 2018
if te.TTL == 0 && !strutil.StrListContains(te.Policies, "root") {
te.TTL = sysView.DefaultLeaseTTL()
// Only calculate a TTL if you are A) periodic, B) have a TTL, C) do not have a TTL and are not a root token
if periodToUse > 0 || te.TTL > 0 || (te.TTL == 0 && !strutil.StrListContains(te.Policies, "root")) {
Copy link
Member

Choose a reason for hiding this comment

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

Given that only root tokens should not have a TTL, would it be safer here to instead only not run calculateTTL if it's root?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The old logic would still enforce periodic on root tokens, I just kept it that way.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, ok 👍

@hashicorp hashicorp deleted a comment from calvn Apr 2, 2018
@hashicorp hashicorp deleted a comment from vishalnayak Apr 2, 2018
@hashicorp hashicorp deleted a comment from calvn Apr 2, 2018
vault/ttl.go Outdated
}

// We cannot go past this time
maxValidTime = startTime.Add(maxTTL)
Copy link
Contributor

Choose a reason for hiding this comment

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

This switch can be cleaned up a bit if you initialize maxValidTime := startTime.Add(maxTTL) and then have the switch like:

switch {
	case period > 0:
		...
	case increment > 0:
		ttl = increment
	case backendTTL > 0:
		ttl = backendTTL
	default:
		ttl = sysView.DefaultLeaseTTL()
	}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The only problem with this is that maxTTL does not limit the period value. I implemented it this way and it ended up about the same.

@@ -1056,7 +1066,7 @@ func (m *ExpirationManager) revokeEntry(le *leaseEntry) error {
}

// renewEntry is used to attempt renew of an internal entry
func (m *ExpirationManager) renewEntry(le *leaseEntry) (*logical.Response, error) {
func (m *ExpirationManager) renewEntry(le *leaseEntry, estimatedTTL time.Duration) (*logical.Response, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This isn't being used... is it just not implemented yet?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 65e9e1b

@@ -1069,7 +1079,7 @@ func (m *ExpirationManager) renewEntry(le *leaseEntry) (*logical.Response, error

// renewAuthEntry is used to attempt renew of an auth entry. Only the token
// store should get the actual token ID intact.
func (m *ExpirationManager) renewAuthEntry(req *logical.Request, le *leaseEntry) (*logical.Response, error) {
func (m *ExpirationManager) renewAuthEntry(req *logical.Request, le *leaseEntry, estimatedTTL time.Duration) (*logical.Response, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here

@@ -111,11 +111,6 @@ func (b *backend) pathLoginRenew(ctx context.Context, req *logical.Request, d *f
return nil, fmt.Errorf("policies have changed, not renewing")
}

resp, err = framework.LeaseExtend(0, 0, b.System())(ctx, req, d)
Copy link
Member

Choose a reason for hiding this comment

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

I think the request.Auth is not carried over to the response in this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is fixed in 2317f5e

}
}

// CalculateTTL takes all the user-specifie, backend, and system inputs and calculates
Copy link
Member

Choose a reason for hiding this comment

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

s/specifie/specific

// 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
Member

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.

}
if ttl > 0 {
expireTime := time.Now().Add(ttl)
// Adding a small buffer since the TTL will be calculated again afeter this call
Copy link
Member

Choose a reason for hiding this comment

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

s/afeter/after

// cap the increment to whatever is left
if maxValidTime.Before(proposedExpiration) {
increment = maxValidTime.Sub(now)
// cap the increment to whatever is left, with a small buffer due to
Copy link
Member

Choose a reason for hiding this comment

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

The comment says that there will be a small buffer, but we aren't adding any buffer.

@@ -739,56 +755,40 @@ func (m *ExpirationManager) RenewToken(req *logical.Request, source string, toke
return logical.ErrorResponse(err.Error()), logical.ErrInvalidRequest
}

sysView := m.router.MatchingSystemView(le.Path)
Copy link
Member

Choose a reason for hiding this comment

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

Can we move this to be just above CalculateTTL?

}
}
return f(ctx, req, d)
req.Auth.Period = te.Period
Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason for not doing req.Auth.TTL = te.TTL here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The original implementation did not take into account the token entry TTL in calculating a new TTL. I think the token entry TTL is just the original TTL and we just use the mount configured values when calculating new TTLs.

Copy link
Member

@vishalnayak vishalnayak left a comment

Choose a reason for hiding this comment

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

LGTM! 🎉

@chrishoffman chrishoffman merged commit 6e35206 into master Apr 3, 2018
@chrishoffman chrishoffman deleted the core-auth-ttl branch April 3, 2018 16:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants