Skip to content

Commit

Permalink
✨ Adding VHost Support (#119)
Browse files Browse the repository at this point in the history
* ✨ Implemented Support for VHost
* ✅ Updated Tests for VHost
* 📝 Documented VHost Env usage
  • Loading branch information
Templum authored May 3, 2021
1 parent 972bdf2 commit 0f20540
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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*
Expand Down
15 changes: 9 additions & 6 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)

Expand All @@ -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) {
Expand Down
8 changes: 5 additions & 3 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand Down

0 comments on commit 0f20540

Please sign in to comment.