2016-06-21 00:09:17 +00:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2018-08-16 10:38:13 +00:00
|
|
|
"reflect"
|
2016-06-21 00:09:17 +00:00
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBackend_PathListRoles(t *testing.T) {
|
|
|
|
var resp *logical.Response
|
|
|
|
var err error
|
|
|
|
config := logical.TestBackendConfig()
|
|
|
|
config.StorageView = &logical.InmemStorage{}
|
|
|
|
|
|
|
|
b := Backend()
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := b.Setup(context.Background(), config); err != nil {
|
2016-06-21 00:09:17 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
roleData := map[string]interface{}{
|
2018-08-16 10:38:13 +00:00
|
|
|
"role_arns": []string{"arn:aws:iam::123456789012:role/path/RoleName"},
|
|
|
|
"credential_type": assumedRoleCred,
|
2018-10-02 14:14:16 +00:00
|
|
|
"default_sts_ttl": 3600,
|
2016-06-21 00:09:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
roleReq := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: roleData,
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 1; i <= 10; i++ {
|
|
|
|
roleReq.Path = "roles/testrole" + strconv.Itoa(i)
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), roleReq)
|
2016-06-21 00:09:17 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("bad: role creation failed. resp:%#v\n err:%v", resp, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), &logical.Request{
|
2016-06-21 00:09:17 +00:00
|
|
|
Operation: logical.ListOperation,
|
|
|
|
Path: "roles",
|
|
|
|
Storage: config.StorageView,
|
|
|
|
})
|
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("bad: listing roles failed. resp:%#v\n err:%v", resp, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp.Data["keys"].([]string)) != 10 {
|
|
|
|
t.Fatalf("failed to list all 10 roles")
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), &logical.Request{
|
2016-06-21 00:09:17 +00:00
|
|
|
Operation: logical.ListOperation,
|
|
|
|
Path: "roles/",
|
|
|
|
Storage: config.StorageView,
|
|
|
|
})
|
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("bad: listing roles failed. resp:%#v\n err:%v", resp, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp.Data["keys"].([]string)) != 10 {
|
|
|
|
t.Fatalf("failed to list all 10 roles")
|
|
|
|
}
|
|
|
|
}
|
2018-08-16 10:38:13 +00:00
|
|
|
|
|
|
|
func TestUpgradeLegacyPolicyEntry(t *testing.T) {
|
|
|
|
var input string
|
|
|
|
var expected awsRoleEntry
|
|
|
|
var output *awsRoleEntry
|
|
|
|
|
|
|
|
input = "arn:aws:iam::123456789012:role/path/RoleName"
|
|
|
|
expected = awsRoleEntry{
|
|
|
|
CredentialTypes: []string{assumedRoleCred},
|
|
|
|
RoleArns: []string{input},
|
|
|
|
ProhibitFlexibleCredPath: true,
|
|
|
|
Version: 1,
|
|
|
|
}
|
|
|
|
output = upgradeLegacyPolicyEntry(input)
|
|
|
|
if output.InvalidData != "" {
|
|
|
|
t.Fatalf("bad: error processing upgrade of %q: got invalid data of %v", input, output.InvalidData)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(*output, expected) {
|
|
|
|
t.Fatalf("bad: expected %#v; received %#v", expected, *output)
|
|
|
|
}
|
|
|
|
|
|
|
|
input = "arn:aws:iam::123456789012:policy/MyPolicy"
|
|
|
|
expected = awsRoleEntry{
|
|
|
|
CredentialTypes: []string{iamUserCred},
|
|
|
|
PolicyArns: []string{input},
|
|
|
|
ProhibitFlexibleCredPath: true,
|
|
|
|
Version: 1,
|
|
|
|
}
|
|
|
|
output = upgradeLegacyPolicyEntry(input)
|
|
|
|
if output.InvalidData != "" {
|
|
|
|
t.Fatalf("bad: error processing upgrade of %q: got invalid data of %v", input, output.InvalidData)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(*output, expected) {
|
|
|
|
t.Fatalf("bad: expected %#v; received %#v", expected, *output)
|
|
|
|
}
|
|
|
|
|
|
|
|
input = "arn:aws:iam::aws:policy/AWSManagedPolicy"
|
|
|
|
expected.PolicyArns = []string{input}
|
|
|
|
output = upgradeLegacyPolicyEntry(input)
|
|
|
|
if output.InvalidData != "" {
|
|
|
|
t.Fatalf("bad: error processing upgrade of %q: got invalid data of %v", input, output.InvalidData)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(*output, expected) {
|
|
|
|
t.Fatalf("bad: expected %#v; received %#v", expected, *output)
|
|
|
|
}
|
|
|
|
|
|
|
|
input = `
|
|
|
|
{
|
|
|
|
"Version": "2012-10-07",
|
|
|
|
"Statement": [
|
|
|
|
{
|
|
|
|
"Effect": "Allow",
|
|
|
|
"Action": "ec2:Describe*",
|
|
|
|
"Resource": "*"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}`
|
|
|
|
compacted, err := compactJSON(input)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error parsing JSON: %v", err)
|
|
|
|
}
|
|
|
|
expected = awsRoleEntry{
|
|
|
|
CredentialTypes: []string{iamUserCred, federationTokenCred},
|
|
|
|
PolicyDocument: compacted,
|
|
|
|
ProhibitFlexibleCredPath: true,
|
|
|
|
Version: 1,
|
|
|
|
}
|
|
|
|
output = upgradeLegacyPolicyEntry(input)
|
|
|
|
if output.InvalidData != "" {
|
|
|
|
t.Fatalf("bad: error processing upgrade of %q: got invalid data of %v", input, output.InvalidData)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(*output, expected) {
|
|
|
|
t.Fatalf("bad: expected %#v; received %#v", expected, *output)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Due to lack of prior input validation, this could exist in the storage, and we need
|
|
|
|
// to be able to read it out in some fashion, so have to handle this in a poor fashion
|
|
|
|
input = "arn:gobbledygook"
|
|
|
|
expected = awsRoleEntry{
|
|
|
|
InvalidData: input,
|
|
|
|
Version: 1,
|
|
|
|
}
|
|
|
|
output = upgradeLegacyPolicyEntry(input)
|
|
|
|
if !reflect.DeepEqual(*output, expected) {
|
|
|
|
t.Fatalf("bad: expected %#v; received %#v", expected, *output)
|
|
|
|
}
|
|
|
|
}
|