Skip to content

Commit

Permalink
Fix configuration with env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
hypnoglow committed Oct 24, 2019
1 parent 40e3b89 commit 03d7af1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
6 changes: 3 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@ import (
"github.com/spf13/cobra"

"github.com/ory/gojsonschema"

"github.com/ory/oathkeeper/driver/configuration"
"github.com/ory/viper"
"github.com/ory/x/viperx"

"github.com/ory/x/logrusx"
"github.com/ory/x/viperx"
)

var schemas = packr.New("schemas", "../.schemas")
Expand Down Expand Up @@ -63,6 +62,7 @@ func init() {
}

cobra.OnInitialize(func() {
configuration.BindEnvs()
viperx.InitializeConfig("oathkeeper", "", nil)

logger = logrusx.New()
Expand Down
11 changes: 8 additions & 3 deletions driver/configuration/provider_viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const (
)

func BindEnvs() {
if err := viper.BindEnv(
keys := []string{
ViperKeyProxyReadTimeout,
ViperKeyProxyWriteTimeout,
ViperKeyProxyIdleTimeout,
Expand All @@ -112,8 +112,13 @@ func BindEnvs() {
ViperKeyAuthenticatorOAuth2ClientCredentialsIsEnabled,
ViperKeyAuthenticatorOAuth2TokenIntrospectionIsEnabled,
ViperKeyAuthenticatorUnauthorizedIsEnabled,
); err != nil {
panic(err.Error())
}

for _, key := range keys {
env := strings.ToUpper(strings.ReplaceAll(key, ".", "_"))
if err := viper.BindEnv(key, env); err != nil {
panic(err.Error())
}
}
}

Expand Down
19 changes: 19 additions & 0 deletions driver/configuration/provider_viper_public_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/url"
"os"
"testing"

"github.com/rs/cors"
Expand Down Expand Up @@ -342,3 +343,21 @@ func TestAuthenticatorOAuth2TokenIntrospectionPreAuthorization(t *testing.T) {
})
}
}

func TestViperProvider_envs(t *testing.T) {
p := NewViperProvider(logrus.New())
BindEnvs()

assert.Equal(t, ":4455", p.ProxyServeAddress())
assert.Equal(t, false, p.AuthenticatorIsEnabled("oauth2_introspection"))

err := os.Setenv("SERVE_PROXY_HOST", "foo")
require.NoError(t, err)
err = os.Setenv("SERVE_PROXY_PORT", "8080")
require.NoError(t, err)
err = os.Setenv("AUTHENTICATORS_OAUTH2_INTROSPECTION_ENABLED", "true")
require.NoError(t, err)

assert.Equal(t, "foo:8080", p.ProxyServeAddress())
assert.Equal(t, true, p.AuthenticatorIsEnabled("oauth2_introspection"))
}

0 comments on commit 03d7af1

Please sign in to comment.