Skip to content

Commit

Permalink
one more benchmark fix, populate session mem-cache if enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
dencoded committed May 23, 2018
1 parent ce8741a commit f567f76
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 41 deletions.
8 changes: 6 additions & 2 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,9 @@ func (t BaseMiddleware) CheckSessionAndIdentityForValidKey(key string) (user.Ses
if found {
// If exists, assume it has been authorized and pass on
// cache it
go SessionCache.Set(cacheKey, session, cache.DefaultExpiration)
if !t.Spec.GlobalConfig.LocalSessionCache.DisableCacheSessionState {
go SessionCache.Set(cacheKey, session, cache.DefaultExpiration)
}

// Check for a policy, if there is a policy, pull it and overwrite the session values
if err := t.ApplyPolicies(key, &session); err != nil {
Expand All @@ -341,7 +343,9 @@ func (t BaseMiddleware) CheckSessionAndIdentityForValidKey(key string) (user.Ses
log.Info("Recreating session for key: ", key)

// cache it
go SessionCache.Set(cacheKey, session, cache.DefaultExpiration)
if !t.Spec.GlobalConfig.LocalSessionCache.DisableCacheSessionState {
go SessionCache.Set(cacheKey, session, cache.DefaultExpiration)
}

// Check for a policy, if there is a policy, pull it and overwrite the session values
if err := t.ApplyPolicies(key, &session); err != nil {
Expand Down
35 changes: 21 additions & 14 deletions mw_hmac_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ import (
"testing"
"time"

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

"github.com/TykTechnologies/tyk/apidef"
"github.com/TykTechnologies/tyk/config"
"github.com/TykTechnologies/tyk/user"

"github.com/justinas/alice"
)

const hmacAuthDef = `{
Expand Down Expand Up @@ -97,7 +98,7 @@ func waitTimeout(wg *sync.WaitGroup, timeout time.Duration) bool {
}
}

func testPrepareHMACAuthSessionPass(tb testing.TB, eventWG *sync.WaitGroup, withHeader bool) (string, *APISpec, *http.Request) {
func testPrepareHMACAuthSessionPass(tb testing.TB, eventWG *sync.WaitGroup, withHeader bool, isBench bool) (string, *APISpec, *http.Request, string) {
spec := createSpecTest(tb, hmacAuthDef)
session := createHMACAuthSession()

Expand All @@ -109,8 +110,14 @@ func testPrepareHMACAuthSessionPass(tb testing.TB, eventWG *sync.WaitGroup, with
"AuthFailure": {&testAuthFailEventHandler{cb}},
}

// Basic auth sessions are stored as {org-id}{username}, so we need to append it here when we create the session.
spec.SessionManager.UpdateSession("9876", session, 60, false)
sessionKey := ""
if isBench {
sessionKey = uuid.New()
} else {
sessionKey = "9876"
}

spec.SessionManager.UpdateSession(sessionKey, session, 60, false)

req := testReq(tb, "GET", "/", nil)

Expand Down Expand Up @@ -141,17 +148,17 @@ func testPrepareHMACAuthSessionPass(tb testing.TB, eventWG *sync.WaitGroup, with
sigString := base64.StdEncoding.EncodeToString(h.Sum(nil))
encodedString := url.QueryEscape(sigString)

return encodedString, spec, req
return encodedString, spec, req, sessionKey
}

func TestHMACAuthSessionPass(t *testing.T) {
// Should not receive an AuthFailure event
var eventWG sync.WaitGroup
eventWG.Add(1)
encodedString, spec, req := testPrepareHMACAuthSessionPass(t, &eventWG, false)
encodedString, spec, req, sessionKey := testPrepareHMACAuthSessionPass(t, &eventWG, false, false)

recorder := httptest.NewRecorder()
req.Header.Set("Authorization", fmt.Sprintf("Signature keyId=\"9876\",algorithm=\"hmac-sha1\",signature=\"%s\"", encodedString))
req.Header.Set("Authorization", fmt.Sprintf("Signature keyId=\"%s\",algorithm=\"hmac-sha1\",signature=\"%s\"", sessionKey, encodedString))

chain := getHMACAuthChain(spec)
chain.ServeHTTP(recorder, req)
Expand All @@ -171,10 +178,10 @@ func BenchmarkHMACAuthSessionPass(b *testing.B) {

var eventWG sync.WaitGroup
eventWG.Add(b.N)
encodedString, spec, req := testPrepareHMACAuthSessionPass(b, &eventWG, false)
encodedString, spec, req, sessionKey := testPrepareHMACAuthSessionPass(b, &eventWG, false, true)

recorder := httptest.NewRecorder()
req.Header.Set("Authorization", fmt.Sprintf("Signature keyId=\"9876\",algorithm=\"hmac-sha1\",signature=\"%s\"", encodedString))
req.Header.Set("Authorization", fmt.Sprintf("Signature keyId=\"%s\",algorithm=\"hmac-sha1\",signature=\"%s\"", sessionKey, encodedString))

chain := getHMACAuthChain(spec)

Expand Down Expand Up @@ -398,10 +405,10 @@ func TestHMACAuthSessionPassWithHeaderField(t *testing.T) {
// Should not receive an AuthFailure event
var eventWG sync.WaitGroup
eventWG.Add(1)
encodedString, spec, req := testPrepareHMACAuthSessionPass(t, &eventWG, true)
encodedString, spec, req, sessionKey := testPrepareHMACAuthSessionPass(t, &eventWG, true, false)

recorder := httptest.NewRecorder()
req.Header.Set("Authorization", fmt.Sprintf("Signature keyId=\"9876\",algorithm=\"hmac-sha1\",headers=\"(request-target) date x-test-1 x-test-2\",signature=\"%s\"", encodedString))
req.Header.Set("Authorization", fmt.Sprintf("Signature keyId=\"%s\",algorithm=\"hmac-sha1\",headers=\"(request-target) date x-test-1 x-test-2\",signature=\"%s\"", sessionKey, encodedString))

chain := getHMACAuthChain(spec)
chain.ServeHTTP(recorder, req)
Expand All @@ -421,10 +428,10 @@ func BenchmarkHMACAuthSessionPassWithHeaderField(b *testing.B) {

var eventWG sync.WaitGroup
eventWG.Add(b.N)
encodedString, spec, req := testPrepareHMACAuthSessionPass(b, &eventWG, true)
encodedString, spec, req, sessionKey := testPrepareHMACAuthSessionPass(b, &eventWG, true, true)

recorder := httptest.NewRecorder()
req.Header.Set("Authorization", fmt.Sprintf("Signature keyId=\"9876\",algorithm=\"hmac-sha1\",headers=\"(request-target) date x-test-1 x-test-2\",signature=\"%s\"", encodedString))
req.Header.Set("Authorization", fmt.Sprintf("Signature keyId=\"%s\",algorithm=\"hmac-sha1\",headers=\"(request-target) date x-test-1 x-test-2\",signature=\"%s\"", sessionKey, encodedString))

chain := getHMACAuthChain(spec)

Expand Down
53 changes: 34 additions & 19 deletions mw_jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/dgrijalva/jwt-go"
"github.com/lonelycode/go-uuid/uuid"

"github.com/TykTechnologies/tyk/test"
"github.com/TykTechnologies/tyk/user"
Expand Down Expand Up @@ -88,7 +89,7 @@ func createJWTSessionWithRSAWithPolicy(policyID string) *user.SessionState {

// JWTSessionHMAC

func prepareJWTSessionHMAC(tb testing.TB) string {
func prepareJWTSessionHMAC(tb testing.TB, isBench bool) string {
spec := buildAndLoadAPI(func(spec *APISpec) {
spec.UseKeylessAccess = false
spec.JWTSigningMethod = "hmac"
Expand All @@ -98,6 +99,9 @@ func prepareJWTSessionHMAC(tb testing.TB) string {

session := createJWTSession()
tokenKID := testKey(tb, "token")
if isBench {
tokenKID += "-" + uuid.New()
}
spec.SessionManager.UpdateSession(tokenKID, session, 60, false)

jwtToken := createJWKTokenHMAC(func(t *jwt.Token) {
Expand All @@ -113,7 +117,7 @@ func TestJWTSessionHMAC(t *testing.T) {
ts := newTykTestServer()
defer ts.Close()

jwtToken := prepareJWTSessionHMAC(t)
jwtToken := prepareJWTSessionHMAC(t, false)

authHeaders := map[string]string{"authorization": jwtToken}
t.Run("Request with valid JWT signed with HMAC", func(t *testing.T) {
Expand All @@ -129,7 +133,7 @@ func BenchmarkJWTSessionHMAC(b *testing.B) {
ts := newTykTestServer()
defer ts.Close()

jwtToken := prepareJWTSessionHMAC(b)
jwtToken := prepareJWTSessionHMAC(b, true)

authHeaders := map[string]string{"authorization": jwtToken}
for i := 0; i < b.N; i++ {
Expand All @@ -140,7 +144,7 @@ func BenchmarkJWTSessionHMAC(b *testing.B) {
}

// JWTSessionRSA
func prepareJWTSessionRSA(tb testing.TB) (*APISpec, string) {
func prepareJWTSessionRSA(tb testing.TB, isBench bool) (*APISpec, string) {
spec := buildAndLoadAPI(func(spec *APISpec) {
spec.UseKeylessAccess = false
spec.JWTSigningMethod = "rsa"
Expand All @@ -150,6 +154,9 @@ func prepareJWTSessionRSA(tb testing.TB) (*APISpec, string) {

session := createJWTSessionWithRSA()
tokenKID := testKey(tb, "token")
if isBench {
tokenKID += "-" + uuid.New()
}
spec.SessionManager.UpdateSession(tokenKID, session, 60, false)

jwtToken := createJWKToken(func(t *jwt.Token) {
Expand All @@ -165,7 +172,7 @@ func TestJWTSessionRSA(t *testing.T) {
ts := newTykTestServer()
defer ts.Close()

_, jwtToken := prepareJWTSessionRSA(t)
_, jwtToken := prepareJWTSessionRSA(t, false)

authHeaders := map[string]string{"authorization": jwtToken}
t.Run("Request with valid JWT", func(t *testing.T) {
Expand All @@ -181,7 +188,7 @@ func BenchmarkJWTSessionRSA(b *testing.B) {
ts := newTykTestServer()
defer ts.Close()

_, jwtToken := prepareJWTSessionRSA(b)
_, jwtToken := prepareJWTSessionRSA(b, true)

authHeaders := map[string]string{"authorization": jwtToken}
for i := 0; i < b.N; i++ {
Expand All @@ -195,7 +202,7 @@ func TestJWTSessionFailRSA_EmptyJWT(t *testing.T) {
ts := newTykTestServer()
defer ts.Close()

prepareJWTSessionRSA(t)
prepareJWTSessionRSA(t, false)

authHeaders := map[string]string{"authorization": ""}
t.Run("Request with empty authorization header", func(t *testing.T) {
Expand All @@ -209,7 +216,7 @@ func TestJWTSessionFailRSA_NoAuthHeader(t *testing.T) {
ts := newTykTestServer()
defer ts.Close()

prepareJWTSessionRSA(t)
prepareJWTSessionRSA(t, false)

authHeaders := map[string]string{}
t.Run("Request without authorization header", func(t *testing.T) {
Expand All @@ -223,7 +230,7 @@ func TestJWTSessionFailRSA_MalformedJWT(t *testing.T) {
ts := newTykTestServer()
defer ts.Close()

_, jwtToken := prepareJWTSessionRSA(t)
_, jwtToken := prepareJWTSessionRSA(t, false)

authHeaders := map[string]string{"authorization": jwtToken + "ajhdkjhsdfkjashdkajshdkajhsdkajhsd"}
t.Run("Request with malformed JWT", func(t *testing.T) {
Expand All @@ -237,7 +244,7 @@ func TestJWTSessionFailRSA_MalformedJWT_NOTRACK(t *testing.T) {
ts := newTykTestServer()
defer ts.Close()

spec, jwtToken := prepareJWTSessionRSA(t)
spec, jwtToken := prepareJWTSessionRSA(t, false)
spec.DoNotTrack = true

authHeaders := map[string]string{"authorization": jwtToken + "ajhdkjhsdfkjashdkajshdkajhsdkajhsd"}
Expand All @@ -252,7 +259,7 @@ func TestJWTSessionFailRSA_WrongJWT(t *testing.T) {
ts := newTykTestServer()
defer ts.Close()

prepareJWTSessionRSA(t)
prepareJWTSessionRSA(t, false)

authHeaders := map[string]string{"authorization": "123"}
t.Run("Request with invalid JWT", func(t *testing.T) {
Expand All @@ -264,7 +271,7 @@ func TestJWTSessionFailRSA_WrongJWT(t *testing.T) {

// TestJWTSessionRSABearer

func prepareJWTSessionRSABearer(tb testing.TB) string {
func prepareJWTSessionRSABearer(tb testing.TB, isBench bool) string {
spec := buildAndLoadAPI(func(spec *APISpec) {
spec.UseKeylessAccess = false
spec.JWTSigningMethod = "rsa"
Expand All @@ -274,6 +281,9 @@ func prepareJWTSessionRSABearer(tb testing.TB) string {

session := createJWTSessionWithRSA()
tokenKID := testKey(tb, "token")
if isBench {
tokenKID += "-" + uuid.New()
}
spec.SessionManager.UpdateSession(tokenKID, session, 60, false)

jwtToken := createJWKToken(func(t *jwt.Token) {
Expand All @@ -289,7 +299,7 @@ func TestJWTSessionRSABearer(t *testing.T) {
ts := newTykTestServer()
defer ts.Close()

jwtToken := prepareJWTSessionRSABearer(t)
jwtToken := prepareJWTSessionRSABearer(t, false)

authHeaders := map[string]string{"authorization": "Bearer " + jwtToken}
t.Run("Request with valid Bearer", func(t *testing.T) {
Expand All @@ -305,7 +315,7 @@ func BenchmarkJWTSessionRSABearer(b *testing.B) {
ts := newTykTestServer()
defer ts.Close()

jwtToken := prepareJWTSessionRSABearer(b)
jwtToken := prepareJWTSessionRSABearer(b, true)

authHeaders := map[string]string{"authorization": "Bearer " + jwtToken}

Expand All @@ -320,7 +330,7 @@ func TestJWTSessionRSABearerInvalid(t *testing.T) {
ts := newTykTestServer()
defer ts.Close()

jwtToken := prepareJWTSessionRSABearer(t)
jwtToken := prepareJWTSessionRSABearer(t, false)

authHeaders := map[string]string{"authorization": "Bearer: " + jwtToken} // extra ":"
t.Run("Request with invalid Bearer", func(t *testing.T) {
Expand All @@ -332,7 +342,7 @@ func TestJWTSessionRSABearerInvalid(t *testing.T) {

// JWTSessionRSAWithRawSourceOnWithClientID

func prepareJWTSessionRSAWithRawSourceOnWithClientID() string {
func prepareJWTSessionRSAWithRawSourceOnWithClientID(isBench bool) string {
spec := buildAndLoadAPI(func(spec *APISpec) {
spec.APIID = "777888"
spec.OrgID = "default"
Expand All @@ -356,7 +366,12 @@ func prepareJWTSessionRSAWithRawSourceOnWithClientID() string {
}
})

tokenID := "1234567891010101"
tokenID := ""
if isBench {
tokenID = uuid.New()
} else {
tokenID = "1234567891010101"
}
session := createJWTSessionWithRSAWithPolicy(policyID)

spec.SessionManager.ResetQuota(tokenID, session)
Expand All @@ -377,7 +392,7 @@ func TestJWTSessionRSAWithRawSourceOnWithClientID(t *testing.T) {
ts := newTykTestServer()
defer ts.Close()

jwtToken := prepareJWTSessionRSAWithRawSourceOnWithClientID()
jwtToken := prepareJWTSessionRSAWithRawSourceOnWithClientID(false)
authHeaders := map[string]string{"authorization": jwtToken}

t.Run("Initial request with no policy base field in JWT", func(t *testing.T) {
Expand All @@ -393,7 +408,7 @@ func BenchmarkJWTSessionRSAWithRawSourceOnWithClientID(b *testing.B) {
ts := newTykTestServer()
defer ts.Close()

jwtToken := prepareJWTSessionRSAWithRawSourceOnWithClientID()
jwtToken := prepareJWTSessionRSAWithRawSourceOnWithClientID(true)
authHeaders := map[string]string{"authorization": jwtToken}

for i := 0; i < b.N; i++ {
Expand Down
12 changes: 6 additions & 6 deletions mw_organization_activity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ func BenchmarkProcessRequestLiveQuotaLimit(b *testing.B) {
b,
ts,
map[string]interface{}{
"quota_max": 1000000,
"quota_remaining": 1000000,
"quota_renewal_rate": 3,
"quota_max": 100000000,
"quota_remaining": 100000000,
"quota_renewal_rate": 300,
},
)

Expand Down Expand Up @@ -199,9 +199,9 @@ func BenchmarkProcessRequestOffThreadQuotaLimit(b *testing.B) {
b,
ts,
map[string]interface{}{
"quota_max": 1000000,
"quota_remaining": 1000000,
"quota_renewal_rate": 3,
"quota_max": 100000000,
"quota_remaining": 100000000,
"quota_renewal_rate": 300,
},
)

Expand Down
1 change: 1 addition & 0 deletions mw_virtual_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ func (d *VirtualEndpoint) ServeHTTPForCache(w http.ResponseWriter, r *http.Reque
if vmeta.UseSession {
session.MetaData = mapStrsToIfaces(newResponseData.SessionMeta)
d.Spec.SessionManager.UpdateSession(token, session, session.Lifetime(d.Spec.SessionLifetime), false)
ctxSetSession(r, session)
}

log.Debug("JSVM Virtual Endpoint execution took: (ns) ", time.Now().UnixNano()-t1)
Expand Down

0 comments on commit f567f76

Please sign in to comment.