Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/fleetctl/fleetctl/generate_gitops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ func (MockClient) GetLabels() ([]*fleet.LabelSpec, error) {
Name: "Label B",
Description: "Label B description",
LabelMembershipType: fleet.LabelMembershipTypeManual,
Hosts: []string{"host1", "host2"},
Hosts: []string{"1", "2"},
}, {
Name: "Label C",
Description: "Label C description",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
description: Label B description
label_membership_type: manual
hosts:
- host1
- host2
- "1"
- "2"
- name: Label C
description: Label C description
label_membership_type: host_vitals
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ labels:
query: SELECT * FROM osquery_info
- description: Label B description
hosts:
- host1
- host2
- "1"
- "2"
label_membership_type: manual
name: Label B
- criteria:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ labels:
query: SELECT * FROM osquery_info
- description: Label B description
hosts:
- host1
- host2
- "1"
- "2"
label_membership_type: manual
name: Label B
- criteria:
Expand Down
8 changes: 4 additions & 4 deletions server/datastore/mysql/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ func (ds *Datastore) GetLabelSpecs(ctx context.Context) ([]*fleet.LabelSpec, err
for _, spec := range specs {
if spec.LabelType != fleet.LabelTypeBuiltIn &&
spec.LabelMembershipType == fleet.LabelMembershipTypeManual {
if err := ds.getLabelHostnames(ctx, spec); err != nil {
if err := ds.getLabelHostIDs(ctx, spec); err != nil {
return nil, err
}
}
Expand Down Expand Up @@ -340,7 +340,7 @@ WHERE name = ?
spec := specs[0]
if spec.LabelType != fleet.LabelTypeBuiltIn &&
spec.LabelMembershipType == fleet.LabelMembershipTypeManual {
err := ds.getLabelHostnames(ctx, spec)
err := ds.getLabelHostIDs(ctx, spec)
if err != nil {
return nil, err
}
Expand All @@ -349,9 +349,9 @@ WHERE name = ?
return spec, nil
}

func (ds *Datastore) getLabelHostnames(ctx context.Context, label *fleet.LabelSpec) error {
func (ds *Datastore) getLabelHostIDs(ctx context.Context, label *fleet.LabelSpec) error {
sql := `
SELECT hostname
SELECT id

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only real functional change. This method is only used by the /spec/labels endpoint and by fleetctl generate-gitops. I posted about the API change in Slack to get @rachaelshaw's feedback but I don't think it should be cause for concern.

FROM hosts
WHERE id IN
(
Expand Down
19 changes: 8 additions & 11 deletions server/datastore/mysql/labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -752,8 +752,8 @@ func setupLabelSpecsTest(t *testing.T, ds fleet.Datastore) []*fleet.LabelSpec {
SeenTime: time.Now(),
OsqueryHostID: ptr.String(strconv.Itoa(i)),
NodeKey: ptr.String(strconv.Itoa(i)),
UUID: strconv.Itoa(i),
Hostname: strconv.Itoa(i),
UUID: fmt.Sprintf("uuid%s", strconv.Itoa(i)),
Hostname: fmt.Sprintf("host%s", strconv.Itoa(i)),
Comment on lines -755 to +756

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test was always a bit wonky in that it used numbers for the UUID and Hostname as well. Since we added ID to the mix of things you can specify for label membership in GitOps, it led to weird results where if you said "I want hosts 1, 2, and 3" you'd get a label with hosts 1, 2, 3 and 4 because host #4 had a UUID and hostname of "3". Changing these to be strings clears up that confusion in the test.

})
require.Nil(t, err)
}
Expand Down Expand Up @@ -790,10 +790,7 @@ func setupLabelSpecsTest(t *testing.T, ds fleet.Datastore) []*fleet.LabelSpec {
err := ds.ApplyLabelSpecs(context.Background(), expectedSpecs)
require.Nil(t, err)

// Because `Hosts` for manual labels matches both host name AND host ID,
// specifying "1" will match both host with ID 1 (whose name is "0")
// and host with name "1".
expectedSpecs[4].Hosts = []string{"0", "1", "2", "3", "4"}
expectedSpecs[4].Hosts = []string{"1", "2", "3", "4"}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see comment above

return expectedSpecs
}

Expand Down Expand Up @@ -1870,8 +1867,8 @@ func testUpdateLabelMembershipByHostIDs(t *testing.T, ds *Datastore) {
require.NoError(t, err)
// label.Hosts contains hostnames
require.Len(t, labelSpec.Hosts, 2)
require.Equal(t, host1.Hostname, labelSpec.Hosts[0])
require.Equal(t, host2.Hostname, labelSpec.Hosts[1])
require.Equal(t, strconv.Itoa(int(host1.ID)), labelSpec.Hosts[0]) //nolint:gosec // dismiss G115
require.Equal(t, strconv.Itoa(int(host2.ID)), labelSpec.Hosts[1]) //nolint:gosec // dismiss G115

labels, err := ds.ListLabelsForHost(ctx, host1.ID)
require.NoError(t, err)
Expand Down Expand Up @@ -1977,9 +1974,9 @@ func testUpdateLabelMembershipByHostIDs(t *testing.T, ds *Datastore) {

// label.Hosts contains hostnames
require.Len(t, labelSpec.Hosts, 3)
require.Equal(t, host1.Hostname, labelSpec.Hosts[0])
require.Equal(t, host2.Hostname, labelSpec.Hosts[1])
require.Equal(t, host3.Hostname, labelSpec.Hosts[2])
require.Equal(t, strconv.Itoa(int(host1.ID)), labelSpec.Hosts[0]) //nolint:gosec // dismiss G115
require.Equal(t, strconv.Itoa(int(host2.ID)), labelSpec.Hosts[1]) //nolint:gosec // dismiss G115
require.Equal(t, strconv.Itoa(int(host3.ID)), labelSpec.Hosts[2]) //nolint:gosec // dismiss G115
}

func testApplyLabelSpecsForSerialUUID(t *testing.T, ds *Datastore) {
Expand Down
2 changes: 1 addition & 1 deletion server/fleet/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func (s *HostsSlice) UnmarshalJSON(data []byte) error {
}

type LabelSpec struct {
ID uint `json:"id"`
ID uint `json:"id" db:"id"`
Name string `json:"name"`
Description string `json:"description"`
Query string `json:"query"`
Expand Down
Loading