Skip to content

Commit

Permalink
Fix basic auth caching (#2239)
Browse files Browse the repository at this point in the history
Basic auth cache was initialized for every API on every API reload.
go-cache by itself consume quite lot of resources, when started in such amounts.

Fix #2238
  • Loading branch information
buger committed Apr 27, 2019
1 parent ef56da7 commit 08dfc0a
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 8 deletions.
4 changes: 1 addition & 3 deletions api_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ import (
"sort"
"strconv"
"strings"
"time"

"github.com/Sirupsen/logrus"
"github.com/gorilla/mux"
"github.com/justinas/alice"
cache "github.com/pmylund/go-cache"

"github.com/TykTechnologies/tyk/apidef"
"github.com/TykTechnologies/tyk/config"
Expand Down Expand Up @@ -353,7 +351,7 @@ func processSpec(spec *APISpec, apisByListen map[string]int,
logger.Info("Checking security policy: OAuth")
}

if mwAppendEnabled(&authArray, &BasicAuthKeyIsValid{baseMid, cache.New(60*time.Second, 60*time.Minute), nil, nil}) {
if mwAppendEnabled(&authArray, &BasicAuthKeyIsValid{baseMid, nil, nil}) {
logger.Info("Checking security policy: Basic")
}

Expand Down
3 changes: 1 addition & 2 deletions multiauth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/justinas/alice"
"github.com/lonelycode/go-uuid/uuid"
cache "github.com/pmylund/go-cache"

"github.com/TykTechnologies/tyk/user"
)
Expand Down Expand Up @@ -78,7 +77,7 @@ func getMultiAuthStandardAndBasicAuthChain(spec *APISpec) http.Handler {
chain := alice.New(mwList(
&IPWhiteListMiddleware{baseMid},
&IPBlackListMiddleware{BaseMiddleware: baseMid},
&BasicAuthKeyIsValid{baseMid, cache.New(60*time.Second, 60*time.Minute), nil, nil},
&BasicAuthKeyIsValid{baseMid, nil, nil},
&AuthKey{baseMid},
&VersionCheck{BaseMiddleware: baseMid},
&KeyExpired{baseMid},
Expand Down
7 changes: 4 additions & 3 deletions mw_basic_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ import (

const defaultBasicAuthTTL = time.Duration(60) * time.Second

var basicAuthCache = cache.New(60*time.Second, 60*time.Minute)

// BasicAuthKeyIsValid uses a username instead of
type BasicAuthKeyIsValid struct {
BaseMiddleware
cache *cache.Cache

bodyUserRegexp *regexp.Regexp
bodyPasswordRegexp *regexp.Regexp
Expand Down Expand Up @@ -224,7 +225,7 @@ func (k *BasicAuthKeyIsValid) doBcryptWithCache(cacheDuration time.Duration, has

hasher := murmur3.New64()
hasher.Write(password)
k.cache.Set(string(hashedPassword), string(hasher.Sum(nil)), cacheDuration)
basicAuthCache.Set(string(hashedPassword), string(hasher.Sum(nil)), cacheDuration)

return nil
}
Expand All @@ -245,7 +246,7 @@ func (k *BasicAuthKeyIsValid) compareHashAndPassword(hash string, password strin
cacheTTL = time.Duration(k.Spec.BasicAuth.CacheTTL) * time.Second
}

cachedPass, inCache := k.cache.Get(hash)
cachedPass, inCache := basicAuthCache.Get(hash)
if !inCache {

logEntry.Debug("cache enabled: miss: bcrypt")
Expand Down

0 comments on commit 08dfc0a

Please sign in to comment.