2017-11-02 20:05:48 +00:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
"github.com/hashicorp/vault/helper/namespace"
|
2017-11-02 20:05:48 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
)
|
|
|
|
|
2017-11-02 20:38:15 +00:00
|
|
|
func TestIdentityStore_Lookup_Entity(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
var resp *logical.Response
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
ctx := namespace.RootContext(nil)
|
|
|
|
i, accessor, _ := testIdentityStoreWithGithubAuth(ctx, t)
|
2017-11-02 20:38:15 +00:00
|
|
|
|
|
|
|
entityReq := &logical.Request{
|
|
|
|
Path: "entity",
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
}
|
2018-09-18 03:03:00 +00:00
|
|
|
resp, err = i.HandleRequest(ctx, entityReq)
|
2017-11-02 20:38:15 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("bad: err: %#v\nresp: %v", err, resp)
|
|
|
|
}
|
|
|
|
entityID := resp.Data["id"].(string)
|
|
|
|
|
2017-11-09 06:29:19 +00:00
|
|
|
aliasReq := &logical.Request{
|
|
|
|
Path: "entity-alias",
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"name": "testaliasname",
|
|
|
|
"mount_accessor": accessor,
|
|
|
|
"entity_id": entityID,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
resp, err = i.HandleRequest(ctx, aliasReq)
|
2017-11-09 06:29:19 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("bad: err: %#v\nresp: %v", err, resp)
|
|
|
|
}
|
|
|
|
aliasID := resp.Data["id"].(string)
|
|
|
|
|
2017-11-02 20:38:15 +00:00
|
|
|
entity, err := i.MemDBEntityByID(entityID, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
lookupReq := &logical.Request{
|
|
|
|
Path: "lookup/entity",
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Data: map[string]interface{}{
|
2017-11-09 06:29:19 +00:00
|
|
|
"id": entityID,
|
2017-11-02 20:38:15 +00:00
|
|
|
},
|
|
|
|
}
|
2018-09-18 03:03:00 +00:00
|
|
|
resp, err = i.HandleRequest(ctx, lookupReq)
|
2017-11-02 20:38:15 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("bad: err: %#v\nresp: %v", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.Data["id"].(string) != entityID {
|
|
|
|
t.Fatalf("bad: entity: %#v", resp.Data)
|
|
|
|
}
|
|
|
|
|
|
|
|
lookupReq.Data = map[string]interface{}{
|
|
|
|
"name": entity.Name,
|
|
|
|
}
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
resp, err = i.HandleRequest(ctx, lookupReq)
|
2017-11-02 20:38:15 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("bad: err: %#v\nresp: %v", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.Data["id"].(string) != entityID {
|
|
|
|
t.Fatalf("bad: entity: %#v", resp.Data)
|
|
|
|
}
|
|
|
|
|
2017-11-09 06:29:19 +00:00
|
|
|
lookupReq.Data = map[string]interface{}{
|
|
|
|
"alias_id": aliasID,
|
|
|
|
}
|
2017-11-02 20:05:48 +00:00
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
resp, err = i.HandleRequest(ctx, lookupReq)
|
2017-11-09 06:29:19 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("bad: err: %#v\nresp: %v", err, resp)
|
|
|
|
}
|
2017-11-02 20:05:48 +00:00
|
|
|
|
2017-11-09 06:29:19 +00:00
|
|
|
if resp.Data["id"].(string) != entityID {
|
|
|
|
t.Fatalf("bad: entity: %#v", resp.Data)
|
2017-11-02 20:05:48 +00:00
|
|
|
}
|
|
|
|
|
2017-11-09 06:29:19 +00:00
|
|
|
lookupReq.Data = map[string]interface{}{
|
|
|
|
"alias_name": "testaliasname",
|
|
|
|
"alias_mount_accessor": accessor,
|
|
|
|
}
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
resp, err = i.HandleRequest(ctx, lookupReq)
|
2017-11-02 20:05:48 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
2017-11-09 06:29:19 +00:00
|
|
|
t.Fatalf("bad: err: %#v\nresp: %v", err, resp)
|
2017-11-02 20:05:48 +00:00
|
|
|
}
|
|
|
|
|
2017-11-09 06:29:19 +00:00
|
|
|
if resp.Data["id"].(string) != entityID {
|
|
|
|
t.Fatalf("bad: entity: %#v", resp.Data)
|
2017-11-02 20:05:48 +00:00
|
|
|
}
|
|
|
|
|
2017-11-09 06:29:19 +00:00
|
|
|
// Supply 2 query criteria
|
|
|
|
lookupReq.Data = map[string]interface{}{
|
|
|
|
"id": entityID,
|
|
|
|
"name": entity.Name,
|
2017-11-02 20:05:48 +00:00
|
|
|
}
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
resp, err = i.HandleRequest(ctx, lookupReq)
|
2017-11-09 06:29:19 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if resp == nil || !resp.IsError() {
|
|
|
|
t.Fatalf("expected an error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Supply alias name and skip accessor
|
|
|
|
lookupReq.Data = map[string]interface{}{
|
|
|
|
"alias_name": "testaliasname",
|
2017-11-02 20:05:48 +00:00
|
|
|
}
|
2017-11-09 06:29:19 +00:00
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
resp, err = i.HandleRequest(ctx, lookupReq)
|
2017-11-09 06:29:19 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2017-11-02 20:05:48 +00:00
|
|
|
}
|
2017-11-09 06:29:19 +00:00
|
|
|
if resp == nil || !resp.IsError() {
|
|
|
|
t.Fatalf("expected an error")
|
2017-11-02 20:05:48 +00:00
|
|
|
}
|
|
|
|
|
2017-11-09 06:29:19 +00:00
|
|
|
// Supply alias accessor and skip name
|
2017-11-02 20:05:48 +00:00
|
|
|
lookupReq.Data = map[string]interface{}{
|
2017-11-09 06:29:19 +00:00
|
|
|
"alias_mount_accessor": accessor,
|
2017-11-02 20:05:48 +00:00
|
|
|
}
|
2017-11-09 06:29:19 +00:00
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
resp, err = i.HandleRequest(ctx, lookupReq)
|
2017-11-09 06:29:19 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2017-11-02 20:05:48 +00:00
|
|
|
}
|
2017-11-09 06:29:19 +00:00
|
|
|
if resp == nil || !resp.IsError() {
|
|
|
|
t.Fatalf("expected an error")
|
2017-11-02 20:05:48 +00:00
|
|
|
}
|
|
|
|
|
2017-11-09 06:29:19 +00:00
|
|
|
// Don't supply any criteria
|
|
|
|
lookupReq.Data = nil
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
resp, err = i.HandleRequest(ctx, lookupReq)
|
2017-11-09 06:29:19 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2017-11-02 20:05:48 +00:00
|
|
|
}
|
2017-11-09 06:29:19 +00:00
|
|
|
if resp == nil || !resp.IsError() {
|
|
|
|
t.Fatalf("expected an error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the alias in the entity
|
|
|
|
aliasReq.Path = "entity-alias/id/" + aliasID
|
|
|
|
aliasReq.Operation = logical.DeleteOperation
|
2018-09-18 03:03:00 +00:00
|
|
|
resp, err = i.HandleRequest(ctx, aliasReq)
|
2017-11-02 20:05:48 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
2017-11-09 06:29:19 +00:00
|
|
|
t.Fatalf("bad: err: %#v\nresp: %v", err, resp)
|
2017-11-02 20:05:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lookupReq.Data = map[string]interface{}{
|
2017-11-09 06:29:19 +00:00
|
|
|
"alias_id": aliasID,
|
2017-11-02 20:05:48 +00:00
|
|
|
}
|
2017-11-09 06:29:19 +00:00
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
resp, err = i.HandleRequest(ctx, lookupReq)
|
2017-11-02 20:05:48 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
2017-11-09 06:29:19 +00:00
|
|
|
t.Fatalf("bad: err: %#v\nresp: %v", err, resp)
|
2017-11-02 20:05:48 +00:00
|
|
|
}
|
2017-11-09 06:29:19 +00:00
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("expected a nil response")
|
2017-11-02 20:05:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-09 06:29:19 +00:00
|
|
|
func TestIdentityStore_Lookup_Group(t *testing.T) {
|
2017-11-02 20:05:48 +00:00
|
|
|
var err error
|
|
|
|
var resp *logical.Response
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
ctx := namespace.RootContext(nil)
|
|
|
|
i, accessor, _ := testIdentityStoreWithGithubAuth(ctx, t)
|
2017-11-02 20:05:48 +00:00
|
|
|
|
|
|
|
groupReq := &logical.Request{
|
|
|
|
Path: "group",
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
}
|
2018-09-18 03:03:00 +00:00
|
|
|
resp, err = i.HandleRequest(ctx, groupReq)
|
2017-11-02 20:05:48 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("bad: resp: %#v\n err: %#v\n", resp, err)
|
|
|
|
}
|
|
|
|
groupID := resp.Data["id"].(string)
|
2017-11-09 06:29:19 +00:00
|
|
|
groupName := resp.Data["name"].(string)
|
2017-11-02 20:05:48 +00:00
|
|
|
|
|
|
|
lookupReq := &logical.Request{
|
2017-11-09 06:29:19 +00:00
|
|
|
Path: "lookup/group",
|
2017-11-02 20:05:48 +00:00
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Data: map[string]interface{}{
|
2017-11-09 06:29:19 +00:00
|
|
|
"id": groupID,
|
2017-11-02 20:05:48 +00:00
|
|
|
},
|
|
|
|
}
|
2017-11-09 06:29:19 +00:00
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
resp, err = i.HandleRequest(ctx, lookupReq)
|
2017-11-02 20:05:48 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("bad: resp: %#v\n err: %#v\n", resp, err)
|
|
|
|
}
|
2017-11-09 06:29:19 +00:00
|
|
|
if resp.Data["id"].(string) != groupID {
|
|
|
|
t.Fatalf("failed to lookup group")
|
2017-11-02 20:05:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lookupReq.Data = map[string]interface{}{
|
2017-11-09 06:29:19 +00:00
|
|
|
"name": groupName,
|
2017-11-02 20:05:48 +00:00
|
|
|
}
|
2017-11-09 06:29:19 +00:00
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
resp, err = i.HandleRequest(ctx, lookupReq)
|
2017-11-02 20:05:48 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("bad: resp: %#v\n err: %#v\n", resp, err)
|
|
|
|
}
|
2017-11-09 06:29:19 +00:00
|
|
|
if resp.Data["id"].(string) != groupID {
|
|
|
|
t.Fatalf("failed to lookup group")
|
2017-11-02 20:05:48 +00:00
|
|
|
}
|
|
|
|
|
2017-11-09 06:29:19 +00:00
|
|
|
// Query using an invalid alias_id
|
2017-11-02 20:05:48 +00:00
|
|
|
lookupReq.Data = map[string]interface{}{
|
2017-11-09 06:29:19 +00:00
|
|
|
"alias_id": "invalidaliasid",
|
2017-11-02 20:05:48 +00:00
|
|
|
}
|
2018-09-18 03:03:00 +00:00
|
|
|
resp, err = i.HandleRequest(ctx, lookupReq)
|
2017-11-02 20:05:48 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("bad: resp: %#v\n err: %#v\n", resp, err)
|
|
|
|
}
|
2017-11-09 06:29:19 +00:00
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("expected a nil response")
|
2017-11-02 20:05:48 +00:00
|
|
|
}
|
|
|
|
|
2017-11-09 06:29:19 +00:00
|
|
|
groupReq.Data = map[string]interface{}{
|
|
|
|
"type": "external",
|
2017-11-02 20:05:48 +00:00
|
|
|
}
|
2018-09-18 03:03:00 +00:00
|
|
|
resp, err = i.HandleRequest(ctx, groupReq)
|
2017-11-02 20:05:48 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("bad: resp: %#v\n err: %#v\n", resp, err)
|
|
|
|
}
|
2017-11-09 06:29:19 +00:00
|
|
|
groupID = resp.Data["id"].(string)
|
2017-11-02 20:05:48 +00:00
|
|
|
|
2017-11-09 06:29:19 +00:00
|
|
|
aliasReq := &logical.Request{
|
|
|
|
Path: "group-alias",
|
2017-11-02 20:05:48 +00:00
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Data: map[string]interface{}{
|
2017-11-09 06:29:19 +00:00
|
|
|
"canonical_id": groupID,
|
|
|
|
"name": "testgroupalias",
|
|
|
|
"mount_accessor": accessor,
|
2017-11-02 20:05:48 +00:00
|
|
|
},
|
|
|
|
}
|
2018-09-18 03:03:00 +00:00
|
|
|
resp, err = i.HandleRequest(ctx, aliasReq)
|
2017-11-09 06:29:19 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("bad: resp: %#v\n err: %#v\n", resp, err)
|
|
|
|
}
|
|
|
|
aliasID := resp.Data["id"].(string)
|
|
|
|
|
|
|
|
lookupReq.Data = map[string]interface{}{
|
|
|
|
"alias_id": aliasID,
|
|
|
|
}
|
2017-11-02 20:05:48 +00:00
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
resp, err = i.HandleRequest(ctx, lookupReq)
|
2017-11-02 20:05:48 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("bad: resp: %#v\n err: %#v\n", resp, err)
|
|
|
|
}
|
|
|
|
if resp.Data["id"].(string) != groupID {
|
|
|
|
t.Fatalf("failed to lookup group")
|
|
|
|
}
|
|
|
|
|
2017-11-09 06:29:19 +00:00
|
|
|
lookupReq.Data = map[string]interface{}{
|
|
|
|
"alias_name": "testgroupalias",
|
|
|
|
"alias_mount_accessor": accessor,
|
2017-11-02 20:05:48 +00:00
|
|
|
}
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
resp, err = i.HandleRequest(ctx, lookupReq)
|
2017-11-02 20:05:48 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("bad: resp: %#v\n err: %#v\n", resp, err)
|
|
|
|
}
|
|
|
|
if resp.Data["id"].(string) != groupID {
|
|
|
|
t.Fatalf("failed to lookup group")
|
|
|
|
}
|
2017-11-09 06:29:19 +00:00
|
|
|
|
|
|
|
// Supply 2 query criteria
|
|
|
|
lookupReq.Data = map[string]interface{}{
|
|
|
|
"id": groupID,
|
|
|
|
"name": groupName,
|
|
|
|
}
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
resp, err = i.HandleRequest(ctx, lookupReq)
|
2017-11-09 06:29:19 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if resp == nil || !resp.IsError() {
|
|
|
|
t.Fatalf("expected an error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Supply alias name and skip accessor
|
|
|
|
lookupReq.Data = map[string]interface{}{
|
|
|
|
"alias_name": "testgroupalias",
|
|
|
|
}
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
resp, err = i.HandleRequest(ctx, lookupReq)
|
2017-11-09 06:29:19 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if resp == nil || !resp.IsError() {
|
|
|
|
t.Fatalf("expected an error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Supply alias accessor and skip name
|
|
|
|
lookupReq.Data = map[string]interface{}{
|
|
|
|
"alias_mount_accessor": accessor,
|
|
|
|
}
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
resp, err = i.HandleRequest(ctx, lookupReq)
|
2017-11-09 06:29:19 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if resp == nil || !resp.IsError() {
|
|
|
|
t.Fatalf("expected an error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't supply any criteria
|
|
|
|
lookupReq.Data = nil
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
resp, err = i.HandleRequest(ctx, lookupReq)
|
2017-11-09 06:29:19 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if resp == nil || !resp.IsError() {
|
|
|
|
t.Fatalf("expected an error")
|
|
|
|
}
|
2017-11-02 20:05:48 +00:00
|
|
|
}
|