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

Add ability for revoke-prefix and revoke-force to work on single leases #4450

Merged
merged 1 commit into from
Apr 26, 2018
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
13 changes: 11 additions & 2 deletions vault/expiration.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,9 +599,18 @@ func (m *ExpirationManager) revokePrefixCommon(prefix string, force bool) error
defer m.restoreRequestLock.Unlock()
}

// Ensure there is a trailing slash
// Ensure there is a trailing slash; or, if there is no slash, see if there
// is a matching specific ID
if !strings.HasSuffix(prefix, "/") {
prefix = prefix + "/"
le, err := m.loadEntry(prefix)
if err == nil && le != nil {
if err := m.revokeCommon(prefix, force, false); err != nil {
return errwrap.Wrapf(fmt.Sprintf("failed to revoke %q: {{err}}", prefix), err)
}
return nil
} else {
prefix = prefix + "/"
}
}

// Accumulate existing leases
Expand Down
72 changes: 72 additions & 0 deletions vault/expiration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1546,6 +1546,78 @@ func TestExpiration_RevokeForce(t *testing.T) {
}
}

func TestExpiration_RevokeForceSingle(t *testing.T) {
core, _, _, root := TestCoreWithTokenStore(t)

core.logicalBackends["badrenew"] = badRenewFactory
me := &MountEntry{
Table: mountTableType,
Path: "badrenew/",
Type: "badrenew",
Accessor: "badrenewaccessor",
}

err := core.mount(context.Background(), me)
if err != nil {
t.Fatal(err)
}

req := &logical.Request{
Operation: logical.ReadOperation,
Path: "badrenew/creds",
ClientToken: root,
}

resp, err := core.HandleRequest(req)
if err != nil {
t.Fatal(err)
}
if resp == nil {
t.Fatal("response was nil")
}
if resp.Secret == nil {
t.Fatalf("response secret was nil, response was %#v", *resp)
}
leaseID := resp.Secret.LeaseID

req.Operation = logical.UpdateOperation
req.Path = "sys/leases/lookup"
req.Data = map[string]interface{}{"lease_id": leaseID}
resp, err = core.HandleRequest(req)
if err != nil {
t.Fatal(err)
}
if resp == nil {
t.Fatal("nil response")
}
if resp.Data["id"].(string) != leaseID {
t.Fatalf("expected id %q, got %q", leaseID, resp.Data["id"].(string))
}

req.Path = "sys/revoke-prefix/" + leaseID

resp, err = core.HandleRequest(req)
if err == nil {
t.Fatal("expected error")
}

req.Path = "sys/revoke-force/" + leaseID
resp, err = core.HandleRequest(req)
if err != nil {
t.Fatalf("got error: %s", err)
}

req.Path = "sys/leases/lookup"
req.Data = map[string]interface{}{"lease_id": leaseID}
resp, err = core.HandleRequest(req)
if err == nil {
t.Fatal("expected error")
}
if !strings.Contains(err.Error(), "invalid request") {
t.Fatalf("bad error: %v", err)
}
}

func badRenewFactory(ctx context.Context, conf *logical.BackendConfig) (logical.Backend, error) {
be := &framework.Backend{
Paths: []*framework.Path{
Expand Down