Skip to content

Commit

Permalink
Added STATS_USER_ENV and STATS_PASS_ENV [fixes #186]
Browse files Browse the repository at this point in the history
  • Loading branch information
vfarcic committed Mar 20, 2017
1 parent 32ad351 commit 268fa36
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ ENV CONNECTION_MODE="http-server-close" \
MODE="default" \
PROXY_INSTANCE_NAME="docker-flow" \
SERVICE_NAME="proxy" \
STATS_USER="admin" STATS_PASS="admin" \
STATS_USER="admin" STATS_USER_ENV="STATS_USER" STATS_PASS="admin" STATS_PASS_ENV="STATS_PASS" \
TIMEOUT_HTTP_REQUEST="5" TIMEOUT_HTTP_KEEP_ALIVE="15" TIMEOUT_CLIENT="20" TIMEOUT_CONNECT="5" TIMEOUT_QUEUE="30" TIMEOUT_SERVER="20" TIMEOUT_TUNNEL="3600" \
USERS="" \
EXTRA_FRONTEND="" \
Expand Down
2 changes: 2 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ The following environment variables can be used to configure the *Docker Flow Pr
|SERVICE_NAME |The name of the service. It must be the same as the value of the `--name` argument used to create the proxy service. Used only in the *swarm* mode.|No|proxy|my-proxy|
|SKIP_ADDRESS_VALIDATION|Whether to skip validating service address before reconfiguring the proxy.|No|false|true|
|STATS_USER |Username for the statistics page |No |admin |my-user|
|STATS_USER_ENV |The name of the environment variable that holds the username for the statistics page|No|STATS_USER|MY_USER|
|STATS_PASS |Password for the statistics page |No |admin |my-pass|
|STATS_PASS_ENV |The name of the environment variable that holds the password for the statistics page|No|STATS_PASS|MY_PASS|
|TIMEOUT_CLIENT |The client timeout in seconds |No |20 |5 |
|TIMEOUT_CONNECT |The connect timeout in seconds |No |5 |3 |
|TIMEOUT_QUEUE |The queue timeout in seconds |No |30 |10 |
Expand Down
4 changes: 2 additions & 2 deletions proxy/ha_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ func (m HaProxy) getConfigData() ConfigData {
d.TimeoutTunnel = GetSecretOrEnvVar("TIMEOUT_TUNNEL", "3600")
d.TimeoutHttpRequest = GetSecretOrEnvVar("TIMEOUT_HTTP_REQUEST", "5")
d.TimeoutHttpKeepAlive = GetSecretOrEnvVar("TIMEOUT_HTTP_KEEP_ALIVE", "15")
d.StatsUser = GetSecretOrEnvVar("STATS_USER", "admin")
d.StatsPass = GetSecretOrEnvVar("STATS_PASS", "admin")
d.StatsUser = GetSecretOrEnvVar(os.Getenv("STATS_USER_ENV"), "admin")
d.StatsPass = GetSecretOrEnvVar(os.Getenv("STATS_PASS_ENV"), "admin")
usersString := GetSecretOrEnvVar("USERS", "")
encryptedString := GetSecretOrEnvVar("USERS_PASS_ENCRYPTED", "")
if len(usersString) > 0 {
Expand Down
42 changes: 40 additions & 2 deletions proxy/ha_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ config2 fe content
config1 be content
config2 be content`
os.Setenv("STATS_USER_ENV", "STATS_USER")
os.Setenv("STATS_PASS_ENV", "STATS_PASS")
suite.Run(t, s)
}

Expand Down Expand Up @@ -1107,7 +1109,7 @@ func (s HaProxyTestSuite) Test_CreateConfigFromTemplates_ReplacesValuesWithEnvVa
{"STATS_PASS", "stats auth admin:admin", "stats auth admin:my-pass", "my-pass"},
}
for _, t := range tests {
timeoutOrig := os.Getenv(t.envKey)
envOrig := os.Getenv(t.envKey)
os.Setenv(t.envKey, t.value)
var actualData string
expectedData := fmt.Sprintf(
Expand All @@ -1124,7 +1126,43 @@ func (s HaProxyTestSuite) Test_CreateConfigFromTemplates_ReplacesValuesWithEnvVa

s.Equal(expectedData, actualData)

os.Setenv(t.envKey, timeoutOrig)
os.Setenv(t.envKey, envOrig)
}
}

func (s HaProxyTestSuite) Test_CreateConfigFromTemplates_UsersStatsUserEnvAndStatsPassEnv() {
tests := []struct {
envKey string
before string
after string
value string
envKeyName string
}{
{"MY_USER", "stats auth admin:admin", "stats auth my-user:admin", "my-user", "STATS_USER_ENV"},
{"MY_PASS", "stats auth admin:admin", "stats auth admin:my-pass", "my-pass", "STATS_PASS_ENV"},
}
for _, t := range tests {
os.Setenv(t.envKeyName, t.envKey)
envOrig := os.Getenv(t.envKey)
envKeyOrig := os.Getenv(t.envKeyName)
os.Setenv(t.envKey, t.value)
var actualData string
expectedData := fmt.Sprintf(
"%s%s",
strings.Replace(s.TemplateContent, t.before, t.after, -1),
s.ServicesContent,
)
writeFile = func(filename string, data []byte, perm os.FileMode) error {
actualData = string(data)
return nil
}

NewHaProxy(s.TemplatesPath, s.ConfigsPath).CreateConfigFromTemplates()

s.Equal(expectedData, actualData)

os.Setenv(t.envKey, envOrig)
os.Setenv(t.envKeyName, envKeyOrig)
}
}

Expand Down

0 comments on commit 268fa36

Please sign in to comment.