Add team failing policies webhook (#4633)

* add config to teams
* update api docs
* update tests
This commit is contained in:
Michal Nicpon
2022-03-21 13:16:47 -06:00
committed by GitHub
parent ecdfd627b6
commit 7b671ac2a3
22 changed files with 844 additions and 266 deletions
@@ -236,7 +236,7 @@ func (ds *cachedMysql) SaveTeam(ctx context.Context, team *fleet.Team) (*fleet.T
key := fmt.Sprintf(teamAgentOptionsKey, team.ID)
ds.c.Set(key, team.AgentOptions, ds.teamAgentOptionsExp)
ds.c.Set(key, team.Config.AgentOptions, ds.teamAgentOptionsExp)
return team, nil
}
@@ -276,10 +276,12 @@ func TestCachedTeamAgentOptions(t *testing.T) {
`)
testTeam := &fleet.Team{
ID: 1,
CreatedAt: time.Now(),
Name: "test",
AgentOptions: &testOptions,
ID: 1,
CreatedAt: time.Now(),
Name: "test",
Config: fleet.TeamConfig{
AgentOptions: &testOptions,
},
}
deleted := false
@@ -306,10 +308,12 @@ func TestCachedTeamAgentOptions(t *testing.T) {
{}
`)
updateTeam := &fleet.Team{
ID: testTeam.ID,
CreatedAt: testTeam.CreatedAt,
Name: testTeam.Name,
AgentOptions: &updateOptions,
ID: testTeam.ID,
CreatedAt: testTeam.CreatedAt,
Name: testTeam.Name,
Config: fleet.TeamConfig{
AgentOptions: &updateOptions,
},
}
_, err = ds.SaveTeam(context.Background(), updateTeam)
@@ -0,0 +1,28 @@
package tables
import (
"database/sql"
"github.com/pkg/errors"
)
func init() {
MigrationClient.AddMigration(Up_20220309133956, Down_20220309133956)
}
func Up_20220309133956(tx *sql.Tx) error {
if _, err := tx.Exec(`ALTER TABLE teams ADD COLUMN config JSON`); err != nil {
return errors.Wrap(err, "add config column to teams table")
}
if _, err := tx.Exec(`UPDATE teams SET config = JSON_SET('{}', '$.agent_options', agent_options)`); err != nil {
return errors.Wrap(err, "migrate agent_options")
}
if _, err := tx.Exec(`ALTER TABLE teams DROP COLUMN agent_options`); err != nil {
return errors.Wrap(err, "drop agent_options column in teams table")
}
return nil
}
func Down_20220309133956(tx *sql.Tx) error {
return nil
}
@@ -0,0 +1,70 @@
package tables
import (
"database/sql/driver"
"encoding/json"
"fmt"
"testing"
"github.com/fleetdm/fleet/v4/server/ptr"
"github.com/stretchr/testify/require"
)
type Team20220309133956 struct {
Name string `db:"name"`
Config TeamConfig20220309133956 `db:"config"`
}
type TeamConfig20220309133956 struct {
AgentOptions *json.RawMessage `json:"agent_options" db:"agent_options"`
}
// Scan implements the sql.Scanner interface
func (t *TeamConfig20220309133956) Scan(val interface{}) error {
switch v := val.(type) {
case []byte:
return json.Unmarshal(v, t)
case string:
return json.Unmarshal([]byte(v), t)
case nil: // sql NULL
return nil
default:
return fmt.Errorf("unsupported type: %T", v)
}
}
// Value implements the sql.Valuer interface
func (t TeamConfig20220309133956) Value() (driver.Value, error) {
return json.Marshal(t)
}
func TestUp_20220309133956(t *testing.T) {
db := applyUpToPrev(t)
teams := []Team20220309133956{
{
Name: "test1",
},
{
Name: "test2",
Config: TeamConfig20220309133956{
AgentOptions: ptr.RawMessage(json.RawMessage(`{"config": {"options": {"logger_plugin": "tls", "pack_delimiter": "/", "logger_tls_period": 10, "distributed_plugin": "tls", "disable_distributed": false, "logger_tls_endpoint": "/api/v1/osquery/log", "distributed_interval": 10, "distributed_tls_max_attempts": 3}, "decorators": {"load": ["SELECT uuid AS host_uuid FROM system_info;", "SELECT hostname AS hostname FROM system_info;"]}}, "overrides": {}}`)),
},
},
}
_, err := db.Exec(`
INSERT INTO teams (name, agent_options)
VALUES (?, ?), (?, ?)
`, teams[0].Name, teams[0].Config.AgentOptions, teams[1].Name, teams[1].Config.AgentOptions)
require.NoError(t, err)
applyNext(t, db)
var actual []Team20220309133956
err = db.Select(&actual, `SELECT name, config from teams`)
require.NoError(t, err)
require.JSONEq(t, string(*teams[1].Config.AgentOptions), string(*actual[1].Config.AgentOptions))
require.Equal(t, teams, actual)
}
File diff suppressed because one or more lines are too long
+14 -12
View File
@@ -20,16 +20,16 @@ func (ds *Datastore) NewTeam(ctx context.Context, team *fleet.Team) (*fleet.Team
query := `
INSERT INTO teams (
name,
agent_options,
description
) VALUES ( ?, ?, ? )
description,
config
) VALUES (?, ?, ?)
`
result, err := tx.ExecContext(
ctx,
query,
team.Name,
team.AgentOptions,
team.Description,
team.Config,
)
if err != nil {
return ctxerr.Wrap(ctx, err, "insert team")
@@ -173,13 +173,15 @@ func saveUsersForTeamDB(ctx context.Context, exec sqlx.ExecerContext, team *flee
func (ds *Datastore) SaveTeam(ctx context.Context, team *fleet.Team) (*fleet.Team, error) {
err := ds.withRetryTxx(ctx, func(tx sqlx.ExtContext) error {
query := `
UPDATE teams SET
name = ?,
agent_options = ?,
description = ?
WHERE id = ?
`
_, err := tx.ExecContext(ctx, query, team.Name, team.AgentOptions, team.Description, team.ID)
UPDATE teams
SET
name = ?,
description = ?,
config = ?
WHERE
id = ?
`
_, err := tx.ExecContext(ctx, query, team.Name, team.Description, team.Config, team.ID)
if err != nil {
return ctxerr.Wrap(ctx, err, "saving team")
}
@@ -291,7 +293,7 @@ func amountTeamsDB(ctx context.Context, db sqlx.QueryerContext) (int, error) {
// TeamAgentOptions loads the agents options of a team.
func (ds *Datastore) TeamAgentOptions(ctx context.Context, tid uint) (*json.RawMessage, error) {
sql := `SELECT agent_options FROM teams WHERE id = ?`
sql := `SELECT config->"$.agent_options" FROM teams WHERE id = ?`
var agentOptions *json.RawMessage
if err := sqlx.GetContext(ctx, ds.reader, &agentOptions, sql, tid); err != nil {
return nil, ctxerr.Wrap(ctx, err, "select team")
+4 -2
View File
@@ -307,8 +307,10 @@ func testTeamsAgentOptions(t *testing.T, ds *Datastore) {
agentOptions := json.RawMessage(`{"config":{"foo":"bar"},"overrides":{"platforms":{"darwin":{"foo":"override"}}}}`)
team2, err := ds.NewTeam(context.Background(), &fleet.Team{
Name: "team2",
AgentOptions: &agentOptions,
Name: "team2",
Config: fleet.TeamConfig{
AgentOptions: &agentOptions,
},
})
require.NoError(t, err)