2015-03-19 02:36:17 +00:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
2018-01-19 06:44:44 +00:00
|
|
|
"context"
|
2015-03-19 02:36:17 +00:00
|
|
|
"reflect"
|
2017-02-17 04:09:39 +00:00
|
|
|
"strings"
|
2015-03-19 02:36:17 +00:00
|
|
|
"testing"
|
2020-10-27 15:24:43 +00:00
|
|
|
"time"
|
2015-03-19 17:39:47 +00:00
|
|
|
|
2020-10-27 15:24:43 +00:00
|
|
|
metrics "github.com/armon/go-metrics"
|
|
|
|
"github.com/hashicorp/vault/helper/metricsutil"
|
2018-09-18 03:03:00 +00:00
|
|
|
"github.com/hashicorp/vault/helper/namespace"
|
2019-04-13 07:44:06 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/jsonutil"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2015-03-19 02:36:17 +00:00
|
|
|
)
|
|
|
|
|
2018-02-09 19:04:25 +00:00
|
|
|
func TestAuth_ReadOnlyViewDuringMount(t *testing.T) {
|
2020-10-27 15:24:43 +00:00
|
|
|
c, _, _, _ := TestCoreUnsealedWithMetrics(t)
|
2018-02-09 19:04:25 +00:00
|
|
|
c.credentialBackends["noop"] = func(ctx context.Context, config *logical.BackendConfig) (logical.Backend, error) {
|
|
|
|
err := config.StorageView.Put(ctx, &logical.StorageEntry{
|
|
|
|
Key: "bar",
|
|
|
|
Value: []byte("baz"),
|
|
|
|
})
|
|
|
|
if err == nil || !strings.Contains(err.Error(), logical.ErrSetupReadOnly.Error()) {
|
|
|
|
t.Fatalf("expected a read-only error")
|
|
|
|
}
|
2018-11-07 01:21:24 +00:00
|
|
|
return &NoopBackend{
|
|
|
|
BackendType: logical.TypeCredential,
|
|
|
|
}, nil
|
2018-02-09 19:04:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
me := &MountEntry{
|
|
|
|
Table: credentialTableType,
|
|
|
|
Path: "foo",
|
|
|
|
Type: "noop",
|
|
|
|
}
|
2018-11-05 16:11:32 +00:00
|
|
|
err := c.enableCredential(namespace.RootContext(nil), me)
|
2018-02-09 19:04:25 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-27 15:24:43 +00:00
|
|
|
func TestAuthMountMetrics(t *testing.T) {
|
|
|
|
c, _, _, _ := TestCoreUnsealedWithMetrics(t)
|
|
|
|
c.credentialBackends["noop"] = func(ctx context.Context, config *logical.BackendConfig) (logical.Backend, error) {
|
|
|
|
return &NoopBackend{
|
|
|
|
BackendType: logical.TypeCredential,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
mountKeyName := "core.mount_table.num_entries.type|auth||local|false||"
|
|
|
|
mountMetrics := &c.metricsHelper.LoopMetrics.Metrics
|
|
|
|
loadMetric, ok := mountMetrics.Load(mountKeyName)
|
|
|
|
var numEntriesMetric metricsutil.GaugeMetric = loadMetric.(metricsutil.GaugeMetric)
|
|
|
|
|
|
|
|
// 1 default nonlocal auth backend
|
|
|
|
if !ok || numEntriesMetric.Value != 1 {
|
|
|
|
t.Fatalf("Auth values should be: %+v", numEntriesMetric)
|
|
|
|
}
|
|
|
|
|
|
|
|
me := &MountEntry{
|
|
|
|
Table: credentialTableType,
|
|
|
|
Path: "foo",
|
|
|
|
Type: "noop",
|
|
|
|
}
|
|
|
|
err := c.enableCredential(namespace.RootContext(nil), me)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
mountMetrics = &c.metricsHelper.LoopMetrics.Metrics
|
|
|
|
loadMetric, ok = mountMetrics.Load(mountKeyName)
|
|
|
|
numEntriesMetric = loadMetric.(metricsutil.GaugeMetric)
|
|
|
|
if !ok || numEntriesMetric.Value != 2 {
|
|
|
|
t.Fatalf("mount metrics for num entries do not match true values")
|
|
|
|
}
|
|
|
|
if len(numEntriesMetric.Key) != 3 ||
|
|
|
|
numEntriesMetric.Key[0] != "core" ||
|
|
|
|
numEntriesMetric.Key[1] != "mount_table" ||
|
|
|
|
numEntriesMetric.Key[2] != "num_entries" {
|
|
|
|
t.Fatalf("mount metrics for num entries have wrong key")
|
|
|
|
}
|
|
|
|
if len(numEntriesMetric.Labels) != 2 ||
|
|
|
|
numEntriesMetric.Labels[0].Name != "type" ||
|
|
|
|
numEntriesMetric.Labels[0].Value != "auth" ||
|
|
|
|
numEntriesMetric.Labels[1].Name != "local" ||
|
|
|
|
numEntriesMetric.Labels[1].Value != "false" {
|
|
|
|
t.Fatalf("mount metrics for num entries have wrong labels")
|
|
|
|
}
|
|
|
|
mountSizeKeyName := "core.mount_table.size.type|auth||local|false||"
|
|
|
|
loadMetric, ok = mountMetrics.Load(mountSizeKeyName)
|
|
|
|
sizeMetric := loadMetric.(metricsutil.GaugeMetric)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("mount metrics for size do not match exist")
|
|
|
|
}
|
|
|
|
if len(sizeMetric.Key) != 3 ||
|
|
|
|
sizeMetric.Key[0] != "core" ||
|
|
|
|
sizeMetric.Key[1] != "mount_table" ||
|
|
|
|
sizeMetric.Key[2] != "size" {
|
|
|
|
t.Fatalf("mount metrics for size have wrong key")
|
|
|
|
}
|
|
|
|
if len(sizeMetric.Labels) != 2 ||
|
|
|
|
sizeMetric.Labels[0].Name != "type" ||
|
|
|
|
sizeMetric.Labels[0].Value != "auth" ||
|
|
|
|
sizeMetric.Labels[1].Name != "local" ||
|
|
|
|
sizeMetric.Labels[1].Value != "false" {
|
|
|
|
t.Fatalf("mount metrics for size have wrong labels")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-19 02:36:17 +00:00
|
|
|
func TestCore_DefaultAuthTable(t *testing.T) {
|
2020-10-27 15:24:43 +00:00
|
|
|
c, keys, _, _ := TestCoreUnsealedWithMetrics(t)
|
2015-03-19 02:36:17 +00:00
|
|
|
verifyDefaultAuthTable(t, c.auth)
|
|
|
|
|
|
|
|
// Start a second core with same physical
|
2020-10-27 15:24:43 +00:00
|
|
|
inmemSink := metrics.NewInmemSink(1000000*time.Hour, 2000000*time.Hour)
|
2015-04-29 01:12:57 +00:00
|
|
|
conf := &CoreConfig{
|
2020-10-27 15:24:43 +00:00
|
|
|
Physical: c.physical,
|
|
|
|
DisableMlock: true,
|
|
|
|
BuiltinRegistry: NewMockBuiltinRegistry(),
|
|
|
|
MetricSink: metricsutil.NewClusterMetricSink("test-cluster", inmemSink),
|
|
|
|
MetricsHelper: metricsutil.NewMetricsHelper(inmemSink, false),
|
2015-04-29 01:12:57 +00:00
|
|
|
}
|
2015-03-19 02:36:17 +00:00
|
|
|
c2, err := NewCore(conf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-01-17 20:43:10 +00:00
|
|
|
for i, key := range keys {
|
|
|
|
unseal, err := TestCoreUnseal(c2, key)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if i+1 == len(keys) && !unseal {
|
|
|
|
t.Fatalf("should be unsealed")
|
|
|
|
}
|
2015-03-19 02:36:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify matching mount tables
|
|
|
|
if !reflect.DeepEqual(c.auth, c2.auth) {
|
|
|
|
t.Fatalf("mismatch: %v %v", c.auth, c2.auth)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-19 17:39:47 +00:00
|
|
|
func TestCore_EnableCredential(t *testing.T) {
|
2020-10-27 15:24:43 +00:00
|
|
|
c, keys, _, _ := TestCoreUnsealedWithMetrics(t)
|
2018-01-19 06:44:44 +00:00
|
|
|
c.credentialBackends["noop"] = func(context.Context, *logical.BackendConfig) (logical.Backend, error) {
|
2018-11-07 01:21:24 +00:00
|
|
|
return &NoopBackend{
|
|
|
|
BackendType: logical.TypeCredential,
|
|
|
|
}, nil
|
2015-03-19 17:39:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
me := &MountEntry{
|
2016-05-26 17:38:51 +00:00
|
|
|
Table: credentialTableType,
|
|
|
|
Path: "foo",
|
|
|
|
Type: "noop",
|
2015-03-19 17:39:47 +00:00
|
|
|
}
|
2018-11-05 16:11:32 +00:00
|
|
|
err := c.enableCredential(namespace.RootContext(nil), me)
|
2015-03-19 17:39:47 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-11-05 16:11:32 +00:00
|
|
|
match := c.router.MatchingMount(namespace.RootContext(nil), "auth/foo/bar")
|
2015-03-19 17:39:47 +00:00
|
|
|
if match != "auth/foo/" {
|
2018-09-18 03:03:00 +00:00
|
|
|
t.Fatalf("missing mount, match: %q", match)
|
2015-03-19 17:39:47 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 15:24:43 +00:00
|
|
|
inmemSink := metrics.NewInmemSink(1000000*time.Hour, 2000000*time.Hour)
|
2015-04-29 01:12:57 +00:00
|
|
|
conf := &CoreConfig{
|
2020-10-27 15:24:43 +00:00
|
|
|
Physical: c.physical,
|
|
|
|
DisableMlock: true,
|
|
|
|
BuiltinRegistry: NewMockBuiltinRegistry(),
|
|
|
|
MetricSink: metricsutil.NewClusterMetricSink("test-cluster", inmemSink),
|
|
|
|
MetricsHelper: metricsutil.NewMetricsHelper(inmemSink, false),
|
2015-04-29 01:12:57 +00:00
|
|
|
}
|
2015-03-19 17:39:47 +00:00
|
|
|
c2, err := NewCore(conf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
c2.credentialBackends["noop"] = func(context.Context, *logical.BackendConfig) (logical.Backend, error) {
|
2018-11-07 01:21:24 +00:00
|
|
|
return &NoopBackend{
|
|
|
|
BackendType: logical.TypeCredential,
|
|
|
|
}, nil
|
2015-11-02 16:01:00 +00:00
|
|
|
}
|
2017-01-17 20:43:10 +00:00
|
|
|
for i, key := range keys {
|
|
|
|
unseal, err := TestCoreUnseal(c2, key)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if i+1 == len(keys) && !unseal {
|
|
|
|
t.Fatalf("should be unsealed")
|
|
|
|
}
|
2015-03-19 17:39:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify matching auth tables
|
|
|
|
if !reflect.DeepEqual(c.auth, c2.auth) {
|
|
|
|
t.Fatalf("mismatch: %v %v", c.auth, c2.auth)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-17 04:09:39 +00:00
|
|
|
// Test that the local table actually gets populated as expected with local
|
|
|
|
// entries, and that upon reading the entries from both are recombined
|
|
|
|
// correctly
|
|
|
|
func TestCore_EnableCredential_Local(t *testing.T) {
|
2020-10-27 15:24:43 +00:00
|
|
|
c, _, _, _ := TestCoreUnsealedWithMetrics(t)
|
2018-01-19 06:44:44 +00:00
|
|
|
c.credentialBackends["noop"] = func(context.Context, *logical.BackendConfig) (logical.Backend, error) {
|
2018-11-07 01:21:24 +00:00
|
|
|
return &NoopBackend{
|
|
|
|
BackendType: logical.TypeCredential,
|
|
|
|
}, nil
|
2017-02-17 04:09:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.auth = &MountTable{
|
|
|
|
Type: credentialTableType,
|
|
|
|
Entries: []*MountEntry{
|
|
|
|
&MountEntry{
|
2018-03-21 19:04:27 +00:00
|
|
|
Table: credentialTableType,
|
|
|
|
Path: "noop/",
|
|
|
|
Type: "noop",
|
|
|
|
UUID: "abcd",
|
|
|
|
Accessor: "noop-abcd",
|
|
|
|
BackendAwareUUID: "abcde",
|
2018-09-18 03:03:00 +00:00
|
|
|
NamespaceID: namespace.RootNamespaceID,
|
2018-11-05 16:11:32 +00:00
|
|
|
namespace: namespace.RootNamespace,
|
2017-02-17 04:09:39 +00:00
|
|
|
},
|
|
|
|
&MountEntry{
|
2018-03-21 19:04:27 +00:00
|
|
|
Table: credentialTableType,
|
|
|
|
Path: "noop2/",
|
|
|
|
Type: "noop",
|
|
|
|
UUID: "bcde",
|
|
|
|
Accessor: "noop-bcde",
|
|
|
|
BackendAwareUUID: "bcdea",
|
2018-09-18 03:03:00 +00:00
|
|
|
NamespaceID: namespace.RootNamespaceID,
|
2018-11-05 16:11:32 +00:00
|
|
|
namespace: namespace.RootNamespace,
|
2017-02-17 04:09:39 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Both should set up successfully
|
2018-01-19 06:44:44 +00:00
|
|
|
err := c.setupCredentials(context.Background())
|
2017-02-17 04:09:39 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
rawLocal, err := c.barrier.Get(context.Background(), coreLocalAuthConfigPath)
|
2017-02-17 04:09:39 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if rawLocal == nil {
|
|
|
|
t.Fatal("expected non-nil local credential")
|
|
|
|
}
|
|
|
|
localCredentialTable := &MountTable{}
|
|
|
|
if err := jsonutil.DecodeJSON(rawLocal.Value, localCredentialTable); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(localCredentialTable.Entries) > 0 {
|
|
|
|
t.Fatalf("expected no entries in local credential table, got %#v", localCredentialTable)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.auth.Entries[1].Local = true
|
2018-04-11 18:32:55 +00:00
|
|
|
if err := c.persistAuth(context.Background(), c.auth, nil); err != nil {
|
2017-02-17 04:09:39 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
rawLocal, err = c.barrier.Get(context.Background(), coreLocalAuthConfigPath)
|
2017-02-17 04:09:39 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if rawLocal == nil {
|
|
|
|
t.Fatal("expected non-nil local credential")
|
|
|
|
}
|
|
|
|
localCredentialTable = &MountTable{}
|
|
|
|
if err := jsonutil.DecodeJSON(rawLocal.Value, localCredentialTable); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(localCredentialTable.Entries) != 1 {
|
|
|
|
t.Fatalf("expected one entry in local credential table, got %#v", localCredentialTable)
|
|
|
|
}
|
|
|
|
|
|
|
|
oldCredential := c.auth
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := c.loadCredentials(context.Background()); err != nil {
|
2017-02-17 04:09:39 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(oldCredential, c.auth) {
|
|
|
|
t.Fatalf("expected\n%#v\ngot\n%#v\n", oldCredential, c.auth)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(c.auth.Entries) != 2 {
|
|
|
|
t.Fatalf("expected two credential entries, got %#v", localCredentialTable)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-12 00:18:52 +00:00
|
|
|
func TestCore_EnableCredential_twice_409(t *testing.T) {
|
2020-10-27 15:24:43 +00:00
|
|
|
c, _, _, _ := TestCoreUnsealedWithMetrics(t)
|
2018-01-19 06:44:44 +00:00
|
|
|
c.credentialBackends["noop"] = func(context.Context, *logical.BackendConfig) (logical.Backend, error) {
|
2018-11-07 01:21:24 +00:00
|
|
|
return &NoopBackend{
|
|
|
|
BackendType: logical.TypeCredential,
|
|
|
|
}, nil
|
2015-08-12 00:18:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
me := &MountEntry{
|
2016-05-26 17:38:51 +00:00
|
|
|
Table: credentialTableType,
|
|
|
|
Path: "foo",
|
|
|
|
Type: "noop",
|
2015-08-12 00:18:52 +00:00
|
|
|
}
|
2018-11-05 16:11:32 +00:00
|
|
|
err := c.enableCredential(namespace.RootContext(nil), me)
|
2015-08-12 00:18:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-09-15 16:27:22 +00:00
|
|
|
// 2nd should be a 409 error
|
2018-11-05 16:11:32 +00:00
|
|
|
err2 := c.enableCredential(namespace.RootContext(nil), me)
|
2015-09-15 16:27:22 +00:00
|
|
|
switch err2.(type) {
|
|
|
|
case logical.HTTPCodedError:
|
|
|
|
if err2.(logical.HTTPCodedError).Code() != 409 {
|
|
|
|
t.Fatalf("invalid code given")
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
t.Fatalf("expected a different error type")
|
|
|
|
}
|
2015-08-12 00:18:52 +00:00
|
|
|
}
|
|
|
|
|
2015-03-19 17:39:47 +00:00
|
|
|
func TestCore_EnableCredential_Token(t *testing.T) {
|
2020-10-27 15:24:43 +00:00
|
|
|
c, _, _, _ := TestCoreUnsealedWithMetrics(t)
|
2015-03-19 17:39:47 +00:00
|
|
|
me := &MountEntry{
|
2016-05-26 17:38:51 +00:00
|
|
|
Table: credentialTableType,
|
|
|
|
Path: "foo",
|
|
|
|
Type: "token",
|
2015-03-19 17:39:47 +00:00
|
|
|
}
|
2018-11-05 16:11:32 +00:00
|
|
|
err := c.enableCredential(namespace.RootContext(nil), me)
|
2015-03-19 17:39:47 +00:00
|
|
|
if err.Error() != "token credential backend cannot be instantiated" {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCore_DisableCredential(t *testing.T) {
|
2020-10-27 15:24:43 +00:00
|
|
|
c, keys, _, _ := TestCoreUnsealedWithMetrics(t)
|
2018-01-19 06:44:44 +00:00
|
|
|
c.credentialBackends["noop"] = func(context.Context, *logical.BackendConfig) (logical.Backend, error) {
|
2018-11-07 01:21:24 +00:00
|
|
|
return &NoopBackend{
|
|
|
|
BackendType: logical.TypeCredential,
|
|
|
|
}, nil
|
2015-03-19 17:39:47 +00:00
|
|
|
}
|
|
|
|
|
2018-11-05 16:11:32 +00:00
|
|
|
err := c.disableCredential(namespace.RootContext(nil), "foo")
|
2018-09-18 03:03:00 +00:00
|
|
|
if err != nil && !strings.HasPrefix(err.Error(), "no matching mount") {
|
|
|
|
t.Fatal(err)
|
2015-03-19 17:39:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
me := &MountEntry{
|
2016-05-26 17:38:51 +00:00
|
|
|
Table: credentialTableType,
|
|
|
|
Path: "foo",
|
|
|
|
Type: "noop",
|
2015-03-19 17:39:47 +00:00
|
|
|
}
|
2018-11-05 16:11:32 +00:00
|
|
|
err = c.enableCredential(namespace.RootContext(nil), me)
|
2015-03-19 17:39:47 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-11-05 16:11:32 +00:00
|
|
|
err = c.disableCredential(namespace.RootContext(nil), "foo")
|
2017-07-13 17:57:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
2015-03-19 17:39:47 +00:00
|
|
|
}
|
|
|
|
|
2018-11-05 16:11:32 +00:00
|
|
|
match := c.router.MatchingMount(namespace.RootContext(nil), "auth/foo/bar")
|
2015-03-19 17:39:47 +00:00
|
|
|
if match != "" {
|
|
|
|
t.Fatalf("backend present")
|
|
|
|
}
|
|
|
|
|
2020-10-27 15:24:43 +00:00
|
|
|
inmemSink := metrics.NewInmemSink(1000000*time.Hour, 2000000*time.Hour)
|
2015-04-29 01:12:57 +00:00
|
|
|
conf := &CoreConfig{
|
2020-10-27 15:24:43 +00:00
|
|
|
Physical: c.physical,
|
|
|
|
DisableMlock: true,
|
|
|
|
BuiltinRegistry: NewMockBuiltinRegistry(),
|
|
|
|
MetricSink: metricsutil.NewClusterMetricSink("test-cluster", inmemSink),
|
|
|
|
MetricsHelper: metricsutil.NewMetricsHelper(inmemSink, false),
|
2015-04-29 01:12:57 +00:00
|
|
|
}
|
2015-03-19 17:39:47 +00:00
|
|
|
c2, err := NewCore(conf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-01-17 20:43:10 +00:00
|
|
|
for i, key := range keys {
|
|
|
|
unseal, err := TestCoreUnseal(c2, key)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if i+1 == len(keys) && !unseal {
|
|
|
|
t.Fatalf("should be unsealed")
|
|
|
|
}
|
2015-03-19 17:39:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify matching mount tables
|
|
|
|
if !reflect.DeepEqual(c.auth, c2.auth) {
|
|
|
|
t.Fatalf("mismatch: %v %v", c.auth, c2.auth)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCore_DisableCredential_Protected(t *testing.T) {
|
2020-10-27 15:24:43 +00:00
|
|
|
c, _, _, _ := TestCoreUnsealedWithMetrics(t)
|
2018-11-05 16:11:32 +00:00
|
|
|
err := c.disableCredential(namespace.RootContext(nil), "token")
|
2017-07-13 17:57:14 +00:00
|
|
|
if err.Error() != "token credential backend cannot be disabled" {
|
|
|
|
t.Fatalf("err: %v", err)
|
2015-03-19 17:39:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-03 23:15:34 +00:00
|
|
|
func TestCore_DisableCredential_Cleanup(t *testing.T) {
|
|
|
|
noop := &NoopBackend{
|
2018-11-07 01:21:24 +00:00
|
|
|
Login: []string{"login"},
|
|
|
|
BackendType: logical.TypeCredential,
|
2015-04-03 23:15:34 +00:00
|
|
|
}
|
2020-10-27 15:24:43 +00:00
|
|
|
c, _, _, _ := TestCoreUnsealedWithMetrics(t)
|
2018-01-19 06:44:44 +00:00
|
|
|
c.credentialBackends["noop"] = func(context.Context, *logical.BackendConfig) (logical.Backend, error) {
|
2015-04-03 23:15:34 +00:00
|
|
|
return noop, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
me := &MountEntry{
|
2016-05-26 17:38:51 +00:00
|
|
|
Table: credentialTableType,
|
|
|
|
Path: "foo",
|
|
|
|
Type: "noop",
|
2015-04-03 23:15:34 +00:00
|
|
|
}
|
2018-11-05 16:11:32 +00:00
|
|
|
err := c.enableCredential(namespace.RootContext(nil), me)
|
2015-04-03 23:15:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the view
|
2018-11-05 16:11:32 +00:00
|
|
|
view := c.router.MatchingStorageByAPIPath(namespace.RootContext(nil), "auth/foo/")
|
2015-04-03 23:15:34 +00:00
|
|
|
|
|
|
|
// Inject data
|
|
|
|
se := &logical.StorageEntry{
|
|
|
|
Key: "plstodelete",
|
|
|
|
Value: []byte("test"),
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := view.Put(context.Background(), se); err != nil {
|
2015-04-03 23:15:34 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate a new token auth
|
|
|
|
noop.Response = &logical.Response{
|
|
|
|
Auth: &logical.Auth{
|
|
|
|
Policies: []string{"foo"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
r := &logical.Request{
|
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "auth/foo/login",
|
|
|
|
}
|
2018-11-05 16:11:32 +00:00
|
|
|
resp, err := c.HandleRequest(namespace.RootContext(nil), r)
|
2015-04-03 23:15:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp.Auth.ClientToken == "" {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disable should cleanup
|
2018-11-05 16:11:32 +00:00
|
|
|
err = c.disableCredential(namespace.RootContext(nil), "foo")
|
2017-07-13 17:57:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
2015-04-03 23:15:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Token should be revoked
|
2018-11-05 16:11:32 +00:00
|
|
|
te, err := c.tokenStore.Lookup(namespace.RootContext(nil), resp.Auth.ClientToken)
|
2015-04-03 23:15:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if te != nil {
|
|
|
|
t.Fatalf("bad: %#v", te)
|
|
|
|
}
|
|
|
|
|
|
|
|
// View should be empty
|
2018-01-19 06:44:44 +00:00
|
|
|
out, err := logical.CollectKeys(context.Background(), view)
|
2015-04-03 23:15:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(out) != 0 {
|
|
|
|
t.Fatalf("bad: %#v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-19 02:36:17 +00:00
|
|
|
func TestDefaultAuthTable(t *testing.T) {
|
2020-10-27 15:24:43 +00:00
|
|
|
c, _, _, _ := TestCoreUnsealedWithMetrics(t)
|
2017-06-26 17:14:36 +00:00
|
|
|
table := c.defaultAuthTable()
|
2015-03-19 02:36:17 +00:00
|
|
|
verifyDefaultAuthTable(t, table)
|
|
|
|
}
|
|
|
|
|
2015-03-19 16:54:57 +00:00
|
|
|
func verifyDefaultAuthTable(t *testing.T, table *MountTable) {
|
2015-03-19 02:36:17 +00:00
|
|
|
if len(table.Entries) != 1 {
|
|
|
|
t.Fatalf("bad: %v", table.Entries)
|
|
|
|
}
|
2016-05-26 17:38:51 +00:00
|
|
|
if table.Type != credentialTableType {
|
|
|
|
t.Fatalf("bad: %v", *table)
|
|
|
|
}
|
2015-03-19 02:36:17 +00:00
|
|
|
for idx, entry := range table.Entries {
|
|
|
|
switch idx {
|
|
|
|
case 0:
|
2015-04-03 21:24:00 +00:00
|
|
|
if entry.Path != "token/" {
|
2015-03-19 02:36:17 +00:00
|
|
|
t.Fatalf("bad: %v", entry)
|
|
|
|
}
|
|
|
|
if entry.Type != "token" {
|
|
|
|
t.Fatalf("bad: %v", entry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if entry.Description == "" {
|
|
|
|
t.Fatalf("bad: %v", entry)
|
|
|
|
}
|
|
|
|
if entry.UUID == "" {
|
|
|
|
t.Fatalf("bad: %v", entry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-05 23:55:40 +00:00
|
|
|
|
|
|
|
func TestCore_CredentialInitialize(t *testing.T) {
|
|
|
|
{
|
|
|
|
backend := &InitializableBackend{
|
|
|
|
&NoopBackend{
|
|
|
|
BackendType: logical.TypeCredential,
|
|
|
|
}, false}
|
|
|
|
|
2020-10-27 15:24:43 +00:00
|
|
|
c, _, _, _ := TestCoreUnsealedWithMetrics(t)
|
2019-07-05 23:55:40 +00:00
|
|
|
c.credentialBackends["initable"] = func(context.Context, *logical.BackendConfig) (logical.Backend, error) {
|
|
|
|
return backend, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
me := &MountEntry{
|
|
|
|
Table: credentialTableType,
|
|
|
|
Path: "foo/",
|
|
|
|
Type: "initable",
|
|
|
|
}
|
|
|
|
err := c.enableCredential(namespace.RootContext(nil), me)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !backend.isInitialized {
|
|
|
|
t.Fatal("backend is not initialized")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
backend := &InitializableBackend{
|
|
|
|
&NoopBackend{
|
|
|
|
BackendType: logical.TypeCredential,
|
|
|
|
}, false}
|
|
|
|
|
2020-10-27 15:24:43 +00:00
|
|
|
c, _, _, _ := TestCoreUnsealedWithMetrics(t)
|
2019-07-05 23:55:40 +00:00
|
|
|
c.credentialBackends["initable"] = func(context.Context, *logical.BackendConfig) (logical.Backend, error) {
|
|
|
|
return backend, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
c.auth = &MountTable{
|
|
|
|
Type: credentialTableType,
|
|
|
|
Entries: []*MountEntry{
|
|
|
|
&MountEntry{
|
|
|
|
Table: credentialTableType,
|
|
|
|
Path: "foo/",
|
|
|
|
Type: "initable",
|
|
|
|
UUID: "abcd",
|
|
|
|
Accessor: "initable-abcd",
|
|
|
|
BackendAwareUUID: "abcde",
|
|
|
|
NamespaceID: namespace.RootNamespaceID,
|
|
|
|
namespace: namespace.RootNamespace,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := c.setupCredentials(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// run the postUnseal funcs, so that the backend will be inited
|
|
|
|
for _, f := range c.postUnsealFuncs {
|
|
|
|
f()
|
|
|
|
}
|
|
|
|
|
|
|
|
if !backend.isInitialized {
|
|
|
|
t.Fatal("backend is not initialized")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|