2018-09-26 15:27:12 +00:00
|
|
|
package awsauth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2018-09-26 15:27:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBackend_pathConfigIdentity(t *testing.T) {
|
|
|
|
config := logical.TestBackendConfig()
|
|
|
|
storage := &logical.InmemStorage{}
|
|
|
|
config.StorageView = storage
|
|
|
|
|
|
|
|
b, err := Backend(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = b.Setup(context.Background(), config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-01-09 23:28:29 +00:00
|
|
|
// Check if default values are returned before setting the configuration
|
2018-09-26 15:27:12 +00:00
|
|
|
resp, err := b.HandleRequest(context.Background(), &logical.Request{
|
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "config/identity",
|
|
|
|
Storage: storage,
|
|
|
|
})
|
2019-01-09 23:28:29 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("bad: err: %v\nresp: %#v", err, resp)
|
2018-09-26 15:27:12 +00:00
|
|
|
}
|
2019-01-30 20:46:43 +00:00
|
|
|
if resp.Data["iam_alias"] == nil || resp.Data["iam_alias"] != identityAliasRoleID {
|
2019-01-09 23:28:29 +00:00
|
|
|
t.Fatalf("bad: iam_alias; expected: %q, actual: %q", identityAliasIAMUniqueID, resp.Data["iam_alias"])
|
|
|
|
}
|
2019-01-30 20:46:43 +00:00
|
|
|
if resp.Data["ec2_alias"] == nil || resp.Data["ec2_alias"] != identityAliasRoleID {
|
2019-01-09 23:28:29 +00:00
|
|
|
t.Fatalf("bad: ec2_alias; expected: %q, actual: %q", identityAliasIAMUniqueID, resp.Data["ec2_alias"])
|
2018-09-26 15:27:12 +00:00
|
|
|
}
|
|
|
|
|
2019-01-09 23:28:29 +00:00
|
|
|
// Invalid value for iam_alias
|
2018-09-26 15:27:12 +00:00
|
|
|
data := map[string]interface{}{
|
|
|
|
"iam_alias": "invalid",
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err = b.HandleRequest(context.Background(), &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "config/identity",
|
|
|
|
Data: data,
|
|
|
|
Storage: storage,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if resp == nil {
|
|
|
|
t.Fatalf("nil response from invalid config/identity request")
|
|
|
|
}
|
|
|
|
if !resp.IsError() {
|
|
|
|
t.Fatalf("received non-error response from invalid config/identity request: %#v", resp)
|
|
|
|
}
|
|
|
|
|
2019-01-09 23:28:29 +00:00
|
|
|
// Valid value for iam_alias but invalid value for ec2_alias
|
2018-09-26 15:27:12 +00:00
|
|
|
data["iam_alias"] = identityAliasIAMFullArn
|
2019-01-09 23:28:29 +00:00
|
|
|
data["ec2_alias"] = "invalid"
|
2018-09-26 15:27:12 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "config/identity",
|
|
|
|
Data: data,
|
|
|
|
Storage: storage,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-01-09 23:28:29 +00:00
|
|
|
if resp == nil {
|
|
|
|
t.Fatalf("nil response from invalid config/identity request")
|
|
|
|
}
|
|
|
|
if !resp.IsError() {
|
|
|
|
t.Fatalf("received non-error response from invalid config/identity request: %#v", resp)
|
2018-09-26 15:27:12 +00:00
|
|
|
}
|
|
|
|
|
2019-01-09 23:28:29 +00:00
|
|
|
// Valid value for both iam_alias and ec2_alias
|
|
|
|
data["ec2_alias"] = identityAliasEC2ImageID
|
|
|
|
resp, err = b.HandleRequest(context.Background(), &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "config/identity",
|
|
|
|
Data: data,
|
|
|
|
Storage: storage,
|
|
|
|
})
|
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("bad: err: %v\nresp: %#v", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if both values are stored properly
|
2018-09-26 15:27:12 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), &logical.Request{
|
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "config/identity",
|
|
|
|
Storage: storage,
|
|
|
|
})
|
2019-01-09 23:28:29 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("bad: err: %v\nresp: %#v", err, resp)
|
2018-09-26 15:27:12 +00:00
|
|
|
}
|
2019-01-09 23:28:29 +00:00
|
|
|
if resp.Data["iam_alias"] != identityAliasIAMFullArn {
|
|
|
|
t.Fatalf("bad: expected response with iam_alias value of %q; got %#v", identityAliasIAMFullArn, resp.Data["iam_alias"])
|
|
|
|
}
|
|
|
|
if resp.Data["ec2_alias"] != identityAliasEC2ImageID {
|
|
|
|
t.Fatalf("bad: expected response with ec2_alias value of %q; got %#v", identityAliasEC2ImageID, resp.Data["ec2_alias"])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Modify one field and ensure that the other one is unchanged
|
|
|
|
data["ec2_alias"] = identityAliasEC2InstanceID
|
|
|
|
delete(data, "iam_alias")
|
|
|
|
resp, err = b.HandleRequest(context.Background(), &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "config/identity",
|
|
|
|
Data: data,
|
|
|
|
Storage: storage,
|
|
|
|
})
|
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("bad: err: %v\nresp: %#v", err, resp)
|
|
|
|
}
|
|
|
|
resp, err = b.HandleRequest(context.Background(), &logical.Request{
|
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "config/identity",
|
|
|
|
Storage: storage,
|
|
|
|
})
|
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("bad: err: %v\nresp: %#v", err, resp)
|
|
|
|
}
|
|
|
|
if resp.Data["iam_alias"] != identityAliasIAMFullArn {
|
|
|
|
t.Fatalf("bad: expected response with iam_alias value of %q; got %#v", identityAliasIAMFullArn, resp.Data["iam_alias"])
|
|
|
|
}
|
|
|
|
if resp.Data["ec2_alias"] != identityAliasEC2InstanceID {
|
|
|
|
t.Fatalf("bad: expected response with ec2_alias value of %q; got %#v", identityAliasEC2ImageID, resp.Data["ec2_alias"])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update both iam_alias and ec2_alias
|
|
|
|
data["iam_alias"] = identityAliasIAMUniqueID
|
|
|
|
data["ec2_alias"] = identityAliasEC2InstanceID
|
|
|
|
resp, err = b.HandleRequest(context.Background(), &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "config/identity",
|
|
|
|
Data: data,
|
|
|
|
Storage: storage,
|
|
|
|
})
|
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("bad: err: %v\nresp: %#v", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if updates were stored properly
|
|
|
|
resp, err = b.HandleRequest(context.Background(), &logical.Request{
|
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "config/identity",
|
|
|
|
Storage: storage,
|
|
|
|
})
|
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("bad: err: %v\nresp: %#v", err, resp)
|
|
|
|
}
|
|
|
|
if resp.Data["iam_alias"] != identityAliasIAMUniqueID {
|
|
|
|
t.Fatalf("bad: expected response with iam_alias value of %q; got %#v", identityAliasIAMFullArn, resp.Data["iam_alias"])
|
|
|
|
}
|
|
|
|
if resp.Data["ec2_alias"] != identityAliasEC2InstanceID {
|
|
|
|
t.Fatalf("bad: expected response with ec2_alias value of %q; got %#v", identityAliasEC2ImageID, resp.Data["ec2_alias"])
|
2018-09-26 15:27:12 +00:00
|
|
|
}
|
|
|
|
}
|