From 92ac12c6284a93d72ddacbdd9a0a3c450ae4115e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Thu, 2 Nov 2017 14:45:10 +0000 Subject: [PATCH] all: various cleanups and simplifications I had been stashing these bit by bit over the last couple of weeks, since they were too small to submit separately. --- main.go | 7 ++++--- mw_hmac.go | 5 +---- oauth_manager.go | 9 +++------ reverse_proxy.go | 7 ++----- service_discovery_test.go | 16 ---------------- 5 files changed, 10 insertions(+), 34 deletions(-) diff --git a/main.go b/main.go index efce2c8bf39..5e7a191eb3f 100644 --- a/main.go +++ b/main.go @@ -461,12 +461,13 @@ func loadCustomMiddleware(spec *APISpec) ([]string, apidef.MiddlewareDefinition, "prefix": "main", }).Debug("Loading file middleware from ", path) - mwDef := apidef.MiddlewareDefinition{} - mwDef.Name = strings.Split(filepath.Base(path), ".")[0] + mwDef := apidef.MiddlewareDefinition{ + Name: strings.Split(filepath.Base(path), ".")[0], + Path: path, + } log.WithFields(logrus.Fields{ "prefix": "main", }).Debug("-- Middleware name ", mwDef.Name) - mwDef.Path = path mwDef.RequireSession = strings.HasSuffix(mwDef.Name, "_with_session") if mwDef.RequireSession { switch folder.name { diff --git a/mw_hmac.go b/mw_hmac.go index a9bc9bfc561..c970399c35d 100644 --- a/mw_hmac.go +++ b/mw_hmac.go @@ -87,10 +87,7 @@ func (hm *HMACMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Request, encodedSignature := generateEncodedSignature(signatureString, secret) // Compare - matchPass := false - if encodedSignature == fieldValues.Signature { - matchPass = true - } + matchPass := encodedSignature == fieldValues.Signature // Check for lower case encoding (.Net issues, again) if !matchPass { diff --git a/oauth_manager.go b/oauth_manager.go index 63717350642..a9500c76b7c 100644 --- a/oauth_manager.go +++ b/oauth_manager.go @@ -546,8 +546,7 @@ func (r *RedisOsinStorageInterface) LoadAuthorize(code string) (*osin.AuthorizeD return nil, err } - authData := osin.AuthorizeData{} - authData.Client = new(OAuthClient) + authData := osin.AuthorizeData{Client: new(OAuthClient)} if err := json.Unmarshal([]byte(authJSON), &authData); err != nil { log.Error("Couldn't unmarshal OAuth auth data object (LoadAuthorize): ", err) return nil, err @@ -646,8 +645,7 @@ func (r *RedisOsinStorageInterface) LoadAccess(token string) (*osin.AccessData, return nil, err } - accessData := osin.AccessData{} - accessData.Client = new(OAuthClient) + accessData := osin.AccessData{Client: new(OAuthClient)} if err := json.Unmarshal([]byte(accessJSON), &accessData); err != nil { log.Error("Couldn't unmarshal OAuth auth data object (LoadAccess): ", err) return nil, err @@ -679,8 +677,7 @@ func (r *RedisOsinStorageInterface) LoadRefresh(token string) (*osin.AccessData, } // new interface means having to make this nested... ick. - accessData := osin.AccessData{} - accessData.Client = new(OAuthClient) + accessData := osin.AccessData{Client: new(OAuthClient)} if err := json.Unmarshal([]byte(accessJSON), &accessData); err != nil { log.Error("Couldn't unmarshal OAuth auth data object (LoadRefresh): ", err, "; Decoding: ", accessJSON) diff --git a/reverse_proxy.go b/reverse_proxy.go index de9cfea26c0..1d986607f5d 100644 --- a/reverse_proxy.go +++ b/reverse_proxy.go @@ -44,17 +44,16 @@ func urlFromService(spec *APISpec) (*apidef.HostList, error) { doCacheRefresh := func() (*apidef.HostList, error) { log.Debug("--> Refreshing") spec.ServiceRefreshInProgress = true + defer func() { spec.ServiceRefreshInProgress = false }() sd := ServiceDiscovery{} sd.Init(&spec.Proxy.ServiceDiscovery) data, err := sd.Target(spec.Proxy.ServiceDiscovery.QueryEndpoint) if err != nil { - spec.ServiceRefreshInProgress = false return nil, err } + spec.HasRun = true // Set the cached value if data.Len() == 0 { - spec.HasRun = true - spec.ServiceRefreshInProgress = false log.Warning("[PROXY][SD] Service Discovery returned empty host list! Returning last good set.") if spec.LastGoodHostList == nil { @@ -68,8 +67,6 @@ func urlFromService(spec *APISpec) (*apidef.HostList, error) { ServiceCache.Set(spec.APIID, data, cache.DefaultExpiration) // Stash it too spec.LastGoodHostList = data - spec.HasRun = true - spec.ServiceRefreshInProgress = false return data, nil } diff --git a/service_discovery_test.go b/service_discovery_test.go index 9964b4b8d41..0836004e47b 100644 --- a/service_discovery_test.go +++ b/service_discovery_test.go @@ -160,27 +160,17 @@ const mesosphere = `{ func configureService(name string, sd *ServiceDiscovery) string { switch name { case "consul": - sd.isNested = false sd.isTargetList = true sd.endpointReturnsList = true sd.portSeperate = true sd.dataPath = "Address" - sd.parentPath = "" sd.portPath = "ServicePort" return consul case "etcd": - sd.isNested = false - sd.isTargetList = false - sd.endpointReturnsList = false - sd.portSeperate = false sd.dataPath = "node.value" - sd.parentPath = "" - sd.portPath = "" return etcd case "nested": sd.isNested = true - sd.isTargetList = false - sd.endpointReturnsList = false sd.portSeperate = true sd.dataPath = "hostname" sd.parentPath = "node.value" @@ -189,7 +179,6 @@ func configureService(name string, sd *ServiceDiscovery) string { case "nested_list": sd.isNested = true sd.isTargetList = true - sd.endpointReturnsList = false sd.portSeperate = true sd.dataPath = "hostname" sd.parentPath = "node.value" @@ -205,25 +194,20 @@ func configureService(name string, sd *ServiceDiscovery) string { sd.portPath = "port" return nested_consul case "mesosphere": - sd.isNested = false sd.isTargetList = true - sd.endpointReturnsList = false sd.portSeperate = true sd.dataPath = "host" sd.parentPath = "tasks" sd.portPath = "ports" return mesosphere case "eureka": - sd.isNested = false sd.isTargetList = true - sd.endpointReturnsList = false sd.portSeperate = true sd.dataPath = "hostName" sd.parentPath = "application.instance" sd.portPath = "port.$" return eureka_real } - return "" }