2021-06-02 16:11:30 +00:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
2021-06-04 19:25:41 +00:00
|
|
|
"path"
|
2021-06-02 16:11:30 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
uuid "github.com/hashicorp/go-uuid"
|
|
|
|
"github.com/hashicorp/vault/helper/namespace"
|
2021-06-04 19:25:41 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2021-06-02 16:11:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type basicLeaseTestInfo struct {
|
|
|
|
id string
|
|
|
|
mount string
|
|
|
|
expire time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
// add an irrevocable lease for test purposes
|
|
|
|
// returns the lease ID and expire time
|
|
|
|
func addIrrevocableLease(t *testing.T, m *ExpirationManager, pathPrefix string, ns *namespace.Namespace) *basicLeaseTestInfo {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
uuid, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error generating uuid: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ns == nil {
|
|
|
|
ns = namespace.RootNamespace
|
|
|
|
}
|
|
|
|
|
2021-06-04 19:25:41 +00:00
|
|
|
leaseID := path.Join(pathPrefix, "lease"+uuid)
|
|
|
|
|
2021-06-02 16:11:30 +00:00
|
|
|
if ns != namespace.RootNamespace {
|
2021-06-04 19:25:41 +00:00
|
|
|
leaseID = fmt.Sprintf("%s.%s", leaseID, ns.ID)
|
2021-06-02 16:11:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
randomTimeDelta := time.Duration(rand.Int31n(24))
|
|
|
|
le := &leaseEntry{
|
2021-06-04 19:25:41 +00:00
|
|
|
LeaseID: leaseID,
|
|
|
|
Path: pathPrefix,
|
2021-06-02 16:11:30 +00:00
|
|
|
namespace: ns,
|
|
|
|
IssueTime: time.Now(),
|
|
|
|
ExpireTime: time.Now().Add(randomTimeDelta * time.Hour),
|
|
|
|
RevokeErr: "some error message",
|
|
|
|
}
|
|
|
|
|
|
|
|
m.pendingLock.Lock()
|
|
|
|
defer m.pendingLock.Unlock()
|
|
|
|
|
|
|
|
if err := m.persistEntry(context.Background(), le); err != nil {
|
|
|
|
t.Fatalf("error persisting irrevocable lease: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
m.updatePendingInternal(le)
|
|
|
|
|
|
|
|
return &basicLeaseTestInfo{
|
|
|
|
id: le.LeaseID,
|
|
|
|
expire: le.ExpireTime,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// InjectIrrevocableLeases injects `count` irrevocable leases (currently to a
|
|
|
|
// single mount).
|
|
|
|
// It returns a map of the mount accessor to the number of leases stored there
|
|
|
|
func (c *Core) InjectIrrevocableLeases(t *testing.T, ctx context.Context, count int) map[string]int {
|
|
|
|
out := make(map[string]int)
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
le := addIrrevocableLease(t, c.expiration, "foo/", namespace.RootNamespace)
|
|
|
|
|
|
|
|
mountAccessor := c.expiration.getLeaseMountAccessor(ctx, le.id)
|
|
|
|
if _, ok := out[mountAccessor]; !ok {
|
|
|
|
out[mountAccessor] = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
out[mountAccessor]++
|
|
|
|
}
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
2021-06-04 19:25:41 +00:00
|
|
|
|
|
|
|
type backend struct {
|
|
|
|
path string
|
|
|
|
ns *namespace.Namespace
|
|
|
|
}
|
|
|
|
|
|
|
|
// set up multiple mounts, and return a mapping of the path to the mount accessor
|
|
|
|
func mountNoopBackends(t *testing.T, c *Core, backends []*backend) map[string]string {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
// enable the noop backend
|
|
|
|
c.logicalBackends["noop"] = func(ctx context.Context, config *logical.BackendConfig) (logical.Backend, error) {
|
|
|
|
return &NoopBackend{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pathToMount := make(map[string]string)
|
|
|
|
for _, backend := range backends {
|
|
|
|
me := &MountEntry{
|
|
|
|
Table: mountTableType,
|
|
|
|
Path: backend.path,
|
|
|
|
Type: "noop",
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCtx := namespace.ContextWithNamespace(context.Background(), backend.ns)
|
|
|
|
err := c.mount(nsCtx, me)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err mounting backend %s: %v", backend.path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
mount := c.router.MatchingMountEntry(nsCtx, backend.path)
|
|
|
|
if mount == nil {
|
|
|
|
t.Fatalf("couldn't find mount for path %s", backend.path)
|
|
|
|
}
|
|
|
|
pathToMount[backend.path] = mount.Accessor
|
|
|
|
}
|
|
|
|
|
|
|
|
return pathToMount
|
|
|
|
}
|