2015-03-21 16:19:37 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2015-11-02 21:16:51 +00:00
|
|
|
"bufio"
|
2015-03-21 16:19:37 +00:00
|
|
|
"encoding/base64"
|
2015-04-27 18:08:37 +00:00
|
|
|
"fmt"
|
2015-03-21 16:19:37 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2015-03-21 16:23:44 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2015-03-21 16:19:37 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
logicaltest "github.com/hashicorp/vault/logical/testing"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBackend_basic(t *testing.T) {
|
2016-04-12 00:00:15 +00:00
|
|
|
if os.Getenv(logicaltest.TestEnvVar) == "" {
|
|
|
|
t.Skip(fmt.Sprintf("Acceptance tests skipped unless env '%s' set", logicaltest.TestEnvVar))
|
2016-02-29 19:58:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-03-21 16:19:37 +00:00
|
|
|
config, process := testStartConsulServer(t)
|
|
|
|
defer testStopConsulServer(t, process)
|
|
|
|
|
2015-11-02 21:16:51 +00:00
|
|
|
b, _ := Factory(logical.TestBackendConfig())
|
2015-03-21 16:19:37 +00:00
|
|
|
logicaltest.Test(t, logicaltest.TestCase{
|
2016-04-05 19:10:44 +00:00
|
|
|
AcceptanceTest: true,
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Backend: b,
|
2015-03-21 16:19:37 +00:00
|
|
|
Steps: []logicaltest.TestStep{
|
|
|
|
testAccStepConfig(t, config),
|
2015-05-26 00:33:31 +00:00
|
|
|
testAccStepWritePolicy(t, "test", testPolicy, ""),
|
2015-03-21 16:23:44 +00:00
|
|
|
testAccStepReadToken(t, "test", config),
|
2015-03-21 16:19:37 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-11-02 21:16:51 +00:00
|
|
|
func TestBackend_management(t *testing.T) {
|
2016-04-12 00:00:15 +00:00
|
|
|
if os.Getenv(logicaltest.TestEnvVar) == "" {
|
|
|
|
t.Skip(fmt.Sprintf("Acceptance tests skipped unless env '%s' set", logicaltest.TestEnvVar))
|
2016-02-29 19:58:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-11-02 21:16:51 +00:00
|
|
|
config, process := testStartConsulServer(t)
|
|
|
|
defer testStopConsulServer(t, process)
|
|
|
|
|
|
|
|
b, _ := Factory(logical.TestBackendConfig())
|
|
|
|
logicaltest.Test(t, logicaltest.TestCase{
|
2016-04-05 19:10:44 +00:00
|
|
|
AcceptanceTest: true,
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Backend: b,
|
2015-11-02 21:16:51 +00:00
|
|
|
Steps: []logicaltest.TestStep{
|
|
|
|
testAccStepConfig(t, config),
|
|
|
|
testAccStepWriteManagementPolicy(t, "test", ""),
|
|
|
|
testAccStepReadManagementToken(t, "test", config),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-04-27 18:08:37 +00:00
|
|
|
func TestBackend_crud(t *testing.T) {
|
2016-04-12 00:00:15 +00:00
|
|
|
if os.Getenv(logicaltest.TestEnvVar) == "" {
|
|
|
|
t.Skip(fmt.Sprintf("Acceptance tests skipped unless env '%s' set", logicaltest.TestEnvVar))
|
2016-02-29 19:58:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-27 18:08:37 +00:00
|
|
|
_, process := testStartConsulServer(t)
|
|
|
|
defer testStopConsulServer(t, process)
|
|
|
|
|
2015-11-02 21:16:51 +00:00
|
|
|
b, _ := Factory(logical.TestBackendConfig())
|
2015-04-27 18:08:37 +00:00
|
|
|
logicaltest.Test(t, logicaltest.TestCase{
|
2016-04-05 19:10:44 +00:00
|
|
|
AcceptanceTest: true,
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Backend: b,
|
2015-04-27 18:08:37 +00:00
|
|
|
Steps: []logicaltest.TestStep{
|
2015-05-26 00:33:31 +00:00
|
|
|
testAccStepWritePolicy(t, "test", testPolicy, ""),
|
2016-01-30 01:45:38 +00:00
|
|
|
testAccStepReadPolicy(t, "test", testPolicy, 0),
|
2015-05-26 00:33:31 +00:00
|
|
|
testAccStepDeletePolicy(t, "test"),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackend_role_lease(t *testing.T) {
|
2016-04-12 00:00:15 +00:00
|
|
|
if os.Getenv(logicaltest.TestEnvVar) == "" {
|
|
|
|
t.Skip(fmt.Sprintf("Acceptance tests skipped unless env '%s' set", logicaltest.TestEnvVar))
|
2016-02-29 19:58:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-05-26 00:33:31 +00:00
|
|
|
_, process := testStartConsulServer(t)
|
|
|
|
defer testStopConsulServer(t, process)
|
|
|
|
|
2015-11-02 21:16:51 +00:00
|
|
|
b, _ := Factory(logical.TestBackendConfig())
|
2015-05-26 00:33:31 +00:00
|
|
|
logicaltest.Test(t, logicaltest.TestCase{
|
2016-04-05 19:10:44 +00:00
|
|
|
AcceptanceTest: true,
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Backend: b,
|
2015-05-26 00:33:31 +00:00
|
|
|
Steps: []logicaltest.TestStep{
|
|
|
|
testAccStepWritePolicy(t, "test", testPolicy, "6h"),
|
|
|
|
testAccStepReadPolicy(t, "test", testPolicy, 6*time.Hour),
|
2015-04-27 18:08:37 +00:00
|
|
|
testAccStepDeletePolicy(t, "test"),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-03-21 16:19:37 +00:00
|
|
|
func testStartConsulServer(t *testing.T) (map[string]interface{}, *os.Process) {
|
2015-03-29 23:20:34 +00:00
|
|
|
if _, err := exec.LookPath("consul"); err != nil {
|
2016-03-07 15:09:36 +00:00
|
|
|
t.Errorf("consul not found: %s", err)
|
2015-03-29 23:20:34 +00:00
|
|
|
}
|
|
|
|
|
2015-03-21 16:19:37 +00:00
|
|
|
td, err := ioutil.TempDir("", "vault")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tf, err := ioutil.TempFile("", "vault")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if _, err := tf.Write([]byte(strings.TrimSpace(testConsulConfig))); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
tf.Close()
|
|
|
|
|
|
|
|
cmd := exec.Command(
|
|
|
|
"consul", "agent",
|
|
|
|
"-server",
|
|
|
|
"-bootstrap",
|
2015-11-02 21:16:51 +00:00
|
|
|
"-advertise", "127.0.0.1",
|
2015-03-21 16:19:37 +00:00
|
|
|
"-config-file", tf.Name(),
|
|
|
|
"-data-dir", td)
|
2015-11-02 21:16:51 +00:00
|
|
|
stdout, _ := cmd.StdoutPipe()
|
|
|
|
stderr, _ := cmd.StderrPipe()
|
|
|
|
stdoutScanner := bufio.NewScanner(stdout)
|
|
|
|
stderrScanner := bufio.NewScanner(stderr)
|
|
|
|
stdoutScanFunc := func() {
|
|
|
|
for stdoutScanner.Scan() {
|
|
|
|
t.Logf("Consul stdout: %s\n", stdoutScanner.Text())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
stderrScanFunc := func() {
|
|
|
|
for stderrScanner.Scan() {
|
|
|
|
t.Logf("Consul stderr: %s\n", stderrScanner.Text())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if os.Getenv("VAULT_VERBOSE_ACC_TESTS") != "" {
|
|
|
|
go stdoutScanFunc()
|
|
|
|
go stderrScanFunc()
|
|
|
|
}
|
|
|
|
|
2015-03-21 16:19:37 +00:00
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
t.Fatalf("error starting Consul: %s", err)
|
|
|
|
}
|
|
|
|
// Give Consul time to startup
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
|
|
|
|
config := map[string]interface{}{
|
|
|
|
"address": "127.0.0.1:8500",
|
|
|
|
"token": "test",
|
|
|
|
}
|
|
|
|
return config, cmd.Process
|
|
|
|
}
|
|
|
|
|
|
|
|
func testStopConsulServer(t *testing.T, p *os.Process) {
|
|
|
|
p.Kill()
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccPreCheck(t *testing.T) {
|
|
|
|
if _, err := exec.LookPath("consul"); err != nil {
|
|
|
|
t.Fatal("consul must be on PATH")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccStepConfig(
|
|
|
|
t *testing.T, config map[string]interface{}) logicaltest.TestStep {
|
|
|
|
return logicaltest.TestStep{
|
2016-01-07 15:30:47 +00:00
|
|
|
Operation: logical.UpdateOperation,
|
2015-04-19 05:28:53 +00:00
|
|
|
Path: "config/access",
|
2015-03-21 16:19:37 +00:00
|
|
|
Data: config,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-21 16:23:44 +00:00
|
|
|
func testAccStepReadToken(
|
|
|
|
t *testing.T, name string, conf map[string]interface{}) logicaltest.TestStep {
|
2015-03-21 16:19:37 +00:00
|
|
|
return logicaltest.TestStep{
|
|
|
|
Operation: logical.ReadOperation,
|
2015-04-27 20:59:56 +00:00
|
|
|
Path: "creds/" + name,
|
2015-03-21 16:19:37 +00:00
|
|
|
Check: func(resp *logical.Response) error {
|
|
|
|
var d struct {
|
|
|
|
Token string `mapstructure:"token"`
|
|
|
|
}
|
|
|
|
if err := mapstructure.Decode(resp.Data, &d); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Printf("[WARN] Generated token: %s", d.Token)
|
|
|
|
|
2015-03-21 16:23:44 +00:00
|
|
|
// Build a client and verify that the credentials work
|
|
|
|
config := api.DefaultConfig()
|
|
|
|
config.Address = conf["address"].(string)
|
|
|
|
config.Token = d.Token
|
|
|
|
client, err := api.NewClient(config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[WARN] Verifying that the generated token works...")
|
|
|
|
_, err = client.KV().Put(&api.KVPair{
|
|
|
|
Key: "foo",
|
|
|
|
Value: []byte("bar"),
|
|
|
|
}, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-21 16:19:37 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-02 21:16:51 +00:00
|
|
|
func testAccStepReadManagementToken(
|
|
|
|
t *testing.T, name string, conf map[string]interface{}) logicaltest.TestStep {
|
|
|
|
return logicaltest.TestStep{
|
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "creds/" + name,
|
|
|
|
Check: func(resp *logical.Response) error {
|
|
|
|
var d struct {
|
|
|
|
Token string `mapstructure:"token"`
|
|
|
|
}
|
|
|
|
if err := mapstructure.Decode(resp.Data, &d); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Printf("[WARN] Generated token: %s", d.Token)
|
|
|
|
|
|
|
|
// Build a client and verify that the credentials work
|
|
|
|
config := api.DefaultConfig()
|
|
|
|
config.Address = conf["address"].(string)
|
|
|
|
config.Token = d.Token
|
|
|
|
client, err := api.NewClient(config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[WARN] Verifying that the generated token works...")
|
|
|
|
_, _, err = client.ACL().Create(&api.ACLEntry{
|
|
|
|
Type: "management",
|
|
|
|
Name: "test2",
|
|
|
|
}, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-26 00:33:31 +00:00
|
|
|
func testAccStepWritePolicy(t *testing.T, name string, policy string, lease string) logicaltest.TestStep {
|
2015-03-21 16:19:37 +00:00
|
|
|
return logicaltest.TestStep{
|
2016-01-07 15:30:47 +00:00
|
|
|
Operation: logical.UpdateOperation,
|
2015-04-27 20:59:56 +00:00
|
|
|
Path: "roles/" + name,
|
2015-03-21 16:19:37 +00:00
|
|
|
Data: map[string]interface{}{
|
|
|
|
"policy": base64.StdEncoding.EncodeToString([]byte(policy)),
|
2015-05-26 00:33:31 +00:00
|
|
|
"lease": lease,
|
2015-03-21 16:19:37 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-02 21:16:51 +00:00
|
|
|
func testAccStepWriteManagementPolicy(t *testing.T, name string, lease string) logicaltest.TestStep {
|
|
|
|
return logicaltest.TestStep{
|
2016-01-07 15:30:47 +00:00
|
|
|
Operation: logical.UpdateOperation,
|
2015-11-02 21:16:51 +00:00
|
|
|
Path: "roles/" + name,
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"token_type": "management",
|
|
|
|
"lease": lease,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-26 00:33:31 +00:00
|
|
|
func testAccStepReadPolicy(t *testing.T, name string, policy string, lease time.Duration) logicaltest.TestStep {
|
2015-04-27 18:08:37 +00:00
|
|
|
return logicaltest.TestStep{
|
|
|
|
Operation: logical.ReadOperation,
|
2015-04-27 20:59:56 +00:00
|
|
|
Path: "roles/" + name,
|
2015-04-27 18:08:37 +00:00
|
|
|
Check: func(resp *logical.Response) error {
|
|
|
|
policyRaw := resp.Data["policy"].(string)
|
|
|
|
out, err := base64.StdEncoding.DecodeString(policyRaw)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if string(out) != policy {
|
|
|
|
return fmt.Errorf("mismatch: %s %s", out, policy)
|
|
|
|
}
|
2015-05-26 00:33:31 +00:00
|
|
|
|
|
|
|
leaseRaw := resp.Data["lease"].(string)
|
|
|
|
l, err := time.ParseDuration(leaseRaw)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if l != lease {
|
|
|
|
return fmt.Errorf("mismatch: %v %v", l, lease)
|
|
|
|
}
|
2015-04-27 18:08:37 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccStepDeletePolicy(t *testing.T, name string) logicaltest.TestStep {
|
|
|
|
return logicaltest.TestStep{
|
|
|
|
Operation: logical.DeleteOperation,
|
2015-04-27 20:59:56 +00:00
|
|
|
Path: "roles/" + name,
|
2015-04-27 18:08:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-21 16:19:37 +00:00
|
|
|
const testPolicy = `
|
|
|
|
key "" {
|
|
|
|
policy = "write"
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
const testConsulConfig = `
|
|
|
|
{
|
|
|
|
"datacenter": "test",
|
|
|
|
"acl_datacenter": "test",
|
|
|
|
"acl_master_token": "test"
|
|
|
|
}
|
|
|
|
`
|