git.iko.soy/typeable/oauth2-proxy
on master help sign in
Replace options loading with viper
commitc5be09ca483dd2cd8f23675c754ff12bb5f27704 authorJoel Speed <joel.speed@hotmail.co.uk> dateMon 13 Apr 2020, 11:34 (7 years ago) parent8749cbb
9 files changed+27−147
env_options.go−54 env_options_test.go−64 go.mod−2 go.sum+5−2 main.go+11−13 oauthproxy.go+2−2 options.go+5−6 options_test.go+1−1 pkg/apis/options/sessions.go+3−3
env_options.godeleted−54
@@ -1,54 +0,0 @@
1−package main
2−
3−import (
4− "os"
5− "reflect"
6− "strings"
7−)
8−
9−// EnvOptions holds program options loaded from the process environment
10−type EnvOptions map[string]interface{}
11−
12−// LoadEnvForStruct loads environment variables for each field in an options
13−// struct passed into it.
14−//
15−// Fields in the options struct must have an `env` and `cfg` tag to be read
16−// from the environment
17−func (cfg EnvOptions) LoadEnvForStruct(options interface{}) {
18− val := reflect.ValueOf(options)
19− var typ reflect.Type
20− if val.Kind() == reflect.Ptr {
21− typ = val.Elem().Type()
22− } else {
23− typ = val.Type()
24− }
25−
26− for i := 0; i < typ.NumField(); i++ {
27− // pull out the struct tags:
28− // flag - the name of the command line flag
29− // deprecated - (optional) the name of the deprecated command line flag
30− // cfg - (optional, defaults to underscored flag) the name of the config file option
31− field := typ.Field(i)
32− fieldV := reflect.Indirect(val).Field(i)
33−
34− if field.Type.Kind() == reflect.Struct {
35− cfg.LoadEnvForStruct(fieldV.Interface())
36− continue
37− }
38−
39− flagName := field.Tag.Get("flag")
40− envName := field.Tag.Get("env")
41− cfgName := field.Tag.Get("cfg")
42− if cfgName == "" && flagName != "" {
43− cfgName = strings.ReplaceAll(flagName, "-", "_")
44− }
45− if envName == "" || cfgName == "" {
46− // resolvable fields must have the `env` and `cfg` struct tag
47− continue
48− }
49− v := os.Getenv(envName)
50− if v != "" {
51− cfg[cfgName] = v
52− }
53− }
54−}
env_options_test.godeleted−64
@@ -1,64 +0,0 @@
1−package main_test
2−
3−import (
4− "os"
5− "testing"
6−
7− proxy "github.com/oauth2-proxy/oauth2-proxy"
8− "github.com/stretchr/testify/assert"
9−)
10−
11−type EnvTest struct {
12− TestField string `cfg:"target_field" env:"TEST_ENV_FIELD"`
13− EnvTestEmbed
14− Named EnvTestNamed
15−}
16−
17−type EnvTestEmbed struct {
18− TestFieldEmbed string `cfg:"target_field_embed" env:"TEST_ENV_FIELD_EMBED"`
19−}
20−
21−type EnvTestNamed struct {
22− TestFieldNamed string `cfg:"target_field_named" env:"TEST_ENV_FIELD_NAMED"`
23−}
24−
25−func TestLoadEnvForStruct(t *testing.T) {
26−
27− cfg := make(proxy.EnvOptions)
28− cfg.LoadEnvForStruct(&EnvTest{})
29−
30− _, ok := cfg["target_field"]
31− assert.Equal(t, ok, false)
32−
33− os.Setenv("TEST_ENV_FIELD", "1234abcd")
34− cfg.LoadEnvForStruct(&EnvTest{})
35− v := cfg["target_field"]
36− assert.Equal(t, v, "1234abcd")
37−}
38−
39−func TestLoadEnvForStructWithEmbeddedFields(t *testing.T) {
40−
41− cfg := make(proxy.EnvOptions)
42− cfg.LoadEnvForStruct(&EnvTest{})
43−
44− _, ok := cfg["target_field_embed"]
45− assert.Equal(t, ok, false)
46−
47− os.Setenv("TEST_ENV_FIELD_EMBED", "1234abcd")
48− cfg.LoadEnvForStruct(&EnvTest{})
49− v := cfg["target_field_embed"]
50− assert.Equal(t, v, "1234abcd")
51−}
52−
53−func TestLoadEnvForStructWithNamedStructFields(t *testing.T) {
54− cfg := make(proxy.EnvOptions)
55− cfg.LoadEnvForStruct(&EnvTest{})
56−
57− _, ok := cfg["target_field_named"]
58− assert.Equal(t, ok, false)
59−
60− os.Setenv("TEST_ENV_FIELD_NAMED", "1234abcd")
61− cfg.LoadEnvForStruct(&EnvTest{})
62− v := cfg["target_field_named"]
63− assert.Equal(t, v, "1234abcd")
64−}
go.mod−2
@@ -3,7 +3,6 @@ module github.com/oauth2-proxy/oauth2-proxy
33go 1.14
44
55require (
6− github.com/BurntSushi/toml v0.3.1
76 github.com/alicebob/miniredis/v2 v2.11.2
87 github.com/bitly/go-simplejson v0.5.0
98 github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect
@@ -14,7 +13,6 @@ require (
1413 github.com/kr/pretty v0.2.0 // indirect
1514 github.com/mbland/hmacauth v0.0.0-20170912233209-44256dfd4bfa
1615 github.com/mitchellh/mapstructure v1.1.2
17− github.com/mreiferson/go-options v1.0.0
1816 github.com/onsi/ginkgo v1.12.0
1917 github.com/onsi/gomega v1.9.0
2018 github.com/pquerna/cachecontrol v0.0.0-20180517163645-1555304b9b35 // indirect
go.sum+5−2
@@ -33,6 +33,7 @@ github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7
3333github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
3434github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
3535github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
36+github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3637github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3738github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
3839github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
@@ -74,6 +75,7 @@ github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OI
7475github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
7576github.com/googleapis/gax-go/v2 v2.0.5 h1:sjZBwGj9Jlw33ImPtvFviGYvseOtDM7hkSKB7+Tv3SM=
7677github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
78+github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
7779github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
7880github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
7981github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
@@ -89,6 +91,7 @@ github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpO
8991github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
9092github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
9193github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
94+github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
9295github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
9396github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
9497github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
@@ -111,8 +114,6 @@ github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh
111114github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
112115github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
113116github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
114−github.com/mreiferson/go-options v1.0.0 h1:RMLidydGlDWpL+lQTXo0bVIf/XT2CTq7AEJMoz5/VWs=
115−github.com/mreiferson/go-options v1.0.0/go.mod h1:zHtCks/HQvOt8ATyfwVe3JJq2PPuImzXINPRTC03+9w=
116117github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
117118github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
118119github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
@@ -142,7 +143,9 @@ github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7z
142143github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
143144github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
144145github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
146+github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
145147github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
148+github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
146149github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
147150github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
148151github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
main.go+11−13
@@ -12,9 +12,9 @@ import (
1212 "syscall"
1313 "time"
1414
15− "github.com/BurntSushi/toml"
16− options "github.com/mreiferson/go-options"
15+ "github.com/oauth2-proxy/oauth2-proxy/pkg/apis/options"
1716 "github.com/oauth2-proxy/oauth2-proxy/pkg/logger"
17+ "github.com/spf13/pflag"
1818)
1919
2020func main() {
@@ -149,7 +149,10 @@ func main() {
149149
150150 flagSet.String("user-id-claim", "email", "which claim contains the user ID")
151151
152− flagSet.Parse(os.Args[1:])
152+ pflagSet := pflag.NewFlagSet("oauth2-proxy", pflag.ExitOnError)
153+ pflagSet.AddGoFlagSet(flagSet)
154+
155+ pflagSet.Parse(os.Args[1:])
153156
154157 if *showVersion {
155158 fmt.Printf("oauth2-proxy %s (built with %s)\n", VERSION, runtime.Version())
@@ -157,18 +160,13 @@ func main() {
157160 }
158161
159162 opts := NewOptions()
160−
161− cfg := make(EnvOptions)
162− if *config != "" {
163− _, err := toml.DecodeFile(*config, &cfg)
164− if err != nil {
165− logger.Fatalf("ERROR: failed to load config file %s - %s", *config, err)
166− }
163+ err := options.Load(*config, pflagSet, opts)
164+ if err != nil {
165+ logger.Printf("ERROR: Failed to load config: %v", err)
166+ os.Exit(1)
167167 }
168− cfg.LoadEnvForStruct(opts)
169− options.Resolve(opts, flagSet, cfg)
170168
171− err := opts.Validate()
169+ err = opts.Validate()
172170 if err != nil {
173171 logger.Printf("%s", err)
174172 os.Exit(1)
oauthproxy.go+2−2
@@ -247,7 +247,7 @@ func NewOAuthProxy(opts *Options, validator func(string) bool) *OAuthProxy {
247247 panic(fmt.Sprintf("unknown upstream protocol %s", u.Scheme))
248248 }
249249 }
250− for _, u := range opts.CompiledRegex {
250+ for _, u := range opts.compiledRegex {
251251 logger.Printf("compiled skip-auth-regex => %q", u)
252252 }
253253
@@ -303,7 +303,7 @@ func NewOAuthProxy(opts *Options, validator func(string) bool) *OAuthProxy {
303303 skipAuthPreflight: opts.SkipAuthPreflight,
304304 skipJwtBearerTokens: opts.SkipJwtBearerTokens,
305305 jwtBearerVerifiers: opts.jwtBearerVerifiers,
306− compiledRegex: opts.CompiledRegex,
306+ compiledRegex: opts.compiledRegex,
307307 SetXAuthRequest: opts.SetXAuthRequest,
308308 PassBasicAuth: opts.PassBasicAuth,
309309 SetBasicAuth: opts.SetBasicAuth,
options.go+5−6
@@ -64,9 +64,8 @@ type Options struct {
6464 Banner string `flag:"banner" cfg:"banner" env:"OAUTH2_PROXY_BANNER"`
6565 Footer string `flag:"footer" cfg:"footer" env:"OAUTH2_PROXY_FOOTER"`
6666
67− Cookie options.CookieOptions
68−
69− Session options.SessionOptions
67+ Cookie options.CookieOptions `cfg:",squash"`
68+ Session options.SessionOptions `cfg:",squash"`
7069
7170 Upstreams []string `flag:"upstream" cfg:"upstreams" env:"OAUTH2_PROXY_UPSTREAMS"`
7271 SkipAuthRegex []string `flag:"skip-auth-regex" cfg:"skip_auth_regex" env:"OAUTH2_PROXY_SKIP_AUTH_REGEX"`
@@ -132,7 +131,7 @@ type Options struct {
132131 // internal values that are set after config validation
133132 redirectURL *url.URL
134133 proxyURLs []*url.URL
135− CompiledRegex []*regexp.Regexp
134+ compiledRegex []*regexp.Regexp
136135 provider providers.Provider
137136 sessionStore sessionsapi.SessionStore
138137 signatureData *SignatureData
@@ -370,12 +369,12 @@ func (o *Options) Validate() error {
370369 }
371370
372371 for _, u := range o.SkipAuthRegex {
373− CompiledRegex, err := regexp.Compile(u)
372+ compiledRegex, err := regexp.Compile(u)
374373 if err != nil {
375374 msgs = append(msgs, fmt.Sprintf("error compiling regex=%q %s", u, err))
376375 continue
377376 }
378− o.CompiledRegex = append(o.CompiledRegex, CompiledRegex)
377+ o.compiledRegex = append(o.compiledRegex, compiledRegex)
379378 }
380379 msgs = parseProviderInfo(o, msgs)
381380
options_test.go+1−1
@@ -165,7 +165,7 @@ func TestCompiledRegex(t *testing.T) {
165165 o.SkipAuthRegex = regexps
166166 assert.Equal(t, nil, o.Validate())
167167 actual := make([]string, 0)
168− for _, regex := range o.CompiledRegex {
168+ for _, regex := range o.compiledRegex {
169169 actual = append(actual, regex.String())
170170 }
171171 assert.Equal(t, regexps, actual)
pkg/apis/options/sessions.go+3−3
@@ -4,9 +4,9 @@ import "github.com/oauth2-proxy/oauth2-proxy/pkg/encryption"
44
55// SessionOptions contains configuration options for the SessionStore providers.
66type SessionOptions struct {
7− Type string `flag:"session-store-type" cfg:"session_store_type" env:"OAUTH2_PROXY_SESSION_STORE_TYPE"`
8− Cipher *encryption.Cipher
9− Redis RedisStoreOptions
7+ Type string `flag:"session-store-type" cfg:"session_store_type" env:"OAUTH2_PROXY_SESSION_STORE_TYPE"`
8+ Cipher *encryption.Cipher `cfg:",internal"`
9+ Redis RedisStoreOptions `cfg:",squash"`
1010}
1111
1212// CookieSessionStoreType is used to indicate the CookieSessionStore should be