2017-10-11 17:21:20 +00:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/vault/helper/identity"
|
2018-09-18 03:03:00 +00:00
|
|
|
"github.com/hashicorp/vault/helper/namespace"
|
2017-10-11 17:21:20 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
)
|
|
|
|
|
|
|
|
// This test is required because MemDB does not take care of ensuring
|
|
|
|
// uniqueness of indexes that are marked unique.
|
|
|
|
func TestIdentityStore_AliasSameAliasNames(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
var resp *logical.Response
|
2018-09-18 03:03:00 +00:00
|
|
|
|
|
|
|
ctx := namespace.RootContext(nil)
|
|
|
|
is, githubAccessor, _ := testIdentityStoreWithGithubAuth(ctx, t)
|
2017-10-11 17:21:20 +00:00
|
|
|
|
|
|
|
aliasData := map[string]interface{}{
|
|
|
|
"name": "testaliasname",
|
|
|
|
"mount_accessor": githubAccessor,
|
|
|
|
}
|
|
|
|
|
|
|
|
aliasReq := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
2017-11-14 01:59:42 +00:00
|
|
|
Path: "entity-alias",
|
2017-10-11 17:21:20 +00:00
|
|
|
Data: aliasData,
|
|
|
|
}
|
|
|
|
|
2017-11-02 20:05:48 +00:00
|
|
|
// Register an alias
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register another alias with same name
|
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 {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if resp == nil || !resp.IsError() {
|
|
|
|
t.Fatalf("expected an error due to alias name not being unique")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIdentityStore_MemDBAliasIndexes(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
ctx := namespace.RootContext(nil)
|
|
|
|
is, githubAccessor, _ := testIdentityStoreWithGithubAuth(ctx, t)
|
2017-10-11 17:21:20 +00:00
|
|
|
if is == nil {
|
|
|
|
t.Fatal("failed to create test identity store")
|
|
|
|
}
|
|
|
|
|
2018-04-03 02:17:33 +00:00
|
|
|
validateMountResp := is.core.router.validateMountByAccessor(githubAccessor)
|
2017-10-11 17:21:20 +00:00
|
|
|
if validateMountResp == nil {
|
|
|
|
t.Fatal("failed to validate github auth mount")
|
|
|
|
}
|
|
|
|
|
|
|
|
entity := &identity.Entity{
|
|
|
|
ID: "testentityid",
|
|
|
|
Name: "testentityname",
|
|
|
|
}
|
|
|
|
|
|
|
|
entity.BucketKeyHash = is.entityPacker.BucketKeyHashByItemID(entity.ID)
|
|
|
|
|
2018-06-24 11:45:53 +00:00
|
|
|
txn := is.db.Txn(true)
|
|
|
|
defer txn.Abort()
|
|
|
|
err = is.MemDBUpsertEntityInTxn(txn, entity)
|
2017-10-11 17:21:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-06-24 11:45:53 +00:00
|
|
|
txn.Commit()
|
2017-10-11 17:21:20 +00:00
|
|
|
|
|
|
|
alias := &identity.Alias{
|
2017-11-02 20:05:48 +00:00
|
|
|
CanonicalID: entity.ID,
|
2017-10-11 17:21:20 +00:00
|
|
|
ID: "testaliasid",
|
|
|
|
MountAccessor: githubAccessor,
|
|
|
|
MountType: validateMountResp.MountType,
|
|
|
|
Name: "testaliasname",
|
|
|
|
Metadata: map[string]string{
|
|
|
|
"testkey1": "testmetadatavalue1",
|
|
|
|
"testkey2": "testmetadatavalue2",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-06-24 11:45:53 +00:00
|
|
|
txn = is.db.Txn(true)
|
|
|
|
defer txn.Abort()
|
|
|
|
err = is.MemDBUpsertAliasInTxn(txn, alias, false)
|
2017-10-11 17:21:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-06-24 11:45:53 +00:00
|
|
|
txn.Commit()
|
2017-10-11 17:21:20 +00:00
|
|
|
|
2017-11-02 20:05:48 +00:00
|
|
|
aliasFetched, err := is.MemDBAliasByID("testaliasid", false, false)
|
2017-10-11 17:21:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(alias, aliasFetched) {
|
|
|
|
t.Fatalf("bad: mismatched aliases; expected: %#v\n actual: %#v\n", alias, aliasFetched)
|
|
|
|
}
|
|
|
|
|
2017-11-02 20:05:48 +00:00
|
|
|
aliasFetched, err = is.MemDBAliasByFactors(validateMountResp.MountAccessor, "testaliasname", false, false)
|
2017-10-11 17:21:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(alias, aliasFetched) {
|
|
|
|
t.Fatalf("bad: mismatched aliases; expected: %#v\n actual: %#v\n", alias, aliasFetched)
|
|
|
|
}
|
|
|
|
|
|
|
|
alias2 := &identity.Alias{
|
2017-11-02 20:05:48 +00:00
|
|
|
CanonicalID: entity.ID,
|
2017-10-11 17:21:20 +00:00
|
|
|
ID: "testaliasid2",
|
|
|
|
MountAccessor: validateMountResp.MountAccessor,
|
|
|
|
MountType: validateMountResp.MountType,
|
|
|
|
Name: "testaliasname2",
|
|
|
|
Metadata: map[string]string{
|
|
|
|
"testkey1": "testmetadatavalue1",
|
|
|
|
"testkey3": "testmetadatavalue3",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-06-24 11:45:53 +00:00
|
|
|
txn = is.db.Txn(true)
|
|
|
|
defer txn.Abort()
|
|
|
|
err = is.MemDBUpsertAliasInTxn(txn, alias2, false)
|
2017-10-11 17:21:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-06-24 11:45:53 +00:00
|
|
|
err = is.MemDBDeleteAliasByIDInTxn(txn, "testaliasid", false)
|
2017-10-11 17:21:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-06-24 11:45:53 +00:00
|
|
|
txn.Commit()
|
2017-10-11 17:21:20 +00:00
|
|
|
|
2017-11-02 20:05:48 +00:00
|
|
|
aliasFetched, err = is.MemDBAliasByID("testaliasid", false, false)
|
2017-10-11 17:21:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if aliasFetched != nil {
|
|
|
|
t.Fatalf("expected a nil alias")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIdentityStore_AliasRegister(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
var resp *logical.Response
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
ctx := namespace.RootContext(nil)
|
|
|
|
is, githubAccessor, _ := testIdentityStoreWithGithubAuth(ctx, t)
|
2017-10-11 17:21:20 +00:00
|
|
|
|
|
|
|
if is == nil {
|
|
|
|
t.Fatal("failed to create test alias store")
|
|
|
|
}
|
|
|
|
|
|
|
|
aliasData := map[string]interface{}{
|
|
|
|
"name": "testaliasname",
|
|
|
|
"mount_accessor": githubAccessor,
|
|
|
|
"metadata": []string{"organization=hashicorp", "team=vault"},
|
|
|
|
}
|
|
|
|
|
|
|
|
aliasReq := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
2017-11-14 01:59:42 +00:00
|
|
|
Path: "entity-alias",
|
2017-10-11 17:21:20 +00:00
|
|
|
Data: aliasData,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register the alias
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
idRaw, ok := resp.Data["id"]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("alias id not present in alias register response")
|
|
|
|
}
|
|
|
|
|
|
|
|
id := idRaw.(string)
|
|
|
|
if id == "" {
|
|
|
|
t.Fatalf("invalid alias id in alias register response")
|
|
|
|
}
|
|
|
|
|
2017-11-02 20:05:48 +00:00
|
|
|
entityIDRaw, ok := resp.Data["canonical_id"]
|
2017-10-11 17:21:20 +00:00
|
|
|
if !ok {
|
|
|
|
t.Fatalf("entity id not present in alias register response")
|
|
|
|
}
|
|
|
|
|
|
|
|
entityID := entityIDRaw.(string)
|
|
|
|
if entityID == "" {
|
|
|
|
t.Fatalf("invalid entity id in alias register response")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIdentityStore_AliasUpdate(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
var resp *logical.Response
|
2018-09-18 03:03:00 +00:00
|
|
|
ctx := namespace.RootContext(nil)
|
|
|
|
is, githubAccessor, _ := testIdentityStoreWithGithubAuth(ctx, t)
|
2017-10-11 17:21:20 +00:00
|
|
|
|
|
|
|
aliasData := map[string]interface{}{
|
|
|
|
"name": "testaliasname",
|
|
|
|
"mount_accessor": githubAccessor,
|
|
|
|
}
|
|
|
|
|
|
|
|
aliasReq := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
2017-11-14 01:59:42 +00:00
|
|
|
Path: "entity-alias",
|
2017-10-11 17:21:20 +00:00
|
|
|
Data: aliasData,
|
|
|
|
}
|
|
|
|
|
2017-11-02 20:05:48 +00:00
|
|
|
// This will create an alias and a corresponding entity
|
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)
|
|
|
|
}
|
|
|
|
aliasID := resp.Data["id"].(string)
|
|
|
|
|
|
|
|
updateData := map[string]interface{}{
|
|
|
|
"name": "updatedaliasname",
|
|
|
|
"mount_accessor": githubAccessor,
|
|
|
|
}
|
|
|
|
|
|
|
|
aliasReq.Data = updateData
|
2017-11-14 01:59:42 +00:00
|
|
|
aliasReq.Path = "entity-alias/id/" + aliasID
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
aliasReq.Operation = logical.ReadOperation
|
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)
|
|
|
|
}
|
|
|
|
|
2018-07-25 02:01:58 +00:00
|
|
|
if resp.Data["name"] != "updatedaliasname" {
|
2017-10-11 17:21:20 +00:00
|
|
|
t.Fatalf("failed to update alias information; \n response data: %#v\n", resp.Data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIdentityStore_AliasUpdate_ByID(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
var resp *logical.Response
|
2018-09-18 03:03:00 +00:00
|
|
|
ctx := namespace.RootContext(nil)
|
|
|
|
is, githubAccessor, _ := testIdentityStoreWithGithubAuth(ctx, t)
|
2017-10-11 17:21:20 +00:00
|
|
|
|
|
|
|
updateData := map[string]interface{}{
|
|
|
|
"name": "updatedaliasname",
|
|
|
|
"mount_accessor": githubAccessor,
|
|
|
|
}
|
|
|
|
|
|
|
|
updateReq := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
2017-11-14 01:59:42 +00:00
|
|
|
Path: "entity-alias/id/invalidaliasid",
|
2017-10-11 17:21:20 +00:00
|
|
|
Data: updateData,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to update an non-existent alias
|
2018-09-18 03:03:00 +00:00
|
|
|
resp, err = is.HandleRequest(ctx, updateReq)
|
2017-10-11 17:21:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if resp == nil || !resp.IsError() {
|
|
|
|
t.Fatalf("expected an error due to invalid alias id")
|
|
|
|
}
|
|
|
|
|
|
|
|
registerData := map[string]interface{}{
|
|
|
|
"name": "testaliasname",
|
|
|
|
"mount_accessor": githubAccessor,
|
|
|
|
}
|
|
|
|
|
|
|
|
registerReq := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
2017-11-14 01:59:42 +00:00
|
|
|
Path: "entity-alias",
|
2017-10-11 17:21:20 +00:00
|
|
|
Data: registerData,
|
|
|
|
}
|
|
|
|
|
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("alias id not present in response")
|
|
|
|
}
|
|
|
|
id := idRaw.(string)
|
|
|
|
if id == "" {
|
|
|
|
t.Fatalf("invalid alias id")
|
|
|
|
}
|
|
|
|
|
2017-11-14 01:59:42 +00:00
|
|
|
updateReq.Path = "entity-alias/id/" + id
|
2018-09-18 03:03:00 +00:00
|
|
|
resp, err = is.HandleRequest(ctx, updateReq)
|
2017-10-11 17:21:20 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%v resp:%#v", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
readReq := &logical.Request{
|
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: updateReq.Path,
|
|
|
|
}
|
2018-09-18 03:03:00 +00:00
|
|
|
resp, err = is.HandleRequest(ctx, readReq)
|
2017-10-11 17:21:20 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%v resp:%#v", err, resp)
|
|
|
|
}
|
|
|
|
|
2018-07-25 02:01:58 +00:00
|
|
|
if resp.Data["name"] != "updatedaliasname" {
|
2017-10-11 17:21:20 +00:00
|
|
|
t.Fatalf("failed to update alias information; \n response data: %#v\n", resp.Data)
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(registerReq.Data, "name")
|
|
|
|
|
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 {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if resp == nil || !resp.IsError() {
|
|
|
|
t.Fatalf("expected error due to missing alias name")
|
|
|
|
}
|
|
|
|
|
|
|
|
registerReq.Data["name"] = "testaliasname"
|
|
|
|
delete(registerReq.Data, "mount_accessor")
|
|
|
|
|
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 {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if resp == nil || !resp.IsError() {
|
|
|
|
t.Fatalf("expected error due to missing mount accessor")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIdentityStore_AliasReadDelete(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
var resp *logical.Response
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
ctx := namespace.RootContext(nil)
|
|
|
|
is, githubAccessor, _ := testIdentityStoreWithGithubAuth(ctx, t)
|
2017-10-11 17:21:20 +00:00
|
|
|
|
|
|
|
registerData := map[string]interface{}{
|
|
|
|
"name": "testaliasname",
|
|
|
|
"mount_accessor": githubAccessor,
|
|
|
|
"metadata": []string{"organization=hashicorp", "team=vault"},
|
|
|
|
}
|
|
|
|
|
|
|
|
registerReq := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
2017-11-14 01:59:42 +00:00
|
|
|
Path: "entity-alias",
|
2017-10-11 17:21:20 +00:00
|
|
|
Data: registerData,
|
|
|
|
}
|
|
|
|
|
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("alias id not present in response")
|
|
|
|
}
|
|
|
|
id := idRaw.(string)
|
|
|
|
if id == "" {
|
|
|
|
t.Fatalf("invalid alias id")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read it back using alias id
|
|
|
|
aliasReq := &logical.Request{
|
|
|
|
Operation: logical.ReadOperation,
|
2017-11-14 01:59:42 +00:00
|
|
|
Path: "entity-alias/id/" + id,
|
2017-10-11 17:21:20 +00:00
|
|
|
}
|
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.Data["id"].(string) == "" ||
|
2017-11-02 20:05:48 +00:00
|
|
|
resp.Data["canonical_id"].(string) == "" ||
|
2017-10-11 17:21:20 +00:00
|
|
|
resp.Data["name"].(string) != registerData["name"] ||
|
|
|
|
resp.Data["mount_type"].(string) != "github" {
|
|
|
|
t.Fatalf("bad: alias read response; \nexpected: %#v \nactual: %#v\n", registerData, resp.Data)
|
|
|
|
}
|
|
|
|
|
|
|
|
aliasReq.Operation = logical.DeleteOperation
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
aliasReq.Operation = logical.ReadOperation
|
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("bad: alias read response; expected: nil, actual: %#v\n", resp)
|
|
|
|
}
|
|
|
|
}
|