2020-04-22 20:59:40 +00:00
|
|
|
package mysql
|
|
|
|
|
|
|
|
import (
|
2021-09-14 12:11:07 +00:00
|
|
|
"context"
|
2021-09-07 16:48:04 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/x509"
|
|
|
|
"crypto/x509/pkix"
|
2021-09-01 19:50:52 +00:00
|
|
|
"database/sql"
|
2021-09-07 16:48:04 +00:00
|
|
|
"encoding/pem"
|
2021-11-15 14:11:38 +00:00
|
|
|
"errors"
|
2021-09-07 16:48:04 +00:00
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
"net"
|
|
|
|
"os"
|
2020-04-22 20:59:40 +00:00
|
|
|
"testing"
|
2021-09-01 19:50:52 +00:00
|
|
|
"time"
|
2020-04-22 20:59:40 +00:00
|
|
|
|
2021-03-17 18:23:52 +00:00
|
|
|
"github.com/DATA-DOG/go-sqlmock"
|
|
|
|
"github.com/VividCortex/mysqlerr"
|
2021-09-07 16:48:04 +00:00
|
|
|
"github.com/WatchBeam/clock"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/config"
|
2021-06-26 04:46:51 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/ptr"
|
2021-03-17 18:23:52 +00:00
|
|
|
"github.com/go-kit/kit/log"
|
|
|
|
"github.com/go-sql-driver/mysql"
|
|
|
|
"github.com/jmoiron/sqlx"
|
2020-04-22 20:59:40 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-03-17 18:23:52 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-04-22 20:59:40 +00:00
|
|
|
)
|
|
|
|
|
2021-09-01 19:50:52 +00:00
|
|
|
func TestDatastoreReplica(t *testing.T) {
|
|
|
|
// a bit unfortunate to create temp databases just for this - could be mixed
|
|
|
|
// with other tests when/if we move to subtests to minimize the number of
|
|
|
|
// databases created for tests (see #1805).
|
|
|
|
|
|
|
|
t.Run("noreplica", func(t *testing.T) {
|
|
|
|
ds := CreateMySQLDSWithOptions(t, nil)
|
|
|
|
defer ds.Close()
|
|
|
|
require.Equal(t, ds.reader, ds.writer)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("replica", func(t *testing.T) {
|
|
|
|
opts := &DatastoreTestOptions{Replica: true}
|
|
|
|
ds := CreateMySQLDSWithOptions(t, opts)
|
|
|
|
defer ds.Close()
|
|
|
|
require.NotEqual(t, ds.reader, ds.writer)
|
|
|
|
|
|
|
|
// create a new host
|
2021-09-14 12:11:07 +00:00
|
|
|
host, err := ds.NewHost(context.Background(), &fleet.Host{
|
2021-09-01 19:50:52 +00:00
|
|
|
DetailUpdatedAt: time.Now(),
|
|
|
|
LabelUpdatedAt: time.Now(),
|
2021-09-27 19:27:38 +00:00
|
|
|
PolicyUpdatedAt: time.Now(),
|
2021-09-01 19:50:52 +00:00
|
|
|
SeenTime: time.Now(),
|
|
|
|
NodeKey: "1",
|
|
|
|
UUID: "1",
|
|
|
|
Hostname: "foo.local",
|
|
|
|
PrimaryIP: "192.168.1.1",
|
|
|
|
PrimaryMac: "30-65-EC-6F-C4-58",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, host)
|
|
|
|
|
|
|
|
// trying to read it fails, not replicated yet
|
2021-11-18 17:36:35 +00:00
|
|
|
_, err = ds.Host(context.Background(), host.ID, false)
|
2021-09-01 19:50:52 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, errors.Is(err, sql.ErrNoRows))
|
|
|
|
|
|
|
|
opts.RunReplication()
|
|
|
|
|
|
|
|
// now it can read it
|
2021-11-18 17:36:35 +00:00
|
|
|
host2, err := ds.Host(context.Background(), host.ID, false)
|
2021-09-01 19:50:52 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, host.ID, host2.ID)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-04-22 20:59:40 +00:00
|
|
|
func TestSanitizeColumn(t *testing.T) {
|
2021-02-17 00:53:42 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2020-04-22 20:59:40 +00:00
|
|
|
testCases := []struct {
|
|
|
|
input string
|
|
|
|
output string
|
|
|
|
}{
|
|
|
|
{"foobar-column", "foobar-column"},
|
|
|
|
{"foobar_column", "foobar_column"},
|
|
|
|
{"foobar;column", "foobarcolumn"},
|
|
|
|
{"foobar#", "foobar"},
|
|
|
|
{"foobar*baz", "foobarbaz"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range testCases {
|
|
|
|
t.Run(tt.input, func(t *testing.T) {
|
2021-02-17 00:53:42 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2020-04-22 20:59:40 +00:00
|
|
|
assert.Equal(t, tt.output, sanitizeColumn(tt.input))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-02-17 00:53:42 +00:00
|
|
|
|
|
|
|
func TestSearchLike(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
inSQL string
|
|
|
|
inParams []interface{}
|
|
|
|
match string
|
|
|
|
columns []string
|
|
|
|
outSQL string
|
|
|
|
outParams []interface{}
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
inSQL: "SELECT * FROM HOSTS WHERE TRUE",
|
|
|
|
inParams: []interface{}{},
|
|
|
|
match: "foobar",
|
|
|
|
columns: []string{"hostname"},
|
|
|
|
outSQL: "SELECT * FROM HOSTS WHERE TRUE AND (hostname LIKE ?)",
|
|
|
|
outParams: []interface{}{"%foobar%"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
inSQL: "SELECT * FROM HOSTS WHERE TRUE",
|
|
|
|
inParams: []interface{}{3},
|
|
|
|
match: "foobar",
|
|
|
|
columns: []string{},
|
|
|
|
outSQL: "SELECT * FROM HOSTS WHERE TRUE",
|
|
|
|
outParams: []interface{}{3},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
inSQL: "SELECT * FROM HOSTS WHERE TRUE",
|
|
|
|
inParams: []interface{}{1},
|
|
|
|
match: "foobar",
|
|
|
|
columns: []string{"hostname"},
|
|
|
|
outSQL: "SELECT * FROM HOSTS WHERE TRUE AND (hostname LIKE ?)",
|
|
|
|
outParams: []interface{}{1, "%foobar%"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
inSQL: "SELECT * FROM HOSTS WHERE TRUE",
|
|
|
|
inParams: []interface{}{1},
|
|
|
|
match: "foobar",
|
|
|
|
columns: []string{"hostname", "uuid"},
|
|
|
|
outSQL: "SELECT * FROM HOSTS WHERE TRUE AND (hostname LIKE ? OR uuid LIKE ?)",
|
|
|
|
outParams: []interface{}{1, "%foobar%", "%foobar%"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
inSQL: "SELECT * FROM HOSTS WHERE TRUE",
|
|
|
|
inParams: []interface{}{1},
|
|
|
|
match: "foobar",
|
|
|
|
columns: []string{"hostname", "uuid"},
|
|
|
|
outSQL: "SELECT * FROM HOSTS WHERE TRUE AND (hostname LIKE ? OR uuid LIKE ?)",
|
|
|
|
outParams: []interface{}{1, "%foobar%", "%foobar%"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
inSQL: "SELECT * FROM HOSTS WHERE 1=1",
|
|
|
|
inParams: []interface{}{1},
|
|
|
|
match: "forty_%",
|
|
|
|
columns: []string{"ipv4", "uuid"},
|
|
|
|
outSQL: "SELECT * FROM HOSTS WHERE 1=1 AND (ipv4 LIKE ? OR uuid LIKE ?)",
|
|
|
|
outParams: []interface{}{1, "%forty\\_\\%%", "%forty\\_\\%%"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
inSQL: "SELECT * FROM HOSTS WHERE 1=1",
|
|
|
|
inParams: []interface{}{1},
|
|
|
|
match: "forty_%",
|
|
|
|
columns: []string{"ipv4", "uuid"},
|
|
|
|
outSQL: "SELECT * FROM HOSTS WHERE 1=1 AND (ipv4 LIKE ? OR uuid LIKE ?)",
|
|
|
|
outParams: []interface{}{1, "%forty\\_\\%%", "%forty\\_\\%%"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range testCases {
|
|
|
|
t.Run("", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
sql, params := searchLike(tt.inSQL, tt.inParams, tt.match, tt.columns...)
|
|
|
|
assert.Equal(t, tt.outSQL, sql)
|
|
|
|
assert.Equal(t, tt.outParams, params)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-03-17 18:23:52 +00:00
|
|
|
|
|
|
|
func mockDatastore(t *testing.T) (sqlmock.Sqlmock, *Datastore) {
|
|
|
|
db, mock, err := sqlmock.New()
|
|
|
|
require.NoError(t, err)
|
2021-09-01 19:50:52 +00:00
|
|
|
dbmock := sqlx.NewDb(db, "sqlmock")
|
2021-03-17 18:23:52 +00:00
|
|
|
ds := &Datastore{
|
2021-09-01 19:50:52 +00:00
|
|
|
writer: dbmock,
|
|
|
|
reader: dbmock,
|
2021-03-17 18:23:52 +00:00
|
|
|
logger: log.NewNopLogger(),
|
|
|
|
}
|
|
|
|
|
|
|
|
return mock, ds
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWithRetryTxxSuccess(t *testing.T) {
|
|
|
|
mock, ds := mockDatastore(t)
|
|
|
|
defer ds.Close()
|
|
|
|
|
|
|
|
mock.ExpectBegin()
|
|
|
|
mock.ExpectExec("SELECT 1").WillReturnResult(sqlmock.NewResult(1, 1))
|
|
|
|
mock.ExpectCommit()
|
|
|
|
|
2021-09-14 14:44:02 +00:00
|
|
|
require.NoError(t, ds.withRetryTxx(context.Background(), func(tx sqlx.ExtContext) error {
|
|
|
|
_, err := tx.ExecContext(context.Background(), "SELECT 1")
|
2021-03-17 18:23:52 +00:00
|
|
|
return err
|
|
|
|
}))
|
|
|
|
|
|
|
|
require.NoError(t, mock.ExpectationsWereMet())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWithRetryTxxRollbackSuccess(t *testing.T) {
|
|
|
|
mock, ds := mockDatastore(t)
|
|
|
|
defer ds.Close()
|
|
|
|
|
|
|
|
mock.ExpectBegin()
|
|
|
|
mock.ExpectExec("SELECT 1").WillReturnError(errors.New("fail"))
|
|
|
|
mock.ExpectRollback()
|
|
|
|
|
2021-09-14 14:44:02 +00:00
|
|
|
require.Error(t, ds.withRetryTxx(context.Background(), func(tx sqlx.ExtContext) error {
|
|
|
|
_, err := tx.ExecContext(context.Background(), "SELECT 1")
|
2021-03-17 18:23:52 +00:00
|
|
|
return err
|
|
|
|
}))
|
|
|
|
|
|
|
|
require.NoError(t, mock.ExpectationsWereMet())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWithRetryTxxRollbackError(t *testing.T) {
|
|
|
|
mock, ds := mockDatastore(t)
|
|
|
|
defer ds.Close()
|
|
|
|
|
|
|
|
mock.ExpectBegin()
|
|
|
|
mock.ExpectExec("SELECT 1").WillReturnError(errors.New("fail"))
|
|
|
|
mock.ExpectRollback().WillReturnError(errors.New("rollback failed"))
|
|
|
|
|
2021-09-14 14:44:02 +00:00
|
|
|
require.Error(t, ds.withRetryTxx(context.Background(), func(tx sqlx.ExtContext) error {
|
|
|
|
_, err := tx.ExecContext(context.Background(), "SELECT 1")
|
2021-03-17 18:23:52 +00:00
|
|
|
return err
|
|
|
|
}))
|
|
|
|
|
|
|
|
require.NoError(t, mock.ExpectationsWereMet())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWithRetryTxxRetrySuccess(t *testing.T) {
|
|
|
|
mock, ds := mockDatastore(t)
|
|
|
|
defer ds.Close()
|
|
|
|
|
|
|
|
mock.ExpectBegin()
|
|
|
|
// Return a retryable error
|
|
|
|
mock.ExpectExec("SELECT 1").WillReturnError(&mysql.MySQLError{Number: mysqlerr.ER_LOCK_DEADLOCK})
|
|
|
|
mock.ExpectRollback()
|
|
|
|
mock.ExpectBegin()
|
|
|
|
mock.ExpectExec("SELECT 1").WillReturnResult(sqlmock.NewResult(1, 1))
|
|
|
|
mock.ExpectCommit()
|
|
|
|
|
2021-09-14 14:44:02 +00:00
|
|
|
assert.NoError(t, ds.withRetryTxx(context.Background(), func(tx sqlx.ExtContext) error {
|
|
|
|
_, err := tx.ExecContext(context.Background(), "SELECT 1")
|
2021-03-17 18:23:52 +00:00
|
|
|
return err
|
|
|
|
}))
|
|
|
|
|
|
|
|
require.NoError(t, mock.ExpectationsWereMet())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWithRetryTxxCommitRetrySuccess(t *testing.T) {
|
|
|
|
mock, ds := mockDatastore(t)
|
|
|
|
defer ds.Close()
|
|
|
|
|
|
|
|
mock.ExpectBegin()
|
|
|
|
mock.ExpectExec("SELECT 1").WillReturnResult(sqlmock.NewResult(1, 1))
|
|
|
|
// Return a retryable error
|
|
|
|
mock.ExpectCommit().WillReturnError(&mysql.MySQLError{Number: mysqlerr.ER_LOCK_DEADLOCK})
|
|
|
|
mock.ExpectBegin()
|
|
|
|
mock.ExpectExec("SELECT 1").WillReturnResult(sqlmock.NewResult(1, 1))
|
|
|
|
mock.ExpectCommit()
|
|
|
|
|
2021-09-14 14:44:02 +00:00
|
|
|
assert.NoError(t, ds.withRetryTxx(context.Background(), func(tx sqlx.ExtContext) error {
|
|
|
|
_, err := tx.ExecContext(context.Background(), "SELECT 1")
|
2021-03-17 18:23:52 +00:00
|
|
|
return err
|
|
|
|
}))
|
|
|
|
|
|
|
|
require.NoError(t, mock.ExpectationsWereMet())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWithRetryTxxCommitError(t *testing.T) {
|
|
|
|
mock, ds := mockDatastore(t)
|
|
|
|
defer ds.Close()
|
|
|
|
|
|
|
|
mock.ExpectBegin()
|
|
|
|
mock.ExpectExec("SELECT 1").WillReturnResult(sqlmock.NewResult(1, 1))
|
|
|
|
// Return a retryable error
|
|
|
|
mock.ExpectCommit().WillReturnError(errors.New("fail"))
|
|
|
|
|
2021-09-14 14:44:02 +00:00
|
|
|
assert.Error(t, ds.withRetryTxx(context.Background(), func(tx sqlx.ExtContext) error {
|
|
|
|
_, err := tx.ExecContext(context.Background(), "SELECT 1")
|
2021-03-17 18:23:52 +00:00
|
|
|
return err
|
|
|
|
}))
|
|
|
|
|
|
|
|
require.NoError(t, mock.ExpectationsWereMet())
|
|
|
|
}
|
2021-04-05 18:04:53 +00:00
|
|
|
|
|
|
|
func TestAppendListOptionsToSQL(t *testing.T) {
|
2021-11-15 14:34:51 +00:00
|
|
|
sql := "SELECT * FROM my_table"
|
2021-06-06 22:07:29 +00:00
|
|
|
opts := fleet.ListOptions{
|
2021-04-05 18:04:53 +00:00
|
|
|
OrderKey: "name",
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := appendListOptionsToSQL(sql, opts)
|
2021-11-15 14:34:51 +00:00
|
|
|
expected := "SELECT * FROM my_table ORDER BY name ASC LIMIT 1000000"
|
2021-04-05 18:04:53 +00:00
|
|
|
if actual != expected {
|
|
|
|
t.Error("Expected", expected, "Actual", actual)
|
|
|
|
}
|
|
|
|
|
2021-11-15 14:34:51 +00:00
|
|
|
sql = "SELECT * FROM my_table"
|
2021-06-06 22:07:29 +00:00
|
|
|
opts.OrderDirection = fleet.OrderDescending
|
2021-04-05 18:04:53 +00:00
|
|
|
actual = appendListOptionsToSQL(sql, opts)
|
2021-11-15 14:34:51 +00:00
|
|
|
expected = "SELECT * FROM my_table ORDER BY name DESC LIMIT 1000000"
|
2021-04-05 18:04:53 +00:00
|
|
|
if actual != expected {
|
|
|
|
t.Error("Expected", expected, "Actual", actual)
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
opts = fleet.ListOptions{
|
2021-04-05 18:04:53 +00:00
|
|
|
PerPage: 10,
|
|
|
|
}
|
|
|
|
|
2021-11-15 14:34:51 +00:00
|
|
|
sql = "SELECT * FROM my_table"
|
2021-04-05 18:04:53 +00:00
|
|
|
actual = appendListOptionsToSQL(sql, opts)
|
2021-11-15 14:34:51 +00:00
|
|
|
expected = "SELECT * FROM my_table LIMIT 10"
|
2021-04-05 18:04:53 +00:00
|
|
|
if actual != expected {
|
|
|
|
t.Error("Expected", expected, "Actual", actual)
|
|
|
|
}
|
|
|
|
|
2021-11-15 14:34:51 +00:00
|
|
|
sql = "SELECT * FROM my_table"
|
2021-04-05 18:04:53 +00:00
|
|
|
opts.Page = 2
|
|
|
|
actual = appendListOptionsToSQL(sql, opts)
|
2021-11-15 14:34:51 +00:00
|
|
|
expected = "SELECT * FROM my_table LIMIT 10 OFFSET 20"
|
2021-04-05 18:04:53 +00:00
|
|
|
if actual != expected {
|
|
|
|
t.Error("Expected", expected, "Actual", actual)
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
opts = fleet.ListOptions{}
|
2021-11-15 14:34:51 +00:00
|
|
|
sql = "SELECT * FROM my_table"
|
2021-04-05 18:04:53 +00:00
|
|
|
actual = appendListOptionsToSQL(sql, opts)
|
2021-11-15 14:34:51 +00:00
|
|
|
expected = "SELECT * FROM my_table LIMIT 1000000"
|
2021-04-05 18:04:53 +00:00
|
|
|
|
|
|
|
if actual != expected {
|
|
|
|
t.Error("Expected", expected, "Actual", actual)
|
|
|
|
}
|
|
|
|
}
|
2021-05-25 04:34:08 +00:00
|
|
|
|
|
|
|
func TestWhereFilterHostsByTeams(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
testCases := []struct {
|
2021-06-06 22:07:29 +00:00
|
|
|
filter fleet.TeamFilter
|
2021-05-25 04:34:08 +00:00
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
// No teams or global role
|
|
|
|
{
|
2021-06-06 22:07:29 +00:00
|
|
|
filter: fleet.TeamFilter{
|
|
|
|
User: &fleet.User{},
|
2021-05-25 04:34:08 +00:00
|
|
|
},
|
|
|
|
expected: "FALSE",
|
|
|
|
},
|
|
|
|
{
|
2021-06-06 22:07:29 +00:00
|
|
|
filter: fleet.TeamFilter{
|
|
|
|
User: &fleet.User{Teams: []fleet.UserTeam{}},
|
2021-05-25 04:34:08 +00:00
|
|
|
},
|
|
|
|
expected: "FALSE",
|
|
|
|
},
|
|
|
|
|
|
|
|
// Global role
|
|
|
|
{
|
2021-06-06 22:07:29 +00:00
|
|
|
filter: fleet.TeamFilter{
|
|
|
|
User: &fleet.User{GlobalRole: ptr.String(fleet.RoleAdmin)},
|
2021-05-25 04:34:08 +00:00
|
|
|
},
|
|
|
|
expected: "TRUE",
|
|
|
|
},
|
|
|
|
{
|
2021-06-06 22:07:29 +00:00
|
|
|
filter: fleet.TeamFilter{
|
|
|
|
User: &fleet.User{GlobalRole: ptr.String(fleet.RoleMaintainer)},
|
2021-05-25 04:34:08 +00:00
|
|
|
},
|
|
|
|
expected: "TRUE",
|
|
|
|
},
|
|
|
|
{
|
2021-06-06 22:07:29 +00:00
|
|
|
filter: fleet.TeamFilter{
|
|
|
|
User: &fleet.User{GlobalRole: ptr.String(fleet.RoleObserver)},
|
2021-05-25 04:34:08 +00:00
|
|
|
},
|
|
|
|
expected: "FALSE",
|
|
|
|
},
|
|
|
|
{
|
2021-06-06 22:07:29 +00:00
|
|
|
filter: fleet.TeamFilter{
|
|
|
|
User: &fleet.User{GlobalRole: ptr.String(fleet.RoleObserver)},
|
2021-05-25 04:34:08 +00:00
|
|
|
IncludeObserver: true,
|
|
|
|
},
|
|
|
|
expected: "TRUE",
|
|
|
|
},
|
|
|
|
|
|
|
|
// Team roles
|
|
|
|
{
|
2021-06-06 22:07:29 +00:00
|
|
|
filter: fleet.TeamFilter{
|
|
|
|
User: &fleet.User{
|
|
|
|
Teams: []fleet.UserTeam{
|
|
|
|
{Role: fleet.RoleObserver, Team: fleet.Team{ID: 1}},
|
2021-05-25 04:34:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "FALSE",
|
|
|
|
},
|
|
|
|
{
|
2021-06-06 22:07:29 +00:00
|
|
|
filter: fleet.TeamFilter{
|
|
|
|
User: &fleet.User{
|
|
|
|
Teams: []fleet.UserTeam{
|
|
|
|
{Role: fleet.RoleObserver, Team: fleet.Team{ID: 1}},
|
2021-05-25 04:34:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
IncludeObserver: true,
|
|
|
|
},
|
|
|
|
expected: "hosts.team_id IN (1)",
|
|
|
|
},
|
|
|
|
{
|
2021-06-06 22:07:29 +00:00
|
|
|
filter: fleet.TeamFilter{
|
|
|
|
User: &fleet.User{
|
|
|
|
Teams: []fleet.UserTeam{
|
|
|
|
{Role: fleet.RoleObserver, Team: fleet.Team{ID: 1}},
|
|
|
|
{Role: fleet.RoleObserver, Team: fleet.Team{ID: 2}},
|
2021-05-25 04:34:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "FALSE",
|
|
|
|
},
|
|
|
|
{
|
2021-06-06 22:07:29 +00:00
|
|
|
filter: fleet.TeamFilter{
|
|
|
|
User: &fleet.User{
|
|
|
|
Teams: []fleet.UserTeam{
|
|
|
|
{Role: fleet.RoleObserver, Team: fleet.Team{ID: 1}},
|
|
|
|
{Role: fleet.RoleMaintainer, Team: fleet.Team{ID: 2}},
|
2021-05-25 04:34:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "hosts.team_id IN (2)",
|
|
|
|
},
|
|
|
|
{
|
2021-06-06 22:07:29 +00:00
|
|
|
filter: fleet.TeamFilter{
|
|
|
|
User: &fleet.User{
|
|
|
|
Teams: []fleet.UserTeam{
|
|
|
|
{Role: fleet.RoleObserver, Team: fleet.Team{ID: 1}},
|
|
|
|
{Role: fleet.RoleMaintainer, Team: fleet.Team{ID: 2}},
|
2021-05-25 04:34:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
IncludeObserver: true,
|
|
|
|
},
|
|
|
|
expected: "hosts.team_id IN (1,2)",
|
|
|
|
},
|
|
|
|
{
|
2021-06-06 22:07:29 +00:00
|
|
|
filter: fleet.TeamFilter{
|
|
|
|
User: &fleet.User{
|
|
|
|
Teams: []fleet.UserTeam{
|
|
|
|
{Role: fleet.RoleObserver, Team: fleet.Team{ID: 1}},
|
|
|
|
{Role: fleet.RoleMaintainer, Team: fleet.Team{ID: 2}},
|
2021-05-25 04:34:08 +00:00
|
|
|
// Invalid role should be ignored
|
2021-06-06 22:07:29 +00:00
|
|
|
{Role: "bad", Team: fleet.Team{ID: 37}},
|
2021-05-25 04:34:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "hosts.team_id IN (2)",
|
|
|
|
},
|
|
|
|
{
|
2021-06-06 22:07:29 +00:00
|
|
|
filter: fleet.TeamFilter{
|
|
|
|
User: &fleet.User{
|
|
|
|
Teams: []fleet.UserTeam{
|
|
|
|
{Role: fleet.RoleObserver, Team: fleet.Team{ID: 1}},
|
|
|
|
{Role: fleet.RoleMaintainer, Team: fleet.Team{ID: 2}},
|
|
|
|
{Role: fleet.RoleAdmin, Team: fleet.Team{ID: 3}},
|
2021-05-25 04:34:08 +00:00
|
|
|
// Invalid role should be ignored
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "hosts.team_id IN (2,3)",
|
|
|
|
},
|
2021-11-09 14:35:36 +00:00
|
|
|
{
|
|
|
|
filter: fleet.TeamFilter{
|
|
|
|
TeamID: ptr.Uint(1),
|
|
|
|
},
|
|
|
|
expected: "FALSE",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
filter: fleet.TeamFilter{
|
|
|
|
User: &fleet.User{GlobalRole: ptr.String(fleet.RoleObserver)},
|
|
|
|
IncludeObserver: true,
|
|
|
|
TeamID: ptr.Uint(1),
|
|
|
|
},
|
|
|
|
expected: "hosts.team_id = 1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
filter: fleet.TeamFilter{
|
|
|
|
User: &fleet.User{GlobalRole: ptr.String(fleet.RoleObserver)},
|
|
|
|
IncludeObserver: false,
|
|
|
|
TeamID: ptr.Uint(1),
|
|
|
|
},
|
|
|
|
expected: "FALSE",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
filter: fleet.TeamFilter{
|
|
|
|
User: &fleet.User{GlobalRole: ptr.String(fleet.RoleAdmin)},
|
|
|
|
IncludeObserver: false,
|
|
|
|
TeamID: ptr.Uint(1),
|
|
|
|
},
|
|
|
|
expected: "hosts.team_id = 1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
filter: fleet.TeamFilter{
|
|
|
|
User: &fleet.User{
|
|
|
|
Teams: []fleet.UserTeam{
|
|
|
|
{Role: fleet.RoleObserver, Team: fleet.Team{ID: 1}},
|
|
|
|
{Role: fleet.RoleMaintainer, Team: fleet.Team{ID: 2}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TeamID: ptr.Uint(3),
|
|
|
|
},
|
|
|
|
expected: "FALSE",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
filter: fleet.TeamFilter{
|
|
|
|
User: &fleet.User{
|
|
|
|
Teams: []fleet.UserTeam{
|
|
|
|
{Role: fleet.RoleObserver, Team: fleet.Team{ID: 1}},
|
|
|
|
{Role: fleet.RoleMaintainer, Team: fleet.Team{ID: 2}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TeamID: ptr.Uint(2),
|
|
|
|
},
|
|
|
|
expected: "hosts.team_id = 2",
|
|
|
|
},
|
2021-05-25 04:34:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range testCases {
|
|
|
|
tt := tt
|
|
|
|
t.Run("", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
ds := &Datastore{logger: log.NewNopLogger()}
|
|
|
|
sql := ds.whereFilterHostsByTeams(tt.filter, "hosts")
|
|
|
|
assert.Equal(t, tt.expected, sql)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-05-27 20:18:00 +00:00
|
|
|
|
|
|
|
func TestWhereOmitIDs(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
omits []uint
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
omits: nil,
|
|
|
|
expected: "TRUE",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
omits: []uint{},
|
|
|
|
expected: "TRUE",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
omits: []uint{1, 3, 4},
|
|
|
|
expected: "id NOT IN (1,3,4)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
omits: []uint{42},
|
|
|
|
expected: "id NOT IN (42)",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range testCases {
|
|
|
|
tt := tt
|
|
|
|
t.Run("", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
ds := &Datastore{logger: log.NewNopLogger()}
|
|
|
|
sql := ds.whereOmitIDs("id", tt.omits)
|
|
|
|
assert.Equal(t, tt.expected, sql)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-09-07 16:48:04 +00:00
|
|
|
|
|
|
|
func TestWithRetryTxWithRollback(t *testing.T) {
|
|
|
|
mock, ds := mockDatastore(t)
|
|
|
|
defer ds.Close()
|
|
|
|
|
|
|
|
mock.ExpectBegin()
|
|
|
|
mock.ExpectExec("SELECT 1").WillReturnError(errors.New("let's rollback!"))
|
|
|
|
mock.ExpectRollback()
|
|
|
|
|
2021-09-14 14:44:02 +00:00
|
|
|
assert.Error(t, ds.withRetryTxx(context.Background(), func(tx sqlx.ExtContext) error {
|
|
|
|
_, err := tx.ExecContext(context.Background(), "SELECT 1")
|
2021-09-07 16:48:04 +00:00
|
|
|
return err
|
|
|
|
}))
|
|
|
|
|
|
|
|
require.NoError(t, mock.ExpectationsWereMet())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWithRetryTxWillRollbackWhenPanic(t *testing.T) {
|
|
|
|
mock, ds := mockDatastore(t)
|
|
|
|
defer ds.Close()
|
|
|
|
defer func() { recover() }()
|
|
|
|
|
|
|
|
mock.ExpectBegin()
|
|
|
|
mock.ExpectExec("SELECT 1").WillReturnError(errors.New("let's rollback!"))
|
|
|
|
mock.ExpectRollback()
|
|
|
|
|
2021-09-14 14:44:02 +00:00
|
|
|
assert.Error(t, ds.withRetryTxx(context.Background(), func(tx sqlx.ExtContext) error {
|
2021-09-07 16:48:04 +00:00
|
|
|
panic("ROLLBACK")
|
|
|
|
}))
|
|
|
|
|
|
|
|
require.NoError(t, mock.ExpectationsWereMet())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWithTxWithRollback(t *testing.T) {
|
|
|
|
mock, ds := mockDatastore(t)
|
|
|
|
defer ds.Close()
|
|
|
|
|
|
|
|
mock.ExpectBegin()
|
|
|
|
mock.ExpectExec("SELECT 1").WillReturnError(errors.New("let's rollback!"))
|
|
|
|
mock.ExpectRollback()
|
|
|
|
|
2021-09-14 14:44:02 +00:00
|
|
|
assert.Error(t, ds.withTx(context.Background(), func(tx sqlx.ExtContext) error {
|
|
|
|
_, err := tx.ExecContext(context.Background(), "SELECT 1")
|
2021-09-07 16:48:04 +00:00
|
|
|
return err
|
|
|
|
}))
|
|
|
|
|
|
|
|
require.NoError(t, mock.ExpectationsWereMet())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWithTxWillRollbackWhenPanic(t *testing.T) {
|
|
|
|
mock, ds := mockDatastore(t)
|
|
|
|
defer ds.Close()
|
|
|
|
defer func() { recover() }()
|
|
|
|
|
|
|
|
mock.ExpectBegin()
|
|
|
|
mock.ExpectExec("SELECT 1").WillReturnError(errors.New("let's rollback!"))
|
|
|
|
mock.ExpectRollback()
|
|
|
|
|
2021-09-14 14:44:02 +00:00
|
|
|
assert.Error(t, ds.withTx(context.Background(), func(tx sqlx.ExtContext) error {
|
2021-09-07 16:48:04 +00:00
|
|
|
panic("ROLLBACK")
|
|
|
|
}))
|
|
|
|
|
|
|
|
require.NoError(t, mock.ExpectationsWereMet())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewReadsPasswordFromDisk(t *testing.T) {
|
|
|
|
passwordFile, err := os.CreateTemp(t.TempDir(), "*.passwordtest")
|
|
|
|
require.NoError(t, err)
|
|
|
|
_, err = passwordFile.WriteString(testPassword)
|
|
|
|
require.NoError(t, err)
|
|
|
|
passwordPath := passwordFile.Name()
|
|
|
|
require.NoError(t, passwordFile.Close())
|
|
|
|
|
|
|
|
dbName := t.Name()
|
|
|
|
|
|
|
|
// Create a datastore client in order to run migrations as usual
|
|
|
|
mysqlConfig := config.MysqlConfig{
|
|
|
|
Username: testUsername,
|
|
|
|
Password: "",
|
|
|
|
PasswordPath: passwordPath,
|
|
|
|
Address: testAddress,
|
|
|
|
Database: dbName,
|
|
|
|
}
|
|
|
|
ds, err := newDSWithConfig(t, dbName, mysqlConfig)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer ds.Close()
|
|
|
|
require.NoError(t, ds.HealthCheck())
|
|
|
|
}
|
|
|
|
|
|
|
|
func newDSWithConfig(t *testing.T, dbName string, config config.MysqlConfig) (*Datastore, error) {
|
|
|
|
db, err := sql.Open(
|
|
|
|
"mysql",
|
|
|
|
fmt.Sprintf("%s:%s@tcp(%s)/?multiStatements=true", testUsername, testPassword, testAddress),
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
_, err = db.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %s; CREATE DATABASE %s;", dbName, dbName))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ds, err := New(config, clock.NewMockClock(), Logger(log.NewNopLogger()), LimitAttempts(1))
|
|
|
|
return ds, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateTestCert(t *testing.T) (string, string) {
|
|
|
|
privateKeyCA, err := rsa.GenerateKey(rand.Reader, 1024)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
|
|
|
|
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
|
|
|
|
require.NoError(t, err)
|
|
|
|
template := x509.Certificate{
|
|
|
|
SerialNumber: serialNumber,
|
|
|
|
Subject: pkix.Name{
|
|
|
|
Organization: []string{"aa"},
|
|
|
|
},
|
|
|
|
NotBefore: time.Now().Add(-1 * time.Duration(24) * time.Hour),
|
|
|
|
NotAfter: time.Now().Add(24 * time.Hour),
|
|
|
|
IsCA: true,
|
|
|
|
KeyUsage: x509.KeyUsageDigitalSignature,
|
|
|
|
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
|
|
|
|
BasicConstraintsValid: true,
|
|
|
|
IPAddresses: []net.IP{net.ParseIP("127.0.0.1")},
|
|
|
|
}
|
|
|
|
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &privateKeyCA.PublicKey, privateKeyCA)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
publicPem, err := os.CreateTemp(t.TempDir(), "*-ca.pem")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, pem.Encode(publicPem, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}))
|
|
|
|
require.NoError(t, publicPem.Close())
|
|
|
|
|
|
|
|
keyPem, err := os.CreateTemp(t.TempDir(), "*-key.pem")
|
|
|
|
require.NoError(t, err)
|
|
|
|
privateKeyBytes := x509.MarshalPKCS1PrivateKey(privateKeyCA)
|
|
|
|
require.NoError(t, pem.Encode(keyPem, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: privateKeyBytes}))
|
|
|
|
require.NoError(t, keyPem.Close())
|
|
|
|
|
|
|
|
return publicPem.Name(), keyPem.Name()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewUsesRegisterTLS(t *testing.T) {
|
|
|
|
dbName := t.Name()
|
|
|
|
|
|
|
|
ca, _ := generateTestCert(t)
|
|
|
|
cert, key := generateTestCert(t)
|
|
|
|
|
|
|
|
mysqlConfig := config.MysqlConfig{
|
|
|
|
Username: testUsername,
|
|
|
|
Password: testPassword,
|
|
|
|
Address: testAddress,
|
|
|
|
Database: dbName,
|
|
|
|
TLSCA: ca,
|
|
|
|
TLSCert: cert,
|
|
|
|
TLSKey: key,
|
|
|
|
}
|
|
|
|
// This fails because the certificate mysql is using is different than the one generated here
|
|
|
|
_, err := newDSWithConfig(t, dbName, mysqlConfig)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Equal(t, "x509: certificate is not valid for any names, but wanted to match localhost", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWhereFilterTeas(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
filter fleet.TeamFilter
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
// No teams or global role
|
|
|
|
{
|
|
|
|
filter: fleet.TeamFilter{User: nil},
|
|
|
|
expected: "FALSE",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
filter: fleet.TeamFilter{
|
|
|
|
User: &fleet.User{GlobalRole: ptr.String(fleet.RoleAdmin)},
|
|
|
|
},
|
|
|
|
expected: "TRUE",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
filter: fleet.TeamFilter{
|
|
|
|
User: &fleet.User{GlobalRole: ptr.String(fleet.RoleObserver)},
|
|
|
|
IncludeObserver: false,
|
|
|
|
},
|
|
|
|
expected: "FALSE",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
filter: fleet.TeamFilter{
|
|
|
|
User: &fleet.User{GlobalRole: ptr.String(fleet.RoleObserver)},
|
|
|
|
IncludeObserver: true,
|
|
|
|
},
|
|
|
|
expected: "TRUE",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
filter: fleet.TeamFilter{User: &fleet.User{Teams: []fleet.UserTeam{{Team: fleet.Team{ID: 1}, Role: fleet.RoleAdmin}}}},
|
|
|
|
expected: "t.id IN (1)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
filter: fleet.TeamFilter{User: &fleet.User{Teams: []fleet.UserTeam{{Team: fleet.Team{ID: 1}, Role: fleet.RoleMaintainer}}}},
|
|
|
|
expected: "t.id IN (1)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
filter: fleet.TeamFilter{User: &fleet.User{Teams: []fleet.UserTeam{{Team: fleet.Team{ID: 1}, Role: fleet.RoleObserver}}}},
|
|
|
|
expected: "FALSE",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
filter: fleet.TeamFilter{
|
|
|
|
User: &fleet.User{Teams: []fleet.UserTeam{{Team: fleet.Team{ID: 1}, Role: fleet.RoleMaintainer}}},
|
|
|
|
IncludeObserver: true,
|
|
|
|
},
|
|
|
|
expected: "t.id IN (1)",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range testCases {
|
|
|
|
tt := tt
|
|
|
|
t.Run("", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
ds := &Datastore{logger: log.NewNopLogger()}
|
|
|
|
sql := ds.whereFilterTeams(tt.filter, "t")
|
|
|
|
assert.Equal(t, tt.expected, sql)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-11-22 17:06:12 +00:00
|
|
|
|
|
|
|
func TestCompareVersions(t *testing.T) {
|
|
|
|
for _, tc := range []struct {
|
|
|
|
name string
|
|
|
|
|
|
|
|
v1 []int64
|
|
|
|
v2 []int64
|
|
|
|
|
|
|
|
expMissing []int64
|
|
|
|
expUnknown []int64
|
|
|
|
expEqual bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "both-empty",
|
|
|
|
v1: nil,
|
|
|
|
v2: nil,
|
|
|
|
expEqual: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "equal",
|
|
|
|
v1: []int64{1, 2, 3},
|
|
|
|
v2: []int64{1, 2, 3},
|
|
|
|
expEqual: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "equal-out-of-order",
|
|
|
|
v1: []int64{1, 2, 3},
|
|
|
|
v2: []int64{1, 3, 2},
|
|
|
|
expEqual: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "empty-with-unknown",
|
|
|
|
v1: nil,
|
|
|
|
v2: []int64{1},
|
|
|
|
expEqual: false,
|
|
|
|
expUnknown: []int64{1},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "empty-with-missing",
|
|
|
|
v1: []int64{1},
|
|
|
|
v2: nil,
|
|
|
|
expEqual: false,
|
|
|
|
expMissing: []int64{1},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "missing",
|
|
|
|
v1: []int64{1, 2, 3},
|
|
|
|
v2: []int64{1, 3},
|
|
|
|
expMissing: []int64{2},
|
|
|
|
expEqual: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "unknown",
|
|
|
|
v1: []int64{1, 2, 3},
|
|
|
|
v2: []int64{1, 2, 3, 4},
|
|
|
|
expUnknown: []int64{4},
|
|
|
|
expEqual: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "missing-and-unknown",
|
|
|
|
v1: []int64{1, 2, 3},
|
|
|
|
v2: []int64{1, 2, 4},
|
|
|
|
expMissing: []int64{3},
|
|
|
|
expUnknown: []int64{4},
|
|
|
|
expEqual: false,
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
missing, unknown, equal := compareVersions(tc.v1, tc.v2)
|
|
|
|
require.Equal(t, tc.expMissing, missing)
|
|
|
|
require.Equal(t, tc.expUnknown, unknown)
|
|
|
|
require.Equal(t, tc.expEqual, equal)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|