Skip to content

[9.0](backport #8503) fix(7794): update Windows re-enrollment logic and rename test#8571

Merged
ebeahan merged 1 commit into9.0from
mergify/bp/9.0/pr-8503
Jun 17, 2025
Merged

[9.0](backport #8503) fix(7794): update Windows re-enrollment logic and rename test#8571
ebeahan merged 1 commit into9.0from
mergify/bp/9.0/pr-8503

Conversation

@mergify
Copy link
Contributor

@mergify mergify bot commented Jun 17, 2025

Type of change:

  • Bug

What does this PR do?

This PR updates the Windows enrollment logic in Elastic Agent, specifically addressing the overly strict file ownership checks that were introduced in a previous fix. Instead of removing the Windows-specific logic, this change implements the file ownership check as a no-op for Windows. The platform-specific files remain, but the check is now always allowed on Windows. This is a partial revert of the earlier logic, with the intent to restore expected behavior for privileged users on Windows.

Why is it important?

The original bug (see issue #4889, fixed in PR #6144) was that if the agent was installed unprivileged, running enroll as a privileged user would break the agent. The fix at the time was to disallow re-enrollment as root if the agent was installed unprivileged, using a strict file owner check. This worked for Unix, where impersonation is easy, but on Windows, due to how the elastic-agent-user is set up, impersonation is not feasible.

The strict SID check on Windows turned out to be too restrictive: even if the agent was installed as an admin, other admin users could not re-enroll, leading to the current issue (#7794). The workaround (changing file ownership to SYSTEM) was not reliable.

The changes in this PR implement the ownership check as a no-op for Windows, allowing any admin to re-enroll. The platform-specific files are retained for clarity and maintainability. This change also reintroduces the original risk: if the agent is installed unprivileged, a privileged user can still break the agent by re-enrolling. The platform-specific files are retained for clarity and maintainability.

Related issues

Checklist

  • I have read and understood the pull request guidelines of this project.
  • My code follows the style guidelines of this project
  • [ ] I have commented my code, particularly in hard-to-understand areas
  • [ ] I have made corresponding changes to the documentation
  • [ ] I have made corresponding change to the default configuration files
  • I have added tests that prove my fix is effective or that my feature works
  • I have added an entry in ./changelog/fragments using the changelog tool
  • I have added an integration test or an E2E test

Disruptive User Impact

  • Windows users who previously could not re-enroll the agent as an admin should now be able to do so.
  • The original risk (privileged re-enrollment breaking unprivileged installs) is reintroduced for Windows.

How to test this PR locally

  1. Build the agent on Windows.
  2. Attempt to enroll, unenroll, and re-enroll the agent as both privileged and unprivileged users.
  3. Privileged re-enroll should work. Unprivileged re-enroll should fail.
  4. Run the renamed integration test (re-enroll_test.go) to verify correct behavior.

Related issues


This is an automatic backport of pull request #8503 done by [Mergify](https://mergify.com).

* fix(7794): update Windows re-enrollment logic and rename test

- Updated enroll.go to add a Windows-specific condition for root/Administrator privilege checks.
- Removed Windows-specific file owner check implementation and its test.
- Renamed integration test from enroll_unprivileged_test.go to re-enroll_test.go to better reflect its purpose.

* fix(7794): implement no-op ownership check for Windows - Adds a Windows-specific implementation of isOwnerExec that always returns true (no-op), addressing build and re-enrollment issues. - Retains platform-specific files for clarity and maintainability. - Adds a Windows-specific test to ensure code coverage is maintained. - Removes the Windows OS check from enroll.go, relying on the no-op for Windows.

* changelog: add fragment for Windows re-enrollment bugfix

* fix(7794): remove unnecessary build tag

* fix(7794): enhance re-enrollment tests with structured test cases

- Renamed the test function to TestRenEnroll for clarity.
- Introduced structured test cases for unprivileged and privileged agent re-enrollment scenarios.
- Removed Windows-specific checks and consolidated assertions into the test cases.
- Updated the test logic to improve readability and maintainability.

* fix(7794): enhance re-enrollment test structure and OS handling

* fix(7794): reverted to individual test functions because ci enforces where in the test function define is called

* fix(7794): removed unnecessary var definition

* fix(7794): fix bool value for privileged test case

* fix(7794): added status check after second enroll, moved file owner error to enroll.go

* fix(7794): remove unenroll step from integration tests

* fix(7794): refactored test code

* fix(7794): added comment explaining why windows is excluded from unprivileged test case

(cherry picked from commit 00f1662)
@mergify mergify bot added the backport label Jun 17, 2025
@mergify mergify bot requested a review from a team as a code owner June 17, 2025 20:23
@mergify mergify bot added the backport label Jun 17, 2025
@mergify mergify bot requested review from blakerouse and removed request for a team June 17, 2025 20:23
@mergify mergify bot requested a review from pkoutsovasilis June 17, 2025 20:23
@github-actions github-actions bot added the Team:Elastic-Agent-Control-Plane Label for the Agent Control Plane team label Jun 17, 2025
@elasticmachine
Copy link
Contributor

Pinging @elastic/elastic-agent-control-plane (Team:Elastic-Agent-Control-Plane)

@kaanyalti kaanyalti enabled auto-merge (squash) June 17, 2025 21:13
@elastic-sonarqube
Copy link

@ebeahan ebeahan disabled auto-merge June 17, 2025 22:23
@ebeahan ebeahan merged commit 341f976 into 9.0 Jun 17, 2025
20 of 22 checks passed
@ebeahan ebeahan deleted the mergify/bp/9.0/pr-8503 branch June 17, 2025 22:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport Team:Elastic-Agent-Control-Plane Label for the Agent Control Plane team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants