Fix duplicate label check behavior on spec apply endpoint (#38056)

## Testing

- [x] Added/updated automated tests

For unreleased bug fixes in a release candidate, one of:

- [x] Confirmed that the fix is not expected to adversely impact load
test results
This commit is contained in:
Ian Littman 2026-01-08 16:18:47 -06:00 committed by GitHub
parent 9f29fd1ce9
commit 8a1a5988b1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 51 additions and 1 deletions

View file

@ -182,7 +182,7 @@ func (ds *Datastore) ApplyLabelSpecsWithAuthor(ctx context.Context, specs []*fle
(existingLabel.TeamID != nil && spec.TeamID == nil ||
existingLabel.TeamID == nil && spec.TeamID != nil ||
(existingLabel.TeamID != nil && spec.TeamID != nil && *existingLabel.TeamID != *spec.TeamID)) {
return ctxerr.Wrap(ctx, err, "one or more specified labels exists on another team")
return ctxerr.New(ctx, "one or more specified labels exists on another team")
}
}
}

View file

@ -105,6 +105,7 @@ func TestLabels(t *testing.T) {
{"UpdateLabelMembershipForTransferredHost", testUpdateLabelMembershipForTransferredHost},
{"SetAsideLabels", testSetAsideLabels},
{"ApplyLabelSpecsWithManualTeamLabels", testApplyLabelSpecsWithManualTeamLabels},
{"ApplyLabelSpecsErrorsWhenLabelExistsOnAnotherTeam", testApplyLabelSpecsErrorsWhenLabelExistsOnAnotherTeam},
}
// call TruncateTables first to remove migration-created labels
TruncateTables(t, ds)
@ -3307,6 +3308,55 @@ func testSetAsideLabels(t *testing.T, ds *Datastore) {
}
}
func testApplyLabelSpecsErrorsWhenLabelExistsOnAnotherTeam(t *testing.T, ds *Datastore) {
ctx := t.Context()
// Create two teams
team1, err := ds.NewTeam(ctx, &fleet.Team{Name: "team1_label_conflict"})
require.NoError(t, err)
team2, err := ds.NewTeam(ctx, &fleet.Team{Name: "team2_label_conflict"})
require.NoError(t, err)
// Create a label on team1
err = ds.ApplyLabelSpecs(ctx, []*fleet.LabelSpec{
{Name: "conflicting-label", Query: "SELECT 1", TeamID: &team1.ID},
})
require.NoError(t, err)
// Try to create a label with the same name on team2 - should error
err = ds.ApplyLabelSpecs(ctx, []*fleet.LabelSpec{
{Name: "conflicting-label", Query: "SELECT 2", TeamID: &team2.ID},
})
require.Error(t, err)
require.Contains(t, err.Error(), "one or more specified labels exists on another team")
// Try to create a label with the same name globally (no team) - should error
err = ds.ApplyLabelSpecs(ctx, []*fleet.LabelSpec{
{Name: "conflicting-label", Query: "SELECT 3"},
})
require.Error(t, err)
require.Contains(t, err.Error(), "one or more specified labels exists on another team")
// Create a global label
err = ds.ApplyLabelSpecs(ctx, []*fleet.LabelSpec{
{Name: "global-label", Query: "SELECT 4"},
})
require.NoError(t, err)
// Try to create a label with the same name on a team - should error
err = ds.ApplyLabelSpecs(ctx, []*fleet.LabelSpec{
{Name: "global-label", Query: "SELECT 5", TeamID: &team2.ID},
})
require.Error(t, err)
require.Contains(t, err.Error(), "one or more specified labels exists on another team")
// Updating the original label on the same team should still work
err = ds.ApplyLabelSpecs(ctx, []*fleet.LabelSpec{
{Name: "conflicting-label", Query: "SELECT 1 updated", TeamID: &team1.ID},
})
require.NoError(t, err)
}
func testApplyLabelSpecsWithManualTeamLabels(t *testing.T, ds *Datastore) {
ctx := t.Context()
teamFilter := fleet.TeamFilter{User: test.UserAdmin}