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"
|
|
|
|
"testing"
|
|
|
|
|
2021-07-16 00:17:31 +00:00
|
|
|
"github.com/hashicorp/go-secure-stdlib/base62"
|
2019-04-22 16:26:10 +00:00
|
|
|
"github.com/hashicorp/vault/helper/testhelpers/docker"
|
2020-09-15 14:01:26 +00:00
|
|
|
logicaltest "github.com/hashicorp/vault/helper/testhelpers/logical"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/jsonutil"
|
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2021-10-20 16:46:37 +00:00
|
|
|
rabbithole "github.com/michaelklishin/rabbit-hole/v2"
|
2015-11-18 16:25:42 +00:00
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
)
|
|
|
|
|
2018-04-04 20:09:26 +00:00
|
|
|
const (
|
|
|
|
envRabbitMQConnectionURI = "RABBITMQ_CONNECTION_URI"
|
|
|
|
envRabbitMQUsername = "RABBITMQ_USERNAME"
|
|
|
|
envRabbitMQPassword = "RABBITMQ_PASSWORD"
|
|
|
|
)
|
|
|
|
|
2019-10-30 21:19:49 +00:00
|
|
|
const (
|
|
|
|
testTags = "administrator"
|
|
|
|
testVHosts = `{"/": {"configure": ".*", "write": ".*", "read": ".*"}}`
|
|
|
|
testVHostTopics = `{"/": {"amq.topic": {"write": ".*", "read": ".*"}}}`
|
2020-06-11 22:08:20 +00:00
|
|
|
|
|
|
|
roleName = "web"
|
2019-10-30 21:19:49 +00:00
|
|
|
)
|
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
func prepareRabbitMQTestContainer(t *testing.T) (func(), string) {
|
2018-04-04 20:09:26 +00:00
|
|
|
if os.Getenv(envRabbitMQConnectionURI) != "" {
|
2020-09-15 14:01:26 +00:00
|
|
|
return func() {}, os.Getenv(envRabbitMQConnectionURI)
|
2018-04-04 20:09:26 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
runner, err := docker.NewServiceRunner(docker.RunOptions{
|
2022-11-02 17:33:17 +00:00
|
|
|
ImageRepo: "docker.mirror.hashicorp.services/library/rabbitmq",
|
2021-10-20 16:46:37 +00:00
|
|
|
ImageTag: "3-management",
|
2020-09-15 14:01:26 +00:00
|
|
|
ContainerName: "rabbitmq",
|
|
|
|
Ports: []string{"15672/tcp"},
|
|
|
|
})
|
2018-04-04 20:09:26 +00:00
|
|
|
if err != nil {
|
2020-09-15 14:01:26 +00:00
|
|
|
t.Fatalf("could not start docker rabbitmq: %s", err)
|
2018-04-04 20:09:26 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
svc, err := runner.StartService(context.Background(), func(ctx context.Context, host string, port int) (docker.ServiceConfig, error) {
|
|
|
|
connURL := fmt.Sprintf("http://%s:%d", host, port)
|
|
|
|
rmqc, err := rabbithole.NewClient(connURL, "guest", "guest")
|
2018-04-04 20:09:26 +00:00
|
|
|
if err != nil {
|
2020-09-15 14:01:26 +00:00
|
|
|
return nil, err
|
2018-04-04 20:09:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = rmqc.Overview()
|
|
|
|
if err != nil {
|
2020-09-15 14:01:26 +00:00
|
|
|
return nil, err
|
2018-04-04 20:09:26 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
return docker.NewServiceURLParse(connURL)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not start docker rabbitmq: %s", err)
|
2018-04-04 20:09:26 +00:00
|
|
|
}
|
2020-09-15 14:01:26 +00:00
|
|
|
return svc.Cleanup, svc.Config.URL().String()
|
2018-04-04 20:09:26 +00:00
|
|
|
}
|
|
|
|
|
2015-11-18 16:25:42 +00:00
|
|
|
func TestBackend_basic(t *testing.T) {
|
2018-01-19 06:44:44 +00:00
|
|
|
b, _ := Factory(context.Background(), logical.TestBackendConfig())
|
2015-11-18 16:25:42 +00:00
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
cleanup, uri := prepareRabbitMQTestContainer(t)
|
2018-04-04 20:09:26 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
2015-11-18 16:25:42 +00:00
|
|
|
logicaltest.Test(t, logicaltest.TestCase{
|
2018-11-07 01:21:24 +00:00
|
|
|
PreCheck: testAccPreCheckFunc(t, uri),
|
|
|
|
LogicalBackend: b,
|
2015-11-18 16:25:42 +00:00
|
|
|
Steps: []logicaltest.TestStep{
|
2020-06-11 22:08:20 +00:00
|
|
|
testAccStepConfig(t, uri, ""),
|
2015-11-18 16:25:42 +00:00
|
|
|
testAccStepRole(t),
|
2020-06-11 22:08:20 +00:00
|
|
|
testAccStepReadCreds(t, b, uri, roleName),
|
2015-11-18 16:25:42 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-27 21:14:59 +00:00
|
|
|
func TestBackend_returnsErrs(t *testing.T) {
|
|
|
|
b, _ := Factory(context.Background(), logical.TestBackendConfig())
|
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
cleanup, uri := prepareRabbitMQTestContainer(t)
|
2020-03-27 21:14:59 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
logicaltest.Test(t, logicaltest.TestCase{
|
|
|
|
PreCheck: testAccPreCheckFunc(t, uri),
|
|
|
|
LogicalBackend: b,
|
|
|
|
Steps: []logicaltest.TestStep{
|
2020-06-11 22:08:20 +00:00
|
|
|
testAccStepConfig(t, uri, ""),
|
2020-03-27 21:14:59 +00:00
|
|
|
{
|
|
|
|
Operation: logical.CreateOperation,
|
2020-06-11 22:08:20 +00:00
|
|
|
Path: fmt.Sprintf("roles/%s", roleName),
|
2020-03-27 21:14:59 +00:00
|
|
|
Data: map[string]interface{}{
|
|
|
|
"tags": testTags,
|
|
|
|
"vhosts": `{"invalid":{"write": ".*", "read": ".*"}}`,
|
|
|
|
"vhost_topics": testVHostTopics,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Operation: logical.ReadOperation,
|
2020-06-11 22:08:20 +00:00
|
|
|
Path: fmt.Sprintf("creds/%s", roleName),
|
2020-03-27 21:14:59 +00:00
|
|
|
ErrorOk: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-11-18 16:25:42 +00:00
|
|
|
func TestBackend_roleCrud(t *testing.T) {
|
2018-01-19 06:44:44 +00:00
|
|
|
b, _ := Factory(context.Background(), logical.TestBackendConfig())
|
2015-11-18 16:25:42 +00:00
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
cleanup, uri := prepareRabbitMQTestContainer(t)
|
2018-04-04 20:09:26 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
2015-11-18 16:25:42 +00:00
|
|
|
logicaltest.Test(t, logicaltest.TestCase{
|
2018-11-07 01:21:24 +00:00
|
|
|
PreCheck: testAccPreCheckFunc(t, uri),
|
|
|
|
LogicalBackend: b,
|
2015-11-18 16:25:42 +00:00
|
|
|
Steps: []logicaltest.TestStep{
|
2020-06-11 22:08:20 +00:00
|
|
|
testAccStepConfig(t, uri, ""),
|
|
|
|
testAccStepRole(t),
|
|
|
|
testAccStepReadRole(t, roleName, testTags, testVHosts, testVHostTopics),
|
|
|
|
testAccStepDeleteRole(t, roleName),
|
|
|
|
testAccStepReadRole(t, roleName, "", "", ""),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackend_roleWithPasswordPolicy(t *testing.T) {
|
|
|
|
if os.Getenv(logicaltest.TestEnvVar) == "" {
|
2022-08-03 18:32:45 +00:00
|
|
|
t.Skip(fmt.Sprintf("Acceptance tests skipped unless env %q set", logicaltest.TestEnvVar))
|
2020-06-11 22:08:20 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
backendConfig := logical.TestBackendConfig()
|
2020-06-17 20:24:38 +00:00
|
|
|
passGen := func() (password string, err error) {
|
|
|
|
return base62.Random(30)
|
|
|
|
}
|
|
|
|
backendConfig.System.(*logical.StaticSystemView).SetPasswordPolicy("testpolicy", passGen)
|
2020-06-11 22:08:20 +00:00
|
|
|
b, _ := Factory(context.Background(), backendConfig)
|
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
cleanup, uri := prepareRabbitMQTestContainer(t)
|
2020-06-11 22:08:20 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
logicaltest.Test(t, logicaltest.TestCase{
|
|
|
|
PreCheck: testAccPreCheckFunc(t, uri),
|
|
|
|
LogicalBackend: b,
|
|
|
|
Steps: []logicaltest.TestStep{
|
|
|
|
testAccStepConfig(t, uri, "testpolicy"),
|
2015-11-18 16:25:42 +00:00
|
|
|
testAccStepRole(t),
|
2020-06-11 22:08:20 +00:00
|
|
|
testAccStepReadCreds(t, b, uri, roleName),
|
2015-11-18 16:25:42 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-06-11 22:08:20 +00:00
|
|
|
func testAccStepConfig(t *testing.T, uri string, passwordPolicy string) logicaltest.TestStep {
|
2018-04-04 20:09:26 +00:00
|
|
|
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{}{
|
2020-06-11 22:08:20 +00:00
|
|
|
"connection_uri": uri,
|
|
|
|
"username": username,
|
|
|
|
"password": password,
|
|
|
|
"password_policy": passwordPolicy,
|
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,
|
2020-06-11 22:08:20 +00:00
|
|
|
Path: fmt.Sprintf("roles/%s", roleName),
|
2015-11-18 16:25:42 +00:00
|
|
|
Data: map[string]interface{}{
|
2019-10-30 21:19:49 +00:00
|
|
|
"tags": testTags,
|
|
|
|
"vhosts": testVHosts,
|
|
|
|
"vhost_topics": testVHostTopics,
|
2015-11-18 16:25:42 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-30 21:19:49 +00:00
|
|
|
func testAccStepReadRole(t *testing.T, name, tags, rawVHosts string, rawVHostTopics string) logicaltest.TestStep {
|
2015-11-18 16:25:42 +00:00
|
|
|
return logicaltest.TestStep{
|
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "roles/" + name,
|
|
|
|
Check: func(resp *logical.Response) error {
|
|
|
|
if resp == nil {
|
2019-10-30 21:19:49 +00:00
|
|
|
if tags == "" && rawVHosts == "" && rawVHostTopics == "" {
|
2015-11-18 16:25:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
var d struct {
|
2019-10-30 21:19:49 +00:00
|
|
|
Tags string `mapstructure:"tags"`
|
|
|
|
VHosts map[string]vhostPermission `mapstructure:"vhosts"`
|
|
|
|
VHostTopics map[string]map[string]vhostTopicPermission `mapstructure:"vhost_topics"`
|
2015-11-18 16:25:42 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-30 21:19:49 +00:00
|
|
|
var vhostTopics map[string]map[string]vhostTopicPermission
|
|
|
|
if err := jsonutil.DecodeJSON([]byte(rawVHostTopics), &vhostTopics); err != nil {
|
|
|
|
return fmt.Errorf("bad expected vhostTopics %#v: %s", vhostTopics, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for host, permissions := range vhostTopics {
|
|
|
|
for exchange, permission := range permissions {
|
|
|
|
actualPermissions, ok := d.VHostTopics[host]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("expected vhost topics: %s", host)
|
|
|
|
}
|
|
|
|
|
|
|
|
actualPermission, ok := actualPermissions[exchange]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("expected vhost topic exchange: %s", exchange)
|
|
|
|
}
|
|
|
|
|
|
|
|
if actualPermission.Write != permission.Write {
|
|
|
|
return fmt.Errorf("expected permission %s to be %s, got %s", "write", permission.Write, actualPermission.Write)
|
|
|
|
}
|
|
|
|
|
|
|
|
if actualPermission.Read != permission.Read {
|
|
|
|
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
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|