-
Notifications
You must be signed in to change notification settings - Fork 859
Fix patch policy bugs #43420
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
Fix patch policy bugs #43420
Changes from all commits
93da312
0cb4659
9e0976f
3181a1b
9214b6b
f175b9c
e7b64bf
0b1af18
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,2 @@ | ||
| - Fixed bug where renaming a patch policy in a GitOps file caused it to be deleted initially. | ||
| - Fixed a nil pointer dereference in the contributor api spec/policies. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1478,6 +1478,11 @@ func (ds *Datastore) ApplyPolicySpecs(ctx context.Context, authorID uint, specs | |
|
|
||
| // generate new up-to-date patch policy | ||
| if spec.Type == fleet.PolicyTypePatch { | ||
| if fmaTitleID == nil { | ||
| return ctxerr.Wrap(ctx, &fleet.BadRequestError{ | ||
| Message: fmt.Sprintf("fleet_maintained_app_slug must be set for patch policy: %s", spec.Name), | ||
| }) | ||
| } | ||
| installer, err := ds.getPatchPolicyInstaller(ctx, ptr.ValOrZero(teamID), *fmaTitleID) | ||
| if err != nil { | ||
| return ctxerr.Wrap(ctx, err, "getting patch policy installer") | ||
|
|
@@ -1517,6 +1522,7 @@ func (ds *Datastore) ApplyPolicySpecs(ctx context.Context, authorID uint, specs | |
| var ( | ||
| shouldRemoveAllPolicyMemberships bool | ||
| removePolicyStats bool | ||
| shouldUpdatePatchPolicyName bool | ||
| ) | ||
| if insertOnDuplicateDidInsertOrUpdate(res) { | ||
| // Figure out if the query, platform, software installer, VPP app, or script changed. | ||
|
|
@@ -1548,7 +1554,16 @@ func (ds *Datastore) ApplyPolicySpecs(ctx context.Context, authorID uint, specs | |
| if teamID == nil { | ||
| err = sqlx.GetContext(ctx, tx, &lastID, "SELECT id FROM policies WHERE name = ? AND team_id is NULL", spec.Name) | ||
| } else { | ||
| err = sqlx.GetContext(ctx, tx, &lastID, "SELECT id FROM policies WHERE name = ? AND team_id = ?", spec.Name, teamID) | ||
| // Patch policies are unique by patch_software_title_id so we need to get them by that, and update their name | ||
| // so that it doesn't get deleted later. | ||
| if spec.Type == fleet.PolicyTypePatch { | ||
| err = sqlx.GetContext(ctx, tx, &lastID, "SELECT id FROM policies WHERE patch_software_title_id = ? AND team_id = ?", fmaTitleID, teamID) | ||
| if _, ok := teamIDToPoliciesByName[teamID][spec.Name]; !ok { | ||
| shouldUpdatePatchPolicyName = true | ||
| } | ||
| } else { | ||
| err = sqlx.GetContext(ctx, tx, &lastID, "SELECT id FROM policies WHERE name = ? AND team_id = ?", spec.Name, teamID) | ||
| } | ||
|
Comment on lines
+1557
to
+1566
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. Rename fallback loses the interrupted-cleanup retry signal. This branch only reloads 🤖 Prompt for AI Agents
Member
Author
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. It seems like this situation would only happen the policy was renamed again after the cleanups job interruption? Edit: This seems exceedingly unlikely, considering how we expect patch policies to be used. I also couldn't get needs_full_membership_cleanup to actually be 1 on a patch policy, I attempted to change the software installer automation in gitops but it didn't do anything. That might be a different problem but probably out of scope for this fix.
Member
Author
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. However, after looking into all of this I (claude) added an extra check using that map to avoid updating the name unnecessarily (shouldn't matter anyway) |
||
| } | ||
| if err != nil { | ||
| return ctxerr.Wrap(ctx, err, "select policies id") | ||
|
|
@@ -1595,6 +1610,11 @@ func (ds *Datastore) ApplyPolicySpecs(ctx context.Context, authorID uint, specs | |
| return ctxerr.Wrap(ctx, err, "setting needs_full_membership_cleanup flag") | ||
| } | ||
| } | ||
| if shouldUpdatePatchPolicyName { | ||
| if _, err := tx.ExecContext(ctx, `UPDATE policies SET name = ?, checksum = `+policiesChecksumComputedColumn()+` WHERE id = ?`, spec.Name, policyID); err != nil { | ||
| return ctxerr.Wrap(ctx, err, "setting name for patch policy") | ||
| } | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| // Defer cleanup outside the transaction to avoid long-held row locks on | ||
| // policy_membership. | ||
| pendingCleanups = append(pendingCleanups, policyCleanupArgs{ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This validation still misses the
sql.ErrNoRowspath.fmaTitleID == nilonly happens if the earlier slug lookup returned a row withNULL title_id. When the slug is invalid or not available for that team, the lookup at Line 1320 already fails withsql.ErrNoRows, so this block never runs and the request still bubbles up as an internal datastore error instead of the intended 400.Suggested fix
🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't matter because
ds.getPatchPolicyInstaller(ctx, ptr.ValOrZero(teamID), *fmaTitleID)should return a proper error if the installer is not found