2015-11-18 16:25:42 +00:00
|
|
|
package rabbitmq
|
|
|
|
|
|
|
|
import (
|
2018-01-08 20:26:13 +00:00
|
|
|
"context"
|
2015-11-18 16:25:42 +00:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
2018-04-04 20:09:26 +00:00
|
|
|
"strconv"
|
2015-11-18 16:25:42 +00:00
|
|
|
"testing"
|
|
|
|
|
2016-07-06 16:25:40 +00:00
|
|
|
"github.com/hashicorp/vault/helper/jsonutil"
|
2015-11-18 16:25:42 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
logicaltest "github.com/hashicorp/vault/logical/testing"
|
|
|
|
"github.com/michaelklishin/rabbit-hole"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
2018-04-04 20:09:26 +00:00
|
|
|
dockertest "gopkg.in/ory-am/dockertest.v3"
|
2015-11-18 16:25:42 +00:00
|
|
|
)
|
|
|
|
|
2018-04-04 20:09:26 +00:00
|
|
|
const (
|
|
|
|
envRabbitMQConnectionURI = "RABBITMQ_CONNECTION_URI"
|
|
|
|
envRabbitMQUsername = "RABBITMQ_USERNAME"
|
|
|
|
envRabbitMQPassword = "RABBITMQ_PASSWORD"
|
|
|
|
)
|
|
|
|
|
|
|
|
func prepareRabbitMQTestContainer(t *testing.T) (func(), string, int) {
|
|
|
|
if os.Getenv(envRabbitMQConnectionURI) != "" {
|
|
|
|
return func() {}, os.Getenv(envRabbitMQConnectionURI), 0
|
|
|
|
}
|
|
|
|
|
|
|
|
pool, err := dockertest.NewPool("")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to connect to docker: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
runOpts := &dockertest.RunOptions{
|
|
|
|
Repository: "rabbitmq",
|
|
|
|
Tag: "3-management",
|
|
|
|
}
|
|
|
|
resource, err := pool.RunWithOptions(runOpts)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not start local rabbitmq docker container: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup := func() {
|
|
|
|
err := pool.Purge(resource)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to cleanup local container: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
port, _ := strconv.Atoi(resource.GetPort("15672/tcp"))
|
|
|
|
address := fmt.Sprintf("http://127.0.0.1:%d", port)
|
|
|
|
|
|
|
|
// exponential backoff-retry
|
|
|
|
if err = pool.Retry(func() error {
|
|
|
|
rmqc, err := rabbithole.NewClient(address, "guest", "guest")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = rmqc.Overview()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
cleanup()
|
|
|
|
t.Fatalf("Could not connect to rabbitmq docker container: %s", err)
|
|
|
|
}
|
|
|
|
return cleanup, address, port
|
|
|
|
}
|
|
|
|
|
2015-11-18 16:25:42 +00:00
|
|
|
func TestBackend_basic(t *testing.T) {
|
2016-06-08 14:46:46 +00:00
|
|
|
if os.Getenv(logicaltest.TestEnvVar) == "" {
|
|
|
|
t.Skip(fmt.Sprintf("Acceptance tests skipped unless env '%s' set", logicaltest.TestEnvVar))
|
|
|
|
return
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
b, _ := Factory(context.Background(), logical.TestBackendConfig())
|
2015-11-18 16:25:42 +00:00
|
|
|
|
2018-04-04 20:09:26 +00:00
|
|
|
cleanup, uri, _ := prepareRabbitMQTestContainer(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
2015-11-18 16:25:42 +00:00
|
|
|
logicaltest.Test(t, logicaltest.TestCase{
|
2018-04-04 20:09:26 +00:00
|
|
|
PreCheck: testAccPreCheckFunc(t, uri),
|
2015-11-18 16:25:42 +00:00
|
|
|
Backend: b,
|
|
|
|
Steps: []logicaltest.TestStep{
|
2018-04-04 20:09:26 +00:00
|
|
|
testAccStepConfig(t, uri),
|
2015-11-18 16:25:42 +00:00
|
|
|
testAccStepRole(t),
|
2018-04-04 20:09:26 +00:00
|
|
|
testAccStepReadCreds(t, b, uri, "web"),
|
2015-11-18 16:25:42 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackend_roleCrud(t *testing.T) {
|
2016-06-08 17:00:19 +00:00
|
|
|
if os.Getenv(logicaltest.TestEnvVar) == "" {
|
|
|
|
t.Skip(fmt.Sprintf("Acceptance tests skipped unless env '%s' set", logicaltest.TestEnvVar))
|
|
|
|
return
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
b, _ := Factory(context.Background(), logical.TestBackendConfig())
|
2015-11-18 16:25:42 +00:00
|
|
|
|
2018-04-04 20:09:26 +00:00
|
|
|
cleanup, uri, _ := prepareRabbitMQTestContainer(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
2015-11-18 16:25:42 +00:00
|
|
|
logicaltest.Test(t, logicaltest.TestCase{
|
2018-04-04 20:09:26 +00:00
|
|
|
PreCheck: testAccPreCheckFunc(t, uri),
|
|
|
|
Backend: b,
|
2015-11-18 16:25:42 +00:00
|
|
|
Steps: []logicaltest.TestStep{
|
2018-04-04 20:09:26 +00:00
|
|
|
testAccStepConfig(t, uri),
|
2015-11-18 16:25:42 +00:00
|
|
|
testAccStepRole(t),
|
|
|
|
testAccStepReadRole(t, "web", "administrator", `{"/": {"configure": ".*", "write": ".*", "read": ".*"}}`),
|
|
|
|
testAccStepDeleteRole(t, "web"),
|
|
|
|
testAccStepReadRole(t, "web", "", ""),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-04-04 20:09:26 +00:00
|
|
|
func testAccPreCheckFunc(t *testing.T, uri string) func() {
|
|
|
|
return func() {
|
|
|
|
if uri == "" {
|
|
|
|
t.Fatal("RabbitMQ URI must be set for acceptance tests")
|
|
|
|
}
|
2015-11-18 16:25:42 +00:00
|
|
|
}
|
2018-04-04 20:09:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func testAccStepConfig(t *testing.T, uri string) logicaltest.TestStep {
|
|
|
|
username := os.Getenv(envRabbitMQUsername)
|
|
|
|
if len(username) == 0 {
|
|
|
|
username = "guest"
|
2015-11-18 16:25:42 +00:00
|
|
|
}
|
2018-04-04 20:09:26 +00:00
|
|
|
password := os.Getenv(envRabbitMQPassword)
|
|
|
|
if len(password) == 0 {
|
|
|
|
password = "guest"
|
2015-11-18 16:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return logicaltest.TestStep{
|
2016-05-21 05:51:09 +00:00
|
|
|
Operation: logical.UpdateOperation,
|
2015-11-18 16:25:42 +00:00
|
|
|
Path: "config/connection",
|
|
|
|
Data: map[string]interface{}{
|
2018-04-04 20:09:26 +00:00
|
|
|
"connection_uri": uri,
|
|
|
|
"username": username,
|
|
|
|
"password": password,
|
2015-11-18 16:25:42 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccStepRole(t *testing.T) logicaltest.TestStep {
|
|
|
|
return logicaltest.TestStep{
|
2016-05-21 05:51:09 +00:00
|
|
|
Operation: logical.UpdateOperation,
|
2015-11-18 16:25:42 +00:00
|
|
|
Path: "roles/web",
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"tags": "administrator",
|
|
|
|
"vhosts": `{"/": {"configure": ".*", "write": ".*", "read": ".*"}}`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccStepDeleteRole(t *testing.T, n string) logicaltest.TestStep {
|
|
|
|
return logicaltest.TestStep{
|
|
|
|
Operation: logical.DeleteOperation,
|
|
|
|
Path: "roles/" + n,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-04 20:09:26 +00:00
|
|
|
func testAccStepReadCreds(t *testing.T, b logical.Backend, uri, name string) logicaltest.TestStep {
|
2015-11-18 16:25:42 +00:00
|
|
|
return logicaltest.TestStep{
|
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "creds/" + name,
|
|
|
|
Check: func(resp *logical.Response) error {
|
|
|
|
var d struct {
|
|
|
|
Username string `mapstructure:"username"`
|
|
|
|
Password string `mapstructure:"password"`
|
|
|
|
}
|
|
|
|
if err := mapstructure.Decode(resp.Data, &d); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Printf("[WARN] Generated credentials: %v", d)
|
|
|
|
|
|
|
|
client, err := rabbithole.NewClient(uri, d.Username, d.Password)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = client.ListVhosts()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to list vhosts with generated credentials: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-01-08 20:26:13 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), &logical.Request{
|
2015-11-18 16:25:42 +00:00
|
|
|
Operation: logical.RevokeOperation,
|
|
|
|
Secret: &logical.Secret{
|
|
|
|
InternalData: map[string]interface{}{
|
|
|
|
"secret_type": "creds",
|
|
|
|
"username": d.Username,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
if resp.IsError() {
|
2018-04-09 18:35:21 +00:00
|
|
|
return fmt.Errorf("error on resp: %#v", *resp)
|
2015-11-18 16:25:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err = rabbithole.NewClient(uri, d.Username, d.Password)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = client.ListVhosts()
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected to fail listing vhosts: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccStepReadRole(t *testing.T, name, tags, rawVHosts string) logicaltest.TestStep {
|
|
|
|
return logicaltest.TestStep{
|
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "roles/" + name,
|
|
|
|
Check: func(resp *logical.Response) error {
|
|
|
|
if resp == nil {
|
|
|
|
if tags == "" && rawVHosts == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
var d struct {
|
|
|
|
Tags string `mapstructure:"tags"`
|
|
|
|
VHosts map[string]vhostPermission `mapstructure:"vhosts"`
|
|
|
|
}
|
|
|
|
if err := mapstructure.Decode(resp.Data, &d); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.Tags != tags {
|
|
|
|
return fmt.Errorf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
var vhosts map[string]vhostPermission
|
2016-07-06 16:25:40 +00:00
|
|
|
if err := jsonutil.DecodeJSON([]byte(rawVHosts), &vhosts); err != nil {
|
2015-11-18 16:25:42 +00:00
|
|
|
return fmt.Errorf("bad expected vhosts %#v: %s", vhosts, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for host, permission := range vhosts {
|
|
|
|
actualPermission, ok := d.VHosts[host]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("expected vhost: %s", host)
|
|
|
|
}
|
|
|
|
|
|
|
|
if actualPermission.Configure != permission.Configure {
|
2016-05-21 06:28:00 +00:00
|
|
|
return fmt.Errorf("expected permission %s to be %s, got %s", "configure", permission.Configure, actualPermission.Configure)
|
2015-11-18 16:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if actualPermission.Write != permission.Write {
|
2016-05-21 06:28:00 +00:00
|
|
|
return fmt.Errorf("expected permission %s to be %s, got %s", "write", permission.Write, actualPermission.Write)
|
2015-11-18 16:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if actualPermission.Read != permission.Read {
|
2016-05-21 06:28:00 +00:00
|
|
|
return fmt.Errorf("expected permission %s to be %s, got %s", "read", permission.Read, actualPermission.Read)
|
2015-11-18 16:25:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|