Skip to content

Commit

Permalink
Updated storage handler error responses
Browse files Browse the repository at this point in the history
  • Loading branch information
lonelycode committed Sep 2, 2015
1 parent 3eaba69 commit 849e042
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 42 deletions.
33 changes: 28 additions & 5 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ func doAddOrUpdate(keyName string, newSession SessionState, dontReset bool) erro
thisAPISpec.SessionManager.ResetQuota(keyName, newSession)
newSession.QuotaRenews = time.Now().Unix() + newSession.QuotaRenewalRate
}
thisAPISpec.SessionManager.UpdateSession(keyName, newSession, thisAPISpec.SessionLifetime)
err := thisAPISpec.SessionManager.UpdateSession(keyName, newSession, thisAPISpec.SessionLifetime)
if err != nil {
return err
}
}
} else {
log.WithFields(logrus.Fields{
Expand All @@ -102,7 +105,10 @@ func doAddOrUpdate(keyName string, newSession SessionState, dontReset bool) erro
spec.SessionManager.ResetQuota(keyName, newSession)
newSession.QuotaRenews = time.Now().Unix() + newSession.QuotaRenewalRate
}
spec.SessionManager.UpdateSession(keyName, newSession, spec.SessionLifetime)
err := spec.SessionManager.UpdateSession(keyName, newSession, spec.SessionLifetime)
if err != nil {
return err
}
}
} else {
log.Error("Master keys disallowed in configuration, key not added.")
Expand Down Expand Up @@ -724,7 +730,11 @@ func handleOrgAddOrUpdate(keyName string, r *http.Request) ([]byte, int) {
DefaultQuotaStore.RemoveSession(rawKey)
}

thisSessionManager.UpdateSession(keyName, newSession, 0)
err := thisSessionManager.UpdateSession(keyName, newSession, 0)
if err != nil {
responseMessage = createError("Error writing to key store " + err.Error())
return responseMessage, 400
}

log.WithFields(logrus.Fields{
"key": keyName,
Expand Down Expand Up @@ -946,11 +956,19 @@ func createKeyHandler(w http.ResponseWriter, r *http.Request) {
thisAPISpec.SessionManager.ResetQuota(newKey, newSession)
newSession.QuotaRenews = time.Now().Unix() + newSession.QuotaRenewalRate
}
thisAPISpec.SessionManager.UpdateSession(newKey, newSession, thisAPISpec.SessionLifetime)
err := thisAPISpec.SessionManager.UpdateSession(newKey, newSession, thisAPISpec.SessionLifetime)
if err != nil {
responseMessage = createError("Failed to create key - " + err.Error())
DoJSONWrite(w, 403, responseMessage)
return
}
} else {
log.WithFields(logrus.Fields{
"apiID": apiId,
}).Error("Could not create key for this API ID, API doesn't exist.")
responseMessage = createError("Could not create key for this API ID, API doesn't exist.")
DoJSONWrite(w, 403, responseMessage)
return
}
}
} else {
Expand All @@ -963,7 +981,12 @@ func createKeyHandler(w http.ResponseWriter, r *http.Request) {
spec.SessionManager.ResetQuota(newKey, newSession)
newSession.QuotaRenews = time.Now().Unix() + newSession.QuotaRenewalRate
}
spec.SessionManager.UpdateSession(newKey, newSession, spec.SessionLifetime)
err := spec.SessionManager.UpdateSession(newKey, newSession, spec.SessionLifetime)
if err != nil {
responseMessage = createError("Failed to create key - " + err.Error())
DoJSONWrite(w, 403, responseMessage)
return
}
}
} else {
log.Error("Master keys disallowed in configuration, key not added.")
Expand Down
28 changes: 5 additions & 23 deletions auth_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type AuthorisationHandler interface {
// SessionState objects, not identity
type SessionHandler interface {
Init(store StorageHandler)
UpdateSession(keyName string, session SessionState, resetTTLTo int64)
UpdateSession(keyName string, session SessionState, resetTTLTo int64) error
RemoveSession(keyName string)
GetSessionDetail(keyName string) (SessionState, bool)
GetSessions(filter string) []string
Expand Down Expand Up @@ -97,34 +97,16 @@ func (b *DefaultSessionManager) ResetQuota(keyName string, session SessionState)
}

// UpdateSession updates the session state in the storage engine
func (b DefaultSessionManager) UpdateSession(keyName string, session SessionState, resetTTLTo int64) {
func (b DefaultSessionManager) UpdateSession(keyName string, session SessionState, resetTTLTo int64) error {
v, _ := json.Marshal(session)
// var ttl int64
// var err error

// Why do we do this, it is handled in the storage handler!

// if resetTTLTo == 0 {
// ttl, err = b.Store.GetExp(keyName)

// if err != nil {
// log.Error("Failed to get TTL for key: ", err)
// return
// }
// } else {
// // Used on create, we update the TTL of the key
// ttl = resetTTLTo
// }

// by default expire the key if we get a nil value based on the session
// keyExp = (session.Expires - time.Now().Unix()) + 300 // Add 5 minutes to key expiry, just in case

// Keep the TTL
if config.UseAsyncSessionWrite {
go b.Store.SetKey(keyName, string(v), int64(resetTTLTo))
} else {
b.Store.SetKey(keyName, string(v), int64(resetTTLTo))
return nil
}
err := b.Store.SetKey(keyName, string(v), int64(resetTTLTo))
return err

}

Expand Down
3 changes: 2 additions & 1 deletion ldap_auth_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,9 @@ func (l *LDAPStorageHandler) GetKeysAndValuesWithFilter(filter string) map[strin
return s
}

func (l *LDAPStorageHandler) SetKey(cn string, sessionState string, timeout int64) {
func (l *LDAPStorageHandler) SetKey(cn string, sessionState string, timeout int64) error {
l.notifyReadOnly()
return nil
}

func (l *LDAPStorageHandler) DeleteKey(cn string) bool {
Expand Down
6 changes: 3 additions & 3 deletions rpc_storage_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func (r *RPCStorageHandler) GetExp(keyName string) (int64, error) {
}

// SetKey will create (or update) a key value in the store
func (r *RPCStorageHandler) SetKey(keyName string, sessionState string, timeout int64) {
func (r *RPCStorageHandler) SetKey(keyName string, sessionState string, timeout int64) error {
start := time.Now() // get current time
ibd := InboundData{
KeyName: r.fixKey(keyName),
Expand All @@ -216,12 +216,12 @@ func (r *RPCStorageHandler) SetKey(keyName string, sessionState string, timeout

if r.IsAccessError(err) {
r.Login()
r.SetKey(keyName, sessionState, timeout)
return
return r.SetKey(keyName, sessionState, timeout)
}

elapsed := time.Since(start)
log.Debug("SetKey took ", elapsed)
return nil

}

Expand Down
23 changes: 13 additions & 10 deletions storage_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ const (
// StorageHandler is a standard interface to a storage backend,
// used by AuthorisationManager to read and write key values to the backend
type StorageHandler interface {
GetKey(string) (string, error) // Returned string is expected to be a JSON object (SessionState)
SetKey(string, string, int64) // Second input string is expected to be a JSON object (SessionState)
GetExp(string) (int64, error) // Returns expiry of a key
GetKey(string) (string, error) // Returned string is expected to be a JSON object (SessionState)
SetKey(string, string, int64) error // Second input string is expected to be a JSON object (SessionState)
GetExp(string) (int64, error) // Returns expiry of a key
GetKeys(string) []string
DeleteKey(string) bool
DeleteRawKey(string) bool
Expand Down Expand Up @@ -80,8 +80,9 @@ func (s InMemoryStorageManager) GetKey(keyName string) (string, error) {
}

// SetKey updates the in-memory key
func (s InMemoryStorageManager) SetKey(keyName string, sessionState string, timeout int64) {
func (s InMemoryStorageManager) SetKey(keyName string, sessionState string, timeout int64) error {
s.Sessions[keyName] = sessionState
return nil
}

func (s InMemoryStorageManager) GetExp(keyName string) (int64, error) {
Expand Down Expand Up @@ -287,7 +288,7 @@ func (r *RedisStorageManager) GetExp(keyName string) (int64, error) {
}

// SetKey will create (or update) a key value in the store
func (r *RedisStorageManager) SetKey(keyName string, sessionState string, timeout int64) {
func (r *RedisStorageManager) SetKey(keyName string, sessionState string, timeout int64) error {
db := r.pool.Get()
defer db.Close()
log.Debug("[STORE] SET Raw key is: ", keyName)
Expand All @@ -296,21 +297,23 @@ func (r *RedisStorageManager) SetKey(keyName string, sessionState string, timeou
if db == nil {
log.Info("Connection dropped, connecting..")
r.Connect()
r.SetKey(keyName, sessionState, timeout)
return r.SetKey(keyName, sessionState, timeout)
} else {
_, err := db.Do("SET", r.fixKey(keyName), sessionState)
if timeout > 0 {
_, expErr := db.Do("EXPIRE", r.fixKey(keyName), timeout)
if expErr != nil {
log.Error("Could not EXPIRE key")
log.Error(expErr)
log.Error("Could not EXPIRE key: ", expErr)
return expErr
}
}
if err != nil {
log.Error("Error trying to set value:")
log.Error(err)
log.Error("Error trying to set value: ", err)
return err
}
}

return nil
}

// Decrement will decrement a key in redis
Expand Down

0 comments on commit 849e042

Please sign in to comment.