2015-03-20 16:59:48 +00:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2015-04-19 05:13:12 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2015-03-20 16:59:48 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/aws-sdk-go/aws"
|
|
|
|
"github.com/hashicorp/aws-sdk-go/gen/ec2"
|
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
logicaltest "github.com/hashicorp/vault/logical/testing"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBackend_basic(t *testing.T) {
|
|
|
|
logicaltest.Test(t, logicaltest.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Backend: Backend(),
|
|
|
|
Steps: []logicaltest.TestStep{
|
|
|
|
testAccStepConfig(t),
|
|
|
|
testAccStepWritePolicy(t, "test", testPolicy),
|
|
|
|
testAccStepReadUser(t, "test"),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-04-19 05:13:12 +00:00
|
|
|
func TestBackend_policyCrud(t *testing.T) {
|
|
|
|
var compacted bytes.Buffer
|
|
|
|
if err := json.Compact(&compacted, []byte(testPolicy)); err != nil {
|
|
|
|
t.Fatalf("bad: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
logicaltest.Test(t, logicaltest.TestCase{
|
|
|
|
Backend: Backend(),
|
|
|
|
Steps: []logicaltest.TestStep{
|
|
|
|
testAccStepConfig(t),
|
|
|
|
testAccStepWritePolicy(t, "test", testPolicy),
|
|
|
|
testAccStepReadPolicy(t, "test", compacted.String()),
|
|
|
|
testAccStepDeletePolicy(t, "test"),
|
|
|
|
testAccStepReadPolicy(t, "test", ""),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-03-20 16:59:48 +00:00
|
|
|
func testAccPreCheck(t *testing.T) {
|
|
|
|
if v := os.Getenv("AWS_ACCESS_KEY_ID"); v == "" {
|
|
|
|
t.Fatal("AWS_ACCESS_KEY_ID must be set for acceptance tests")
|
|
|
|
}
|
|
|
|
|
|
|
|
if v := os.Getenv("AWS_SECRET_ACCESS_KEY"); v == "" {
|
|
|
|
t.Fatal("AWS_SECRET_ACCESS_KEY must be set for acceptance tests")
|
|
|
|
}
|
|
|
|
|
|
|
|
if v := os.Getenv("AWS_DEFAULT_REGION"); v == "" {
|
|
|
|
log.Println("[INFO] Test: Using us-west-2 as test region")
|
|
|
|
os.Setenv("AWS_DEFAULT_REGION", "us-west-2")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccStepConfig(t *testing.T) logicaltest.TestStep {
|
|
|
|
return logicaltest.TestStep{
|
|
|
|
Operation: logical.WriteOperation,
|
2015-04-19 05:21:31 +00:00
|
|
|
Path: "config/root",
|
2015-03-20 16:59:48 +00:00
|
|
|
Data: map[string]interface{}{
|
|
|
|
"access_key": os.Getenv("AWS_ACCESS_KEY_ID"),
|
|
|
|
"secret_key": os.Getenv("AWS_SECRET_ACCESS_KEY"),
|
|
|
|
"region": os.Getenv("AWS_DEFAULT_REGION"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccStepReadUser(t *testing.T, name string) logicaltest.TestStep {
|
|
|
|
return logicaltest.TestStep{
|
|
|
|
Operation: logical.ReadOperation,
|
2015-04-27 21:20:28 +00:00
|
|
|
Path: "creds/" + name,
|
2015-03-20 16:59:48 +00:00
|
|
|
Check: func(resp *logical.Response) error {
|
|
|
|
var d struct {
|
|
|
|
AccessKey string `mapstructure:"access_key"`
|
|
|
|
SecretKey string `mapstructure:"secret_key"`
|
|
|
|
}
|
|
|
|
if err := mapstructure.Decode(resp.Data, &d); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Printf("[WARN] Generated credentials: %v", d)
|
|
|
|
|
|
|
|
// Sleep sometime because AWS is eventually consistent
|
|
|
|
log.Println("[WARN] Sleeping for 10 seconds waiting for AWS...")
|
|
|
|
time.Sleep(10 * time.Second)
|
|
|
|
|
|
|
|
// Build a client and verify that the credentials work
|
|
|
|
creds := aws.Creds(d.AccessKey, d.SecretKey, "")
|
|
|
|
client := ec2.New(creds, "us-east-1", nil)
|
|
|
|
|
|
|
|
log.Printf("[WARN] Verifying that the generated credentials work...")
|
|
|
|
_, err := client.DescribeInstances(&ec2.DescribeInstancesRequest{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccStepWritePolicy(t *testing.T, name string, policy string) logicaltest.TestStep {
|
|
|
|
return logicaltest.TestStep{
|
|
|
|
Operation: logical.WriteOperation,
|
2015-04-27 21:20:28 +00:00
|
|
|
Path: "roles/" + name,
|
2015-03-20 16:59:48 +00:00
|
|
|
Data: map[string]interface{}{
|
2015-04-01 00:26:41 +00:00
|
|
|
"policy": testPolicy,
|
2015-03-20 16:59:48 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-19 05:13:12 +00:00
|
|
|
func testAccStepDeletePolicy(t *testing.T, n string) logicaltest.TestStep {
|
|
|
|
return logicaltest.TestStep{
|
|
|
|
Operation: logical.DeleteOperation,
|
2015-04-27 21:20:28 +00:00
|
|
|
Path: "roles/" + n,
|
2015-04-19 05:13:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccStepReadPolicy(t *testing.T, name string, value string) logicaltest.TestStep {
|
|
|
|
return logicaltest.TestStep{
|
|
|
|
Operation: logical.ReadOperation,
|
2015-04-27 21:20:28 +00:00
|
|
|
Path: "roles/" + name,
|
2015-04-19 05:13:12 +00:00
|
|
|
Check: func(resp *logical.Response) error {
|
|
|
|
if resp == nil {
|
|
|
|
if value == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
var d struct {
|
|
|
|
Policy string `mapstructure:"policy"`
|
|
|
|
}
|
|
|
|
if err := mapstructure.Decode(resp.Data, &d); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.Policy != value {
|
|
|
|
return fmt.Errorf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-20 16:59:48 +00:00
|
|
|
const testPolicy = `
|
|
|
|
{
|
|
|
|
"Version": "2012-10-17",
|
|
|
|
"Statement": [
|
|
|
|
{
|
|
|
|
"Sid": "Stmt1426528957000",
|
|
|
|
"Effect": "Allow",
|
|
|
|
"Action": [
|
|
|
|
"ec2:*"
|
|
|
|
],
|
|
|
|
"Resource": [
|
|
|
|
"*"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|