Skip to content

Commit

Permalink
Support Redis 6.0 explicitly #4778
Browse files Browse the repository at this point in the history
ref DEV-2129
  • Loading branch information
tung2744 authored Sep 27, 2024
2 parents f6428b3 + 0001fe9 commit 06b597d
Show file tree
Hide file tree
Showing 40 changed files with 262 additions and 163 deletions.
4 changes: 2 additions & 2 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ services:
- "5432:5432"

redis:
# redis:6.0 crashed randomly when I run k6 load test.
image: redis:6.2.6
# Azure cache for Redis supports 6.0 only
image: redis:6.0.20
volumes:
- redis_data:/data
ports:
Expand Down
22 changes: 15 additions & 7 deletions pkg/auth/webapp/error_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import (
"net/http"
"net/url"

goredis "github.com/go-redis/redis/v8"

"github.com/authgear/authgear-server/pkg/api/apierrors"
"github.com/authgear/authgear-server/pkg/lib/config"
"github.com/authgear/authgear-server/pkg/lib/infra/redis"
"github.com/authgear/authgear-server/pkg/lib/infra/redis/appredis"
"github.com/authgear/authgear-server/pkg/util/crypto"
"github.com/authgear/authgear-server/pkg/util/duration"
Expand Down Expand Up @@ -76,7 +75,7 @@ func (c *ErrorService) GetRecoverableError(r *http.Request) (*ErrorState, bool)

var redisValue string
var err error
err = c.RedisHandle.WithConnContext(r.Context(), func(conn *goredis.Conn) error {
err = c.RedisHandle.WithConnContext(r.Context(), func(conn redis.Redis_6_0_Cmdable) error {
redisValue, err = conn.Get(r.Context(), redisKey).Result()
return err
})
Expand Down Expand Up @@ -109,9 +108,18 @@ func (c *ErrorService) GetDelRecoverableError(w http.ResponseWriter, r *http.Req

var redisValue string
var err error
err = c.RedisHandle.WithConnContext(r.Context(), func(conn *goredis.Conn) error {
redisValue, err = conn.GetDel(r.Context(), redisKey).Result()
return err
err = c.RedisHandle.WithConnContext(r.Context(), func(conn redis.Redis_6_0_Cmdable) error {
redisValue, err = conn.Get(r.Context(), redisKey).Result()
if err != nil {
return err
}

_, err = conn.Del(r.Context(), redisKey).Result()
if err != nil {
return err
}

return nil
})
if err != nil {
return nil, false
Expand Down Expand Up @@ -165,7 +173,7 @@ func (c *ErrorService) SetRecoverableError(r *http.Request, value *apierrors.API
}

redisValue := string(dataBytes)
err = c.RedisHandle.WithConnContext(r.Context(), func(conn *goredis.Conn) error {
err = c.RedisHandle.WithConnContext(r.Context(), func(conn redis.Redis_6_0_Cmdable) error {
_, err := conn.Set(r.Context(), redisKey, redisValue, duration.WebError).Result()
return err
})
Expand Down
9 changes: 5 additions & 4 deletions pkg/auth/webapp/session_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
goredis "github.com/go-redis/redis/v8"

"github.com/authgear/authgear-server/pkg/lib/config"
"github.com/authgear/authgear-server/pkg/lib/infra/redis"
"github.com/authgear/authgear-server/pkg/lib/infra/redis/appredis"
"github.com/authgear/authgear-server/pkg/lib/interaction"
)
Expand All @@ -28,7 +29,7 @@ func (s *SessionStoreRedis) Create(session *Session) (err error) {
return
}

err = s.Redis.WithConn(func(conn *goredis.Conn) error {
err = s.Redis.WithConn(func(conn redis.Redis_6_0_Cmdable) error {
ttl := SessionExpiryDuration
_, err = conn.SetNX(ctx, key, bytes, ttl).Result()
if errors.Is(err, goredis.Nil) {
Expand All @@ -50,7 +51,7 @@ func (s *SessionStoreRedis) Update(session *Session) (err error) {
return
}

err = s.Redis.WithConn(func(conn *goredis.Conn) error {
err = s.Redis.WithConn(func(conn redis.Redis_6_0_Cmdable) error {
ttl := SessionExpiryDuration
_, err = conn.SetXX(ctx, key, bytes, ttl).Result()
if errors.Is(err, goredis.Nil) {
Expand All @@ -67,7 +68,7 @@ func (s *SessionStoreRedis) Update(session *Session) (err error) {
func (s *SessionStoreRedis) Get(id string) (session *Session, err error) {
ctx := context.Background()
key := sessionKey(string(s.AppID), id)
err = s.Redis.WithConn(func(conn *goredis.Conn) error {
err = s.Redis.WithConn(func(conn redis.Redis_6_0_Cmdable) error {
data, err := conn.Get(ctx, key).Bytes()
if errors.Is(err, goredis.Nil) {
err = ErrInvalidSession
Expand All @@ -90,7 +91,7 @@ func (s *SessionStoreRedis) Get(id string) (session *Session, err error) {
func (s *SessionStoreRedis) Delete(id string) error {
ctx := context.Background()
key := sessionKey(string(s.AppID), id)
err := s.Redis.WithConn(func(conn *goredis.Conn) error {
err := s.Redis.WithConn(func(conn redis.Redis_6_0_Cmdable) error {
_, err := conn.Del(ctx, key).Result()
return err
})
Expand Down
13 changes: 10 additions & 3 deletions pkg/lib/accountmanagement/redis_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
goredis "github.com/go-redis/redis/v8"

"github.com/authgear/authgear-server/pkg/lib/config"
"github.com/authgear/authgear-server/pkg/lib/infra/redis"
"github.com/authgear/authgear-server/pkg/lib/infra/redis/appredis"
"github.com/authgear/authgear-server/pkg/util/clock"
"github.com/authgear/authgear-server/pkg/util/duration"
Expand Down Expand Up @@ -54,7 +55,7 @@ func (s *RedisStore) GenerateToken(options GenerateTokenOptions) (string, error)

tokenKey := tokenKey(token.AppID, token.TokenHash)

err = s.Redis.WithConnContext(s.Context, func(conn *goredis.Conn) error {
err = s.Redis.WithConnContext(s.Context, func(conn redis.Redis_6_0_Cmdable) error {
_, err = conn.SetNX(s.Context, tokenKey, tokenBytes, ttl).Result()
if errors.Is(err, goredis.Nil) {
return errors.New("account management token collision")
Expand All @@ -76,15 +77,21 @@ func (s *RedisStore) ConsumeToken(tokenStr string) (*Token, error) {
tokenKey := tokenKey(string(s.AppID), tokenHash)

var tokenBytes []byte
err := s.Redis.WithConnContext(s.Context, func(conn *goredis.Conn) error {
err := s.Redis.WithConnContext(s.Context, func(conn redis.Redis_6_0_Cmdable) error {
var err error
tokenBytes, err = conn.GetDel(s.Context, tokenKey).Bytes()
tokenBytes, err = conn.Get(s.Context, tokenKey).Bytes()
if errors.Is(err, goredis.Nil) {
// Token Invalid
return ErrOAuthTokenInvalid
} else if err != nil {
return err
}

_, err = conn.Del(s.Context, tokenKey).Result()
if err != nil {
return err
}

return nil
})
if err != nil {
Expand Down
15 changes: 8 additions & 7 deletions pkg/lib/authenticationflow/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
goredis "github.com/go-redis/redis/v8"

"github.com/authgear/authgear-server/pkg/lib/config"
"github.com/authgear/authgear-server/pkg/lib/infra/redis"
"github.com/authgear/authgear-server/pkg/lib/infra/redis/appredis"
"github.com/authgear/authgear-server/pkg/util/duration"
)
Expand All @@ -27,7 +28,7 @@ func (s *StoreImpl) CreateFlow(flow *Flow) error {
return err
}

return s.Redis.WithConn(func(conn *goredis.Conn) error {
return s.Redis.WithConn(func(conn redis.Redis_6_0_Cmdable) error {
flowKey := redisFlowKey(s.AppID, flow.FlowID)
stateKey := redisFlowStateKey(s.AppID, flow.StateToken)
ttl := Lifetime
Expand All @@ -49,7 +50,7 @@ func (s *StoreImpl) CreateFlow(flow *Flow) error {
func (s *StoreImpl) GetFlowByStateToken(stateToken string) (*Flow, error) {
stateKey := redisFlowStateKey(s.AppID, stateToken)
var flow Flow
err := s.Redis.WithConn(func(conn *goredis.Conn) error {
err := s.Redis.WithConn(func(conn redis.Redis_6_0_Cmdable) error {
bytes, err := conn.Get(s.Context, stateKey).Bytes()
if errors.Is(err, goredis.Nil) {
return ErrFlowNotFound
Expand Down Expand Up @@ -80,7 +81,7 @@ func (s *StoreImpl) GetFlowByStateToken(stateToken string) (*Flow, error) {
func (s *StoreImpl) DeleteFlow(flow *Flow) error {
// We do not delete the states because there are many of them.
// Deleting the flowID is enough to make GetFlowByStateToken to return ErrFlowNotFound.
return s.Redis.WithConn(func(conn *goredis.Conn) error {
return s.Redis.WithConn(func(conn redis.Redis_6_0_Cmdable) error {
flowKey := redisFlowKey(s.AppID, flow.FlowID)

_, err := conn.Del(s.Context, flowKey).Result()
Expand All @@ -98,7 +99,7 @@ func (s *StoreImpl) CreateSession(session *Session) error {
return err
}

return s.Redis.WithConn(func(conn *goredis.Conn) error {
return s.Redis.WithConn(func(conn redis.Redis_6_0_Cmdable) error {
sessionKey := redisFlowSessionKey(s.AppID, session.FlowID)
ttl := Lifetime

Expand All @@ -114,7 +115,7 @@ func (s *StoreImpl) CreateSession(session *Session) error {
func (s *StoreImpl) GetSession(flowID string) (*Session, error) {
sessionKey := redisFlowSessionKey(s.AppID, flowID)
var session Session
err := s.Redis.WithConn(func(conn *goredis.Conn) error {
err := s.Redis.WithConn(func(conn redis.Redis_6_0_Cmdable) error {
bytes, err := conn.Get(s.Context, sessionKey).Bytes()
if errors.Is(err, goredis.Nil) {
return ErrFlowNotFound
Expand All @@ -139,7 +140,7 @@ func (s *StoreImpl) UpdateSession(session *Session) error {
return err
}

return s.Redis.WithConn(func(conn *goredis.Conn) error {
return s.Redis.WithConn(func(conn redis.Redis_6_0_Cmdable) error {
sessionKey := redisFlowSessionKey(s.AppID, session.FlowID)

ttl := Lifetime
Expand All @@ -153,7 +154,7 @@ func (s *StoreImpl) UpdateSession(session *Session) error {
}

func (s *StoreImpl) DeleteSession(session *Session) error {
return s.Redis.WithConn(func(conn *goredis.Conn) error {
return s.Redis.WithConn(func(conn redis.Redis_6_0_Cmdable) error {
sessionKey := redisFlowSessionKey(s.AppID, session.FlowID)

_, err := conn.Del(s.Context, sessionKey).Result()
Expand Down
7 changes: 4 additions & 3 deletions pkg/lib/authn/authenticationinfo/store_redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
goredis "github.com/go-redis/redis/v8"

"github.com/authgear/authgear-server/pkg/lib/config"
"github.com/authgear/authgear-server/pkg/lib/infra/redis"
"github.com/authgear/authgear-server/pkg/lib/infra/redis/appredis"
"github.com/authgear/authgear-server/pkg/util/duration"
)
Expand All @@ -29,7 +30,7 @@ func (s *StoreRedis) Save(entry *Entry) (err error) {

key := authenticationInfoEntryKey(s.AppID, entry.ID)

err = s.Redis.WithConn(func(conn *goredis.Conn) error {
err = s.Redis.WithConn(func(conn redis.Redis_6_0_Cmdable) error {
_, err := conn.Set(s.Context, key, jsonBytes, ttl).Result()
return err
})
Expand All @@ -42,7 +43,7 @@ func (s *StoreRedis) Save(entry *Entry) (err error) {

func (s *StoreRedis) Get(entryID string) (entry *Entry, err error) {
key := authenticationInfoEntryKey(s.AppID, entryID)
err = s.Redis.WithConn(func(conn *goredis.Conn) error {
err = s.Redis.WithConn(func(conn redis.Redis_6_0_Cmdable) error {
data, err := conn.Get(s.Context, key).Bytes()
if errors.Is(err, goredis.Nil) {
return ErrNotFound
Expand All @@ -62,7 +63,7 @@ func (s *StoreRedis) Get(entryID string) (entry *Entry, err error) {

func (s *StoreRedis) Delete(entryID string) (err error) {
key := authenticationInfoEntryKey(s.AppID, entryID)
return s.Redis.WithConn(func(conn *goredis.Conn) error {
return s.Redis.WithConn(func(conn redis.Redis_6_0_Cmdable) error {
_, err := conn.Del(s.Context, key).Result()
if err != nil {
return err
Expand Down
7 changes: 4 additions & 3 deletions pkg/lib/authn/challenge/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
goredis "github.com/go-redis/redis/v8"

"github.com/authgear/authgear-server/pkg/lib/config"
"github.com/authgear/authgear-server/pkg/lib/infra/redis"
"github.com/authgear/authgear-server/pkg/lib/infra/redis/appredis"
"github.com/authgear/authgear-server/pkg/util/clock"
)
Expand Down Expand Up @@ -36,7 +37,7 @@ func (p *Provider) Create(purpose Purpose) (*Challenge, error) {
return nil, err
}

err = p.Redis.WithConn(func(conn *goredis.Conn) error {
err = p.Redis.WithConn(func(conn redis.Redis_6_0_Cmdable) error {
_, err = conn.SetNX(ctx, key, data, ttl).Result()
if errors.Is(err, goredis.Nil) {
return errors.New("fail to create new challenge")
Expand All @@ -59,7 +60,7 @@ func (p *Provider) Get(token string) (*Challenge, error) {

c := &Challenge{}

err := p.Redis.WithConn(func(conn *goredis.Conn) error {
err := p.Redis.WithConn(func(conn redis.Redis_6_0_Cmdable) error {
data, err := conn.Get(ctx, key).Bytes()
if errors.Is(err, goredis.Nil) {
return ErrInvalidChallenge
Expand Down Expand Up @@ -87,7 +88,7 @@ func (p *Provider) Consume(token string) (*Purpose, error) {

c := &Challenge{}

err := p.Redis.WithConn(func(conn *goredis.Conn) error {
err := p.Redis.WithConn(func(conn redis.Redis_6_0_Cmdable) error {
data, err := conn.Get(ctx, key).Bytes()
if errors.Is(err, goredis.Nil) {
return ErrInvalidChallenge
Expand Down
13 changes: 7 additions & 6 deletions pkg/lib/authn/identity/anonymous/store_redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
goredis "github.com/go-redis/redis/v8"

"github.com/authgear/authgear-server/pkg/lib/config"
"github.com/authgear/authgear-server/pkg/lib/infra/redis"
"github.com/authgear/authgear-server/pkg/lib/infra/redis/appredis"
"github.com/authgear/authgear-server/pkg/util/clock"
)
Expand All @@ -25,7 +26,7 @@ type StoreRedis struct {

func (s *StoreRedis) GetPromotionCode(codeHash string) (*PromotionCode, error) {
c := &PromotionCode{}
err := s.Redis.WithConn(func(conn *goredis.Conn) error {
err := s.Redis.WithConn(func(conn redis.Redis_6_0_Cmdable) error {
data, err := s.get(conn, promotionCodeKey(string(s.AppID), codeHash))
if err != nil {
return err
Expand All @@ -43,18 +44,18 @@ func (s *StoreRedis) GetPromotionCode(codeHash string) (*PromotionCode, error) {
}

func (s *StoreRedis) CreatePromotionCode(code *PromotionCode) error {
return s.Redis.WithConn(func(conn *goredis.Conn) error {
return s.Redis.WithConn(func(conn redis.Redis_6_0_Cmdable) error {
return s.save(conn, promotionCodeKey(code.AppID, code.CodeHash), code, code.ExpireAt)
})
}

func (s *StoreRedis) DeletePromotionCode(code *PromotionCode) error {
return s.Redis.WithConn(func(conn *goredis.Conn) error {
return s.Redis.WithConn(func(conn redis.Redis_6_0_Cmdable) error {
return s.del(conn, promotionCodeKey(code.AppID, code.CodeHash))
})
}

func (s *StoreRedis) get(conn *goredis.Conn, key string) ([]byte, error) {
func (s *StoreRedis) get(conn redis.Redis_6_0_Cmdable, key string) ([]byte, error) {
ctx := context.Background()
data, err := conn.Get(ctx, key).Bytes()
if errors.Is(err, goredis.Nil) {
Expand All @@ -65,7 +66,7 @@ func (s *StoreRedis) get(conn *goredis.Conn, key string) ([]byte, error) {
return data, nil
}

func (s *StoreRedis) save(conn *goredis.Conn, key string, value interface{}, expireAt time.Time) error {
func (s *StoreRedis) save(conn redis.Redis_6_0_Cmdable, key string, value interface{}, expireAt time.Time) error {
ctx := context.Background()
data, err := json.Marshal(value)
if err != nil {
Expand All @@ -82,7 +83,7 @@ func (s *StoreRedis) save(conn *goredis.Conn, key string, value interface{}, exp
return nil
}

func (s *StoreRedis) del(conn *goredis.Conn, key string) error {
func (s *StoreRedis) del(conn redis.Redis_6_0_Cmdable, key string) error {
ctx := context.Background()
_, err := conn.Del(ctx, key).Result()
return err
Expand Down
11 changes: 6 additions & 5 deletions pkg/lib/authn/mfa/store_device_token_redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
goredis "github.com/go-redis/redis/v8"

"github.com/authgear/authgear-server/pkg/lib/config"
"github.com/authgear/authgear-server/pkg/lib/infra/redis"
"github.com/authgear/authgear-server/pkg/lib/infra/redis/appredis"
"github.com/authgear/authgear-server/pkg/util/clock"
)
Expand All @@ -22,7 +23,7 @@ type StoreDeviceTokenRedis struct {

func (s *StoreDeviceTokenRedis) Get(userID string, token string) (*DeviceToken, error) {
var deviceToken *DeviceToken
err := s.Redis.WithConn(func(conn *goredis.Conn) error {
err := s.Redis.WithConn(func(conn redis.Redis_6_0_Cmdable) error {
ctx := context.Background()
key := redisDeviceTokensKey(s.AppID, userID)

Expand Down Expand Up @@ -63,7 +64,7 @@ func (s *StoreDeviceTokenRedis) Get(userID string, token string) (*DeviceToken,
}

func (s *StoreDeviceTokenRedis) Create(token *DeviceToken) error {
err := s.Redis.WithConn(func(conn *goredis.Conn) error {
err := s.Redis.WithConn(func(conn redis.Redis_6_0_Cmdable) error {
ctx := context.Background()
key := redisDeviceTokensKey(s.AppID, token.UserID)

Expand Down Expand Up @@ -96,7 +97,7 @@ func (s *StoreDeviceTokenRedis) Create(token *DeviceToken) error {
}

func (s *StoreDeviceTokenRedis) DeleteAll(userID string) error {
err := s.Redis.WithConn(func(conn *goredis.Conn) error {
err := s.Redis.WithConn(func(conn redis.Redis_6_0_Cmdable) error {
key := redisDeviceTokensKey(s.AppID, userID)
ctx := context.Background()
_, err := conn.Del(ctx, key).Result()
Expand All @@ -109,7 +110,7 @@ func (s *StoreDeviceTokenRedis) DeleteAll(userID string) error {
func (s *StoreDeviceTokenRedis) HasTokens(userID string) (bool, error) {
hasTokens := false

err := s.Redis.WithConn(func(conn *goredis.Conn) error {
err := s.Redis.WithConn(func(conn redis.Redis_6_0_Cmdable) error {
ctx := context.Background()
key := redisDeviceTokensKey(s.AppID, userID)
_, err := conn.Get(ctx, key).Bytes()
Expand All @@ -127,7 +128,7 @@ func (s *StoreDeviceTokenRedis) HasTokens(userID string) (bool, error) {
return hasTokens, err
}

func (s *StoreDeviceTokenRedis) saveTokens(conn *goredis.Conn, key string, tokens map[string]*DeviceToken, ttl time.Duration) error {
func (s *StoreDeviceTokenRedis) saveTokens(conn redis.Redis_6_0_Cmdable, key string, tokens map[string]*DeviceToken, ttl time.Duration) error {
ctx := context.Background()

if len(tokens) > 0 {
Expand Down
Loading

0 comments on commit 06b597d

Please sign in to comment.