From 402303bc5dd1080a0caff9eb64ddde868d413d23 Mon Sep 17 00:00:00 2001 From: Tomas Touceda Date: Wed, 18 Aug 2021 15:55:48 -0300 Subject: [PATCH] Add All Linux label (#1582) * Add All Linux label * Change name to Linux instead of All Linux to see if e2e likes it better * Revert "Change name to Linux instead of All Linux to see if e2e likes it better" This reverts commit 26b79f214e3b744e73270c544f89bb698575f6ea. * Fix all linux label insert --- changes/issue-1557-all-linux-label | 1 + go.sum | 1 + ...20210806135609_AddAllLinuxBuiltInLabels.go | 36 +++++++++++++++++++ server/service/service_labels_test.go | 15 ++++++++ 4 files changed, 53 insertions(+) create mode 100644 changes/issue-1557-all-linux-label create mode 100644 server/datastore/mysql/migrations/data/20210806135609_AddAllLinuxBuiltInLabels.go diff --git a/changes/issue-1557-all-linux-label b/changes/issue-1557-all-linux-label new file mode 100644 index 0000000000..1b19f9308c --- /dev/null +++ b/changes/issue-1557-all-linux-label @@ -0,0 +1 @@ +* Add a new built in label "All Linux" to target all hosts that run any linux flavor. diff --git a/go.sum b/go.sum index c4ed901b0c..19a147aa6f 100644 --- a/go.sum +++ b/go.sum @@ -855,6 +855,7 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= github.com/zclconf/go-cty v1.1.0/go.mod h1:xnAOWiHeOqg2nWS62VtQ7pbOu17FtxJNW8RLEih+O3s= github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q= +github.com/ziutek/mymysql v1.5.4 h1:GB0qdRGsTwQSBVYuVShFBKaXSnSnYYC2d9knnE1LHFs= github.com/ziutek/mymysql v1.5.4/go.mod h1:LMSpPZ6DbqWFxNCHW77HeMg9I646SAhApZ/wKdgO/C0= github.com/zwass/kit v0.0.0-20210625184505-ec5b5c5cce9c h1:TWQ2UvXPkhPxI2KmApKBOCaV6yD2N4mlvqFQ/DlPtpQ= github.com/zwass/kit v0.0.0-20210625184505-ec5b5c5cce9c/go.mod h1:OYYulo9tUqRadRLwB0+LE914sa1ui2yL7OrcU3Q/1XY= diff --git a/server/datastore/mysql/migrations/data/20210806135609_AddAllLinuxBuiltInLabels.go b/server/datastore/mysql/migrations/data/20210806135609_AddAllLinuxBuiltInLabels.go new file mode 100644 index 0000000000..22551ef08e --- /dev/null +++ b/server/datastore/mysql/migrations/data/20210806135609_AddAllLinuxBuiltInLabels.go @@ -0,0 +1,36 @@ +package data + +import ( + "database/sql" + + "github.com/fleetdm/fleet/v4/server/fleet" +) + +func init() { + MigrationClient.AddMigration(Up_20210806135609, Down_20210806135609) +} + +func Up_20210806135609(tx *sql.Tx) error { + _, err := tx.Exec(` + INSERT INTO labels ( + name, + description, + query, + platform, + label_type + ) VALUES (?, ?, ?, ?, ?)`, + "All Linux", "All Linux distributions", + "SELECT 1 FROM osquery_info WHERE build_platform LIKE '%ubuntu%' OR build_distro LIKE '%centos%';", + "", + fleet.LabelTypeBuiltIn, + ) + if err != nil { + return err + } + + return nil +} + +func Down_20210806135609(tx *sql.Tx) error { + return nil +} diff --git a/server/service/service_labels_test.go b/server/service/service_labels_test.go index 6ba3940642..e8b52581cc 100644 --- a/server/service/service_labels_test.go +++ b/server/service/service_labels_test.go @@ -7,6 +7,7 @@ import ( "github.com/fleetdm/fleet/v4/server/fleet" "github.com/fleetdm/fleet/v4/server/test" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestGetLabel(t *testing.T) { @@ -27,3 +28,17 @@ func TestGetLabel(t *testing.T) { assert.Nil(t, err) assert.Equal(t, label.ID, labelVerify.ID) } + +func TestGetLabels(t *testing.T) { + ds := mysql.CreateMySQLDS(t) + defer ds.Close() + + require.NoError(t, ds.MigrateTables()) + require.NoError(t, ds.MigrateData()) + + svc := newTestService(ds, nil, nil) + + labels, err := svc.ListLabels(test.UserContext(test.UserAdmin), fleet.ListOptions{Page: 0, PerPage: 1000}) + require.NoError(t, err) + require.Len(t, labels, 7) +}