2015-03-13 17:55:54 +00:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2016-03-09 21:47:58 +00:00
|
|
|
"fmt"
|
2015-03-13 17:55:54 +00:00
|
|
|
"reflect"
|
2015-03-16 21:59:37 +00:00
|
|
|
"sort"
|
2015-03-13 17:55:54 +00:00
|
|
|
"strings"
|
2017-02-16 18:16:06 +00:00
|
|
|
"sync"
|
2015-03-13 17:55:54 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
2015-03-15 21:53:41 +00:00
|
|
|
|
2015-12-16 17:56:20 +00:00
|
|
|
"github.com/hashicorp/go-uuid"
|
2017-02-16 18:16:06 +00:00
|
|
|
"github.com/hashicorp/vault/helper/logformat"
|
2015-03-15 21:53:41 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
2016-03-09 21:47:58 +00:00
|
|
|
"github.com/hashicorp/vault/logical/framework"
|
2017-02-16 18:16:06 +00:00
|
|
|
"github.com/hashicorp/vault/physical"
|
2017-08-03 17:24:27 +00:00
|
|
|
"github.com/hashicorp/vault/physical/inmem"
|
2017-02-16 18:16:06 +00:00
|
|
|
log "github.com/mgutz/logxi/v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
testImagePull sync.Once
|
2015-03-13 17:55:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// mockExpiration returns a mock expiration manager
|
2017-02-16 18:16:06 +00:00
|
|
|
func mockExpiration(t testing.TB) *ExpirationManager {
|
2016-01-09 02:21:02 +00:00
|
|
|
_, ts, _, _ := TestCoreWithTokenStore(t)
|
2015-04-10 21:59:49 +00:00
|
|
|
return ts.expiration
|
2015-03-13 17:55:54 +00:00
|
|
|
}
|
|
|
|
|
2017-02-16 18:16:06 +00:00
|
|
|
func mockBackendExpiration(t testing.TB, backend physical.Backend) (*Core, *ExpirationManager) {
|
|
|
|
c, ts, _, _ := TestCoreWithBackendTokenStore(t, backend)
|
|
|
|
return c, ts.expiration
|
|
|
|
}
|
|
|
|
|
2017-05-02 20:54:03 +00:00
|
|
|
func TestExpiration_Tidy(t *testing.T) {
|
2017-05-02 21:11:35 +00:00
|
|
|
var err error
|
|
|
|
|
2017-05-02 20:54:03 +00:00
|
|
|
exp := mockExpiration(t)
|
2017-09-11 18:49:08 +00:00
|
|
|
if err := exp.Restore(nil); err != nil {
|
2017-09-05 15:09:00 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-05-02 20:54:03 +00:00
|
|
|
|
|
|
|
// Set up a count function to calculate number of leases
|
|
|
|
count := 0
|
|
|
|
countFunc := func(leaseID string) {
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan the storage with the count func set
|
2017-05-02 21:11:35 +00:00
|
|
|
if err = logical.ScanView(exp.idView, countFunc); err != nil {
|
2017-05-02 20:54:03 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that there are no leases to begin with
|
|
|
|
if count != 0 {
|
|
|
|
t.Fatalf("bad: lease count; expected:0 actual:%d", count)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a lease entry without a client token in it
|
|
|
|
le := &leaseEntry{
|
|
|
|
LeaseID: "lease/with/no/client/token",
|
|
|
|
Path: "foo/bar",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Persist the invalid lease entry
|
2017-05-02 21:11:35 +00:00
|
|
|
if err = exp.persistEntry(le); err != nil {
|
2017-05-02 20:54:03 +00:00
|
|
|
t.Fatalf("error persisting entry: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
count = 0
|
2017-05-02 21:11:35 +00:00
|
|
|
if err = logical.ScanView(exp.idView, countFunc); err != nil {
|
2017-05-02 20:54:03 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the storage was successful and that the count of leases is
|
|
|
|
// now 1
|
|
|
|
if count != 1 {
|
|
|
|
t.Fatalf("bad: lease count; expected:1 actual:%d", count)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the tidy operation
|
2017-05-02 21:11:35 +00:00
|
|
|
err = exp.Tidy()
|
2017-05-02 21:56:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-05-02 20:54:03 +00:00
|
|
|
|
|
|
|
count = 0
|
|
|
|
if err := logical.ScanView(exp.idView, countFunc); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Post the tidy operation, the invalid lease entry should have been gone
|
|
|
|
if count != 0 {
|
|
|
|
t.Fatalf("bad: lease count; expected:0 actual:%d", count)
|
|
|
|
}
|
2017-05-02 21:11:35 +00:00
|
|
|
|
|
|
|
// Set a revoked/invalid token in the lease entry
|
|
|
|
le.ClientToken = "invalidtoken"
|
|
|
|
|
|
|
|
// Persist the invalid lease entry
|
|
|
|
if err = exp.persistEntry(le); err != nil {
|
|
|
|
t.Fatalf("error persisting entry: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
count = 0
|
|
|
|
if err = logical.ScanView(exp.idView, countFunc); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the storage was successful and that the count of leases is
|
|
|
|
// now 1
|
|
|
|
if count != 1 {
|
|
|
|
t.Fatalf("bad: lease count; expected:1 actual:%d", count)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the tidy operation
|
|
|
|
err = exp.Tidy()
|
2017-05-02 21:56:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-05-02 21:11:35 +00:00
|
|
|
|
|
|
|
count = 0
|
|
|
|
if err = logical.ScanView(exp.idView, countFunc); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-05-02 21:15:26 +00:00
|
|
|
|
2017-05-02 21:56:15 +00:00
|
|
|
// Post the tidy operation, the invalid lease entry should have been gone
|
|
|
|
if count != 0 {
|
|
|
|
t.Fatalf("bad: lease count; expected:0 actual:%d", count)
|
|
|
|
}
|
|
|
|
|
2017-05-02 21:15:26 +00:00
|
|
|
// Attach an invalid token with 2 leases
|
|
|
|
if err = exp.persistEntry(le); err != nil {
|
|
|
|
t.Fatalf("error persisting entry: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
le.LeaseID = "another/invalid/lease"
|
|
|
|
if err = exp.persistEntry(le); err != nil {
|
|
|
|
t.Fatalf("error persisting entry: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the tidy operation
|
|
|
|
err = exp.Tidy()
|
2017-05-02 21:56:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-05-02 21:15:26 +00:00
|
|
|
|
|
|
|
count = 0
|
|
|
|
if err = logical.ScanView(exp.idView, countFunc); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-05-02 21:56:15 +00:00
|
|
|
|
|
|
|
// Post the tidy operation, the invalid lease entry should have been gone
|
|
|
|
if count != 0 {
|
|
|
|
t.Fatalf("bad: lease count; expected:0 actual:%d", count)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < 1000; i++ {
|
|
|
|
req := &logical.Request{
|
2017-05-02 22:06:59 +00:00
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "invalid/lease/" + fmt.Sprintf("%d", i+1),
|
|
|
|
ClientToken: "invalidtoken",
|
2017-05-02 21:56:15 +00:00
|
|
|
}
|
|
|
|
resp := &logical.Response{
|
|
|
|
Secret: &logical.Secret{
|
|
|
|
LeaseOptions: logical.LeaseOptions{
|
|
|
|
TTL: 100 * time.Millisecond,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"test_key": "test_value",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_, err := exp.Register(req, resp)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
count = 0
|
|
|
|
if err = logical.ScanView(exp.idView, countFunc); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that there are 1000 leases now
|
|
|
|
if count != 1000 {
|
|
|
|
t.Fatalf("bad: lease count; expected:1000 actual:%d", count)
|
|
|
|
}
|
|
|
|
|
|
|
|
errCh1 := make(chan error)
|
|
|
|
errCh2 := make(chan error)
|
|
|
|
|
|
|
|
// Initiate tidy of the above 1000 invalid leases in quick succession. Only
|
|
|
|
// one tidy operation can be in flight at any time. One of these requests
|
|
|
|
// should error out.
|
|
|
|
go func() {
|
|
|
|
errCh1 <- exp.Tidy()
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
errCh2 <- exp.Tidy()
|
|
|
|
}()
|
|
|
|
|
|
|
|
var err1, err2 error
|
|
|
|
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
select {
|
|
|
|
case err1 = <-errCh1:
|
|
|
|
case err2 = <-errCh2:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-02 22:06:59 +00:00
|
|
|
if !(err1 != nil && err1.Error() == "tidy operation on leases is already in progress") &&
|
|
|
|
!(err2 != nil && err2.Error() == "tidy operation on leases is already in progress") {
|
2017-09-05 15:09:00 +00:00
|
|
|
t.Fatalf("expected at least one of err1 or err2 to be set; err1: %#v\n err2:%#v\n", err1, err2)
|
2017-05-02 21:56:15 +00:00
|
|
|
}
|
2017-05-02 22:12:03 +00:00
|
|
|
|
|
|
|
root, err := exp.tokenStore.rootToken()
|
2017-07-04 17:58:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-05-02 22:12:03 +00:00
|
|
|
le.ClientToken = root.ID
|
|
|
|
|
|
|
|
// Attach a valid token with the leases
|
|
|
|
if err = exp.persistEntry(le); err != nil {
|
|
|
|
t.Fatalf("error persisting entry: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-05-04 16:41:15 +00:00
|
|
|
// Run the tidy operation
|
|
|
|
err = exp.Tidy()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-05-02 22:12:03 +00:00
|
|
|
count = 0
|
|
|
|
if err = logical.ScanView(exp.idView, countFunc); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Post the tidy operation, the valid lease entry should not get affected
|
|
|
|
if count != 1 {
|
|
|
|
t.Fatalf("bad: lease count; expected:1 actual:%d", count)
|
|
|
|
}
|
2017-05-02 20:54:03 +00:00
|
|
|
}
|
|
|
|
|
2017-08-03 17:24:27 +00:00
|
|
|
// To avoid pulling in deps for all users of the package, don't leave these
|
|
|
|
// uncommented in the public tree
|
|
|
|
/*
|
2017-02-16 18:16:06 +00:00
|
|
|
func BenchmarkExpiration_Restore_Etcd(b *testing.B) {
|
|
|
|
addr := os.Getenv("PHYSICAL_BACKEND_BENCHMARK_ADDR")
|
|
|
|
randPath := fmt.Sprintf("vault-%d/", time.Now().Unix())
|
|
|
|
|
|
|
|
logger := logformat.NewVaultLogger(log.LevelTrace)
|
2017-08-03 17:24:27 +00:00
|
|
|
physicalBackend, err := physEtcd.NewEtcdBackend(map[string]string{
|
2017-02-16 18:16:06 +00:00
|
|
|
"address": addr,
|
|
|
|
"path": randPath,
|
|
|
|
"max_parallel": "256",
|
2017-08-03 17:24:27 +00:00
|
|
|
}, logger)
|
2017-02-16 18:16:06 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
benchmarkExpirationBackend(b, physicalBackend, 10000) // 10,000 leases
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkExpiration_Restore_Consul(b *testing.B) {
|
|
|
|
addr := os.Getenv("PHYSICAL_BACKEND_BENCHMARK_ADDR")
|
|
|
|
randPath := fmt.Sprintf("vault-%d/", time.Now().Unix())
|
|
|
|
|
|
|
|
logger := logformat.NewVaultLogger(log.LevelTrace)
|
2017-08-03 17:24:27 +00:00
|
|
|
physicalBackend, err := physConsul.NewConsulBackend(map[string]string{
|
2017-02-16 18:16:06 +00:00
|
|
|
"address": addr,
|
|
|
|
"path": randPath,
|
|
|
|
"max_parallel": "256",
|
2017-08-03 17:24:27 +00:00
|
|
|
}, logger)
|
2017-02-16 18:16:06 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
benchmarkExpirationBackend(b, physicalBackend, 10000) // 10,000 leases
|
|
|
|
}
|
2017-08-03 17:24:27 +00:00
|
|
|
*/
|
2017-02-16 18:16:06 +00:00
|
|
|
|
|
|
|
func BenchmarkExpiration_Restore_InMem(b *testing.B) {
|
|
|
|
logger := logformat.NewVaultLogger(log.LevelTrace)
|
2017-08-03 17:24:27 +00:00
|
|
|
inm, err := inmem.NewInmem(nil, logger)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
benchmarkExpirationBackend(b, inm, 100000) // 100,000 Leases
|
2017-02-16 18:16:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func benchmarkExpirationBackend(b *testing.B, physicalBackend physical.Backend, numLeases int) {
|
|
|
|
c, exp := mockBackendExpiration(b, physicalBackend)
|
|
|
|
noop := &NoopBackend{}
|
|
|
|
view := NewBarrierView(c.barrier, "logical/")
|
|
|
|
meUUID, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2017-07-01 00:54:05 +00:00
|
|
|
err = exp.router.Mount(noop, "prod/aws/", &MountEntry{Path: "prod/aws/", Type: "noop", UUID: meUUID, Accessor: "noop-accessor"}, view)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2017-02-16 18:16:06 +00:00
|
|
|
|
|
|
|
// Register fake leases
|
|
|
|
for i := 0; i < numLeases; i++ {
|
|
|
|
pathUUID, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &logical.Request{
|
2017-09-05 15:09:00 +00:00
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "prod/aws/" + pathUUID,
|
|
|
|
ClientToken: "root",
|
2017-02-16 18:16:06 +00:00
|
|
|
}
|
|
|
|
resp := &logical.Response{
|
|
|
|
Secret: &logical.Secret{
|
|
|
|
LeaseOptions: logical.LeaseOptions{
|
|
|
|
TTL: 400 * time.Second,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"access_key": "xyz",
|
|
|
|
"secret_key": "abcd",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_, err = exp.Register(req, resp)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop everything
|
|
|
|
err = exp.Stop()
|
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2017-09-11 18:49:08 +00:00
|
|
|
err = exp.Restore(nil)
|
2017-02-16 18:16:06 +00:00
|
|
|
// Restore
|
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b.StopTimer()
|
|
|
|
}
|
|
|
|
|
2015-03-16 22:11:35 +00:00
|
|
|
func TestExpiration_Restore(t *testing.T) {
|
|
|
|
exp := mockExpiration(t)
|
|
|
|
noop := &NoopBackend{}
|
|
|
|
_, barrier, _ := mockBarrier(t)
|
|
|
|
view := NewBarrierView(barrier, "logical/")
|
2016-01-13 18:40:08 +00:00
|
|
|
meUUID, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-07-01 00:54:05 +00:00
|
|
|
err = exp.router.Mount(noop, "prod/aws/", &MountEntry{Path: "prod/aws/", Type: "noop", UUID: meUUID, Accessor: "noop-accessor"}, view)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-03-16 22:11:35 +00:00
|
|
|
|
|
|
|
paths := []string{
|
|
|
|
"prod/aws/foo",
|
|
|
|
"prod/aws/sub/bar",
|
|
|
|
"prod/aws/zip",
|
|
|
|
}
|
|
|
|
for _, path := range paths {
|
|
|
|
req := &logical.Request{
|
2017-05-04 16:41:15 +00:00
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: path,
|
|
|
|
ClientToken: "foobar",
|
2015-03-16 22:11:35 +00:00
|
|
|
}
|
|
|
|
resp := &logical.Response{
|
2015-03-19 22:11:42 +00:00
|
|
|
Secret: &logical.Secret{
|
2015-04-09 19:14:04 +00:00
|
|
|
LeaseOptions: logical.LeaseOptions{
|
2015-08-21 00:47:17 +00:00
|
|
|
TTL: 20 * time.Millisecond,
|
2015-04-09 19:14:04 +00:00
|
|
|
},
|
2015-03-16 22:11:35 +00:00
|
|
|
},
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"access_key": "xyz",
|
|
|
|
"secret_key": "abcd",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_, err := exp.Register(req, resp)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop everything
|
2016-01-13 18:40:08 +00:00
|
|
|
err = exp.Stop()
|
2015-03-16 22:11:35 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore
|
2017-09-11 18:49:08 +00:00
|
|
|
err = exp.Restore(nil)
|
2015-03-16 22:11:35 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure all are reaped
|
|
|
|
start := time.Now()
|
|
|
|
for time.Now().Sub(start) < time.Second {
|
2015-04-29 02:17:45 +00:00
|
|
|
noop.Lock()
|
|
|
|
less := len(noop.Requests) < 3
|
|
|
|
noop.Unlock()
|
|
|
|
|
|
|
|
if less {
|
2015-03-16 22:11:35 +00:00
|
|
|
time.Sleep(5 * time.Millisecond)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
for _, req := range noop.Requests {
|
|
|
|
if req.Operation != logical.RevokeOperation {
|
|
|
|
t.Fatalf("Bad: %v", req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-13 17:55:54 +00:00
|
|
|
func TestExpiration_Register(t *testing.T) {
|
|
|
|
exp := mockExpiration(t)
|
2015-03-15 21:53:41 +00:00
|
|
|
req := &logical.Request{
|
2017-05-04 16:41:15 +00:00
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "prod/aws/foo",
|
|
|
|
ClientToken: "foobar",
|
2015-03-13 17:55:54 +00:00
|
|
|
}
|
2015-03-15 21:53:41 +00:00
|
|
|
resp := &logical.Response{
|
2015-03-19 22:11:42 +00:00
|
|
|
Secret: &logical.Secret{
|
2015-04-09 19:14:04 +00:00
|
|
|
LeaseOptions: logical.LeaseOptions{
|
2015-08-21 00:47:17 +00:00
|
|
|
TTL: time.Hour,
|
2015-04-09 19:14:04 +00:00
|
|
|
},
|
2015-03-13 17:55:54 +00:00
|
|
|
},
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"access_key": "xyz",
|
|
|
|
"secret_key": "abcd",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := exp.Register(req, resp)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasPrefix(id, req.Path) {
|
|
|
|
t.Fatalf("bad: %s", id)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(id) <= len(req.Path) {
|
|
|
|
t.Fatalf("bad: %s", id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-03 00:45:42 +00:00
|
|
|
func TestExpiration_RegisterAuth(t *testing.T) {
|
2015-03-24 01:11:15 +00:00
|
|
|
exp := mockExpiration(t)
|
2016-01-20 00:44:33 +00:00
|
|
|
root, err := exp.tokenStore.rootToken()
|
2015-03-24 01:11:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-04-03 00:45:42 +00:00
|
|
|
auth := &logical.Auth{
|
|
|
|
ClientToken: root.ID,
|
2015-04-09 19:14:04 +00:00
|
|
|
LeaseOptions: logical.LeaseOptions{
|
2015-08-21 00:47:17 +00:00
|
|
|
TTL: time.Hour,
|
2015-04-09 19:14:04 +00:00
|
|
|
},
|
2015-03-24 01:11:15 +00:00
|
|
|
}
|
|
|
|
|
2015-04-03 00:45:42 +00:00
|
|
|
err = exp.RegisterAuth("auth/github/login", auth)
|
2015-03-24 01:11:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-05-12 17:52:33 +00:00
|
|
|
|
|
|
|
err = exp.RegisterAuth("auth/github/../login", auth)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error")
|
|
|
|
}
|
2015-03-24 01:11:15 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 22:43:26 +00:00
|
|
|
func TestExpiration_RegisterAuth_NoLease(t *testing.T) {
|
|
|
|
exp := mockExpiration(t)
|
2016-01-20 00:44:33 +00:00
|
|
|
root, err := exp.tokenStore.rootToken()
|
2015-04-08 22:43:26 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
auth := &logical.Auth{
|
|
|
|
ClientToken: root.ID,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = exp.RegisterAuth("auth/github/login", auth)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should not be able to renew, no expiration
|
2016-07-06 20:42:34 +00:00
|
|
|
resp, err := exp.RenewToken(&logical.Request{}, "auth/github/login", root.ID, 0)
|
2016-07-09 03:16:58 +00:00
|
|
|
if err != nil && (err != logical.ErrInvalidRequest || (resp != nil && resp.IsError() && resp.Error().Error() != "lease not found or lease is not renewable")) {
|
2016-07-08 23:01:36 +00:00
|
|
|
t.Fatalf("bad: err:%v resp:%#v", err, resp)
|
2016-07-08 14:34:49 +00:00
|
|
|
}
|
2016-07-06 20:42:34 +00:00
|
|
|
if resp == nil {
|
|
|
|
t.Fatal("expected a response")
|
|
|
|
}
|
2015-04-08 22:43:26 +00:00
|
|
|
|
|
|
|
// Wait and check token is not invalidated
|
|
|
|
time.Sleep(20 * time.Millisecond)
|
|
|
|
|
|
|
|
// Verify token does not get revoked
|
|
|
|
out, err := exp.tokenStore.Lookup(root.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out == nil {
|
|
|
|
t.Fatalf("missing token")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-16 21:59:37 +00:00
|
|
|
func TestExpiration_Revoke(t *testing.T) {
|
|
|
|
exp := mockExpiration(t)
|
|
|
|
noop := &NoopBackend{}
|
|
|
|
_, barrier, _ := mockBarrier(t)
|
|
|
|
view := NewBarrierView(barrier, "logical/")
|
2016-01-13 18:40:08 +00:00
|
|
|
meUUID, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-07-01 00:54:05 +00:00
|
|
|
err = exp.router.Mount(noop, "prod/aws/", &MountEntry{Path: "prod/aws/", Type: "noop", UUID: meUUID, Accessor: "noop-accessor"}, view)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-03-16 21:59:37 +00:00
|
|
|
|
|
|
|
req := &logical.Request{
|
2017-05-04 16:41:15 +00:00
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "prod/aws/foo",
|
|
|
|
ClientToken: "foobar",
|
2015-03-16 21:59:37 +00:00
|
|
|
}
|
|
|
|
resp := &logical.Response{
|
2015-03-19 22:11:42 +00:00
|
|
|
Secret: &logical.Secret{
|
2015-04-09 19:14:04 +00:00
|
|
|
LeaseOptions: logical.LeaseOptions{
|
2015-08-21 00:47:17 +00:00
|
|
|
TTL: time.Hour,
|
2015-04-09 19:14:04 +00:00
|
|
|
},
|
2015-03-16 21:59:37 +00:00
|
|
|
},
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"access_key": "xyz",
|
|
|
|
"secret_key": "abcd",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := exp.Register(req, resp)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := exp.Revoke(id); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req = noop.Requests[0]
|
|
|
|
if req.Operation != logical.RevokeOperation {
|
|
|
|
t.Fatalf("Bad: %v", req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExpiration_RevokeOnExpire(t *testing.T) {
|
|
|
|
exp := mockExpiration(t)
|
|
|
|
noop := &NoopBackend{}
|
|
|
|
_, barrier, _ := mockBarrier(t)
|
|
|
|
view := NewBarrierView(barrier, "logical/")
|
2016-01-13 18:40:08 +00:00
|
|
|
meUUID, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-07-01 00:54:05 +00:00
|
|
|
err = exp.router.Mount(noop, "prod/aws/", &MountEntry{Path: "prod/aws/", Type: "noop", UUID: meUUID, Accessor: "noop-accessor"}, view)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-03-16 21:59:37 +00:00
|
|
|
|
|
|
|
req := &logical.Request{
|
2017-05-04 16:41:15 +00:00
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "prod/aws/foo",
|
|
|
|
ClientToken: "foobar",
|
2015-03-16 21:59:37 +00:00
|
|
|
}
|
|
|
|
resp := &logical.Response{
|
2015-03-19 22:11:42 +00:00
|
|
|
Secret: &logical.Secret{
|
2015-04-09 19:14:04 +00:00
|
|
|
LeaseOptions: logical.LeaseOptions{
|
2015-08-21 00:47:17 +00:00
|
|
|
TTL: 20 * time.Millisecond,
|
2015-04-09 19:14:04 +00:00
|
|
|
},
|
2015-03-16 21:59:37 +00:00
|
|
|
},
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"access_key": "xyz",
|
|
|
|
"secret_key": "abcd",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-01-13 18:40:08 +00:00
|
|
|
_, err = exp.Register(req, resp)
|
2015-03-16 21:59:37 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
for time.Now().Sub(start) < time.Second {
|
2015-04-29 02:17:45 +00:00
|
|
|
req = nil
|
|
|
|
|
|
|
|
noop.Lock()
|
|
|
|
if len(noop.Requests) > 0 {
|
|
|
|
req = noop.Requests[0]
|
|
|
|
}
|
|
|
|
noop.Unlock()
|
|
|
|
if req == nil {
|
2015-03-16 21:59:37 +00:00
|
|
|
time.Sleep(5 * time.Millisecond)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if req.Operation != logical.RevokeOperation {
|
|
|
|
t.Fatalf("Bad: %v", req)
|
|
|
|
}
|
2015-04-29 02:17:45 +00:00
|
|
|
|
2015-03-16 21:59:37 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExpiration_RevokePrefix(t *testing.T) {
|
|
|
|
exp := mockExpiration(t)
|
|
|
|
noop := &NoopBackend{}
|
|
|
|
_, barrier, _ := mockBarrier(t)
|
|
|
|
view := NewBarrierView(barrier, "logical/")
|
2016-01-13 18:40:08 +00:00
|
|
|
meUUID, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-07-01 00:54:05 +00:00
|
|
|
err = exp.router.Mount(noop, "prod/aws/", &MountEntry{Path: "prod/aws/", Type: "noop", UUID: meUUID, Accessor: "noop-accessor"}, view)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-03-16 21:59:37 +00:00
|
|
|
|
|
|
|
paths := []string{
|
|
|
|
"prod/aws/foo",
|
|
|
|
"prod/aws/sub/bar",
|
|
|
|
"prod/aws/zip",
|
|
|
|
}
|
|
|
|
for _, path := range paths {
|
|
|
|
req := &logical.Request{
|
2017-05-04 16:41:15 +00:00
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: path,
|
|
|
|
ClientToken: "foobar",
|
2015-03-16 21:59:37 +00:00
|
|
|
}
|
|
|
|
resp := &logical.Response{
|
2015-03-19 22:11:42 +00:00
|
|
|
Secret: &logical.Secret{
|
2015-04-09 19:14:04 +00:00
|
|
|
LeaseOptions: logical.LeaseOptions{
|
2015-08-21 00:47:17 +00:00
|
|
|
TTL: 20 * time.Millisecond,
|
2015-04-09 19:14:04 +00:00
|
|
|
},
|
2015-03-16 21:59:37 +00:00
|
|
|
},
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"access_key": "xyz",
|
|
|
|
"secret_key": "abcd",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_, err := exp.Register(req, resp)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should nuke all the keys
|
|
|
|
if err := exp.RevokePrefix("prod/aws/"); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(noop.Requests) != 3 {
|
2015-04-10 21:48:08 +00:00
|
|
|
t.Fatalf("Bad: %v", noop.Requests)
|
|
|
|
}
|
|
|
|
for _, req := range noop.Requests {
|
|
|
|
if req.Operation != logical.RevokeOperation {
|
|
|
|
t.Fatalf("Bad: %v", req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expect := []string{
|
|
|
|
"foo",
|
|
|
|
"sub/bar",
|
|
|
|
"zip",
|
|
|
|
}
|
|
|
|
sort.Strings(noop.Paths)
|
|
|
|
sort.Strings(expect)
|
|
|
|
if !reflect.DeepEqual(noop.Paths, expect) {
|
|
|
|
t.Fatalf("bad: %v", noop.Paths)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExpiration_RevokeByToken(t *testing.T) {
|
|
|
|
exp := mockExpiration(t)
|
|
|
|
noop := &NoopBackend{}
|
|
|
|
_, barrier, _ := mockBarrier(t)
|
|
|
|
view := NewBarrierView(barrier, "logical/")
|
2016-01-13 18:40:08 +00:00
|
|
|
meUUID, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-07-01 00:54:05 +00:00
|
|
|
err = exp.router.Mount(noop, "prod/aws/", &MountEntry{Path: "prod/aws/", Type: "noop", UUID: meUUID, Accessor: "noop-accessor"}, view)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-04-10 21:48:08 +00:00
|
|
|
|
|
|
|
paths := []string{
|
|
|
|
"prod/aws/foo",
|
|
|
|
"prod/aws/sub/bar",
|
|
|
|
"prod/aws/zip",
|
|
|
|
}
|
|
|
|
for _, path := range paths {
|
|
|
|
req := &logical.Request{
|
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: path,
|
|
|
|
ClientToken: "foobarbaz",
|
|
|
|
}
|
|
|
|
resp := &logical.Response{
|
|
|
|
Secret: &logical.Secret{
|
|
|
|
LeaseOptions: logical.LeaseOptions{
|
2015-08-21 00:47:17 +00:00
|
|
|
TTL: 20 * time.Millisecond,
|
2015-04-10 21:48:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"access_key": "xyz",
|
|
|
|
"secret_key": "abcd",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_, err := exp.Register(req, resp)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should nuke all the keys
|
2016-03-31 19:10:25 +00:00
|
|
|
te := &TokenEntry{
|
|
|
|
ID: "foobarbaz",
|
|
|
|
}
|
|
|
|
if err := exp.RevokeByToken(te); err != nil {
|
2015-04-10 21:48:08 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(noop.Requests) != 3 {
|
2015-03-16 21:59:37 +00:00
|
|
|
t.Fatalf("Bad: %v", noop.Requests)
|
|
|
|
}
|
|
|
|
for _, req := range noop.Requests {
|
|
|
|
if req.Operation != logical.RevokeOperation {
|
|
|
|
t.Fatalf("Bad: %v", req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expect := []string{
|
|
|
|
"foo",
|
|
|
|
"sub/bar",
|
|
|
|
"zip",
|
|
|
|
}
|
|
|
|
sort.Strings(noop.Paths)
|
|
|
|
sort.Strings(expect)
|
|
|
|
if !reflect.DeepEqual(noop.Paths, expect) {
|
|
|
|
t.Fatalf("bad: %v", noop.Paths)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-03 18:58:10 +00:00
|
|
|
func TestExpiration_RenewToken(t *testing.T) {
|
|
|
|
exp := mockExpiration(t)
|
2016-01-20 00:44:33 +00:00
|
|
|
root, err := exp.tokenStore.rootToken()
|
2015-04-03 18:58:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register a token
|
|
|
|
auth := &logical.Auth{
|
|
|
|
ClientToken: root.ID,
|
2015-04-09 19:14:04 +00:00
|
|
|
LeaseOptions: logical.LeaseOptions{
|
2015-08-21 00:47:17 +00:00
|
|
|
TTL: time.Hour,
|
2015-04-09 19:14:04 +00:00
|
|
|
Renewable: true,
|
|
|
|
},
|
2015-04-03 18:58:10 +00:00
|
|
|
}
|
2015-04-09 21:23:37 +00:00
|
|
|
err = exp.RegisterAuth("auth/token/login", auth)
|
2015-04-03 18:58:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Renew the token
|
2016-02-26 21:44:35 +00:00
|
|
|
out, err := exp.RenewToken(&logical.Request{}, "auth/token/login", root.ID, 0)
|
2015-04-03 18:58:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-04-11 04:21:06 +00:00
|
|
|
|
2016-03-04 20:03:01 +00:00
|
|
|
if auth.ClientToken != out.Auth.ClientToken {
|
2018-01-18 17:19:18 +00:00
|
|
|
t.Fatalf("bad: %#v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExpiration_RenewToken_period(t *testing.T) {
|
|
|
|
exp := mockExpiration(t)
|
|
|
|
root, err := exp.tokenStore.rootToken()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register a token
|
|
|
|
auth := &logical.Auth{
|
|
|
|
ClientToken: root.ID,
|
|
|
|
LeaseOptions: logical.LeaseOptions{
|
|
|
|
TTL: time.Hour,
|
|
|
|
Renewable: true,
|
|
|
|
},
|
|
|
|
Period: time.Minute,
|
|
|
|
}
|
|
|
|
err = exp.RegisterAuth("auth/token/login", auth)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Renew the token
|
|
|
|
out, err := exp.RenewToken(&logical.Request{}, "auth/token/login", root.ID, 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if auth.ClientToken != out.Auth.ClientToken {
|
|
|
|
t.Fatalf("bad: %#v", out)
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.Auth.TTL > time.Minute {
|
|
|
|
t.Fatalf("expected TTL to be less than 1 minute, got: %s", out.Auth.TTL)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExpiration_RenewToken_period_backend(t *testing.T) {
|
|
|
|
exp := mockExpiration(t)
|
|
|
|
root, err := exp.tokenStore.rootToken()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mount a noop backend
|
|
|
|
noop := &NoopBackend{
|
|
|
|
Response: &logical.Response{
|
|
|
|
Auth: &logical.Auth{
|
|
|
|
LeaseOptions: logical.LeaseOptions{
|
|
|
|
TTL: 10 * time.Second,
|
|
|
|
Renewable: true,
|
|
|
|
},
|
|
|
|
Period: 5 * time.Second,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
DefaultLeaseTTL: 5 * time.Second,
|
|
|
|
MaxLeaseTTL: 5 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, barrier, _ := mockBarrier(t)
|
|
|
|
view := NewBarrierView(barrier, credentialBarrierPrefix)
|
|
|
|
meUUID, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = exp.router.Mount(noop, "auth/foo/", &MountEntry{Path: "auth/foo/", Type: "noop", UUID: meUUID, Accessor: "noop-accessor"}, view)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register a token
|
|
|
|
auth := &logical.Auth{
|
|
|
|
ClientToken: root.ID,
|
|
|
|
LeaseOptions: logical.LeaseOptions{
|
|
|
|
TTL: 10 * time.Second,
|
|
|
|
Renewable: true,
|
|
|
|
IssueTime: time.Now(),
|
|
|
|
},
|
|
|
|
Period: 5 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = exp.RegisterAuth("auth/foo/login", auth)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait 3 seconds
|
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
resp, err := exp.RenewToken(&logical.Request{}, "auth/foo/login", root.ID, 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp == nil {
|
|
|
|
t.Fatal("expected a response")
|
|
|
|
}
|
|
|
|
if resp.Auth.TTL > 5*time.Second {
|
|
|
|
t.Fatalf("expected TTL to be less than or equal to period, got: %s", resp.Auth.TTL)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait another 3 seconds. If period works correctly, this should not fail
|
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
resp, err = exp.RenewToken(&logical.Request{}, "auth/foo/login", root.ID, 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp == nil {
|
|
|
|
t.Fatal("expected a response")
|
|
|
|
}
|
|
|
|
if resp.Auth.TTL < 4*time.Second || resp.Auth.TTL > 5*time.Second {
|
|
|
|
t.Fatalf("expected TTL to be around period's value, got: %s", resp.Auth.TTL)
|
2015-04-06 23:35:39 +00:00
|
|
|
}
|
2015-04-03 18:58:10 +00:00
|
|
|
}
|
|
|
|
|
2015-04-09 00:03:46 +00:00
|
|
|
func TestExpiration_RenewToken_NotRenewable(t *testing.T) {
|
|
|
|
exp := mockExpiration(t)
|
2016-01-20 00:44:33 +00:00
|
|
|
root, err := exp.tokenStore.rootToken()
|
2015-04-09 00:03:46 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register a token
|
|
|
|
auth := &logical.Auth{
|
|
|
|
ClientToken: root.ID,
|
2015-04-09 19:14:04 +00:00
|
|
|
LeaseOptions: logical.LeaseOptions{
|
2015-08-21 00:47:17 +00:00
|
|
|
TTL: time.Hour,
|
2015-04-09 19:14:04 +00:00
|
|
|
Renewable: false,
|
|
|
|
},
|
2015-04-09 00:03:46 +00:00
|
|
|
}
|
|
|
|
err = exp.RegisterAuth("auth/github/login", auth)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to renew the token
|
2016-07-06 20:42:34 +00:00
|
|
|
resp, err := exp.RenewToken(&logical.Request{}, "auth/github/login", root.ID, 0)
|
2016-07-09 03:16:58 +00:00
|
|
|
if err != nil && (err != logical.ErrInvalidRequest || (resp != nil && resp.IsError() && resp.Error().Error() != "lease is not renewable")) {
|
2016-07-08 23:01:36 +00:00
|
|
|
t.Fatalf("bad: err:%v resp:%#v", err, resp)
|
|
|
|
}
|
2016-07-06 20:42:34 +00:00
|
|
|
if resp == nil {
|
|
|
|
t.Fatal("expected a response")
|
|
|
|
}
|
2016-07-08 23:01:36 +00:00
|
|
|
|
2015-04-09 00:03:46 +00:00
|
|
|
}
|
|
|
|
|
2015-03-16 21:59:37 +00:00
|
|
|
func TestExpiration_Renew(t *testing.T) {
|
|
|
|
exp := mockExpiration(t)
|
|
|
|
noop := &NoopBackend{}
|
|
|
|
_, barrier, _ := mockBarrier(t)
|
|
|
|
view := NewBarrierView(barrier, "logical/")
|
2016-01-13 18:40:08 +00:00
|
|
|
meUUID, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-07-01 00:54:05 +00:00
|
|
|
err = exp.router.Mount(noop, "prod/aws/", &MountEntry{Path: "prod/aws/", Type: "noop", UUID: meUUID, Accessor: "noop-accessor"}, view)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-03-16 21:59:37 +00:00
|
|
|
|
|
|
|
req := &logical.Request{
|
2017-05-04 16:41:15 +00:00
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "prod/aws/foo",
|
|
|
|
ClientToken: "foobar",
|
2015-03-16 21:59:37 +00:00
|
|
|
}
|
|
|
|
resp := &logical.Response{
|
2015-03-19 22:11:42 +00:00
|
|
|
Secret: &logical.Secret{
|
2015-04-09 19:14:04 +00:00
|
|
|
LeaseOptions: logical.LeaseOptions{
|
2015-08-21 00:47:17 +00:00
|
|
|
TTL: 20 * time.Millisecond,
|
2015-04-09 19:14:04 +00:00
|
|
|
Renewable: true,
|
|
|
|
},
|
2015-03-16 21:59:37 +00:00
|
|
|
},
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"access_key": "xyz",
|
|
|
|
"secret_key": "abcd",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := exp.Register(req, resp)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
noop.Response = &logical.Response{
|
2015-03-19 22:11:42 +00:00
|
|
|
Secret: &logical.Secret{
|
2015-04-09 19:14:04 +00:00
|
|
|
LeaseOptions: logical.LeaseOptions{
|
2015-08-21 00:47:17 +00:00
|
|
|
TTL: 20 * time.Millisecond,
|
2015-04-09 19:14:04 +00:00
|
|
|
},
|
2015-03-16 21:59:37 +00:00
|
|
|
},
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"access_key": "123",
|
|
|
|
"secret_key": "abcd",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := exp.Renew(id, 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-04-29 02:17:45 +00:00
|
|
|
noop.Lock()
|
|
|
|
defer noop.Unlock()
|
|
|
|
|
2015-03-16 21:59:37 +00:00
|
|
|
if !reflect.DeepEqual(out, noop.Response) {
|
|
|
|
t.Fatalf("Bad: %#v", out)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(noop.Requests) != 1 {
|
|
|
|
t.Fatalf("Bad: %#v", noop.Requests)
|
|
|
|
}
|
|
|
|
req = noop.Requests[0]
|
|
|
|
if req.Operation != logical.RenewOperation {
|
|
|
|
t.Fatalf("Bad: %v", req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-09 00:03:46 +00:00
|
|
|
func TestExpiration_Renew_NotRenewable(t *testing.T) {
|
|
|
|
exp := mockExpiration(t)
|
|
|
|
noop := &NoopBackend{}
|
|
|
|
_, barrier, _ := mockBarrier(t)
|
|
|
|
view := NewBarrierView(barrier, "logical/")
|
2016-01-13 18:40:08 +00:00
|
|
|
meUUID, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-07-01 00:54:05 +00:00
|
|
|
err = exp.router.Mount(noop, "prod/aws/", &MountEntry{Path: "prod/aws/", Type: "noop", UUID: meUUID, Accessor: "noop-accessor"}, view)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-04-09 00:03:46 +00:00
|
|
|
|
|
|
|
req := &logical.Request{
|
2017-05-04 16:41:15 +00:00
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "prod/aws/foo",
|
|
|
|
ClientToken: "foobar",
|
2015-04-09 00:03:46 +00:00
|
|
|
}
|
|
|
|
resp := &logical.Response{
|
|
|
|
Secret: &logical.Secret{
|
2015-04-09 19:14:04 +00:00
|
|
|
LeaseOptions: logical.LeaseOptions{
|
2015-08-21 00:47:17 +00:00
|
|
|
TTL: 20 * time.Millisecond,
|
2015-04-09 19:14:04 +00:00
|
|
|
Renewable: false,
|
|
|
|
},
|
2015-04-09 00:03:46 +00:00
|
|
|
},
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"access_key": "xyz",
|
|
|
|
"secret_key": "abcd",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := exp.Register(req, resp)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = exp.Renew(id, 0)
|
|
|
|
if err.Error() != "lease is not renewable" {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-04-29 02:17:45 +00:00
|
|
|
noop.Lock()
|
|
|
|
defer noop.Unlock()
|
|
|
|
|
2015-04-09 00:03:46 +00:00
|
|
|
if len(noop.Requests) != 0 {
|
|
|
|
t.Fatalf("Bad: %#v", noop.Requests)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-16 21:59:37 +00:00
|
|
|
func TestExpiration_Renew_RevokeOnExpire(t *testing.T) {
|
|
|
|
exp := mockExpiration(t)
|
|
|
|
noop := &NoopBackend{}
|
|
|
|
_, barrier, _ := mockBarrier(t)
|
|
|
|
view := NewBarrierView(barrier, "logical/")
|
2016-01-13 18:40:08 +00:00
|
|
|
meUUID, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-07-01 00:54:05 +00:00
|
|
|
err = exp.router.Mount(noop, "prod/aws/", &MountEntry{Path: "prod/aws/", Type: "noop", UUID: meUUID, Accessor: "noop-accessor"}, view)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-03-16 21:59:37 +00:00
|
|
|
|
|
|
|
req := &logical.Request{
|
2017-05-04 16:41:15 +00:00
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "prod/aws/foo",
|
|
|
|
ClientToken: "foobar",
|
2015-03-16 21:59:37 +00:00
|
|
|
}
|
|
|
|
resp := &logical.Response{
|
2015-03-19 22:11:42 +00:00
|
|
|
Secret: &logical.Secret{
|
2015-04-09 19:14:04 +00:00
|
|
|
LeaseOptions: logical.LeaseOptions{
|
2015-08-21 00:47:17 +00:00
|
|
|
TTL: 20 * time.Millisecond,
|
2015-04-09 19:14:04 +00:00
|
|
|
Renewable: true,
|
|
|
|
},
|
2015-03-16 21:59:37 +00:00
|
|
|
},
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"access_key": "xyz",
|
|
|
|
"secret_key": "abcd",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := exp.Register(req, resp)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
noop.Response = &logical.Response{
|
2015-03-19 22:11:42 +00:00
|
|
|
Secret: &logical.Secret{
|
2015-04-09 19:14:04 +00:00
|
|
|
LeaseOptions: logical.LeaseOptions{
|
2015-08-21 00:47:17 +00:00
|
|
|
TTL: 20 * time.Millisecond,
|
2015-04-09 19:14:04 +00:00
|
|
|
},
|
2015-03-16 21:59:37 +00:00
|
|
|
},
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"access_key": "123",
|
|
|
|
"secret_key": "abcd",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = exp.Renew(id, 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
for time.Now().Sub(start) < time.Second {
|
2015-04-29 02:17:45 +00:00
|
|
|
req = nil
|
|
|
|
|
|
|
|
noop.Lock()
|
|
|
|
if len(noop.Requests) >= 2 {
|
|
|
|
req = noop.Requests[1]
|
|
|
|
}
|
|
|
|
noop.Unlock()
|
|
|
|
|
|
|
|
if req == nil {
|
2015-03-16 21:59:37 +00:00
|
|
|
time.Sleep(5 * time.Millisecond)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if req.Operation != logical.RevokeOperation {
|
|
|
|
t.Fatalf("Bad: %v", req)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-16 20:58:22 +00:00
|
|
|
func TestExpiration_revokeEntry(t *testing.T) {
|
|
|
|
exp := mockExpiration(t)
|
|
|
|
|
|
|
|
noop := &NoopBackend{}
|
|
|
|
_, barrier, _ := mockBarrier(t)
|
|
|
|
view := NewBarrierView(barrier, "logical/")
|
2016-01-13 18:40:08 +00:00
|
|
|
meUUID, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-07-01 00:54:05 +00:00
|
|
|
err = exp.router.Mount(noop, "foo/bar/", &MountEntry{Path: "foo/bar/", Type: "noop", UUID: meUUID, Accessor: "noop-accessor"}, view)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-03-16 20:58:22 +00:00
|
|
|
|
|
|
|
le := &leaseEntry{
|
2015-04-08 20:35:32 +00:00
|
|
|
LeaseID: "foo/bar/1234",
|
2015-03-16 20:58:22 +00:00
|
|
|
Path: "foo/bar",
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"testing": true,
|
|
|
|
},
|
2015-03-19 22:11:42 +00:00
|
|
|
Secret: &logical.Secret{
|
2015-04-09 19:14:04 +00:00
|
|
|
LeaseOptions: logical.LeaseOptions{
|
2015-08-21 00:47:17 +00:00
|
|
|
TTL: time.Minute,
|
2015-04-09 19:14:04 +00:00
|
|
|
},
|
2015-03-16 20:58:22 +00:00
|
|
|
},
|
|
|
|
IssueTime: time.Now(),
|
|
|
|
ExpireTime: time.Now(),
|
|
|
|
}
|
|
|
|
|
2016-01-13 18:40:08 +00:00
|
|
|
err = exp.revokeEntry(le)
|
2015-03-16 20:58:22 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-04-29 02:17:45 +00:00
|
|
|
noop.Lock()
|
|
|
|
defer noop.Unlock()
|
|
|
|
|
2015-03-16 20:58:22 +00:00
|
|
|
req := noop.Requests[0]
|
|
|
|
if req.Operation != logical.RevokeOperation {
|
2017-07-01 00:54:05 +00:00
|
|
|
t.Fatalf("bad: operation; req: %#v", req)
|
2015-03-16 20:58:22 +00:00
|
|
|
}
|
2015-03-19 22:11:42 +00:00
|
|
|
if !reflect.DeepEqual(req.Data, le.Data) {
|
2017-07-01 00:54:05 +00:00
|
|
|
t.Fatalf("bad: data; req: %#v\n le: %#v\n", req, le)
|
2015-03-16 20:58:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-24 01:11:15 +00:00
|
|
|
func TestExpiration_revokeEntry_token(t *testing.T) {
|
|
|
|
exp := mockExpiration(t)
|
2016-01-20 00:44:33 +00:00
|
|
|
root, err := exp.tokenStore.rootToken()
|
2015-03-24 01:11:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-12-16 18:11:55 +00:00
|
|
|
// N.B.: Vault doesn't allow both a secret and auth to be returned, but the
|
|
|
|
// reason for both is that auth needs to be included in order to use the
|
|
|
|
// token store as it's the only mounted backend, *but* RegisterAuth doesn't
|
|
|
|
// actually create the index by token, only Register (for a Secret) does.
|
|
|
|
// So without the Secret we don't do anything when removing the index which
|
|
|
|
// (at the time of writing) now fails because a bug causing every token
|
|
|
|
// expiration to do an extra delete to a non-existent key has been fixed,
|
|
|
|
// and this test relies on this nonstandard behavior.
|
2015-03-24 01:11:15 +00:00
|
|
|
le := &leaseEntry{
|
2015-04-08 20:35:32 +00:00
|
|
|
LeaseID: "foo/bar/1234",
|
2015-04-03 18:58:10 +00:00
|
|
|
Auth: &logical.Auth{
|
|
|
|
ClientToken: root.ID,
|
2015-04-09 19:14:04 +00:00
|
|
|
LeaseOptions: logical.LeaseOptions{
|
2015-08-21 00:47:17 +00:00
|
|
|
TTL: time.Minute,
|
2015-04-09 19:14:04 +00:00
|
|
|
},
|
2015-03-24 01:11:15 +00:00
|
|
|
},
|
2016-12-16 18:11:55 +00:00
|
|
|
Secret: &logical.Secret{
|
|
|
|
LeaseOptions: logical.LeaseOptions{
|
|
|
|
TTL: time.Minute,
|
|
|
|
},
|
|
|
|
},
|
2015-11-04 15:48:44 +00:00
|
|
|
ClientToken: root.ID,
|
|
|
|
Path: "foo/bar",
|
|
|
|
IssueTime: time.Now(),
|
|
|
|
ExpireTime: time.Now(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := exp.persistEntry(le); err != nil {
|
|
|
|
t.Fatalf("error persisting entry: %v", err)
|
|
|
|
}
|
|
|
|
if err := exp.createIndexByToken(le.ClientToken, le.LeaseID); err != nil {
|
|
|
|
t.Fatalf("error creating secondary index: %v", err)
|
|
|
|
}
|
2016-12-16 18:11:55 +00:00
|
|
|
exp.updatePending(le, le.Secret.LeaseTotal())
|
2015-11-04 15:48:44 +00:00
|
|
|
|
|
|
|
indexEntry, err := exp.indexByToken(le.ClientToken, le.LeaseID)
|
|
|
|
if err != nil {
|
2016-04-13 19:38:29 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
2015-11-04 15:48:44 +00:00
|
|
|
}
|
|
|
|
if indexEntry == nil {
|
|
|
|
t.Fatalf("err: should have found a secondary index entry")
|
2015-03-24 01:11:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = exp.revokeEntry(le)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-11-04 15:48:44 +00:00
|
|
|
out, err := exp.tokenStore.Lookup(le.ClientToken)
|
2015-03-24 01:11:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out != nil {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
2015-11-04 15:48:44 +00:00
|
|
|
|
|
|
|
indexEntry, err = exp.indexByToken(le.ClientToken, le.LeaseID)
|
|
|
|
if err != nil {
|
2016-04-13 19:38:29 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
2015-11-04 15:48:44 +00:00
|
|
|
}
|
|
|
|
if indexEntry != nil {
|
|
|
|
t.Fatalf("err: should not have found a secondary index entry")
|
|
|
|
}
|
2015-03-24 01:11:15 +00:00
|
|
|
}
|
|
|
|
|
2015-03-16 20:58:22 +00:00
|
|
|
func TestExpiration_renewEntry(t *testing.T) {
|
|
|
|
exp := mockExpiration(t)
|
|
|
|
|
|
|
|
noop := &NoopBackend{
|
|
|
|
Response: &logical.Response{
|
2015-03-19 22:11:42 +00:00
|
|
|
Secret: &logical.Secret{
|
2015-04-09 19:14:04 +00:00
|
|
|
LeaseOptions: logical.LeaseOptions{
|
|
|
|
Renewable: true,
|
2015-08-21 00:47:17 +00:00
|
|
|
TTL: time.Hour,
|
2015-04-09 19:14:04 +00:00
|
|
|
},
|
2015-03-16 20:58:22 +00:00
|
|
|
},
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"testing": false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_, barrier, _ := mockBarrier(t)
|
|
|
|
view := NewBarrierView(barrier, "logical/")
|
2016-01-13 18:40:08 +00:00
|
|
|
meUUID, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-07-01 00:54:05 +00:00
|
|
|
err = exp.router.Mount(noop, "foo/bar/", &MountEntry{Path: "foo/bar/", Type: "noop", UUID: meUUID, Accessor: "noop-accessor"}, view)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-03-16 20:58:22 +00:00
|
|
|
|
|
|
|
le := &leaseEntry{
|
2015-04-08 20:35:32 +00:00
|
|
|
LeaseID: "foo/bar/1234",
|
2015-03-16 20:58:22 +00:00
|
|
|
Path: "foo/bar",
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"testing": true,
|
|
|
|
},
|
2015-03-19 22:11:42 +00:00
|
|
|
Secret: &logical.Secret{
|
2015-04-09 19:14:04 +00:00
|
|
|
LeaseOptions: logical.LeaseOptions{
|
2015-08-21 00:47:17 +00:00
|
|
|
TTL: time.Minute,
|
2015-04-09 19:14:04 +00:00
|
|
|
},
|
2015-03-16 20:58:22 +00:00
|
|
|
},
|
|
|
|
IssueTime: time.Now(),
|
|
|
|
ExpireTime: time.Now(),
|
|
|
|
}
|
|
|
|
|
2015-03-16 21:59:37 +00:00
|
|
|
resp, err := exp.renewEntry(le, time.Second)
|
2015-03-16 20:58:22 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-04-29 02:17:45 +00:00
|
|
|
noop.Lock()
|
|
|
|
defer noop.Unlock()
|
|
|
|
|
2015-03-16 20:58:22 +00:00
|
|
|
if !reflect.DeepEqual(resp, noop.Response) {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
req := noop.Requests[0]
|
|
|
|
if req.Operation != logical.RenewOperation {
|
|
|
|
t.Fatalf("Bad: %v", req)
|
|
|
|
}
|
2015-03-19 22:11:42 +00:00
|
|
|
if !reflect.DeepEqual(req.Data, le.Data) {
|
2015-03-16 21:59:37 +00:00
|
|
|
t.Fatalf("Bad: %v", req)
|
|
|
|
}
|
2015-08-21 05:27:01 +00:00
|
|
|
if req.Secret.Increment != time.Second {
|
2015-03-16 20:58:22 +00:00
|
|
|
t.Fatalf("Bad: %v", req)
|
|
|
|
}
|
2015-08-21 05:27:01 +00:00
|
|
|
if req.Secret.IssueTime.IsZero() {
|
2015-04-09 18:54:32 +00:00
|
|
|
t.Fatalf("Bad: %v", req)
|
|
|
|
}
|
2015-03-16 20:58:22 +00:00
|
|
|
}
|
|
|
|
|
2015-04-10 21:07:06 +00:00
|
|
|
func TestExpiration_renewAuthEntry(t *testing.T) {
|
|
|
|
exp := mockExpiration(t)
|
|
|
|
|
|
|
|
noop := &NoopBackend{
|
|
|
|
Response: &logical.Response{
|
|
|
|
Auth: &logical.Auth{
|
|
|
|
LeaseOptions: logical.LeaseOptions{
|
|
|
|
Renewable: true,
|
2015-08-21 00:47:17 +00:00
|
|
|
TTL: time.Hour,
|
2015-04-10 21:07:06 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_, barrier, _ := mockBarrier(t)
|
2018-01-18 17:19:18 +00:00
|
|
|
view := NewBarrierView(barrier, "auth/")
|
2016-01-13 18:40:08 +00:00
|
|
|
meUUID, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-07-01 00:54:05 +00:00
|
|
|
err = exp.router.Mount(noop, "auth/foo/", &MountEntry{Path: "auth/foo/", Type: "noop", UUID: meUUID, Accessor: "noop-accessor"}, view)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-04-10 21:07:06 +00:00
|
|
|
|
|
|
|
le := &leaseEntry{
|
|
|
|
LeaseID: "auth/foo/1234",
|
|
|
|
Path: "auth/foo/login",
|
|
|
|
Auth: &logical.Auth{
|
|
|
|
LeaseOptions: logical.LeaseOptions{
|
|
|
|
Renewable: true,
|
2015-08-21 00:47:17 +00:00
|
|
|
TTL: time.Minute,
|
2015-04-10 21:07:06 +00:00
|
|
|
},
|
2015-05-09 18:39:54 +00:00
|
|
|
InternalData: map[string]interface{}{
|
|
|
|
"MySecret": "secret",
|
|
|
|
},
|
2015-04-10 21:07:06 +00:00
|
|
|
},
|
|
|
|
IssueTime: time.Now(),
|
|
|
|
ExpireTime: time.Now().Add(time.Minute),
|
|
|
|
}
|
|
|
|
|
2016-02-26 21:44:35 +00:00
|
|
|
resp, err := exp.renewAuthEntry(&logical.Request{}, le, time.Second)
|
2015-04-10 21:07:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-04-29 02:17:45 +00:00
|
|
|
noop.Lock()
|
|
|
|
defer noop.Unlock()
|
|
|
|
|
2015-04-10 21:07:06 +00:00
|
|
|
if !reflect.DeepEqual(resp, noop.Response) {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
req := noop.Requests[0]
|
|
|
|
if req.Operation != logical.RenewOperation {
|
|
|
|
t.Fatalf("Bad: %v", req)
|
|
|
|
}
|
|
|
|
if req.Path != "login" {
|
|
|
|
t.Fatalf("Bad: %v", req)
|
|
|
|
}
|
2015-08-21 05:27:01 +00:00
|
|
|
if req.Auth.Increment != time.Second {
|
2015-04-10 21:07:06 +00:00
|
|
|
t.Fatalf("Bad: %v", req)
|
|
|
|
}
|
2015-08-21 05:27:01 +00:00
|
|
|
if req.Auth.IssueTime.IsZero() {
|
2015-04-10 21:07:06 +00:00
|
|
|
t.Fatalf("Bad: %v", req)
|
|
|
|
}
|
2015-05-09 18:39:54 +00:00
|
|
|
if req.Auth.InternalData["MySecret"] != "secret" {
|
|
|
|
t.Fatalf("Bad: %v", req)
|
|
|
|
}
|
2015-04-10 21:07:06 +00:00
|
|
|
}
|
|
|
|
|
2015-03-16 20:40:03 +00:00
|
|
|
func TestExpiration_PersistLoadDelete(t *testing.T) {
|
|
|
|
exp := mockExpiration(t)
|
2017-01-24 15:21:41 +00:00
|
|
|
lastTime := time.Now()
|
2015-03-16 20:40:03 +00:00
|
|
|
le := &leaseEntry{
|
2015-04-08 20:35:32 +00:00
|
|
|
LeaseID: "foo/bar/1234",
|
2015-03-16 20:40:03 +00:00
|
|
|
Path: "foo/bar",
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"testing": true,
|
|
|
|
},
|
2015-03-19 22:11:42 +00:00
|
|
|
Secret: &logical.Secret{
|
2015-04-09 19:14:04 +00:00
|
|
|
LeaseOptions: logical.LeaseOptions{
|
2015-08-21 00:47:17 +00:00
|
|
|
TTL: time.Minute,
|
2015-04-09 19:14:04 +00:00
|
|
|
},
|
2015-03-16 20:40:03 +00:00
|
|
|
},
|
2017-01-24 15:21:41 +00:00
|
|
|
IssueTime: lastTime,
|
|
|
|
ExpireTime: lastTime,
|
|
|
|
LastRenewalTime: lastTime,
|
2015-03-16 20:40:03 +00:00
|
|
|
}
|
|
|
|
if err := exp.persistEntry(le); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := exp.loadEntry("foo/bar/1234")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-01-24 16:17:48 +00:00
|
|
|
if !le.LastRenewalTime.Equal(out.LastRenewalTime) ||
|
|
|
|
!le.IssueTime.Equal(out.IssueTime) ||
|
|
|
|
!le.ExpireTime.Equal(out.ExpireTime) {
|
|
|
|
t.Fatalf("bad: expected:\n%#v\nactual:\n%#v", le, out)
|
|
|
|
}
|
2016-07-07 21:44:14 +00:00
|
|
|
le.LastRenewalTime = out.LastRenewalTime
|
2017-01-24 16:17:48 +00:00
|
|
|
le.IssueTime = out.IssueTime
|
|
|
|
le.ExpireTime = out.ExpireTime
|
2015-03-16 20:40:03 +00:00
|
|
|
if !reflect.DeepEqual(out, le) {
|
2017-01-23 19:08:29 +00:00
|
|
|
t.Fatalf("bad: expected:\n%#v\nactual:\n%#v", le, out)
|
2015-03-16 20:40:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = exp.deleteEntry("foo/bar/1234")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err = exp.loadEntry("foo/bar/1234")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out != nil {
|
|
|
|
t.Fatalf("out: %#v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-13 17:55:54 +00:00
|
|
|
func TestLeaseEntry(t *testing.T) {
|
|
|
|
le := &leaseEntry{
|
2015-04-08 20:35:32 +00:00
|
|
|
LeaseID: "foo/bar/1234",
|
2015-03-13 17:55:54 +00:00
|
|
|
Path: "foo/bar",
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"testing": true,
|
|
|
|
},
|
2015-03-19 22:11:42 +00:00
|
|
|
Secret: &logical.Secret{
|
2015-04-09 19:14:04 +00:00
|
|
|
LeaseOptions: logical.LeaseOptions{
|
2017-05-04 02:03:42 +00:00
|
|
|
TTL: time.Minute,
|
|
|
|
Renewable: true,
|
2015-04-09 19:14:04 +00:00
|
|
|
},
|
2015-03-13 17:55:54 +00:00
|
|
|
},
|
2016-07-07 21:44:14 +00:00
|
|
|
IssueTime: time.Now(),
|
|
|
|
ExpireTime: time.Now(),
|
2015-03-13 17:55:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enc, err := le.encode()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := decodeLeaseEntry(enc)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(out.Data, le.Data) {
|
|
|
|
t.Fatalf("got: %#v, expect %#v", out, le)
|
|
|
|
}
|
2017-05-04 02:03:42 +00:00
|
|
|
|
|
|
|
// Test renewability
|
|
|
|
le.ExpireTime = time.Time{}
|
|
|
|
if r, _ := le.renewable(); r {
|
|
|
|
t.Fatal("lease with zero expire time is not renewable")
|
|
|
|
}
|
|
|
|
le.ExpireTime = time.Now().Add(-1 * time.Hour)
|
|
|
|
if r, _ := le.renewable(); r {
|
|
|
|
t.Fatal("lease with expire time in the past is not renewable")
|
|
|
|
}
|
|
|
|
le.ExpireTime = time.Now().Add(1 * time.Hour)
|
|
|
|
if r, err := le.renewable(); !r {
|
|
|
|
t.Fatalf("lease with future expire time is renewable, err: %v", err)
|
|
|
|
}
|
|
|
|
le.Secret.LeaseOptions.Renewable = false
|
|
|
|
if r, _ := le.renewable(); r {
|
|
|
|
t.Fatal("secret is set to not be renewable but returns as renewable")
|
|
|
|
}
|
|
|
|
le.Secret = nil
|
|
|
|
le.Auth = &logical.Auth{
|
|
|
|
LeaseOptions: logical.LeaseOptions{
|
|
|
|
Renewable: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if r, err := le.renewable(); !r {
|
|
|
|
t.Fatalf("auth is renewable but is set to not be, err: %v", err)
|
|
|
|
}
|
|
|
|
le.Auth.LeaseOptions.Renewable = false
|
|
|
|
if r, _ := le.renewable(); r {
|
|
|
|
t.Fatal("auth is set to not be renewable but returns as renewable")
|
|
|
|
}
|
2015-03-13 17:55:54 +00:00
|
|
|
}
|
2016-03-09 21:47:58 +00:00
|
|
|
|
|
|
|
func TestExpiration_RevokeForce(t *testing.T) {
|
|
|
|
core, _, _, root := TestCoreWithTokenStore(t)
|
|
|
|
|
|
|
|
core.logicalBackends["badrenew"] = badRenewFactory
|
|
|
|
me := &MountEntry{
|
2017-07-01 00:54:05 +00:00
|
|
|
Table: mountTableType,
|
|
|
|
Path: "badrenew/",
|
|
|
|
Type: "badrenew",
|
|
|
|
Accessor: "badrenewaccessor",
|
2016-03-09 21:47:58 +00:00
|
|
|
}
|
|
|
|
|
2017-05-09 21:51:09 +00:00
|
|
|
err := core.mount(me)
|
2016-03-09 21:47:58 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Operation = logical.UpdateOperation
|
|
|
|
req.Path = "sys/revoke-prefix/badrenew/creds"
|
|
|
|
|
|
|
|
resp, err = core.HandleRequest(req)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error")
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Path = "sys/revoke-force/badrenew/creds"
|
|
|
|
resp, err = core.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("got error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func badRenewFactory(conf *logical.BackendConfig) (logical.Backend, error) {
|
|
|
|
be := &framework.Backend{
|
|
|
|
Paths: []*framework.Path{
|
|
|
|
&framework.Path{
|
|
|
|
Pattern: "creds",
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
2018-01-08 18:31:38 +00:00
|
|
|
logical.ReadOperation: func(context.Context, *logical.Request, *framework.FieldData) (*logical.Response, error) {
|
2016-03-09 21:47:58 +00:00
|
|
|
resp := &logical.Response{
|
|
|
|
Secret: &logical.Secret{
|
|
|
|
InternalData: map[string]interface{}{
|
|
|
|
"secret_type": "badRenewBackend",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
resp.Secret.TTL = time.Second * 30
|
|
|
|
return resp, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Secrets: []*framework.Secret{
|
|
|
|
&framework.Secret{
|
|
|
|
Type: "badRenewBackend",
|
2018-01-08 18:31:38 +00:00
|
|
|
Revoke: func(context.Context, *logical.Request, *framework.FieldData) (*logical.Response, error) {
|
2016-03-09 21:47:58 +00:00
|
|
|
return nil, fmt.Errorf("always errors")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
Backend plugin system (#2874)
* Add backend plugin changes
* Fix totp backend plugin tests
* Fix logical/plugin InvalidateKey test
* Fix plugin catalog CRUD test, fix NoopBackend
* Clean up commented code block
* Fix system backend mount test
* Set plugin_name to omitempty, fix handleMountTable config parsing
* Clean up comments, keep shim connections alive until cleanup
* Include pluginClient, disallow LookupPlugin call from within a plugin
* Add wrapper around backendPluginClient for proper cleanup
* Add logger shim tests
* Add logger, storage, and system shim tests
* Use pointer receivers for system view shim
* Use plugin name if no path is provided on mount
* Enable plugins for auth backends
* Add backend type attribute, move builtin/plugin/package
* Fix merge conflict
* Fix missing plugin name in mount config
* Add integration tests on enabling auth backend plugins
* Remove dependency cycle on mock-plugin
* Add passthrough backend plugin, use logical.BackendType to determine lease generation
* Remove vault package dependency on passthrough package
* Add basic impl test for passthrough plugin
* Incorporate feedback; set b.backend after shims creation on backendPluginServer
* Fix totp plugin test
* Add plugin backends docs
* Fix tests
* Fix builtin/plugin tests
* Remove flatten from PluginRunner fields
* Move mock plugin to logical/plugin, remove totp and passthrough plugins
* Move pluginMap into newPluginClient
* Do not create storage RPC connection on HandleRequest and HandleExistenceCheck
* Change shim logger's Fatal to no-op
* Change BackendType to uint32, match UX backend types
* Change framework.Backend Setup signature
* Add Setup func to logical.Backend interface
* Move OptionallyEnableMlock call into plugin.Serve, update docs and comments
* Remove commented var in plugin package
* RegisterLicense on logical.Backend interface (#3017)
* Add RegisterLicense to logical.Backend interface
* Update RegisterLicense to use callback func on framework.Backend
* Refactor framework.Backend.RegisterLicense
* plugin: Prevent plugin.SystemViewClient.ResponseWrapData from getting JWTs
* plugin: Revert BackendType to remove TypePassthrough and related references
* Fix typo in plugin backends docs
2017-07-20 17:28:40 +00:00
|
|
|
err := be.Setup(conf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return be, nil
|
2016-03-09 21:47:58 +00:00
|
|
|
}
|