Skip to content

Clear Android cert records on unenroll. - #42920

Merged
getvictor merged 2 commits into
mainfrom
victor/42600-android-cert-clear
Apr 2, 2026
Merged

Clear Android cert records on unenroll.#42920
getvictor merged 2 commits into
mainfrom
victor/42600-android-cert-clear

Conversation

@getvictor

@getvictor getvictor commented Apr 2, 2026

Copy link
Copy Markdown
Member

Related issue: Resolves #42600

Checklist for submitter

If some of the following don't apply, delete the relevant line.

  • Changes file added for user-visible changes in changes/, orbit/changes/ or ee/fleetd-chrome/changes.

Testing

  • Added/updated automated tests
  • QA'd all new/changed functionality manually

Summary by CodeRabbit

  • Bug Fixes
    • Fixed an issue where Android device certificate template records were not properly cleared during unenrollment, which previously resulted in stale certificate statuses after re-enrollment.

@getvictor

Copy link
Copy Markdown
Member Author

@coderabbitai full review

@coderabbitai

coderabbitai Bot commented Apr 2, 2026

Copy link
Copy Markdown
Contributor
✅ Actions performed

Full review triggered.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Clears Android host certificate template tracking records when an Android device is unenrolled, preventing stale certificate template statuses from persisting across re-enrollment (Resolves #42600).

Changes:

  • Delete host_certificate_templates rows during Android unenroll (single-host and bulk unenroll paths).
  • Extend datastore + integration tests to assert certificate template records are removed on unenroll.
  • Add a changelog entry describing the bug fix.

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 2 comments.

File Description
server/datastore/mysql/android.go Deletes Android host certificate template rows when hosts are unenrolled (single + bulk paths).
server/datastore/mysql/android_test.go Adds/extends tests to verify cert template rows are removed on unenroll.
server/service/integration_android_certificate_templates_test.go Updates integration scenario to cover verified template rows being cleared on unenroll.
changes/42600-android-cert-templates-cleared-on-reenroll User-visible changelog entry for the fix.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread server/service/integration_android_certificate_templates_test.go
Comment thread server/service/integration_android_certificate_templates_test.go Outdated
@coderabbitai

coderabbitai Bot commented Apr 2, 2026

Copy link
Copy Markdown
Contributor

Walkthrough

This PR fixes a bug where Android host certificate template records persisted in the database after device unenrollment, resulting in stale certificate statuses on re-enrollment. Changes include adding deletion logic for host_certificate_templates rows in both bulk and single-host Android unenrollment flows within the datastore layer, updating tests to verify the deletion occurs, and documenting the fix in the changelog.

Possibly related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main objective of the PR: clearing Android certificate records during unenrollment.
Linked Issues check ✅ Passed The code changes fully address the linked issue #42600 by clearing host_certificate_templates records in both bulk and single-host Android unenrollment flows with comprehensive test coverage.
Out of Scope Changes check ✅ Passed All changes are directly scoped to the linked issue: bug fix in android.go, test coverage in android_test.go and integration test, and a changes file documenting the fix.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description check ✅ Passed The PR description is mostly complete with a related issue reference and relevant testing/changes file checklist items marked, though some template sections are deleted/not applicable.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch victor/42600-android-cert-clear

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
server/service/integration_android_certificate_templates_test.go (1)

688-693: Consider relaxing the post-unenroll assertion to “nil OR empty.”

The behavior under test is “no profiles remain”; asserting strictly nil may be brittle if response serialization later returns [].

💡 Optional assertion tweak
-	require.Nil(t, getHostResp.Host.MDM.Profiles, "All certificate template records should be cleared on unenroll")
+	require.True(t,
+		getHostResp.Host.MDM.Profiles == nil || len(*getHostResp.Host.MDM.Profiles) == 0,
+		"All certificate template records should be cleared on unenroll",
+	)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@server/service/integration_android_certificate_templates_test.go` around
lines 688 - 693, The test currently asserts require.Nil(t,
getHostResp.Host.MDM.Profiles) which is brittle; change the assertion to accept
either nil or an empty slice so the test validates "no profiles remain"
regardless of JSON serialization. Update the assertion around
getHostResp.Host.MDM.Profiles (in the getHostResponse usage after s.DoJSON) to
check that either getHostResp.Host.MDM.Profiles == nil OR
len(getHostResp.Host.MDM.Profiles) == 0 (e.g., using require.Truef or
require.Empty) so the test passes for both nil and [] outcomes.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@server/datastore/mysql/android.go`:
- Around line 424-438: The deletion of host_certificate_templates is currently
executed using hostUUID without validating it; add a guard to ensure hostUUID is
non-empty (and return/unwrap a contextual error) before calling tx.ExecContext
for the DELETE to avoid accidental mass-deletes. Locate the hostUUID variable
retrieval and, after sqlx.GetContext populates hostUUID (and after the call to
ds.deleteMDMOSCustomSettingsForHost), validate hostUUID is not the empty string
(or otherwise corrupted) and return a ctxerr.Wrap error if it is; only then call
tx.ExecContext(`DELETE FROM host_certificate_templates WHERE host_uuid = ?`,
hostUUID).

---

Nitpick comments:
In `@server/service/integration_android_certificate_templates_test.go`:
- Around line 688-693: The test currently asserts require.Nil(t,
getHostResp.Host.MDM.Profiles) which is brittle; change the assertion to accept
either nil or an empty slice so the test validates "no profiles remain"
regardless of JSON serialization. Update the assertion around
getHostResp.Host.MDM.Profiles (in the getHostResponse usage after s.DoJSON) to
check that either getHostResp.Host.MDM.Profiles == nil OR
len(getHostResp.Host.MDM.Profiles) == 0 (e.g., using require.Truef or
require.Empty) so the test passes for both nil and [] outcomes.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: e507c1f3-3112-428b-8a86-5677e9bb755e

📥 Commits

Reviewing files that changed from the base of the PR and between 04de819 and 3f2fc3e.

📒 Files selected for processing (4)
  • changes/42600-android-cert-templates-cleared-on-reenroll
  • server/datastore/mysql/android.go
  • server/datastore/mysql/android_test.go
  • server/service/integration_android_certificate_templates_test.go

Comment thread server/datastore/mysql/android.go
@getvictor
getvictor marked this pull request as ready for review April 2, 2026 19:23
@getvictor
getvictor requested a review from a team as a code owner April 2, 2026 19:23

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Claude Code Review

This repository is configured for manual code reviews. Comment @claude review to trigger a review and subscribe this PR to future pushes, or @claude review once for a one-time review.

Tip: disable this comment in your organization's Code Review settings.

@codecov

codecov Bot commented Apr 2, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 57.14286% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 66.81%. Comparing base (773edea) to head (c79d38f).
⚠️ Report is 10 commits behind head on main.

Files with missing lines Patch % Lines
server/datastore/mysql/android.go 57.14% 3 Missing and 3 partials ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main   #42920   +/-   ##
=======================================
  Coverage   66.81%   66.81%           
=======================================
  Files        2543     2543           
  Lines      204301   204341   +40     
  Branches     9237     9237           
=======================================
+ Hits       136510   136538   +28     
- Misses      55414    55421    +7     
- Partials    12377    12382    +5     
Flag Coverage Δ
backend 68.61% <57.14%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@getvictor
getvictor merged commit 2118dcb into main Apr 2, 2026
69 of 71 checks passed
@getvictor
getvictor deleted the victor/42600-android-cert-clear branch April 2, 2026 19:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Android host certificate templates not cleared when device is re-enrolled

3 participants