From 8e92e3dc3d3593aa466d0204372cc3cd4b765028 Mon Sep 17 00:00:00 2001 From: Joel Speed Date: Sat, 8 Jun 2019 23:12:56 +0200 Subject: [PATCH] Test overriding values with environment variables --- pkg/apis/options/options.go | 6 +++--- pkg/apis/options/options_test.go | 36 ++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/pkg/apis/options/options.go b/pkg/apis/options/options.go index a422985..06ae36e 100644 --- a/pkg/apis/options/options.go +++ b/pkg/apis/options/options.go @@ -46,9 +46,9 @@ func Load(config io.Reader, configType string, args []string) (*Options, error) // Configure loading of environment variables // All flag options are prefixed by the EnvPrefix - v.SetEnvPrefix("OAUTH2_PROXY_") - // Substitute "-" for "_" so `FOO_BAR` matches the flag `foo-bar` - v.SetEnvKeyReplacer(strings.NewReplacer("-", "_")) + v.SetEnvPrefix("OAUTH2_PROXY") + // Substitute "-" for "_" so `FOO_BAR` matches the config `foo.bar` + v.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) v.AutomaticEnv() // Read the configuration file diff --git a/pkg/apis/options/options_test.go b/pkg/apis/options/options_test.go index 4f273e6..03e0f89 100644 --- a/pkg/apis/options/options_test.go +++ b/pkg/apis/options/options_test.go @@ -3,6 +3,7 @@ package options import ( "bytes" "io" + "os" "testing" "time" @@ -62,6 +63,10 @@ var _ = Describe("Load", func() { `)) }) + It("returns no error", func() { + Expect(err).NotTo(HaveOccurred()) + }) + It("sets the correct configuration", func() { expected := &CookieOptions{ Name: "cookie_name", @@ -76,5 +81,36 @@ var _ = Describe("Load", func() { Expect(opts.Cookie).To(Equal(expected)) }) + Context("with environment configuration", func() { + BeforeEach(func() { + os.Setenv("OAUTH2_PROXY_COOKIE_NAME", "env_cookie_name") + os.Setenv("OAUTH2_PROXY_COOKIE_SECRET", "env_secret_12345") + os.Setenv("OAUTH2_PROXY_COOKIE_DOMAIN", "env.example.com") + os.Setenv("OAUTH2_PROXY_COOKIE_PATH", "/env") + os.Setenv("OAUTH2_PROXY_COOKIE_EXPIRE", "24h") + os.Setenv("OAUTH2_PROXY_COOKIE_REFRESH", "2h") + os.Setenv("OAUTH2_PROXY_COOKIE_SECURE", "true") + os.Setenv("OAUTH2_PROXY_COOKIE_HTTPONLY", "true") + }) + + It("returns no error", func() { + Expect(err).NotTo(HaveOccurred()) + }) + + It("the environment overrides the config file", func() { + expected := &CookieOptions{ + Name: "env_cookie_name", + Secret: "env_secret_12345", + Domain: "env.example.com", + Path: "/env", + Expire: time.Duration(24) * time.Hour, + Refresh: time.Duration(2) * time.Hour, + Secure: true, + HTTPOnly: true, + } + Expect(opts.Cookie).To(Equal(expected)) + }) + }) + }) })