fleet/server/test/new_objects.go

299 lines
8.7 KiB
Go
Raw Normal View History

package test
import (
"context"
"testing"
"time"
"github.com/fleetdm/fleet/v4/server"
2021-06-26 04:46:51 +00:00
"github.com/fleetdm/fleet/v4/server/fleet"
"github.com/fleetdm/fleet/v4/server/ptr"
"github.com/stretchr/testify/require"
)
func NewQueryWithSchedule(t *testing.T, ds fleet.Datastore, teamID *uint, name, q string, authorID uint, saved bool, interval uint, automationsEnabled bool) *fleet.Query {
authorPtr := &authorID
if authorID == 0 {
authorPtr = nil
}
query, err := ds.NewQuery(context.Background(), &fleet.Query{
Name: name,
Query: q,
AuthorID: authorPtr,
Saved: saved,
TeamID: teamID,
Interval: interval,
AutomationsEnabled: automationsEnabled,
Logging: fleet.LoggingSnapshot,
})
require.NoError(t, err)
// Loading gives us the timestamps
query, err = ds.Query(context.Background(), query.ID)
require.NoError(t, err)
return query
}
func NewQuery(t *testing.T, ds fleet.Datastore, teamID *uint, name, q string, authorID uint, saved bool) *fleet.Query {
return NewQueryWithSchedule(t, ds, teamID, name, q, authorID, saved, 0, false)
}
func NewPack(t *testing.T, ds fleet.Datastore, name string) *fleet.Pack {
err := ds.ApplyPackSpecs(context.Background(), []*fleet.PackSpec{{Name: name}})
require.Nil(t, err)
// Loading gives us the timestamps
pack, ok, err := ds.PackByName(context.Background(), name)
require.True(t, ok)
require.NoError(t, err)
return pack
}
func NewCampaign(t *testing.T, ds fleet.Datastore, queryID uint, status fleet.DistributedQueryStatus, now time.Time) *fleet.DistributedQueryCampaign {
campaign, err := ds.NewDistributedQueryCampaign(context.Background(), &fleet.DistributedQueryCampaign{
UpdateCreateTimestamps: fleet.UpdateCreateTimestamps{
CreateTimestamp: fleet.CreateTimestamp{
CreatedAt: now,
},
},
QueryID: queryID,
Status: status,
})
require.NoError(t, err)
// Loading gives us the timestamps
campaign, err = ds.DistributedQueryCampaign(context.Background(), campaign.ID)
require.NoError(t, err)
return campaign
}
func AddHostToCampaign(t *testing.T, ds fleet.Datastore, campaignID, hostID uint) {
_, err := ds.NewDistributedQueryCampaignTarget(
context.Background(),
&fleet.DistributedQueryCampaignTarget{
Type: fleet.TargetHost,
TargetID: hostID,
DistributedQueryCampaignID: campaignID,
})
require.NoError(t, err)
}
func AddLabelToCampaign(t *testing.T, ds fleet.Datastore, campaignID, labelID uint) {
_, err := ds.NewDistributedQueryCampaignTarget(
context.Background(),
&fleet.DistributedQueryCampaignTarget{
Type: fleet.TargetLabel,
TargetID: labelID,
DistributedQueryCampaignID: campaignID,
})
require.NoError(t, err)
}
func AddAllHostsLabel(t *testing.T, ds fleet.Datastore) {
_, err := ds.NewLabel(
context.Background(),
&fleet.Label{
Name: "All Hosts",
Query: "select 1",
LabelType: fleet.LabelTypeBuiltIn,
LabelMembershipType: fleet.LabelMembershipTypeManual,
},
)
require.NoError(t, err)
}
func AddBuiltinLabels(t *testing.T, ds fleet.Datastore) {
builtins := []*fleet.Label{
{
Name: "All Hosts",
Query: "select 1",
LabelType: fleet.LabelTypeBuiltIn,
2024-04-16 15:20:59 +00:00
LabelMembershipType: fleet.LabelMembershipTypeDynamic,
},
{
Name: "macOS",
Query: "select 1 from os_version where platform = 'darwin';",
LabelType: fleet.LabelTypeBuiltIn,
LabelMembershipType: fleet.LabelMembershipTypeDynamic,
},
{
Name: "Ubuntu Linux",
Query: "select 1 from os_version where platform = 'ubuntu';",
LabelType: fleet.LabelTypeBuiltIn,
LabelMembershipType: fleet.LabelMembershipTypeDynamic,
},
{
Name: "CentOS Linux",
Query: "select 1 from os_version where platform = 'centos' or name like '%centos%';",
LabelType: fleet.LabelTypeBuiltIn,
LabelMembershipType: fleet.LabelMembershipTypeDynamic,
},
{
Name: "MS Windows",
Query: "select 1 from os_version where platform = 'windows';",
LabelType: fleet.LabelTypeBuiltIn,
LabelMembershipType: fleet.LabelMembershipTypeDynamic,
},
{
Name: "Red Hat Linux",
Query: "SELECT 1 FROM os_version WHERE name LIKE '%red hat%'",
LabelType: fleet.LabelTypeBuiltIn,
LabelMembershipType: fleet.LabelMembershipTypeDynamic,
},
{
Name: "All Linux",
Query: "SELECT 1 FROM osquery_info WHERE build_platform LIKE '%ubuntu%' OR build_distro LIKE '%centos%';",
LabelType: fleet.LabelTypeBuiltIn,
LabelMembershipType: fleet.LabelMembershipTypeDynamic,
},
{
Name: "chrome",
Query: "select 1 from os_version where platform = 'chrome';",
LabelType: fleet.LabelTypeBuiltIn,
LabelMembershipType: fleet.LabelMembershipTypeDynamic,
},
2024-04-15 20:10:10 +00:00
{
Name: fleet.BuiltinLabelMacOS14Plus,
Query: "select 1 from os_version where platform = 'darwin' and major >= 14;",
LabelType: fleet.LabelTypeBuiltIn,
LabelMembershipType: fleet.LabelMembershipTypeDynamic,
},
iOS/iPadOS as platforms/labels (#20126) #19963 - [X] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://fleetdm.com/docs/contributing/committing-changes#changes-files) for more information. - [X] Input data is properly validated, `SELECT *` is avoided, SQL injection is prevented (using placeholders for values in statements) - [X] Added/updated tests - [X] If database migrations are included, checked table schema to confirm autoupdate - For database migrations: - [X] Checked schema for all modified table for columns that will auto-update timestamps during migration. - [X] Confirmed that updating the timestamps is acceptable, and will not cause unwanted side effects. - [X] Ensured the correct collation is explicitly set for character columns (`COLLATE utf8mb4_unicode_ci`). - [X] Manual QA for all new/changed functionality --- # API changes for dashboard UI changes ## Main dashboard page `GET /api/latest/fleet/host_summary?low_disk_space=32` (see `ios`/`ipados` platforms and `iOS`/`iPadOS` labels) ```json { "totals_hosts_count": 9, "online_count": 0, "offline_count": 9, "mia_count": 0, "missing_30_days_count": 0, "new_count": 0, "all_linux_count": 2, "low_disk_space_count": 3, "builtin_labels": [ { "id": 1, "name": "macOS 14+ (Sonoma+)", "description": "macOS hosts with version 14 and above", "label_type": "builtin" }, { "id": 7, "name": "All Hosts", "description": "All hosts which have enrolled in Fleet", "label_type": "builtin" }, { "id": 8, "name": "macOS", "description": "All macOS hosts", "label_type": "builtin" }, { "id": 9, "name": "Ubuntu Linux", "description": "All Ubuntu hosts", "label_type": "builtin" }, { "id": 10, "name": "CentOS Linux", "description": "All CentOS hosts", "label_type": "builtin" }, { "id": 11, "name": "MS Windows", "description": "All Windows hosts", "label_type": "builtin" }, { "id": 12, "name": "Red Hat Linux", "description": "All Red Hat Enterprise Linux hosts", "label_type": "builtin" }, { "id": 13, "name": "All Linux", "description": "All Linux distributions", "label_type": "builtin" }, { "id": 14, "name": "chrome", "description": "All Chrome hosts", "label_type": "builtin" }, { "id": 15, "name": "iOS", "description": "All iOS hosts", "label_type": "builtin" }, { "id": 16, "name": "iPadOS", "description": "All iPadOS hosts", "label_type": "builtin" } ], "platforms": [ { "platform": "darwin", "hosts_count": 3 }, { "platform": "ios", "hosts_count": 1 }, { "platform": "ipados", "hosts_count": 1 }, { "platform": "rhel", "hosts_count": 1 }, { "platform": "ubuntu", "hosts_count": 1 }, { "platform": "windows", "hosts_count": 2 } ] } ``` ## After selecting a platform `GET /api/latest/fleet/host_summary?platform=ios&low_disk_space=100` (similar with `ipados`) ```json { "totals_hosts_count": 1, "online_count": 0, "offline_count": 1, "mia_count": 0, "missing_30_days_count": 0, "new_count": 0, "all_linux_count": 0, "low_disk_space_count": 1, "builtin_labels": [ { "id": 1, "name": "macOS 14+ (Sonoma+)", "description": "macOS hosts with version 14 and above", "label_type": "builtin" }, { "id": 7, "name": "All Hosts", "description": "All hosts which have enrolled in Fleet", "label_type": "builtin" }, { "id": 8, "name": "macOS", "description": "All macOS hosts", "label_type": "builtin" }, { "id": 9, "name": "Ubuntu Linux", "description": "All Ubuntu hosts", "label_type": "builtin" }, { "id": 10, "name": "CentOS Linux", "description": "All CentOS hosts", "label_type": "builtin" }, { "id": 11, "name": "MS Windows", "description": "All Windows hosts", "label_type": "builtin" }, { "id": 12, "name": "Red Hat Linux", "description": "All Red Hat Enterprise Linux hosts", "label_type": "builtin" }, { "id": 13, "name": "All Linux", "description": "All Linux distributions", "label_type": "builtin" }, { "id": 14, "name": "chrome", "description": "All Chrome hosts", "label_type": "builtin" }, { "id": 15, "name": "iOS", "description": "All iOS hosts", "label_type": "builtin" }, { "id": 16, "name": "iPadOS", "description": "All iPadOS hosts", "label_type": "builtin" } ], "platforms": [ { "platform": "ios", "hosts_count": 1 } ] } ``` ### To populate list of MDM solutions of a selected platform `GET /api/latest/fleet/hosts/summary/mdm\?platform=ios` (similar with `ipados`) ```json { "counts_updated_at": "2024-06-27T21:56:45Z", "mobile_device_management_enrollment_status": { "enrolled_manual_hosts_count": 0, "enrolled_automated_hosts_count": 1, "pending_hosts_count": 0, "unenrolled_hosts_count": 0, "hosts_count": 1 }, "mobile_device_management_solution": [ { "id": 1, "name": "Fleet", "server_url": "https://lucas-fleet.ngrok.app/mdm/apple/mdm", "hosts_count": 1 } ] } ``` ### To populate OS versions of a selected platform `GET /api/latest/fleet/os_versions?platform=ipados` (similar with `ios`) ```json { "meta": { "has_next_results": false, "has_previous_results": false }, "count": 1, "counts_updated_at": "2024-06-27T21:36:12Z", "os_versions": [ { "os_version_id": 7, "hosts_count": 1, "name": "iPadOS 17.5.1", "name_only": "iPadOS", "version": "17.5.1", "platform": "ipados", "vulnerabilities": [] } ] } ``` ## Filtering hosts by the two new `iOS`/`iPadOS` labels Works the same as with other labels.
2024-07-08 21:05:29 +00:00
{
Name: "iOS",
Platform: "ios",
Query: "",
LabelType: fleet.LabelTypeBuiltIn,
LabelMembershipType: fleet.LabelMembershipTypeManual,
},
{
Name: "iPadOS",
Platform: "ipados",
Query: "",
LabelType: fleet.LabelTypeBuiltIn,
LabelMembershipType: fleet.LabelMembershipTypeManual,
},
2024-09-30 16:02:54 +00:00
{
Name: "Fedora Linux",
Platform: "rhel",
Query: "select 1 from os_version where name = 'Fedora Linux';",
LabelType: fleet.LabelTypeBuiltIn,
LabelMembershipType: fleet.LabelMembershipTypeDynamic,
},
{
Name: "Android",
Platform: "android",
Query: "",
LabelType: fleet.LabelTypeBuiltIn,
LabelMembershipType: fleet.LabelMembershipTypeManual,
},
}
names := fleet.ReservedLabelNames()
require.Equal(t, len(builtins), len(names))
storedByName := map[string]*fleet.Label{}
for _, b := range builtins {
stored, err := ds.NewLabel(context.Background(), b)
require.NoError(t, err)
storedByName[stored.Name] = stored
}
require.Len(t, storedByName, len(builtins))
for name := range names {
_, ok := storedByName[name]
require.True(t, ok, "expected label %s to be created", name)
}
}
2022-10-08 12:57:46 +00:00
// NewHostOption is an Option for the NewHost function.
type NewHostOption func(*fleet.Host)
// WithComputerName sets the ComputerName in NewHost.
func WithComputerName(s string) NewHostOption {
return func(h *fleet.Host) {
h.ComputerName = s
}
}
func WithPlatform(s string) NewHostOption {
return func(h *fleet.Host) {
h.Platform = s
}
}
func WithOSVersion(s string) NewHostOption {
return func(h *fleet.Host) {
h.OSVersion = s
}
}
func WithTeamID(teamID uint) NewHostOption {
return func(h *fleet.Host) {
h.TeamID = &teamID
}
}
2022-10-08 12:57:46 +00:00
func NewHost(tb testing.TB, ds fleet.Datastore, name, ip, key, uuid string, now time.Time, options ...NewHostOption) *fleet.Host {
osqueryHostID, _ := server.GenerateRandomText(10)
2022-10-08 12:57:46 +00:00
h := &fleet.Host{
Hostname: name,
NodeKey: &key,
UUID: uuid,
DetailUpdatedAt: now,
LabelUpdatedAt: now,
PolicyUpdatedAt: now,
SeenTime: now,
OsqueryHostID: &osqueryHostID,
Platform: "darwin",
2022-10-08 12:57:46 +00:00
PublicIP: ip,
PrimaryIP: ip,
}
for _, o := range options {
o(h)
}
h, err := ds.NewHost(context.Background(), h)
require.NoError(tb, err)
require.NotZero(tb, h.ID)
require.NoError(tb, ds.MarkHostsSeen(context.Background(), []uint{h.ID}, now))
return h
}
func NewUser(t *testing.T, ds fleet.Datastore, name, email string, admin bool) *fleet.User {
role := fleet.RoleObserver
if admin {
role = fleet.RoleAdmin
}
u, err := ds.NewUser(context.Background(), &fleet.User{
Password: []byte("garbage"),
Salt: "garbage",
Name: name,
Email: email,
GlobalRole: &role,
})
require.NoError(t, err)
require.NotZero(t, u.ID)
return u
}
func NewScheduledQuery(t *testing.T, ds fleet.Datastore, pid, qid, interval uint, snapshot, removed bool, name string) *fleet.ScheduledQuery {
sq, err := ds.NewScheduledQuery(context.Background(), &fleet.ScheduledQuery{
Name: name,
PackID: pid,
QueryID: qid,
Interval: interval,
Snapshot: &snapshot,
Removed: &removed,
Platform: ptr.String("darwin"),
})
require.NoError(t, err)
require.NotZero(t, sq.ID)
return sq
}