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

Exclude /sys/leases/renew from registering with expiration manager #2891

Merged
merged 2 commits into from
Jun 20, 2017
Merged
Show file tree
Hide file tree
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
36 changes: 36 additions & 0 deletions command/renew_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,27 @@ func TestRenewBothWays(t *testing.T) {
t.Fatal("bad lease duration")
}

// Test another
r = client.NewRequest("PUT", "/v1/sys/leases/renew")
body = map[string]interface{}{
"lease_id": secret.LeaseID,
}
if err := r.SetJSONBody(body); err != nil {
t.Fatal(err)
}
resp, err = client.RawRequest(r)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
secret, err = api.ParseSecret(resp.Body)
if err != nil {
t.Fatal(err)
}
if secret.LeaseDuration != 60 {
t.Fatal("bad lease duration")
}

// Test the other
r = client.NewRequest("PUT", "/v1/sys/renew/"+secret.LeaseID)
resp, err = client.RawRequest(r)
Expand All @@ -104,4 +125,19 @@ func TestRenewBothWays(t *testing.T) {
if secret.LeaseDuration != 60 {
t.Fatalf("bad lease duration; secret is %#v\n", *secret)
}

// Test another
r = client.NewRequest("PUT", "/v1/sys/leases/renew/"+secret.LeaseID)
resp, err = client.RawRequest(r)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
secret, err = api.ParseSecret(resp.Body)
if err != nil {
t.Fatal(err)
}
if secret.LeaseDuration != 60 {
t.Fatalf("bad lease duration; secret is %#v\n", *secret)
}
}
2 changes: 2 additions & 0 deletions http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ func Handler(core *vault.Core) http.Handler {
mux.Handle("/v1/sys/unseal", handleSysUnseal(core))
mux.Handle("/v1/sys/renew", handleRequestForwarding(core, handleLogical(core, false, nil)))
mux.Handle("/v1/sys/renew/", handleRequestForwarding(core, handleLogical(core, false, nil)))
mux.Handle("/v1/sys/leases/renew", handleRequestForwarding(core, handleLogical(core, false, nil)))
mux.Handle("/v1/sys/leases/renew/", handleRequestForwarding(core, handleLogical(core, false, nil)))
mux.Handle("/v1/sys/leader", handleSysLeader(core))
mux.Handle("/v1/sys/health", handleSysHealth(core))
mux.Handle("/v1/sys/generate-root/attempt", handleRequestForwarding(core, handleSysGenerateRootAttempt(core)))
Expand Down
23 changes: 21 additions & 2 deletions http/sys_lease_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,33 @@ func TestSysRenew(t *testing.T) {
// read secret
resp = testHttpGet(t, token, addr+"/v1/secret/foo")
var result struct {
LeaseId string `json:"lease_id"`
LeaseID string `json:"lease_id"`
}
if err := jsonutil.DecodeJSONFromReader(resp.Body, &result); err != nil {
t.Fatalf("bad: %s", err)
}

resp = testHttpPut(t, token, addr+"/v1/sys/renew/"+result.LeaseId, nil)
var renewResult struct {
LeaseID string `json:"lease_id"`
Data map[string]interface{} `json:"data"`
}
resp = testHttpPut(t, token, addr+"/v1/sys/renew/"+result.LeaseID, nil)
testResponseStatus(t, resp, 200)
if err := jsonutil.DecodeJSONFromReader(resp.Body, &renewResult); err != nil {
t.Fatal(err)
}
if result.LeaseID != renewResult.LeaseID {
t.Fatal("lease id changed in renew request")
}

resp = testHttpPut(t, token, addr+"/v1/sys/leases/renew/"+result.LeaseID, nil)
testResponseStatus(t, resp, 200)
if err := jsonutil.DecodeJSONFromReader(resp.Body, &renewResult); err != nil {
t.Fatal(err)
}
if result.LeaseID != renewResult.LeaseID {
t.Fatal("lease id changed in renew request")
}
}

func TestSysRevoke(t *testing.T) {
Expand Down
15 changes: 14 additions & 1 deletion vault/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1793,6 +1793,19 @@ func TestCore_RenewSameLease(t *testing.T) {
if resp.Secret.LeaseID != original {
t.Fatalf("lease id changed: %s %s", original, resp.Secret.LeaseID)
}

// Renew the lease (alternate path)
req = logical.TestRequest(t, logical.UpdateOperation, "sys/leases/renew/"+resp.Secret.LeaseID)
req.ClientToken = root
resp, err = c.HandleRequest(req)
if err != nil {
t.Fatalf("err: %v", err)
}

// Verify the lease did not change
if resp.Secret.LeaseID != original {
t.Fatalf("lease id changed: %s %s", original, resp.Secret.LeaseID)
}
}

// Renew of a token should not create a new lease
Expand Down Expand Up @@ -1937,7 +1950,7 @@ path "secret/*" {
}

// Renew the lease
req = logical.TestRequest(t, logical.UpdateOperation, "sys/renew")
req = logical.TestRequest(t, logical.UpdateOperation, "sys/leases/renew")
req.Data = map[string]interface{}{
"lease_id": resp.Secret.LeaseID,
}
Expand Down
3 changes: 2 additions & 1 deletion vault/request_handling.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,8 @@ func (c *Core) handleRequest(req *logical.Request) (retResp *logical.Response, r

// If there is a secret, we must register it with the expiration manager.
// We exclude renewal of a lease, since it does not need to be re-registered
if resp != nil && resp.Secret != nil && !strings.HasPrefix(req.Path, "sys/renew") {
if resp != nil && resp.Secret != nil && !strings.HasPrefix(req.Path, "sys/renew") &&
!strings.HasPrefix(req.Path, "sys/leases/renew") {
// Get the SystemView for the mount
sysView := c.router.MatchingSystemView(req.Path)
if sysView == nil {
Expand Down