Add redis lock feature (#1063)

* Add sensible logging flag to default setup for logger

* Add Redis lock

* Fix default value flag for sensitive logging

* Split RefreshSessionIfNeeded in two methods and use Redis lock

* Small adjustments to doc and code

* Remove sensible logging

* Fix method names in ticket.go

* Revert "Fix method names in ticket.go"

This reverts commit 408ba1a1a5.

* Fix methods name in ticket.go

* Remove block in Redis client get

* Increase lock time to 1 second

* Perform retries, if session store is locked

* Reverse if condition, because it should return if session does not have to be refreshed

* Update go.sum

* Update MockStore

* Return error if loading session fails

* Fix and update tests

* Change validSession to session in docs and strings

* Change validSession to session in docs and strings

* Fix docs

* Fix wrong field name

* Fix linting

* Fix imports for linting

* Revert changes except from locking functionality

* Add lock feature on session state

* Update from master

* Remove errors package, because it is not used

* Only pass context instead of request to lock

* Use lock key

* By default use NoOpLock

* Remove debug output

* Update ticket_test.go

* Map internal error to sessions error

* Add ErrLockNotObtained

* Enable lock peek for all redis clients

* Use lock key prefix consistent

* Fix imports

* Use exists method for peek lock

* Fix imports

* Fix imports

* Fix imports

* Remove own Dockerfile

* Fix imports

* Fix tests for ticket and session store

* Fix session store test

* Update pkg/apis/sessions/interfaces.go

Co-authored-by: Joel Speed <Joel.speed@hotmail.co.uk>

* Do not wrap lock method

Co-authored-by: Joel Speed <Joel.speed@hotmail.co.uk>

* Use errors package for lock constants

* Use better naming for initLock function

* Add comments

* Add session store lock test

* Fix tests

* Fix tests

* Fix tests

* Fix tests

* Add cookies after saving session

* Add mock lock

* Fix imports for mock_lock.go

* Store mock lock for key

* Apply elapsed time on mock lock

* Check if lock is initially applied

* Reuse existing lock

* Test all lock methods

* Update CHANGELOG.md

* Use redis client methods in redis.lock for release an refresh

* Use lock key suffix instead of prefix for lock key

* Add comments for Lock interface

* Update comment for Lock interface

* Update CHANGELOG.md

* Change LockSuffix to const

* Check lock on already loaded session

* Use global var for loadedSession in lock tests

* Use lock instance for refreshing and releasing of lock

* Update possible error type for Refresh

Co-authored-by: Joel Speed <Joel.speed@hotmail.co.uk>
This commit is contained in:
Kevin Kreitner
2021-06-02 19:08:19 +01:00
committed by GitHub
co-authored by Joel Speed
parent 67bfa4b43f
commit f648c54d87
17 changed files with 389 additions and 21 deletions
+48
View File
@@ -0,0 +1,48 @@
package tests
import (
"context"
"time"
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/sessions"
)
type MockLock struct {
expiration time.Duration
elapsed time.Duration
}
func (l *MockLock) Obtain(ctx context.Context, expiration time.Duration) error {
l.expiration = expiration
return nil
}
func (l *MockLock) Peek(ctx context.Context) (bool, error) {
if l.elapsed < l.expiration {
return true, nil
}
return false, nil
}
func (l *MockLock) Refresh(ctx context.Context, expiration time.Duration) error {
if l.expiration <= l.elapsed {
return sessions.ErrNotLocked
}
l.expiration = expiration
l.elapsed = time.Duration(0)
return nil
}
func (l *MockLock) Release(ctx context.Context) error {
if l.expiration <= l.elapsed {
return sessions.ErrNotLocked
}
l.expiration = time.Duration(0)
l.elapsed = time.Duration(0)
return nil
}
// FastForward simulates the flow of time to test expirations
func (l *MockLock) FastForward(duration time.Duration) {
l.elapsed += duration
}
+20 -4
View File
@@ -4,6 +4,8 @@ import (
"context"
"fmt"
"time"
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/sessions"
)
// entry is a MockStore cache entry with an expiration
@@ -15,15 +17,17 @@ type entry struct {
// MockStore is a generic in-memory implementation of persistence.Store
// for mocking in tests
type MockStore struct {
cache map[string]entry
elapsed time.Duration
cache map[string]entry
lockCache map[string]*MockLock
elapsed time.Duration
}
// NewMockStore creates a MockStore
func NewMockStore() *MockStore {
return &MockStore{
cache: map[string]entry{},
elapsed: 0 * time.Second,
cache: map[string]entry{},
lockCache: map[string]*MockLock{},
elapsed: 0 * time.Second,
}
}
@@ -52,7 +56,19 @@ func (s *MockStore) Clear(_ context.Context, key string) error {
return nil
}
func (s *MockStore) Lock(key string) sessions.Lock {
if s.lockCache[key] != nil {
return s.lockCache[key]
}
lock := &MockLock{}
s.lockCache[key] = lock
return lock
}
// FastForward simulates the flow of time to test expirations
func (s *MockStore) FastForward(duration time.Duration) {
for _, mockLock := range s.lockCache {
mockLock.FastForward(duration)
}
s.elapsed += duration
}
+74
View File
@@ -286,6 +286,78 @@ func PersistentSessionStoreInterfaceTests(in *testInput) {
})
})
})
Context("when lock is applied", func() {
var loadedSession *sessionsapi.SessionState
BeforeEach(func() {
resp := httptest.NewRecorder()
err := in.ss().Save(resp, in.request, in.session)
Expect(err).ToNot(HaveOccurred())
for _, cookie := range resp.Result().Cookies() {
in.request.AddCookie(cookie)
}
loadedSession, err = in.ss().Load(in.request)
Expect(err).ToNot(HaveOccurred())
err = loadedSession.ObtainLock(in.request.Context(), 2*time.Minute)
Expect(err).ToNot(HaveOccurred())
isLocked, err := loadedSession.PeekLock(in.request.Context())
Expect(err).ToNot(HaveOccurred())
Expect(isLocked).To(BeTrue())
})
Context("before lock expired", func() {
BeforeEach(func() {
Expect(in.persistentFastForward(time.Minute)).To(Succeed())
})
It("peek returns true on loaded session lock", func() {
l := *loadedSession
isLocked, err := l.PeekLock(in.request.Context())
Expect(err).NotTo(HaveOccurred())
Expect(isLocked).To(BeTrue())
})
It("lock can be released", func() {
l := *loadedSession
err := l.ReleaseLock(in.request.Context())
Expect(err).NotTo(HaveOccurred())
isLocked, err := l.PeekLock(in.request.Context())
Expect(err).NotTo(HaveOccurred())
Expect(isLocked).To(BeFalse())
})
It("lock is refreshed", func() {
l := *loadedSession
err := l.RefreshLock(in.request.Context(), 3*time.Minute)
Expect(err).NotTo(HaveOccurred())
Expect(in.persistentFastForward(2 * time.Minute)).To(Succeed())
isLocked, err := l.PeekLock(in.request.Context())
Expect(err).NotTo(HaveOccurred())
Expect(isLocked).To(BeTrue())
})
})
Context("after lock expired", func() {
BeforeEach(func() {
Expect(in.persistentFastForward(3 * time.Minute)).To(Succeed())
})
It("peek returns false on loaded session lock", func() {
l := *loadedSession
isLocked, err := l.PeekLock(in.request.Context())
Expect(err).NotTo(HaveOccurred())
Expect(isLocked).To(BeFalse())
})
})
})
}
func SessionStoreInterfaceTests(in *testInput) {
@@ -411,9 +483,11 @@ func LoadSessionTests(in *testInput) {
l := *loadedSession
l.CreatedAt = nil
l.ExpiresOn = nil
l.Lock = &sessionsapi.NoOpLock{}
s := *in.session
s.CreatedAt = nil
s.ExpiresOn = nil
s.Lock = &sessionsapi.NoOpLock{}
Expect(l).To(Equal(s))
// Compare time.Time separately