Merge pull request #160 from travisgroth/bugfix/options-by-value

Change Options from pointers to values
This commit is contained in:
Bobby DeSimone 2019-06-05 17:52:33 -07:00 committed by GitHub
commit 070a902756
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 117 additions and 125 deletions

View file

@ -114,8 +114,8 @@ func startRedirectServer(addr string) (*http.Server, error) {
return srv, nil
}
func newAuthenticateService(opt *config.Options, mux *http.ServeMux, rpc *grpc.Server) (*authenticate.Authenticate, error) {
if opt == nil || !config.IsAuthenticate(opt.Services) {
func newAuthenticateService(opt config.Options, mux *http.ServeMux, rpc *grpc.Server) (*authenticate.Authenticate, error) {
if !config.IsAuthenticate(opt.Services) {
return nil, nil
}
service, err := authenticate.New(opt)
@ -127,8 +127,8 @@ func newAuthenticateService(opt *config.Options, mux *http.ServeMux, rpc *grpc.S
return service, nil
}
func newAuthorizeService(opt *config.Options, rpc *grpc.Server) (*authorize.Authorize, error) {
if opt == nil || !config.IsAuthorize(opt.Services) {
func newAuthorizeService(opt config.Options, rpc *grpc.Server) (*authorize.Authorize, error) {
if !config.IsAuthorize(opt.Services) {
return nil, nil
}
service, err := authorize.New(opt)
@ -139,8 +139,8 @@ func newAuthorizeService(opt *config.Options, rpc *grpc.Server) (*authorize.Auth
return service, nil
}
func newProxyService(opt *config.Options, mux *http.ServeMux) (*proxy.Proxy, error) {
if opt == nil || !config.IsProxy(opt.Services) {
func newProxyService(opt config.Options, mux *http.ServeMux) (*proxy.Proxy, error) {
if !config.IsProxy(opt.Services) {
return nil, nil
}
service, err := proxy.New(opt)
@ -151,7 +151,7 @@ func newProxyService(opt *config.Options, mux *http.ServeMux) (*proxy.Proxy, err
return service, nil
}
func wrapMiddleware(o *config.Options, mux *http.ServeMux) http.Handler {
func wrapMiddleware(o config.Options, mux *http.ServeMux) http.Handler {
c := middleware.NewChain()
c = c.Append(log.NewHandler(log.Logger))
c = c.Append(log.AccessHandler(func(r *http.Request, status, size int, duration time.Duration) {
@ -166,7 +166,7 @@ func wrapMiddleware(o *config.Options, mux *http.ServeMux) http.Handler {
Str("url", r.URL.String()).
Msg("http-request")
}))
if o != nil && len(o.Headers) != 0 {
if len(o.Headers) != 0 {
c = c.Append(middleware.SetHeaders(o.Headers))
}
c = c.Append(log.ForwardedAddrHandler("fwd_ip"))
@ -178,10 +178,10 @@ func wrapMiddleware(o *config.Options, mux *http.ServeMux) http.Handler {
return c.Then(mux)
}
func parseOptions(configFile string) (*config.Options, error) {
func parseOptions(configFile string) (config.Options, error) {
o, err := config.OptionsFromViper(configFile)
if err != nil {
return nil, err
return o, err
}
if o.Debug {
log.SetDebugMode()
@ -193,7 +193,7 @@ func parseOptions(configFile string) (*config.Options, error) {
return o, nil
}
func handleConfigUpdate(opt *config.Options, services []config.OptionsUpdater) *config.Options {
func handleConfigUpdate(opt config.Options, services []config.OptionsUpdater) config.Options {
newOpt, err := parseOptions(*configFile)
optChecksum := opt.Checksum()
newOptChecksum := newOpt.Checksum()

View file

@ -78,11 +78,11 @@ func Test_newAuthenticateService(t *testing.T) {
testOpts.ClientSecret = "TEST"
testOpts.SharedKey = "YixWi1MYh77NMECGGIJQevoonYtVF+ZPRkQZrrmeRqM="
testOpts.CookieSecret = "YixWi1MYh77NMECGGIJQevoonYtVF+ZPRkQZrrmeRqM="
testOpts.AuthenticateURL = authURL
testOpts.AuthenticateURL = *authURL
testOpts.Services = tt.s
if tt.Field != "" {
testOptsField := reflect.ValueOf(testOpts).Elem().FieldByName(tt.Field)
testOptsField := reflect.ValueOf(&testOpts).Elem().FieldByName(tt.Field)
testOptsField.Set(reflect.ValueOf(tt).FieldByName("Value"))
}
@ -126,7 +126,7 @@ func Test_newAuthorizeService(t *testing.T) {
}
if tt.Field != "" {
testOptsField := reflect.ValueOf(testOpts).Elem().FieldByName(tt.Field)
testOptsField := reflect.ValueOf(&testOpts).Elem().FieldByName(tt.Field)
testOptsField.Set(reflect.ValueOf(tt).FieldByName("Value"))
}
@ -162,13 +162,17 @@ func Test_newProxyeService(t *testing.T) {
testOpts.Policies = []policy.Policy{
testPolicy,
}
testOpts.AuthenticateURL, _ = url.Parse("https://authenticate.example.com")
testOpts.AuthorizeURL, _ = url.Parse("https://authorize.example.com")
AuthenticateURL, _ := url.Parse("https://authenticate.example.com")
AuthorizeURL, _ := url.Parse("https://authorize.example.com")
testOpts.AuthenticateURL = *AuthenticateURL
testOpts.AuthorizeURL = *AuthorizeURL
testOpts.CookieSecret = "YixWi1MYh77NMECGGIJQevoonYtVF+ZPRkQZrrmeRqM="
testOpts.Services = tt.s
if tt.Field != "" {
testOptsField := reflect.ValueOf(testOpts).Elem().FieldByName(tt.Field)
testOptsField := reflect.ValueOf(&testOpts).Elem().FieldByName(tt.Field)
testOptsField.Set(reflect.ValueOf(tt).FieldByName("Value"))
}
_, err := newProxyService(testOpts, mux)
@ -181,7 +185,7 @@ func Test_newProxyeService(t *testing.T) {
}
func Test_wrapMiddleware(t *testing.T) {
o := &config.Options{
o := config.Options{
Services: "all",
Headers: map[string]string{
"X-Content-Type-Options": "nosniff",
@ -232,7 +236,7 @@ func Test_parseOptions(t *testing.T) {
t.Errorf("parseOptions() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != nil && got.SharedKey != tt.wantSharedKey {
if got.SharedKey != tt.wantSharedKey {
t.Errorf("parseOptions()\n")
t.Errorf("got: %+v\n", got.SharedKey)
t.Errorf("want: %+v\n", tt.wantSharedKey)
@ -247,7 +251,7 @@ type mockService struct {
Updated bool
}
func (m *mockService) UpdateOptions(o *config.Options) error {
func (m *mockService) UpdateOptions(o config.Options) error {
m.Updated = true
if m.fail {
@ -266,7 +270,7 @@ func Test_handleConfigUpdate(t *testing.T) {
tests := []struct {
name string
service *mockService
oldOpts *config.Options
oldOpts config.Options
wantUpdate bool
}{
{"good", &mockService{fail: false}, blankOpts, true},