Fix creation of manual label that allowed overriding existing one (#18474)

This commit is contained in:
Martin Angers
2024-04-22 16:51:50 -04:00
committed by GitHub
parent 35ab9f75b6
commit f21aad446e
2 changed files with 20 additions and 15 deletions
+10
View File
@@ -3780,6 +3780,11 @@ func (s *integrationTestSuite) TestLabels() {
assert.Empty(t, createResp.Label.HostIDs)
lbl1 := createResp.Label.Label
// try to create a manual label with the same name
s.DoJSON("POST", "/api/latest/fleet/labels", &fleet.LabelPayload{Name: lbl1.Name, Hosts: []string{manualHosts[0].UUID}}, http.StatusConflict, &createResp)
// try to create a dynamic label with the same name
s.DoJSON("POST", "/api/latest/fleet/labels", &fleet.LabelPayload{Name: lbl1.Name, Query: "select 2"}, http.StatusConflict, &createResp)
// get the label
var getResp getLabelResponse
s.DoJSON("GET", fmt.Sprintf("/api/latest/fleet/labels/%d", lbl1.ID), nil, http.StatusOK, &getResp)
@@ -3814,6 +3819,11 @@ func (s *integrationTestSuite) TestLabels() {
assert.Empty(t, createResp.Label.HostIDs)
manualLbl2 := createResp.Label.Label
// try to create a manual label with the same name
s.DoJSON("POST", "/api/latest/fleet/labels", &fleet.LabelPayload{Name: manualLbl2.Name, Hosts: []string{manualHosts[0].UUID}}, http.StatusConflict, &createResp)
// try to create a dynamic label with the same name
s.DoJSON("POST", "/api/latest/fleet/labels", &fleet.LabelPayload{Name: manualLbl2.Name, Query: "select 2"}, http.StatusConflict, &createResp)
// get the label
getResp = getLabelResponse{}
s.DoJSON("GET", fmt.Sprintf("/api/latest/fleet/labels/%d", manualLbl2.ID), nil, http.StatusOK, &getResp)
+10 -15
View File
@@ -73,9 +73,14 @@ func (svc *Service) NewLabel(ctx context.Context, p fleet.LabelPayload) (*fleet.
}
}
// if membership type is manual, must use ApplyLabelSpecs (as NewLabel does
// not create label memberships), otherwise NewLabel works for dynamic
// membership. Must resolve the host identifiers to hostname so that
// first create the new label, which will fail if the name is not unique
newLbl, err := svc.ds.NewLabel(ctx, label)
if err != nil {
return nil, nil, err
}
// Next, if membership type is manual, use ApplyLabelSpecs to create label
// memberships. Must resolve the host identifiers to hostname so that
// ApplySpecs can be used.
var hostIDs []uint
if label.LabelMembershipType == fleet.LabelMembershipTypeManual {
@@ -96,21 +101,11 @@ func (svc *Service) NewLabel(ctx context.Context, p fleet.LabelPayload) (*fleet.
return nil, nil, err
}
// must reload it to get the id, and the host IDs
lblIDsByName, err := svc.ds.LabelIDsByName(ctx, []string{label.Name})
// must reload it to get the host IDs
label, hostIDs, err = svc.ds.Label(ctx, newLbl.ID)
if err != nil {
return nil, nil, err
}
label, hostIDs, err = svc.ds.Label(ctx, lblIDsByName[label.Name])
if err != nil {
return nil, nil, err
}
} else {
newLbl, err := svc.ds.NewLabel(ctx, label)
if err != nil {
return nil, nil, err
}
label = newLbl
}
return label, hostIDs, nil
}