diff --git a/README.md b/README.md index 3c0b24aa..23207c23 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,7 @@ RabbitMQ Related: * `RMQ_HOST`: Hostname/ip of Rabbit MQ * `RMQ_PORT`: Port of Rabbit MQ +* `RMQ_VHOST`: Used to specify the vhost for Rabbit MQ, will default to `/` * `RMQ_USER`: Defaults to `guest` * `RMQ_PASS`: Defaults to `pass` * `PATH_TO_TOPOLOGY`: Path to the yaml describing the topology, has _no_ default and is *required* diff --git a/pkg/config/config.go b/pkg/config/config.go index e09bdbe5..f46c748d 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -82,10 +82,11 @@ const ( envSkipVerify = "INSECURE_SKIP_VERIFY" envMaxClientsPerHost = "MAX_CLIENT_PER_HOST" - envRabbitUser = "RMQ_USER" - envRabbitPass = "RMQ_PASS" - envRabbitHost = "RMQ_HOST" - envRabbitPort = "RMQ_PORT" + envRabbitUser = "RMQ_USER" + envRabbitPass = "RMQ_PASS" + envRabbitHost = "RMQ_HOST" + envRabbitPort = "RMQ_PORT" + envRabbitVHost = "RMQ_VHOST" envPathToTopology = "PATH_TO_TOPOLOGY" envRefreshTime = "TOPIC_MAP_REFRESH_TIME" @@ -109,6 +110,7 @@ func getRabbitMQConnectionURL() (string, error) { pass := readFromEnv(envRabbitPass, "pass") host := readFromEnv(envRabbitHost, "localhost") port := readFromEnv(envRabbitPort, "5672") + vhost := readFromEnv(envRabbitVHost, "") parsedPort, err := strconv.Atoi(port) @@ -122,13 +124,14 @@ func getRabbitMQConnectionURL() (string, error) { return "", errors.New(message) } - return fmt.Sprintf("amqp://%s:%s@%s:%s/", user, pass, host, port), nil + return fmt.Sprintf("amqp://%s:%s@%s:%s/%s", user, pass, host, port, vhost), nil } func getSanitizedRabbitMQURL() string { host := readFromEnv(envRabbitHost, "localhost") port := readFromEnv(envRabbitPort, "5672") - return fmt.Sprintf("amqp://%s:%s", host, port) + vhost := readFromEnv(envRabbitVHost, "") + return fmt.Sprintf("amqp://%s:%s/%s", host, port, vhost) } func getTopology() (internal.Topology, error) { diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 8e8c0b2e..04c98a77 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -132,7 +132,7 @@ func TestNewConfig(t *testing.T) { assert.Equal(t, config.GatewayURL, "http://gateway:8080", "Expected default value") assert.Equal(t, config.RabbitConnectionURL, "amqp://user:pass@localhost:5672/", "Expected default value") assert.NotContains(t, config.RabbitSanitizedURL, "user:pass", "Expected credentials not to be present") - assert.Equal(t, config.RabbitSanitizedURL, "amqp://localhost:5672", "Expected default value") + assert.Equal(t, config.RabbitSanitizedURL, "amqp://localhost:5672/", "Expected default value") assert.Equal(t, config.TopicRefreshTime, 30*time.Second, "Expected default value") assert.False(t, config.InsecureSkipVerify, "Expected default value") assert.Equal(t, config.MaxClientsPerHost, 256, "Expected default value") @@ -144,6 +144,7 @@ func TestNewConfig(t *testing.T) { os.Setenv("RMQ_PORT", "1337") os.Setenv("RMQ_USER", "username") os.Setenv("RMQ_PASS", "password") + os.Setenv("RMQ_VHOST", "other") os.Setenv("OPEN_FAAS_GW_URL", "https://gateway") os.Setenv("TOPIC_MAP_REFRESH_TIME", "40s") os.Setenv("INSECURE_SKIP_VERIFY", "true") @@ -154,6 +155,7 @@ func TestNewConfig(t *testing.T) { defer os.Unsetenv("RMQ_PORT") defer os.Unsetenv("RMQ_USER") defer os.Unsetenv("RMQ_PASS") + defer os.Unsetenv("RMQ_VHOST") defer os.Unsetenv("OPEN_FAAS_GW_URL") defer os.Unsetenv("TOPIC_MAP_REFRESH_TIME") defer os.Unsetenv("INSECURE_SKIP_VERIFY") @@ -163,9 +165,9 @@ func TestNewConfig(t *testing.T) { assert.Nil(t, err, "Should not throw") assert.Equal(t, config.GatewayURL, "https://gateway", "Expected override value") - assert.Equal(t, config.RabbitConnectionURL, "amqp://username:password@rabbit:1337/", "Expected override value") + assert.Equal(t, config.RabbitConnectionURL, "amqp://username:password@rabbit:1337/other", "Expected override value") assert.NotContains(t, config.RabbitSanitizedURL, "username:password", "Expected credentials not to be present") - assert.Equal(t, config.RabbitSanitizedURL, "amqp://rabbit:1337", "Expected override value") + assert.Equal(t, config.RabbitSanitizedURL, "amqp://rabbit:1337/other", "Expected override value") assert.Equal(t, config.TopicRefreshTime, 40*time.Second, "Expected override value") assert.True(t, config.InsecureSkipVerify, "Expected override value") assert.Equal(t, config.MaxClientsPerHost, 512, "Expected override value")