Skip to content

Commit

Permalink
Fixed Oauth client listing issue with redis cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
lonelycode committed Oct 6, 2015
1 parent bca6754 commit 8d4b8d1
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ testapps/
tyk.conf
tyk.compose.rpc.conf
tyk.compose.local_rpc.conf
tyk.compose.cluster.conf
tyk.rpc.conf
tyk.rpc.local.conf
/tyk
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# 1.8.3.3

- Gateway Mongo Driver updated to be compatible with MongoDB v3.0
- Fixed OAuth client listings with redis cluster

# 1.8.3.2

Expand Down
13 changes: 13 additions & 0 deletions ldap_auth_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,16 @@ func (s *LDAPStorageHandler) SetRollingWindow(keyName string, per int64, expire
log.Warning("Not Implemented!")
return 0
}

func (s LDAPStorageHandler) GetSet(keyName string) (map[string]string, error) {
log.Error("Not implemented")
return map[string]string{}, nil
}

func (s LDAPStorageHandler) AddToSet(keyName string, value string) {
log.Error("Not implemented")
}

func (s LDAPStorageHandler) RemoveFromSet(keyName string, value string) {
log.Error("Not implemented")
}
35 changes: 30 additions & 5 deletions oauth_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,11 @@ func (o *OAuthManager) HandleAccess(r *http.Request) *osin.Response {
// These enums fix the prefix to use when storing various OAuth keys and data, since we
// delegate everything to the osin framework
const (
AUTH_PREFIX string = "oauth-authorize."
CLIENT_PREFIX string = "oauth-clientid."
ACCESS_PREFIX string = "oauth-access."
REFRESH_PREFIX string = "oauth-refresh."
AUTH_PREFIX string = "oauth-authorize."
CLIENT_PREFIX string = "oauth-clientid."
ACCESS_PREFIX string = "oauth-access."
REFRESH_PREFIX string = "oauth-refresh."
OAUTH_CLIENTSET_PREFIX string = "oauth-clientset."
)

type ExtendedOsinStorageInterface interface {
Expand Down Expand Up @@ -445,7 +446,17 @@ func (r RedisOsinStorageInterface) GetClients(filter string, ignorePrefix bool)
key = filter
}

clientJSON := r.store.GetKeysAndValuesWithFilter(key)
var clientJSON map[string]string
if !config.Storage.EnableCluster {
clientJSON = r.store.GetKeysAndValuesWithFilter(key)
} else {
var getErr error
KeyForSet := OAUTH_CLIENTSET_PREFIX + CLIENT_PREFIX // Org ID
clientJSON, getErr = r.store.GetSet(KeyForSet)
if getErr != nil {
return []osin.Client{}, getErr
}
}

theseClients := []osin.Client{}

Expand Down Expand Up @@ -481,6 +492,11 @@ func (r RedisOsinStorageInterface) SetClient(id string, client osin.Client, igno
log.Warning("CREATING: ", key)

r.store.SetKey(key, string(clientDataJSON), 0)

log.Info("Storing copy in set")

KeyForSet := OAUTH_CLIENTSET_PREFIX + CLIENT_PREFIX // Org ID
r.store.AddToSet(KeyForSet, string(clientDataJSON))
return nil
}

Expand All @@ -491,7 +507,16 @@ func (r RedisOsinStorageInterface) DeleteClient(id string, ignorePrefix bool) er
key = id
}

// Get the raw vals:
clientJSON, storeErr := r.store.GetKey(key)
if storeErr == nil {
log.Info("Removing from set")
KeyForSet := OAUTH_CLIENTSET_PREFIX + CLIENT_PREFIX // Org ID
r.store.RemoveFromSet(KeyForSet, clientJSON)
}

r.store.DeleteKey(key)

return nil
}

Expand Down
67 changes: 65 additions & 2 deletions redis_cluster_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,9 +508,8 @@ func (r *RedisClusterStorageManager) GetAndDeleteSet(keyName string) []interface
}

func (r *RedisClusterStorageManager) AppendToSet(keyName string, value string) {

log.Debug("Pushing to raw key set: ", keyName)
log.Debug("Pushing to fixed key set: ", r.fixKey(keyName))
log.Info("Pushing to fixed key set: ", r.fixKey(keyName))
if r.db == nil {
log.Warning("Connection dropped, connecting..")
r.Connect()
Expand All @@ -527,6 +526,70 @@ func (r *RedisClusterStorageManager) AppendToSet(keyName string, value string) {
}
}

func (r *RedisClusterStorageManager) GetSet(keyName string) (map[string]string, error) {
log.Debug("Getting from key set: ", keyName)
log.Debug("Getting from fixed key set: ", r.fixKey(keyName))
if r.db == nil {
log.Warning("Connection dropped, connecting..")
r.Connect()
r.GetSet(keyName)
} else {
val, err := r.db.Do("SMEMBERS", r.fixKey(keyName))
if err != nil {
log.Error("Error trying to get key set:", err)
return map[string]string{}, err
}

asValues, _ := redis.Strings(val, err)

vals := make(map[string]string)
for i, value := range asValues {
vals[strconv.Itoa(i)] = value
}

return vals, nil
}
return map[string]string{}, nil
}

func (r *RedisClusterStorageManager) AddToSet(keyName string, value string) {
log.Debug("Pushing to raw key set: ", keyName)
log.Debug("Pushing to fixed key set: ", r.fixKey(keyName))
if r.db == nil {
log.Warning("Connection dropped, connecting..")
r.Connect()
r.AddToSet(keyName, value)
} else {
_, err := r.db.Do("SADD", r.fixKey(keyName), value)

if err != nil {
log.Error("Error trying to append keys:")
log.Error(err)
}

return
}
}

func (r *RedisClusterStorageManager) RemoveFromSet(keyName string, value string) {
log.Debug("Removing from raw key set: ", keyName)
log.Debug("Removing from fixed key set: ", r.fixKey(keyName))
if r.db == nil {
log.Warning("Connection dropped, connecting..")
r.Connect()
r.RemoveFromSet(keyName, value)
} else {
_, err := r.db.Do("SREM", r.fixKey(keyName), value)

if err != nil {
log.Error("Error trying to remove keys:")
log.Error(err)
}

return
}
}

// IncrementWithExpire will increment a key in redis
func (r *RedisClusterStorageManager) SetRollingWindow(keyName string, per int64, expire int64) int {

Expand Down
13 changes: 13 additions & 0 deletions rpc_storage_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,19 @@ func (r *RPCStorageHandler) SetRollingWindow(keyName string, per int64, expire i

}

func (r RPCStorageHandler) GetSet(keyName string) (map[string]string, error) {
log.Error("Not implemented")
return map[string]string{}, nil
}

func (r RPCStorageHandler) AddToSet(keyName string, value string) {
log.Error("Not implemented")
}

func (r RPCStorageHandler) RemoveFromSet(keyName string, value string) {
log.Error("Not implemented")
}

func (r RPCStorageHandler) IsAccessError(err error) bool {
if err != nil {
if err.Error() == "Access Denied" {
Expand Down
92 changes: 92 additions & 0 deletions storage_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ type StorageHandler interface {
Decrement(string)
IncrememntWithExpire(string, int64) int64
SetRollingWindow(string, int64, int64) int
GetSet(string) (map[string]string, error)
AddToSet(string, string)
RemoveFromSet(string, string)
}

// InMemoryStorageManager implements the StorageHandler interface,
Expand Down Expand Up @@ -151,6 +154,19 @@ func (s InMemoryStorageManager) DeleteKeys(keys []string) bool {
return true
}

func (s InMemoryStorageManager) GetSet(keyName string) (map[string]string, error) {
log.Error("Not implemented")
return map[string]string{}, nil
}

func (s InMemoryStorageManager) AddToSet(keyName string, value string) {
log.Error("Not implemented")
}

func (s InMemoryStorageManager) RemoveFromSet(keyName string, value string) {
log.Error("Not implemented")
}

// ------------------- REDIS STORAGE MANAGER -------------------------------

// We want to centralise this, this will minimise
Expand Down Expand Up @@ -753,3 +769,79 @@ func (r *RedisStorageManager) SetRollingWindow(keyName string, per int64, expire
}
return 0
}

func (r *RedisStorageManager) GetSet(keyName string) (map[string]string, error) {
log.Debug("Getting from key set: ", keyName)
log.Info("Getting from fixed key set: ", r.fixKey(keyName))

db := r.pool.Get()
defer db.Close()

if db == nil {
log.Warning("Connection dropped, connecting..")
r.Connect()
r.GetSet(keyName)
} else {
val, err := db.Do("SMEMBERS", r.fixKey(keyName))
if err != nil {
log.Error("Error trying to get key set:", err)
return map[string]string{}, err
}

asValues, _ := redis.Strings(val, err)

vals := make(map[string]string)
for i, value := range asValues {
vals[strconv.Itoa(i)] = value
}

return vals, nil
}
return map[string]string{}, nil
}

func (r *RedisStorageManager) AddToSet(keyName string, value string) {
log.Debug("Pushing to raw key set: ", keyName)
log.Info("Pushing to fixed key set: ", r.fixKey(keyName))

db := r.pool.Get()
defer db.Close()

if db == nil {
log.Warning("Connection dropped, connecting..")
r.Connect()
r.AddToSet(keyName, value)
} else {
_, err := db.Do("SADD", r.fixKey(keyName), value)

if err != nil {
log.Error("Error trying to append keys:")
log.Error(err)
}

return
}
}

func (r *RedisStorageManager) RemoveFromSet(keyName string, value string) {
log.Debug("Removing from raw key set: ", keyName)
log.Info("Removing from fixed key set: ", r.fixKey(keyName))

db := r.pool.Get()
defer db.Close()

if db == nil {
log.Warning("Connection dropped, connecting..")
r.Connect()
r.RemoveFromSet(keyName, value)
} else {
_, err := db.Do("SREM", r.fixKey(keyName), value)

if err != nil {
log.Error("Error trying to append keys:")
log.Error(err)
}

return
}
}
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package main

var VERSION string = "v1.8.3.3"
var VERSION string = "v1.8.3.4"

0 comments on commit 8d4b8d1

Please sign in to comment.