2017-10-11 17:21:20 +00:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2020-06-29 18:33:25 +00:00
|
|
|
"strings"
|
2017-10-11 17:21:20 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-06-29 18:33:25 +00:00
|
|
|
"github.com/armon/go-metrics"
|
2018-08-09 20:37:36 +00:00
|
|
|
"github.com/go-test/deep"
|
|
|
|
"github.com/golang/protobuf/ptypes"
|
2018-05-31 14:18:34 +00:00
|
|
|
uuid "github.com/hashicorp/go-uuid"
|
2017-10-11 17:21:20 +00:00
|
|
|
credGithub "github.com/hashicorp/vault/builtin/credential/github"
|
2018-08-09 20:37:36 +00:00
|
|
|
"github.com/hashicorp/vault/helper/identity"
|
2020-06-29 18:33:25 +00:00
|
|
|
"github.com/hashicorp/vault/helper/metricsutil"
|
2018-09-18 03:03:00 +00:00
|
|
|
"github.com/hashicorp/vault/helper/namespace"
|
2018-08-09 20:37:36 +00:00
|
|
|
"github.com/hashicorp/vault/helper/storagepacker"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2017-10-11 17:21:20 +00:00
|
|
|
)
|
|
|
|
|
2019-02-08 21:32:06 +00:00
|
|
|
func TestIdentityStore_UnsealingWhenConflictingAliasNames(t *testing.T) {
|
|
|
|
err := AddTestCredentialBackend("github", credGithub.Factory)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c, unsealKey, root := TestCoreUnsealed(t)
|
|
|
|
|
|
|
|
meGH := &MountEntry{
|
|
|
|
Table: credentialTableType,
|
|
|
|
Path: "github/",
|
|
|
|
Type: "github",
|
|
|
|
Description: "github auth",
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.enableCredential(namespace.RootContext(nil), meGH)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
alias := &identity.Alias{
|
|
|
|
ID: "alias1",
|
|
|
|
CanonicalID: "entity1",
|
|
|
|
MountType: "github",
|
|
|
|
MountAccessor: meGH.Accessor,
|
|
|
|
Name: "githubuser",
|
|
|
|
}
|
|
|
|
entity := &identity.Entity{
|
|
|
|
ID: "entity1",
|
|
|
|
Name: "name1",
|
|
|
|
Policies: []string{"foo", "bar"},
|
|
|
|
Aliases: []*identity.Alias{
|
|
|
|
alias,
|
|
|
|
},
|
|
|
|
NamespaceID: namespace.RootNamespaceID,
|
|
|
|
}
|
2019-05-07 19:29:51 +00:00
|
|
|
entity.BucketKey = c.identityStore.entityPacker.BucketKey(entity.ID)
|
2019-02-08 21:32:06 +00:00
|
|
|
|
|
|
|
err = c.identityStore.upsertEntity(namespace.RootContext(nil), entity, nil, true)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
alias2 := &identity.Alias{
|
|
|
|
ID: "alias2",
|
|
|
|
CanonicalID: "entity2",
|
|
|
|
MountType: "github",
|
|
|
|
MountAccessor: meGH.Accessor,
|
|
|
|
Name: "GITHUBUSER",
|
|
|
|
}
|
|
|
|
entity2 := &identity.Entity{
|
|
|
|
ID: "entity2",
|
|
|
|
Name: "name2",
|
|
|
|
Policies: []string{"foo", "bar"},
|
|
|
|
Aliases: []*identity.Alias{
|
|
|
|
alias2,
|
|
|
|
},
|
|
|
|
NamespaceID: namespace.RootNamespaceID,
|
|
|
|
}
|
2019-05-07 19:29:51 +00:00
|
|
|
entity2.BucketKey = c.identityStore.entityPacker.BucketKey(entity2.ID)
|
2019-02-08 21:32:06 +00:00
|
|
|
|
|
|
|
// Persist the second entity directly without the regular flow. This will skip
|
|
|
|
// merging of these enties.
|
|
|
|
entity2Any, err := ptypes.MarshalAny(entity2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
item := &storagepacker.Item{
|
|
|
|
ID: entity2.ID,
|
|
|
|
Message: entity2Any,
|
|
|
|
}
|
2019-05-01 17:47:41 +00:00
|
|
|
|
|
|
|
ctx := namespace.RootContext(nil)
|
|
|
|
if err = c.identityStore.entityPacker.PutItem(ctx, item); err != nil {
|
2019-02-08 21:32:06 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Seal and ensure that unseal works
|
|
|
|
if err = c.Seal(root); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var unsealed bool
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
unsealed, err = c.Unseal(unsealKey[i])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !unsealed {
|
|
|
|
t.Fatal("still sealed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-31 14:18:34 +00:00
|
|
|
func TestIdentityStore_EntityIDPassthrough(t *testing.T) {
|
|
|
|
// Enable GitHub auth and initialize
|
2018-11-05 16:11:32 +00:00
|
|
|
ctx := namespace.RootContext(nil)
|
2018-09-18 03:03:00 +00:00
|
|
|
is, ghAccessor, core := testIdentityStoreWithGithubAuth(ctx, t)
|
2018-05-31 14:18:34 +00:00
|
|
|
alias := &logical.Alias{
|
|
|
|
MountType: "github",
|
|
|
|
MountAccessor: ghAccessor,
|
|
|
|
Name: "githubuser",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an entity with GitHub alias
|
2018-09-18 03:03:00 +00:00
|
|
|
entity, err := is.CreateOrFetchEntity(ctx, alias)
|
2018-05-31 14:18:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if entity == nil {
|
|
|
|
t.Fatalf("expected a non-nil entity")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a token with the above created entity set on it
|
2018-06-08 21:24:27 +00:00
|
|
|
ent := &logical.TokenEntry{
|
2018-05-31 14:18:34 +00:00
|
|
|
ID: "testtokenid",
|
|
|
|
Path: "test",
|
|
|
|
Policies: []string{"root"},
|
|
|
|
CreationTime: time.Now().Unix(),
|
|
|
|
EntityID: entity.ID,
|
2018-09-18 03:03:00 +00:00
|
|
|
NamespaceID: namespace.RootNamespaceID,
|
2018-05-31 14:18:34 +00:00
|
|
|
}
|
2018-09-18 03:03:00 +00:00
|
|
|
if err := core.tokenStore.create(ctx, ent); err != nil {
|
2018-05-31 14:18:34 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set a request handler to the noop backend which responds with the entity
|
|
|
|
// ID received in the request object
|
|
|
|
requestHandler := func(ctx context.Context, req *logical.Request) (*logical.Response, error) {
|
|
|
|
return &logical.Response{
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"entity_id": req.EntityID,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
noop := &NoopBackend{
|
|
|
|
RequestHandler: requestHandler,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mount the noop backend
|
|
|
|
_, barrier, _ := mockBarrier(t)
|
|
|
|
view := NewBarrierView(barrier, "logical/")
|
|
|
|
meUUID, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-09-18 03:03:00 +00:00
|
|
|
err = core.router.Mount(noop, "test/backend/", &MountEntry{Path: "test/backend/", Type: "noop", UUID: meUUID, Accessor: "noop-accessor", namespace: namespace.RootNamespace}, view)
|
2018-05-31 14:18:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the request with the above created token
|
2018-09-18 03:03:00 +00:00
|
|
|
resp, err := core.HandleRequest(ctx, &logical.Request{
|
2018-05-31 14:18:34 +00:00
|
|
|
ClientToken: "testtokenid",
|
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "test/backend/foo",
|
|
|
|
})
|
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("bad: resp: %#v\n err: %v", resp, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expected entity ID to be in the response
|
|
|
|
if resp.Data["entity_id"] != entity.ID {
|
|
|
|
t.Fatalf("expected entity ID to be passed through to the backend")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-09 15:40:56 +00:00
|
|
|
func TestIdentityStore_CreateOrFetchEntity(t *testing.T) {
|
2018-09-18 03:03:00 +00:00
|
|
|
ctx := namespace.RootContext(nil)
|
|
|
|
is, ghAccessor, _ := testIdentityStoreWithGithubAuth(ctx, t)
|
2017-10-11 17:21:20 +00:00
|
|
|
alias := &logical.Alias{
|
|
|
|
MountType: "github",
|
|
|
|
MountAccessor: ghAccessor,
|
|
|
|
Name: "githubuser",
|
2019-01-23 16:26:50 +00:00
|
|
|
Metadata: map[string]string{
|
|
|
|
"foo": "a",
|
|
|
|
},
|
2017-10-11 17:21:20 +00:00
|
|
|
}
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
entity, err := is.CreateOrFetchEntity(ctx, alias)
|
2017-10-11 17:21:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if entity == nil {
|
|
|
|
t.Fatalf("expected a non-nil entity")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(entity.Aliases) != 1 {
|
|
|
|
t.Fatalf("bad: length of aliases; expected: 1, actual: %d", len(entity.Aliases))
|
|
|
|
}
|
|
|
|
|
|
|
|
if entity.Aliases[0].Name != alias.Name {
|
|
|
|
t.Fatalf("bad: alias name; expected: %q, actual: %q", alias.Name, entity.Aliases[0].Name)
|
|
|
|
}
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
entity, err = is.CreateOrFetchEntity(ctx, alias)
|
2018-02-09 15:40:56 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if entity == nil {
|
|
|
|
t.Fatalf("expected a non-nil entity")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(entity.Aliases) != 1 {
|
|
|
|
t.Fatalf("bad: length of aliases; expected: 1, actual: %d", len(entity.Aliases))
|
|
|
|
}
|
|
|
|
|
|
|
|
if entity.Aliases[0].Name != alias.Name {
|
|
|
|
t.Fatalf("bad: alias name; expected: %q, actual: %q", alias.Name, entity.Aliases[0].Name)
|
2017-10-11 17:21:20 +00:00
|
|
|
}
|
2019-01-23 16:26:50 +00:00
|
|
|
if diff := deep.Equal(entity.Aliases[0].Metadata, map[string]string{"foo": "a"}); diff != nil {
|
|
|
|
t.Fatal(diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a new alias to the entity and verify its existence
|
|
|
|
registerReq := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "entity-alias",
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"name": "githubuser2",
|
|
|
|
"canonical_id": entity.ID,
|
|
|
|
"mount_accessor": ghAccessor,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := is.HandleRequest(ctx, registerReq)
|
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%v resp:%#v", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
entity, err = is.CreateOrFetchEntity(ctx, alias)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if entity == nil {
|
|
|
|
t.Fatalf("expected a non-nil entity")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(entity.Aliases) != 2 {
|
|
|
|
t.Fatalf("bad: length of aliases; expected: 2, actual: %d", len(entity.Aliases))
|
|
|
|
}
|
|
|
|
|
|
|
|
if entity.Aliases[1].Name != "githubuser2" {
|
|
|
|
t.Fatalf("bad: alias name; expected: %q, actual: %q", alias.Name, "githubuser2")
|
|
|
|
}
|
|
|
|
|
|
|
|
if diff := deep.Equal(entity.Aliases[1].Metadata, map[string]string(nil)); diff != nil {
|
|
|
|
t.Fatal(diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change the metadata of an existing alias and verify that
|
|
|
|
// a the change takes effect only for the target alias.
|
|
|
|
alias.Metadata = map[string]string{
|
|
|
|
"foo": "zzzz",
|
|
|
|
}
|
|
|
|
|
|
|
|
entity, err = is.CreateOrFetchEntity(ctx, alias)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if entity == nil {
|
|
|
|
t.Fatalf("expected a non-nil entity")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(entity.Aliases) != 2 {
|
|
|
|
t.Fatalf("bad: length of aliases; expected: 2, actual: %d", len(entity.Aliases))
|
|
|
|
}
|
|
|
|
|
|
|
|
if diff := deep.Equal(entity.Aliases[0].Metadata, map[string]string{"foo": "zzzz"}); diff != nil {
|
|
|
|
t.Fatal(diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
if diff := deep.Equal(entity.Aliases[1].Metadata, map[string]string(nil)); diff != nil {
|
|
|
|
t.Fatal(diff)
|
|
|
|
}
|
2017-10-11 17:21:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIdentityStore_EntityByAliasFactors(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
var resp *logical.Response
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
ctx := namespace.RootContext(nil)
|
|
|
|
is, ghAccessor, _ := testIdentityStoreWithGithubAuth(ctx, t)
|
2017-10-11 17:21:20 +00:00
|
|
|
|
|
|
|
registerData := map[string]interface{}{
|
|
|
|
"name": "testentityname",
|
|
|
|
"metadata": []string{"someusefulkey=someusefulvalue"},
|
|
|
|
"policies": []string{"testpolicy1", "testpolicy2"},
|
|
|
|
}
|
|
|
|
|
|
|
|
registerReq := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "entity",
|
|
|
|
Data: registerData,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register the entity
|
2018-09-18 03:03:00 +00:00
|
|
|
resp, err = is.HandleRequest(ctx, registerReq)
|
2017-10-11 17:21:20 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%v resp:%#v", err, resp)
|
|
|
|
}
|
|
|
|
idRaw, ok := resp.Data["id"]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("entity id not present in response")
|
|
|
|
}
|
|
|
|
entityID := idRaw.(string)
|
|
|
|
if entityID == "" {
|
|
|
|
t.Fatalf("invalid entity id")
|
|
|
|
}
|
|
|
|
|
|
|
|
aliasData := map[string]interface{}{
|
|
|
|
"entity_id": entityID,
|
|
|
|
"name": "alias_name",
|
|
|
|
"mount_accessor": ghAccessor,
|
|
|
|
}
|
|
|
|
aliasReq := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "alias",
|
|
|
|
Data: aliasData,
|
|
|
|
}
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
resp, err = is.HandleRequest(ctx, aliasReq)
|
2017-10-11 17:21:20 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%v resp:%#v", err, resp)
|
|
|
|
}
|
|
|
|
if resp == nil {
|
|
|
|
t.Fatalf("expected a non-nil response")
|
|
|
|
}
|
|
|
|
|
2017-11-02 20:05:48 +00:00
|
|
|
entity, err := is.entityByAliasFactors(ghAccessor, "alias_name", false)
|
2017-10-11 17:21:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if entity == nil {
|
|
|
|
t.Fatalf("expected a non-nil entity")
|
|
|
|
}
|
|
|
|
if entity.ID != entityID {
|
|
|
|
t.Fatalf("bad: entity ID; expected: %q actual: %q", entityID, entity.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIdentityStore_WrapInfoInheritance(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
var resp *logical.Response
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
ctx := namespace.RootContext(nil)
|
|
|
|
core, is, ts, _ := testCoreWithIdentityTokenGithub(ctx, t)
|
2017-10-11 17:21:20 +00:00
|
|
|
|
|
|
|
registerData := map[string]interface{}{
|
|
|
|
"name": "testentityname",
|
|
|
|
"metadata": []string{"someusefulkey=someusefulvalue"},
|
|
|
|
"policies": []string{"testpolicy1", "testpolicy2"},
|
|
|
|
}
|
|
|
|
|
|
|
|
registerReq := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "entity",
|
|
|
|
Data: registerData,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register the entity
|
2018-09-18 03:03:00 +00:00
|
|
|
resp, err = is.HandleRequest(ctx, registerReq)
|
2017-10-11 17:21:20 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%v resp:%#v", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
idRaw, ok := resp.Data["id"]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("entity id not present in response")
|
|
|
|
}
|
|
|
|
entityID := idRaw.(string)
|
|
|
|
if entityID == "" {
|
|
|
|
t.Fatalf("invalid entity id")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a token which has EntityID set and has update permissions to
|
|
|
|
// sys/wrapping/wrap
|
2018-06-08 21:24:27 +00:00
|
|
|
te := &logical.TokenEntry{
|
2017-10-11 17:21:20 +00:00
|
|
|
Path: "test",
|
|
|
|
Policies: []string{"default", responseWrappingPolicyName},
|
|
|
|
EntityID: entityID,
|
2018-06-03 22:14:51 +00:00
|
|
|
TTL: time.Hour,
|
2017-10-11 17:21:20 +00:00
|
|
|
}
|
2018-06-03 22:14:51 +00:00
|
|
|
testMakeTokenDirectly(t, ts, te)
|
2017-10-11 17:21:20 +00:00
|
|
|
|
|
|
|
wrapReq := &logical.Request{
|
|
|
|
Path: "sys/wrapping/wrap",
|
|
|
|
ClientToken: te.ID,
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
WrapInfo: &logical.RequestWrapInfo{
|
|
|
|
TTL: time.Duration(5 * time.Second),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
resp, err = core.HandleRequest(ctx, wrapReq)
|
2017-10-11 17:21:20 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("bad: resp: %#v, err: %v", resp, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.WrapInfo == nil {
|
|
|
|
t.Fatalf("expected a non-nil WrapInfo")
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.WrapInfo.WrappedEntityID != entityID {
|
|
|
|
t.Fatalf("bad: WrapInfo in response not having proper entity ID set; expected: %q, actual:%q", entityID, resp.WrapInfo.WrappedEntityID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIdentityStore_TokenEntityInheritance(t *testing.T) {
|
2018-06-03 22:14:51 +00:00
|
|
|
c, _, _ := TestCoreUnsealed(t)
|
|
|
|
ts := c.tokenStore
|
2017-10-11 17:21:20 +00:00
|
|
|
|
|
|
|
// Create a token which has EntityID set
|
2018-06-08 21:24:27 +00:00
|
|
|
te := &logical.TokenEntry{
|
2017-10-11 17:21:20 +00:00
|
|
|
Path: "test",
|
|
|
|
Policies: []string{"dev", "prod"},
|
|
|
|
EntityID: "testentityid",
|
2018-06-03 22:14:51 +00:00
|
|
|
TTL: time.Hour,
|
2017-10-11 17:21:20 +00:00
|
|
|
}
|
2018-06-03 22:14:51 +00:00
|
|
|
testMakeTokenDirectly(t, ts, te)
|
2017-10-11 17:21:20 +00:00
|
|
|
|
|
|
|
// Create a child token; this should inherit the EntityID
|
|
|
|
tokenReq := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "create",
|
|
|
|
ClientToken: te.ID,
|
|
|
|
}
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
ctx := namespace.RootContext(nil)
|
|
|
|
resp, err := ts.HandleRequest(ctx, tokenReq)
|
2017-10-11 17:21:20 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("bad: resp: %#v err: %v", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.Auth.EntityID != te.EntityID {
|
|
|
|
t.Fatalf("bad: entity ID; expected: %v, actual: %v", te.EntityID, resp.Auth.EntityID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an orphan token; this should not inherit the EntityID
|
|
|
|
tokenReq.Path = "create-orphan"
|
2018-09-18 03:03:00 +00:00
|
|
|
resp, err = ts.HandleRequest(ctx, tokenReq)
|
2017-10-11 17:21:20 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("bad: resp: %#v err: %v", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.Auth.EntityID != "" {
|
|
|
|
t.Fatalf("expected entity ID to be not set")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-09 20:37:36 +00:00
|
|
|
func TestIdentityStore_MergeConflictingAliases(t *testing.T) {
|
|
|
|
err := AddTestCredentialBackend("github", credGithub.Factory)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2019-02-08 21:32:06 +00:00
|
|
|
c, _, _ := TestCoreUnsealed(t)
|
2018-08-09 20:37:36 +00:00
|
|
|
|
|
|
|
meGH := &MountEntry{
|
|
|
|
Table: credentialTableType,
|
|
|
|
Path: "github/",
|
|
|
|
Type: "github",
|
|
|
|
Description: "github auth",
|
|
|
|
}
|
|
|
|
|
2018-11-05 16:11:32 +00:00
|
|
|
err = c.enableCredential(namespace.RootContext(nil), meGH)
|
2018-08-09 20:37:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
alias := &identity.Alias{
|
|
|
|
ID: "alias1",
|
|
|
|
CanonicalID: "entity1",
|
|
|
|
MountType: "github",
|
|
|
|
MountAccessor: meGH.Accessor,
|
|
|
|
Name: "githubuser",
|
|
|
|
}
|
|
|
|
entity := &identity.Entity{
|
|
|
|
ID: "entity1",
|
|
|
|
Name: "name1",
|
|
|
|
Policies: []string{"foo", "bar"},
|
|
|
|
Aliases: []*identity.Alias{
|
|
|
|
alias,
|
|
|
|
},
|
2019-02-08 21:32:06 +00:00
|
|
|
NamespaceID: namespace.RootNamespaceID,
|
2018-08-09 20:37:36 +00:00
|
|
|
}
|
2019-05-07 19:29:51 +00:00
|
|
|
entity.BucketKey = c.identityStore.entityPacker.BucketKey(entity.ID)
|
2019-02-08 21:32:06 +00:00
|
|
|
err = c.identityStore.upsertEntity(namespace.RootContext(nil), entity, nil, true)
|
2018-08-09 20:37:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-02-08 21:32:06 +00:00
|
|
|
alias2 := &identity.Alias{
|
|
|
|
ID: "alias2",
|
|
|
|
CanonicalID: "entity2",
|
|
|
|
MountType: "github",
|
|
|
|
MountAccessor: meGH.Accessor,
|
|
|
|
Name: "githubuser",
|
2018-08-09 20:37:36 +00:00
|
|
|
}
|
2019-02-08 21:32:06 +00:00
|
|
|
entity2 := &identity.Entity{
|
|
|
|
ID: "entity2",
|
|
|
|
Name: "name2",
|
|
|
|
Policies: []string{"bar", "baz"},
|
|
|
|
Aliases: []*identity.Alias{
|
|
|
|
alias2,
|
|
|
|
},
|
|
|
|
NamespaceID: namespace.RootNamespaceID,
|
2018-08-09 20:37:36 +00:00
|
|
|
}
|
|
|
|
|
2019-05-07 19:29:51 +00:00
|
|
|
entity2.BucketKey = c.identityStore.entityPacker.BucketKey(entity2.ID)
|
2018-08-09 20:37:36 +00:00
|
|
|
|
2019-02-08 21:32:06 +00:00
|
|
|
err = c.identityStore.upsertEntity(namespace.RootContext(nil), entity2, nil, true)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2018-08-09 20:37:36 +00:00
|
|
|
}
|
|
|
|
|
2018-11-05 16:11:32 +00:00
|
|
|
newEntity, err := c.identityStore.CreateOrFetchEntity(namespace.RootContext(nil), &logical.Alias{
|
2018-08-09 20:37:36 +00:00
|
|
|
MountAccessor: meGH.Accessor,
|
|
|
|
MountType: "github",
|
|
|
|
Name: "githubuser",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if newEntity == nil {
|
|
|
|
t.Fatal("nil new entity")
|
|
|
|
}
|
|
|
|
|
|
|
|
entityToUse := "entity1"
|
|
|
|
if newEntity.ID == "entity1" {
|
|
|
|
entityToUse = "entity2"
|
|
|
|
}
|
|
|
|
if len(newEntity.MergedEntityIDs) != 1 || newEntity.MergedEntityIDs[0] != entityToUse {
|
|
|
|
t.Fatalf("bad merged entity ids: %v", newEntity.MergedEntityIDs)
|
|
|
|
}
|
|
|
|
if diff := deep.Equal(newEntity.Policies, []string{"bar", "baz", "foo"}); diff != nil {
|
|
|
|
t.Fatal(diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
newEntity, err = c.identityStore.MemDBEntityByID(entityToUse, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if newEntity != nil {
|
|
|
|
t.Fatal("got a non-nil entity")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
func testCoreWithIdentityTokenGithub(ctx context.Context, t *testing.T) (*Core, *IdentityStore, *TokenStore, string) {
|
|
|
|
is, ghAccessor, core := testIdentityStoreWithGithubAuth(ctx, t)
|
2018-06-03 22:14:51 +00:00
|
|
|
return core, is, core.tokenStore, ghAccessor
|
2017-10-11 17:21:20 +00:00
|
|
|
}
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
func testCoreWithIdentityTokenGithubRoot(ctx context.Context, t *testing.T) (*Core, *IdentityStore, *TokenStore, string, string) {
|
|
|
|
is, ghAccessor, core, root := testIdentityStoreWithGithubAuthRoot(ctx, t)
|
2018-06-03 22:14:51 +00:00
|
|
|
return core, is, core.tokenStore, ghAccessor, root
|
2017-11-13 20:31:32 +00:00
|
|
|
}
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
func testIdentityStoreWithGithubAuth(ctx context.Context, t *testing.T) (*IdentityStore, string, *Core) {
|
|
|
|
is, ghA, c, _ := testIdentityStoreWithGithubAuthRoot(ctx, t)
|
2017-11-13 20:31:32 +00:00
|
|
|
return is, ghA, c
|
|
|
|
}
|
|
|
|
|
2018-06-19 16:57:19 +00:00
|
|
|
// testIdentityStoreWithGithubAuthRoot returns an instance of identity store
|
|
|
|
// which is mounted by default. This function also enables the github auth
|
|
|
|
// backend to assist with testing aliases and entities that require an valid
|
|
|
|
// mount accessor of an auth backend.
|
2018-09-18 03:03:00 +00:00
|
|
|
func testIdentityStoreWithGithubAuthRoot(ctx context.Context, t *testing.T) (*IdentityStore, string, *Core, string) {
|
2017-10-11 17:21:20 +00:00
|
|
|
// Add github credential factory to core config
|
|
|
|
err := AddTestCredentialBackend("github", credGithub.Factory)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-11-13 20:31:32 +00:00
|
|
|
c, _, root := TestCoreUnsealed(t)
|
2017-10-11 17:21:20 +00:00
|
|
|
|
|
|
|
meGH := &MountEntry{
|
|
|
|
Table: credentialTableType,
|
|
|
|
Path: "github/",
|
|
|
|
Type: "github",
|
|
|
|
Description: "github auth",
|
|
|
|
}
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
err = c.enableCredential(ctx, meGH)
|
2017-10-11 17:21:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-06-19 16:57:19 +00:00
|
|
|
return c.identityStore, meGH.Accessor, c, root
|
2017-10-11 17:21:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIdentityStore_MetadataKeyRegex(t *testing.T) {
|
|
|
|
key := "validVALID012_-=+/"
|
|
|
|
|
|
|
|
if !metaKeyFormatRegEx(key) {
|
|
|
|
t.Fatal("failed to accept valid metadata key")
|
|
|
|
}
|
|
|
|
|
|
|
|
key = "a:b"
|
|
|
|
if metaKeyFormatRegEx(key) {
|
|
|
|
t.Fatal("accepted invalid metadata key")
|
|
|
|
}
|
|
|
|
}
|
2020-06-29 18:33:25 +00:00
|
|
|
|
|
|
|
func expectSingleCount(t *testing.T, sink *metrics.InmemSink, keyPrefix string) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
intervals := sink.Data()
|
|
|
|
// Test crossed an interval boundary, don't try to deal with it.
|
|
|
|
if len(intervals) > 1 {
|
|
|
|
t.Skip("Detected interval crossing.")
|
|
|
|
}
|
|
|
|
|
|
|
|
var counter *metrics.SampledValue = nil
|
|
|
|
|
|
|
|
for _, c := range intervals[0].Counters {
|
|
|
|
if strings.HasPrefix(c.Name, keyPrefix) {
|
|
|
|
counter = &c
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if counter == nil {
|
|
|
|
t.Fatalf("No %q counter found.", keyPrefix)
|
|
|
|
}
|
|
|
|
|
|
|
|
if counter.Count != 1 {
|
|
|
|
t.Errorf("Counter number of samples %v is not 1.", counter.Count)
|
|
|
|
}
|
|
|
|
|
|
|
|
if counter.Sum != 1.0 {
|
|
|
|
t.Errorf("Counter sum %v is not 1.", counter.Sum)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIdentityStore_NewEntityCounter(t *testing.T) {
|
|
|
|
// Add github credential factory to core config
|
|
|
|
err := AddTestCredentialBackend("github", credGithub.Factory)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c := TestCore(t)
|
|
|
|
|
|
|
|
// Replace metricSink before unsealing
|
|
|
|
// This results in some duplication from the normal test setup function.
|
|
|
|
inmemSink := metrics.NewInmemSink(
|
|
|
|
1000000*time.Hour,
|
|
|
|
2000000*time.Hour)
|
|
|
|
c.metricSink = metricsutil.NewClusterMetricSink("test-cluster", inmemSink)
|
|
|
|
|
|
|
|
testCoreUnsealed(t, c)
|
|
|
|
|
|
|
|
meGH := &MountEntry{
|
|
|
|
Table: credentialTableType,
|
|
|
|
Path: "github/",
|
|
|
|
Type: "github",
|
|
|
|
Description: "github auth",
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := namespace.RootContext(nil)
|
|
|
|
err = c.enableCredential(ctx, meGH)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
is := c.identityStore
|
|
|
|
ghAccessor := meGH.Accessor
|
|
|
|
|
|
|
|
alias := &logical.Alias{
|
|
|
|
MountType: "github",
|
|
|
|
MountAccessor: ghAccessor,
|
|
|
|
Name: "githubuser",
|
|
|
|
Metadata: map[string]string{
|
|
|
|
"foo": "a",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = is.CreateOrFetchEntity(ctx, alias)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectSingleCount(t, inmemSink, "identity.entity.creation")
|
|
|
|
|
|
|
|
_, err = is.CreateOrFetchEntity(ctx, alias)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectSingleCount(t, inmemSink, "identity.entity.creation")
|
|
|
|
}
|