-
Notifications
You must be signed in to change notification settings - Fork 832
make DDM name check case insensitive #43347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| - Fixed an issue where sending a different cased display name for a DDM profile via the batch endpoint, would result in recreating the DDM profile triggering a resend. | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -5108,8 +5108,9 @@ func (ds *Datastore) batchSetMDMAppleDeclarations(ctx context.Context, tx sqlx.E | |||||||||||||||||||||||||||||
| incomingNames := make([]string, len(incomingDeclarations)) | ||||||||||||||||||||||||||||||
| incomingDeclarationsMap := make(map[string]*fleet.MDMAppleDeclaration, len(incomingDeclarations)) | ||||||||||||||||||||||||||||||
| for i, p := range incomingDeclarations { | ||||||||||||||||||||||||||||||
| incomingNames[i] = p.Name | ||||||||||||||||||||||||||||||
| incomingDeclarationsMap[p.Name] = p | ||||||||||||||||||||||||||||||
| name := strings.ToLower(p.Name) | ||||||||||||||||||||||||||||||
| incomingNames[i] = name | ||||||||||||||||||||||||||||||
| incomingDeclarationsMap[name] = p | ||||||||||||||||||||||||||||||
MagnusHJensen marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+5111
to
+5113
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reject duplicate declaration names after normalization. Lowercasing the key here makes two incoming declarations like Proposed fix incomingNames := make([]string, len(incomingDeclarations))
incomingDeclarationsMap := make(map[string]*fleet.MDMAppleDeclaration, len(incomingDeclarations))
for i, p := range incomingDeclarations {
- name := strings.ToLower(p.Name)
- incomingNames[i] = name
- incomingDeclarationsMap[name] = p
+ name := strings.ToLower(p.Name)
+ if existing, ok := incomingDeclarationsMap[name]; ok {
+ return false, &fleet.BadRequestError{
+ Message: fmt.Sprintf(
+ "declaration names %q and %q differ only by letter case; declaration names must be unique",
+ existing.Name, p.Name,
+ ),
+ }
+ }
+ incomingNames[i] = name
+ incomingDeclarationsMap[name] = p
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| teamIDOrZero := ds.teamIDPtrToUint(tmID) | ||||||||||||||||||||||||||||||
|
|
@@ -5121,8 +5122,9 @@ func (ds *Datastore) batchSetMDMAppleDeclarations(ctx context.Context, tx sqlx.E | |||||||||||||||||||||||||||||
| // figure out which declarations we should not delete, and put those into keepNames list | ||||||||||||||||||||||||||||||
| keepNames := make([]string, 0, len(existingDecls)+len(fleetmdm.ListFleetReservedMacOSDeclarationNames())) | ||||||||||||||||||||||||||||||
| for _, p := range existingDecls { | ||||||||||||||||||||||||||||||
| if newP := incomingDeclarationsMap[p.Name]; newP != nil { | ||||||||||||||||||||||||||||||
| if newP := incomingDeclarationsMap[strings.ToLower(p.Name)]; newP != nil { | ||||||||||||||||||||||||||||||
| keepNames = append(keepNames, p.Name) | ||||||||||||||||||||||||||||||
| newP.DeclarationUUID = p.DeclarationUUID | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| keepNames = append(keepNames, fleetmdm.ListFleetReservedMacOSDeclarationNames()...) | ||||||||||||||||||||||||||||||
|
|
@@ -5175,9 +5177,9 @@ func (ds *Datastore) updateDeclarationsLabelAssociations(ctx context.Context, tx | |||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| for _, newlyInsertedDecl := range newlyInsertedDecls { | ||||||||||||||||||||||||||||||
| incomingDecl, ok := incomingDeclarationsMap[newlyInsertedDecl.Name] | ||||||||||||||||||||||||||||||
| incomingDecl, ok := incomingDeclarationsMap[strings.ToLower(newlyInsertedDecl.Name)] | ||||||||||||||||||||||||||||||
| if !ok { | ||||||||||||||||||||||||||||||
| return false, ctxerr.Wrapf(ctx, err, "declaration %q is in the database but was not incoming", | ||||||||||||||||||||||||||||||
| return false, ctxerr.Errorf(ctx, "declaration %q is in the database but was not incoming", | ||||||||||||||||||||||||||||||
| newlyInsertedDecl.Name) | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
@@ -5240,6 +5242,7 @@ ON DUPLICATE KEY UPDATE | |||||||||||||||||||||||||||||
| raw_json = VALUES(raw_json) | ||||||||||||||||||||||||||||||
| ` | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| updatedDeclarationUUIDs := make([]string, 0, len(incomingDeclarations)) | ||||||||||||||||||||||||||||||
| for _, d := range incomingDeclarations { | ||||||||||||||||||||||||||||||
| declUUID := fleet.MDMAppleDeclarationUUIDPrefix + uuid.NewString() | ||||||||||||||||||||||||||||||
| var result sql.Result | ||||||||||||||||||||||||||||||
|
|
@@ -5253,7 +5256,29 @@ ON DUPLICATE KEY UPDATE | |||||||||||||||||||||||||||||
| return false, ctxerr.Wrapf(ctx, err, "insert new/edited declaration with identifier %q", d.Identifier) | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| updatedDB = updatedDB || insertOnDuplicateDidInsertOrUpdate(result) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| if insertOnDuplicateDidInsertOrUpdate(result) { | ||||||||||||||||||||||||||||||
| updatedDeclarationUUIDs = append(updatedDeclarationUUIDs, d.DeclarationUUID) | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
MagnusHJensen marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||
| // If we updated any declarations, make sure to update the associated host entries with the new name | ||||||||||||||||||||||||||||||
| // since other changes should not trigger an update but rather a full replace, name does not. | ||||||||||||||||||||||||||||||
| if len(updatedDeclarationUUIDs) > 0 { | ||||||||||||||||||||||||||||||
| const updateHostDeclarationsName = ` | ||||||||||||||||||||||||||||||
| UPDATE host_mdm_apple_declarations hmad | ||||||||||||||||||||||||||||||
| JOIN mdm_apple_declarations mad ON hmad.declaration_uuid = mad.declaration_uuid | ||||||||||||||||||||||||||||||
| SET hmad.declaration_name = mad.name | ||||||||||||||||||||||||||||||
| WHERE hmad.declaration_uuid IN (?)` | ||||||||||||||||||||||||||||||
| updateHostDeclarationsNameStmt, args, err := sqlx.In(updateHostDeclarationsName, updatedDeclarationUUIDs) | ||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||
| return false, ctxerr.Wrap(ctx, err, "build query to update host declaration names") | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| if _, err := tx.ExecContext(ctx, updateHostDeclarationsNameStmt, args...); err != nil { | ||||||||||||||||||||||||||||||
| return false, ctxerr.Wrap(ctx, err, "update host declaration names") | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| return updatedDB, nil | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.