2021-09-07 16:48:04 +00:00
|
|
|
package mysql
|
|
|
|
|
|
|
|
import (
|
2021-09-14 12:11:07 +00:00
|
|
|
"context"
|
2021-09-07 16:48:04 +00:00
|
|
|
"testing"
|
Add read replica testing helpers and fix non-sso login bug (#4908)
not set on the INSERT.
- OUT: Only sets the ID on the passed session and returns it. (`CreatedAt`, `AccessedAt`, are not set.)
New version:
```go
func (ds *Datastore) NewSession(ctx context.Context, userID uint, sessionKey string) (*fleet.Session, error) {
sqlStatement := `
INSERT INTO sessions (
user_id,
` + "`key`" + `
)
VALUES(?,?)
`
result, err := ds.writer.ExecContext(ctx, sqlStatement, userID, sessionKey)
if err != nil {
return nil, ctxerr.Wrap(ctx, err, "inserting session")
}
id, _ := result.LastInsertId() // cannot fail with the mysql driver
return ds.sessionByID(ctx, ds.writer, uint(id))
}
```
- IN: Define arguments that are truly used when creating a session.
- OUT: Load and return the fleet.Session struct with all values set (using the `ds.writer` to support read replicas correctly).
PS: The new `NewSession` version mimics what we already do with other entities, like policies (`Datastore.NewGlobalPolicy`).
2022-04-04 23:52:05 +00:00
|
|
|
"time"
|
2021-09-07 16:48:04 +00:00
|
|
|
|
Add read replica testing helpers and fix non-sso login bug (#4908)
not set on the INSERT.
- OUT: Only sets the ID on the passed session and returns it. (`CreatedAt`, `AccessedAt`, are not set.)
New version:
```go
func (ds *Datastore) NewSession(ctx context.Context, userID uint, sessionKey string) (*fleet.Session, error) {
sqlStatement := `
INSERT INTO sessions (
user_id,
` + "`key`" + `
)
VALUES(?,?)
`
result, err := ds.writer.ExecContext(ctx, sqlStatement, userID, sessionKey)
if err != nil {
return nil, ctxerr.Wrap(ctx, err, "inserting session")
}
id, _ := result.LastInsertId() // cannot fail with the mysql driver
return ds.sessionByID(ctx, ds.writer, uint(id))
}
```
- IN: Define arguments that are truly used when creating a session.
- OUT: Load and return the fleet.Session struct with all values set (using the `ds.writer` to support read replicas correctly).
PS: The new `NewSession` version mimics what we already do with other entities, like policies (`Datastore.NewGlobalPolicy`).
2022-04-04 23:52:05 +00:00
|
|
|
"github.com/WatchBeam/clock"
|
2021-09-07 16:48:04 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/ptr"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2021-09-20 18:09:38 +00:00
|
|
|
func TestSessions(t *testing.T) {
|
2021-09-07 16:48:04 +00:00
|
|
|
ds := CreateMySQLDS(t)
|
|
|
|
|
2021-09-20 18:09:38 +00:00
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
fn func(t *testing.T, ds *Datastore)
|
|
|
|
}{
|
|
|
|
{"Getters", testSessionsGetters},
|
|
|
|
}
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.name, func(t *testing.T) {
|
|
|
|
defer TruncateTables(t, ds)
|
|
|
|
c.fn(t, ds)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testSessionsGetters(t *testing.T, ds *Datastore) {
|
2021-09-14 12:11:07 +00:00
|
|
|
user, err := ds.NewUser(context.Background(), &fleet.User{
|
2021-09-07 16:48:04 +00:00
|
|
|
Password: []byte("supersecret"),
|
|
|
|
Email: "other@bobcom",
|
|
|
|
GlobalRole: ptr.String(fleet.RoleObserver),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
Add read replica testing helpers and fix non-sso login bug (#4908)
not set on the INSERT.
- OUT: Only sets the ID on the passed session and returns it. (`CreatedAt`, `AccessedAt`, are not set.)
New version:
```go
func (ds *Datastore) NewSession(ctx context.Context, userID uint, sessionKey string) (*fleet.Session, error) {
sqlStatement := `
INSERT INTO sessions (
user_id,
` + "`key`" + `
)
VALUES(?,?)
`
result, err := ds.writer.ExecContext(ctx, sqlStatement, userID, sessionKey)
if err != nil {
return nil, ctxerr.Wrap(ctx, err, "inserting session")
}
id, _ := result.LastInsertId() // cannot fail with the mysql driver
return ds.sessionByID(ctx, ds.writer, uint(id))
}
```
- IN: Define arguments that are truly used when creating a session.
- OUT: Load and return the fleet.Session struct with all values set (using the `ds.writer` to support read replicas correctly).
PS: The new `NewSession` version mimics what we already do with other entities, like policies (`Datastore.NewGlobalPolicy`).
2022-04-04 23:52:05 +00:00
|
|
|
session, err := ds.NewSession(context.Background(), user.ID, "somekey")
|
2021-09-07 16:48:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotZero(t, session.ID)
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
gotByID, err := ds.SessionByID(context.Background(), session.ID)
|
2021-09-07 16:48:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, session.Key, gotByID.Key)
|
2022-02-22 13:12:03 +00:00
|
|
|
require.NotNil(t, gotByID.APIOnly)
|
|
|
|
assert.False(t, *gotByID.APIOnly)
|
2021-09-07 16:48:04 +00:00
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
gotByKey, err := ds.SessionByKey(context.Background(), session.Key)
|
2021-09-07 16:48:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, session.ID, gotByKey.ID)
|
2022-02-22 13:12:03 +00:00
|
|
|
require.NotNil(t, gotByKey.APIOnly)
|
|
|
|
assert.False(t, *gotByKey.APIOnly)
|
2021-09-07 16:48:04 +00:00
|
|
|
|
Add read replica testing helpers and fix non-sso login bug (#4908)
not set on the INSERT.
- OUT: Only sets the ID on the passed session and returns it. (`CreatedAt`, `AccessedAt`, are not set.)
New version:
```go
func (ds *Datastore) NewSession(ctx context.Context, userID uint, sessionKey string) (*fleet.Session, error) {
sqlStatement := `
INSERT INTO sessions (
user_id,
` + "`key`" + `
)
VALUES(?,?)
`
result, err := ds.writer.ExecContext(ctx, sqlStatement, userID, sessionKey)
if err != nil {
return nil, ctxerr.Wrap(ctx, err, "inserting session")
}
id, _ := result.LastInsertId() // cannot fail with the mysql driver
return ds.sessionByID(ctx, ds.writer, uint(id))
}
```
- IN: Define arguments that are truly used when creating a session.
- OUT: Load and return the fleet.Session struct with all values set (using the `ds.writer` to support read replicas correctly).
PS: The new `NewSession` version mimics what we already do with other entities, like policies (`Datastore.NewGlobalPolicy`).
2022-04-04 23:52:05 +00:00
|
|
|
newSession, err := ds.NewSession(context.Background(), user.ID, "somekey2")
|
2021-09-07 16:48:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
sessions, err := ds.ListSessionsForUser(context.Background(), user.ID)
|
2021-09-07 16:48:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, sessions, 2)
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
require.NoError(t, ds.DestroySession(context.Background(), session))
|
2021-09-07 16:48:04 +00:00
|
|
|
|
|
|
|
prevAccessedAt := newSession.AccessedAt
|
|
|
|
|
Add read replica testing helpers and fix non-sso login bug (#4908)
not set on the INSERT.
- OUT: Only sets the ID on the passed session and returns it. (`CreatedAt`, `AccessedAt`, are not set.)
New version:
```go
func (ds *Datastore) NewSession(ctx context.Context, userID uint, sessionKey string) (*fleet.Session, error) {
sqlStatement := `
INSERT INTO sessions (
user_id,
` + "`key`" + `
)
VALUES(?,?)
`
result, err := ds.writer.ExecContext(ctx, sqlStatement, userID, sessionKey)
if err != nil {
return nil, ctxerr.Wrap(ctx, err, "inserting session")
}
id, _ := result.LastInsertId() // cannot fail with the mysql driver
return ds.sessionByID(ctx, ds.writer, uint(id))
}
```
- IN: Define arguments that are truly used when creating a session.
- OUT: Load and return the fleet.Session struct with all values set (using the `ds.writer` to support read replicas correctly).
PS: The new `NewSession` version mimics what we already do with other entities, like policies (`Datastore.NewGlobalPolicy`).
2022-04-04 23:52:05 +00:00
|
|
|
// Advance ds's mock clock time (used by MarkSessionAccessed).
|
|
|
|
mc := ds.clock.(*clock.MockClock)
|
|
|
|
mc.AddTime(1 * time.Second)
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
require.NoError(t, ds.MarkSessionAccessed(context.Background(), newSession))
|
2021-09-07 16:48:04 +00:00
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
sessions, err = ds.ListSessionsForUser(context.Background(), user.ID)
|
2021-09-07 16:48:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, sessions, 1)
|
|
|
|
require.NotEqual(t, prevAccessedAt, sessions[0].AccessedAt)
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
require.NoError(t, ds.DestroyAllSessionsForUser(context.Background(), user.ID))
|
2022-02-22 13:12:03 +00:00
|
|
|
|
|
|
|
// session for a non-existing user
|
Add read replica testing helpers and fix non-sso login bug (#4908)
not set on the INSERT.
- OUT: Only sets the ID on the passed session and returns it. (`CreatedAt`, `AccessedAt`, are not set.)
New version:
```go
func (ds *Datastore) NewSession(ctx context.Context, userID uint, sessionKey string) (*fleet.Session, error) {
sqlStatement := `
INSERT INTO sessions (
user_id,
` + "`key`" + `
)
VALUES(?,?)
`
result, err := ds.writer.ExecContext(ctx, sqlStatement, userID, sessionKey)
if err != nil {
return nil, ctxerr.Wrap(ctx, err, "inserting session")
}
id, _ := result.LastInsertId() // cannot fail with the mysql driver
return ds.sessionByID(ctx, ds.writer, uint(id))
}
```
- IN: Define arguments that are truly used when creating a session.
- OUT: Load and return the fleet.Session struct with all values set (using the `ds.writer` to support read replicas correctly).
PS: The new `NewSession` version mimics what we already do with other entities, like policies (`Datastore.NewGlobalPolicy`).
2022-04-04 23:52:05 +00:00
|
|
|
newSession, err = ds.NewSession(context.Background(), user.ID+1, "someotherkey")
|
2022-02-22 13:12:03 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
gotByKey, err = ds.SessionByKey(context.Background(), newSession.Key)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, newSession.ID, gotByKey.ID)
|
|
|
|
require.Nil(t, gotByKey.APIOnly)
|
|
|
|
|
|
|
|
gotByID, err = ds.SessionByID(context.Background(), newSession.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, newSession.ID, gotByKey.ID)
|
|
|
|
require.Nil(t, gotByKey.APIOnly)
|
|
|
|
|
|
|
|
apiUser, err := ds.NewUser(context.Background(), &fleet.User{
|
|
|
|
Password: []byte("supersecret"),
|
|
|
|
GlobalRole: ptr.String(fleet.RoleObserver),
|
|
|
|
APIOnly: true,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// session for an api user
|
Add read replica testing helpers and fix non-sso login bug (#4908)
not set on the INSERT.
- OUT: Only sets the ID on the passed session and returns it. (`CreatedAt`, `AccessedAt`, are not set.)
New version:
```go
func (ds *Datastore) NewSession(ctx context.Context, userID uint, sessionKey string) (*fleet.Session, error) {
sqlStatement := `
INSERT INTO sessions (
user_id,
` + "`key`" + `
)
VALUES(?,?)
`
result, err := ds.writer.ExecContext(ctx, sqlStatement, userID, sessionKey)
if err != nil {
return nil, ctxerr.Wrap(ctx, err, "inserting session")
}
id, _ := result.LastInsertId() // cannot fail with the mysql driver
return ds.sessionByID(ctx, ds.writer, uint(id))
}
```
- IN: Define arguments that are truly used when creating a session.
- OUT: Load and return the fleet.Session struct with all values set (using the `ds.writer` to support read replicas correctly).
PS: The new `NewSession` version mimics what we already do with other entities, like policies (`Datastore.NewGlobalPolicy`).
2022-04-04 23:52:05 +00:00
|
|
|
apiSession, err := ds.NewSession(context.Background(), apiUser.ID, "someapikey")
|
2022-02-22 13:12:03 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
gotByKey, err = ds.SessionByKey(context.Background(), apiSession.Key)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, apiSession.ID, gotByKey.ID)
|
|
|
|
require.NotNil(t, gotByKey.APIOnly)
|
|
|
|
assert.True(t, *gotByKey.APIOnly)
|
|
|
|
|
|
|
|
gotByID, err = ds.SessionByID(context.Background(), apiSession.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, apiSession.ID, gotByKey.ID)
|
|
|
|
require.NotNil(t, gotByKey.APIOnly)
|
|
|
|
assert.True(t, *gotByKey.APIOnly)
|
2021-09-07 16:48:04 +00:00
|
|
|
}
|