diff --git a/bolt/session.go b/bolt/session.go index 9342675318..f0d0cab251 100644 --- a/bolt/session.go +++ b/bolt/session.go @@ -3,7 +3,6 @@ package bolt import ( "context" "encoding/json" - "fmt" "time" bolt "github.com/coreos/bbolt" @@ -25,6 +24,7 @@ func (c *Client) initializeSessions(ctx context.Context, tx *bolt.Tx) error { // FindSession retrieves the session found at the provided key. func (c *Client) FindSession(ctx context.Context, key string) (*platform.Session, error) { + op := getOp(platform.OpFindSession) var sess *platform.Session err := c.db.View(func(tx *bolt.Tx) error { s, err := c.findSession(ctx, tx, key) @@ -33,33 +33,49 @@ func (c *Client) FindSession(ctx context.Context, key string) (*platform.Session } sess = s - return nil }) if err != nil { - return nil, err + return nil, &platform.Error{ + Err: err, + Op: op, + } } - return sess, sess.Expired() + if err := sess.Expired(); err != nil { + // todo(leodido) > do we want to return session also if expired? + return sess, &platform.Error{ + Err: err, + Op: op, + } + } + return sess, nil } -func (c *Client) findSession(ctx context.Context, tx *bolt.Tx, key string) (*platform.Session, error) { +func (c *Client) findSession(ctx context.Context, tx *bolt.Tx, key string) (*platform.Session, *platform.Error) { v := tx.Bucket(sessionBucket).Get([]byte(key)) if len(v) == 0 { - return nil, fmt.Errorf("session not found") + return nil, &platform.Error{ + Code: platform.ENotFound, + Msg: platform.ErrSessionNotFound, + } } s := &platform.Session{} if err := json.Unmarshal(v, s); err != nil { - return nil, err + return nil, &platform.Error{ + Err: err, + } } // TODO(desa): these values should be cached so it's not so expensive to lookup each time. f := platform.UserResourceMappingFilter{UserID: s.UserID} mappings, err := c.findUserResourceMappings(ctx, tx, f) if err != nil { - return nil, err + return nil, &platform.Error{ + Err: err, + } } ps := make([]platform.Permission, 0, len(mappings)) @@ -73,17 +89,24 @@ func (c *Client) findSession(ctx context.Context, tx *bolt.Tx, key string) (*pla // PutSession puts the session at key. func (c *Client) PutSession(ctx context.Context, s *platform.Session) error { return c.db.Update(func(tx *bolt.Tx) error { - return c.putSession(ctx, tx, s) + if err := c.putSession(ctx, tx, s); err != nil { + return err + } + return nil }) } -func (c *Client) putSession(ctx context.Context, tx *bolt.Tx, s *platform.Session) error { +func (c *Client) putSession(ctx context.Context, tx *bolt.Tx, s *platform.Session) *platform.Error { v, err := json.Marshal(s) if err != nil { - return err + return &platform.Error{ + Err: err, + } } if err := tx.Bucket(sessionBucket).Put([]byte(s.Key), v); err != nil { - return err + return &platform.Error{ + Err: err, + } } return nil } @@ -98,7 +121,10 @@ func (c *Client) ExpireSession(ctx context.Context, key string) error { s.ExpiresAt = time.Now() - return c.putSession(ctx, tx, s) + if err := c.putSession(ctx, tx, s); err != nil { + return err + } + return nil }) } @@ -123,7 +149,7 @@ func (c *Client) CreateSession(ctx context.Context, user string) (*platform.Sess return sess, nil } -func (c *Client) createSession(ctx context.Context, tx *bolt.Tx, user string) (*platform.Session, error) { +func (c *Client) createSession(ctx context.Context, tx *bolt.Tx, user string) (*platform.Session, *platform.Error) { u, pe := c.findUserByName(ctx, tx, user) if pe != nil { return nil, pe @@ -133,7 +159,9 @@ func (c *Client) createSession(ctx context.Context, tx *bolt.Tx, user string) (* s.ID = c.IDGenerator.ID() k, err := c.TokenGenerator.Token() if err != nil { - return nil, err + return nil, &platform.Error{ + Err: err, + } } s.Key = k s.UserID = u.ID diff --git a/bolt/session_test.go b/bolt/session_test.go index fbc3ee13f4..cfe25921bf 100644 --- a/bolt/session_test.go +++ b/bolt/session_test.go @@ -5,10 +5,11 @@ import ( "testing" "github.com/influxdata/platform" + "github.com/influxdata/platform/bolt" platformtesting "github.com/influxdata/platform/testing" ) -func initSessionService(f platformtesting.SessionFields, t *testing.T) (platform.SessionService, func()) { +func initSessionService(f platformtesting.SessionFields, t *testing.T) (platform.SessionService, string, func()) { c, closeFn, err := NewTestClient() if err != nil { t.Fatalf("failed to create new bolt client: %v", err) @@ -26,7 +27,7 @@ func initSessionService(f platformtesting.SessionFields, t *testing.T) (platform t.Fatalf("failed to populate sessions") } } - return c, func() { + return c, bolt.OpPrefix, func() { defer closeFn() for _, u := range f.Users { if err := c.DeleteUser(ctx, u.ID); err != nil {