From 8a0f87ef3d5926c72d69b49375a5dd5feafba044 Mon Sep 17 00:00:00 2001 From: Martin Angers Date: Mon, 8 Apr 2024 15:01:22 -0400 Subject: [PATCH] Rename the builtin label constant --- cmd/fleetctl/apply_test.go | 12 ++++---- cmd/fleetctl/get_test.go | 4 +-- cmd/fleetctl/gitops_test.go | 8 ++--- ee/server/service/mdm.go | 4 +-- ee/server/service/mdm_external_test.go | 2 +- ...403104633_CreateMacOSSonomaBuiltinLabel.go | 4 +-- server/fleet/labels.go | 30 +++++++++++++++++-- 7 files changed, 45 insertions(+), 19 deletions(-) diff --git a/cmd/fleetctl/apply_test.go b/cmd/fleetctl/apply_test.go index a1e506fdb1..6485dbc0ff 100644 --- a/cmd/fleetctl/apply_test.go +++ b/cmd/fleetctl/apply_test.go @@ -178,8 +178,8 @@ func TestApplyTeamSpecs(t *testing.T) { } ds.LabelIDsByNameFunc = func(ctx context.Context, labels []string) (map[string]uint, error) { - require.ElementsMatch(t, labels, []string{fleet.BuiltinMacOS14PlusLabelName}) - return map[string]uint{fleet.BuiltinMacOS14PlusLabelName: 1}, nil + require.ElementsMatch(t, labels, []string{fleet.BuiltinLabelMacOS14Plus}) + return map[string]uint{fleet.BuiltinLabelMacOS14Plus: 1}, nil } ds.SetOrUpdateMDMAppleDeclarationFunc = func(ctx context.Context, declaration *fleet.MDMAppleDeclaration) (*fleet.MDMAppleDeclaration, error) { @@ -585,8 +585,8 @@ func TestApplyAppConfig(t *testing.T) { } ds.LabelIDsByNameFunc = func(ctx context.Context, labels []string) (map[string]uint, error) { - require.ElementsMatch(t, labels, []string{fleet.BuiltinMacOS14PlusLabelName}) - return map[string]uint{fleet.BuiltinMacOS14PlusLabelName: 1}, nil + require.ElementsMatch(t, labels, []string{fleet.BuiltinLabelMacOS14Plus}) + return map[string]uint{fleet.BuiltinLabelMacOS14Plus: 1}, nil } ds.SetOrUpdateMDMAppleDeclarationFunc = func(ctx context.Context, declaration *fleet.MDMAppleDeclaration) (*fleet.MDMAppleDeclaration, error) { @@ -1170,8 +1170,8 @@ func TestApplyAsGitOps(t *testing.T) { return nil } ds.LabelIDsByNameFunc = func(ctx context.Context, labels []string) (map[string]uint, error) { - require.ElementsMatch(t, labels, []string{fleet.BuiltinMacOS14PlusLabelName}) - return map[string]uint{fleet.BuiltinMacOS14PlusLabelName: 1}, nil + require.ElementsMatch(t, labels, []string{fleet.BuiltinLabelMacOS14Plus}) + return map[string]uint{fleet.BuiltinLabelMacOS14Plus: 1}, nil } ds.SetOrUpdateMDMAppleDeclarationFunc = func(ctx context.Context, declaration *fleet.MDMAppleDeclaration) (*fleet.MDMAppleDeclaration, error) { declaration.DeclarationUUID = uuid.NewString() diff --git a/cmd/fleetctl/get_test.go b/cmd/fleetctl/get_test.go index faf1831ecd..5d49607108 100644 --- a/cmd/fleetctl/get_test.go +++ b/cmd/fleetctl/get_test.go @@ -2221,8 +2221,8 @@ func TestGetTeamsYAMLAndApply(t *testing.T) { return nil } ds.LabelIDsByNameFunc = func(ctx context.Context, labels []string) (map[string]uint, error) { - require.ElementsMatch(t, labels, []string{fleet.BuiltinMacOS14PlusLabelName}) - return map[string]uint{fleet.BuiltinMacOS14PlusLabelName: 1}, nil + require.ElementsMatch(t, labels, []string{fleet.BuiltinLabelMacOS14Plus}) + return map[string]uint{fleet.BuiltinLabelMacOS14Plus: 1}, nil } ds.SetOrUpdateMDMAppleDeclarationFunc = func(ctx context.Context, declaration *fleet.MDMAppleDeclaration) (*fleet.MDMAppleDeclaration, error) { declaration.DeclarationUUID = uuid.NewString() diff --git a/cmd/fleetctl/gitops_test.go b/cmd/fleetctl/gitops_test.go index 5612bad113..a0085e1d79 100644 --- a/cmd/fleetctl/gitops_test.go +++ b/cmd/fleetctl/gitops_test.go @@ -176,8 +176,8 @@ func TestBasicTeamGitOps(t *testing.T) { return team, nil } ds.LabelIDsByNameFunc = func(ctx context.Context, labels []string) (map[string]uint, error) { - require.ElementsMatch(t, labels, []string{fleet.BuiltinMacOS14PlusLabelName}) - return map[string]uint{fleet.BuiltinMacOS14PlusLabelName: 1}, nil + require.ElementsMatch(t, labels, []string{fleet.BuiltinLabelMacOS14Plus}) + return map[string]uint{fleet.BuiltinLabelMacOS14Plus: 1}, nil } ds.SetOrUpdateMDMAppleDeclarationFunc = func(ctx context.Context, declaration *fleet.MDMAppleDeclaration) (*fleet.MDMAppleDeclaration, error) { declaration.DeclarationUUID = uuid.NewString() @@ -434,8 +434,8 @@ func TestFullTeamGitOps(t *testing.T) { return job, nil } ds.LabelIDsByNameFunc = func(ctx context.Context, labels []string) (map[string]uint, error) { - require.ElementsMatch(t, labels, []string{fleet.BuiltinMacOS14PlusLabelName}) - return map[string]uint{fleet.BuiltinMacOS14PlusLabelName: 1}, nil + require.ElementsMatch(t, labels, []string{fleet.BuiltinLabelMacOS14Plus}) + return map[string]uint{fleet.BuiltinLabelMacOS14Plus: 1}, nil } ds.SetOrUpdateMDMAppleDeclarationFunc = func(ctx context.Context, declaration *fleet.MDMAppleDeclaration) (*fleet.MDMAppleDeclaration, error) { declaration.DeclarationUUID = uuid.NewString() diff --git a/ee/server/service/mdm.go b/ee/server/service/mdm.go index aa42c21c65..25b3e454d2 100644 --- a/ee/server/service/mdm.go +++ b/ee/server/service/mdm.go @@ -1092,12 +1092,12 @@ func (svc *Service) mdmAppleEditedMacOSUpdates(ctx context.Context, teamID *uint // associate the profile with the built-in label that ensures the host is on // macOS 14+ to receive that profile - lblIDs, err := svc.ds.LabelIDsByName(ctx, []string{fleet.BuiltinMacOS14PlusLabelName}) + lblIDs, err := svc.ds.LabelIDsByName(ctx, []string{fleet.BuiltinLabelMacOS14Plus}) if err != nil { return err } d.Labels = []fleet.ConfigurationProfileLabel{ - {LabelName: fleet.BuiltinMacOS14PlusLabelName, LabelID: lblIDs[fleet.BuiltinMacOS14PlusLabelName]}, + {LabelName: fleet.BuiltinLabelMacOS14Plus, LabelID: lblIDs[fleet.BuiltinLabelMacOS14Plus]}, } decl, err := svc.ds.SetOrUpdateMDMAppleDeclaration(ctx, d) diff --git a/ee/server/service/mdm_external_test.go b/ee/server/service/mdm_external_test.go index 16e932ade0..e394ee0768 100644 --- a/ee/server/service/mdm_external_test.go +++ b/ee/server/service/mdm_external_test.go @@ -189,7 +189,7 @@ func TestGetOrCreatePreassignTeam(t *testing.T) { } ds.LabelIDsByNameFunc = func(ctx context.Context, names []string) (map[string]uint, error) { require.Len(t, names, 1) - require.ElementsMatch(t, names, []string{fleet.BuiltinMacOS14PlusLabelName}) + require.ElementsMatch(t, names, []string{fleet.BuiltinLabelMacOS14Plus}) return map[string]uint{names[0]: 1}, nil } ds.SetOrUpdateMDMAppleDeclarationFunc = func(ctx context.Context, declaration *fleet.MDMAppleDeclaration) (*fleet.MDMAppleDeclaration, error) { diff --git a/server/datastore/mysql/migrations/tables/20240403104633_CreateMacOSSonomaBuiltinLabel.go b/server/datastore/mysql/migrations/tables/20240403104633_CreateMacOSSonomaBuiltinLabel.go index 7d4fc8f6b4..df5151310e 100644 --- a/server/datastore/mysql/migrations/tables/20240403104633_CreateMacOSSonomaBuiltinLabel.go +++ b/server/datastore/mysql/migrations/tables/20240403104633_CreateMacOSSonomaBuiltinLabel.go @@ -32,7 +32,7 @@ func Up_20240403104633(tx *sql.Tx) error { ts := time.Date(2024, 4, 3, 0, 0, 0, 0, time.UTC) _, err := tx.Exec( stmt, - fleet.BuiltinMacOS14PlusLabelName, + fleet.BuiltinLabelMacOS14Plus, "macOS hosts with version 14 and above", `select 1 from os_version where platform = 'darwin' and major >= 14;`, "darwin", @@ -49,7 +49,7 @@ func Up_20240403104633(tx *sql.Tx) error { // across built-in and regular. (I don't think we've done anything // special before, but this seems a bit nicer/clearer as to why the // migration may have failed and how to fix it) - return fmt.Errorf("a label with the name %q already exists, please rename it before applying this migration: %w", fleet.BuiltinMacOS14PlusLabelName, err) + return fmt.Errorf("a label with the name %q already exists, please rename it before applying this migration: %w", fleet.BuiltinLabelMacOS14Plus, err) } } return err diff --git a/server/fleet/labels.go b/server/fleet/labels.go index eb15c469d4..562db945e6 100644 --- a/server/fleet/labels.go +++ b/server/fleet/labels.go @@ -111,8 +111,6 @@ func (l Label) AuthzType() string { const ( LabelKind = "label" - - BuiltinMacOS14PlusLabelName = "macOS 14+ (Sonoma+)" ) type LabelQueryExecution struct { @@ -133,3 +131,31 @@ type LabelSpec struct { LabelMembershipType LabelMembershipType `json:"label_membership_type" db:"label_membership_type"` Hosts []string `json:"hosts,omitempty"` } + +const ( + BuiltinLabelNameAllHosts = "All Hosts" + BuiltinLabelNameMacOS = "macOS" + BuiltinLabelNameUbuntuLinux = "Ubuntu Linux" + BuiltinLabelNameCentOSLinux = "CentOS Linux" + BuiltinLabelNameWindows = "MS Windows" + BuiltinLabelNameRedHatLinux = "Red Hat Linux" + BuiltinLabelNameAllLinux = "All Linux" + BuiltinLabelNameChrome = "chrome" + BuiltinLabelMacOS14Plus = "macOS 14+ (Sonoma+)" +) + +// ReservedLabelNames returns a map of label name strings +// that are reserved by Fleet. +func ReservedLabelNames() map[string]struct{} { + return map[string]struct{}{ + BuiltinLabelNameAllHosts: {}, + BuiltinLabelNameMacOS: {}, + BuiltinLabelNameUbuntuLinux: {}, + BuiltinLabelNameCentOSLinux: {}, + BuiltinLabelNameWindows: {}, + BuiltinLabelNameRedHatLinux: {}, + BuiltinLabelNameAllLinux: {}, + BuiltinLabelNameChrome: {}, + BuiltinLabelMacOS14Plus: {}, + } +}