From 765443bc4178b540dccd4b64e3874f3f7079150b Mon Sep 17 00:00:00 2001 From: Joel Speed Date: Sun, 9 Jun 2019 10:16:44 +0200 Subject: [PATCH] Ensure flag values are set correctly --- pkg/apis/options/options.go | 12 +++++++++++ pkg/apis/options/options_test.go | 36 +++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/pkg/apis/options/options.go b/pkg/apis/options/options.go index 06ae36e..b6cd3bd 100644 --- a/pkg/apis/options/options.go +++ b/pkg/apis/options/options.go @@ -29,6 +29,7 @@ func New() *Options { // correct configuration options func Load(config io.Reader, configType string, args []string) (*Options, error) { flagSet := flag.NewFlagSet("oauth2-proxy", flag.ExitOnError) + flagSet.SetNormalizeFunc(wordSepNormalizeFunc) // Add FlagSets to main flagSet flagSet.AddFlagSet(cookieFlagSet) @@ -68,3 +69,14 @@ func Load(config io.Reader, configType string, args []string) (*Options, error) } return options, nil } + +// wordSepNormalizeFunc replaces "-" in flags entered with "." +// This ensures that flags are mapped to the correct values in the Options struct +func wordSepNormalizeFunc(f *flag.FlagSet, name string) flag.NormalizedName { + from := []string{"-"} + to := "." + for _, sep := range from { + name = strings.Replace(name, sep, to, -1) + } + return flag.NormalizedName(name) +} diff --git a/pkg/apis/options/options_test.go b/pkg/apis/options/options_test.go index 03e0f89..7fef1ba 100644 --- a/pkg/apis/options/options_test.go +++ b/pkg/apis/options/options_test.go @@ -110,7 +110,41 @@ var _ = Describe("Load", func() { } Expect(opts.Cookie).To(Equal(expected)) }) - }) + Context("with flag configuration", func() { + Context("with hyphens", func() { + BeforeEach(func() { + args = []string{ + "--cookie-name=flag_cookie_name", + "--cookie-secret=flag_secret_1234", + "--cookie-domain=flag.example.com", + "--cookie-path=/flag", + "--cookie-expire=48h", + "--cookie-refresh=4h", + "--cookie-secure=false", + "--cookie-httponly=false", + } + }) + + It("returns no error", func() { + Expect(err).NotTo(HaveOccurred()) + }) + + It("the environment overrides the config file", func() { + expected := &CookieOptions{ + Name: "flag_cookie_name", + Secret: "flag_secret_1234", + Domain: "flag.example.com", + Path: "/flag", + Expire: time.Duration(48) * time.Hour, + Refresh: time.Duration(4) * time.Hour, + Secure: false, + HTTPOnly: false, + } + Expect(opts.Cookie).To(Equal(expected)) + }) + }) + }) + }) }) })