Skip to content
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

[Unprivileged] Windows enablement #4264

Merged
merged 55 commits into from
Apr 4, 2024

Conversation

blakerouse
Copy link
Contributor

@blakerouse blakerouse commented Feb 14, 2024

What does this PR do?

Enables the installation mode of --unprivileged for Windows. During installation the elastic-agent-user user and elastic-agent group is created, permissions are set correctly on the copied files, service is created running as the elastic-agent-user. While running in unprivileged mode the daemon creates the vault with the correct permissions and gives users in the elastic-agent group the ability to communicate with the running Elastic Agent daemon.

Why is it important?

Allowing users to install the Elastic Agent with less permissions.

Checklist

  • 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 (cannot test with unit tests)
  • [ ] I have added an entry in ./changelog/fragments using the changelog tool (skipped as we don't want to expose this feature until ready)
  • I have added an integration test or an E2E test

Author's Checklist

  • elastic-agent-user is created
  • elastic-agent group is created
  • elastic-agent-user is added to elastic-agent group
  • file permission/owner is set for elastic-agent-user and elastic-agent group
  • elastic-agent runs correctly

How to test this PR locally

$ .\elastic-agent.exe install -f --unprivileged

Add your local user to elastic-agent group. Be sure to re-open command prompt. Run C:\Program Files\Elastic\Agent\elastic-agent.exe status.

Related issues

@blakerouse blakerouse added Team:Elastic-Agent Label for the Agent team backport-skip labels Feb 14, 2024
@blakerouse blakerouse self-assigned this Feb 14, 2024
Copy link
Contributor

mergify bot commented Feb 14, 2024

This pull request is now in conflicts. Could you fix it? 🙏
To fixup this pull request, you can check out it locally. See documentation: https://help.github.com/articles/checking-out-pull-requests-locally/

git fetch upstream
git checkout -b unprivileged-windows upstream/unprivileged-windows
git merge upstream/main
git push upstream unprivileged-windows

Copy link
Contributor

mergify bot commented Feb 22, 2024

This pull request is now in conflicts. Could you fix it? 🙏
To fixup this pull request, you can check out it locally. See documentation: https://help.github.com/articles/checking-out-pull-requests-locally/

git fetch upstream
git checkout -b unprivileged-windows upstream/unprivileged-windows
git merge upstream/main
git push upstream unprivileged-windows

@elasticmachine
Copy link
Contributor

Pinging @elastic/elastic-agent (Team:Elastic-Agent)

@pierrehilbert pierrehilbert requested review from leehinman and removed request for michalpristas March 21, 2024 16:41
Copy link
Contributor

@leehinman leehinman left a comment

Choose a reason for hiding this comment

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

just documentation requests.


t, err := windows.OpenCurrentProcessToken()
token := windows.Token(0)
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add a comment on why the switch to Token(0) ? We have had so many issues with detecting "admin" I'd just like to be sure we have this documented if things pop up later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

windows.Token(0) is used because its passed into checkTokenMembership that when the token is zero that check uses the current process token. It is a simpler way of saying this processes current token, without the need to grab a handle that needs to be closed for the current process.

What do you want documented here? That windows.Token(0) means the current process token?

Copy link
Contributor

Choose a reason for hiding this comment

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

What do you want documented here? That windows.Token(0) means the current process token?

Probably a few links to MSDN documentation, and just a comment that windows.Token(0) is current process. I'm just thinking in 3 years if we have a bug around this code in some new Azure environment it is nice to know what we were thinking and hopefully pointers to full documentation which might give clues if it has been deprecated etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added some more comments and a link for the windows.Token(0).

"OnFailureResetPeriod": 10,
}
if opts.Password != "" {
option["Password"] = opts.Password
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm pretty sure this password is stored encrypted in the Registry. We need to document how the user would go about changing it. It's not uncommon for password policies to require changing static passwords like this on a regular basis.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know if that is true in this case. The user cannot login at all, it can only be used as a service and the password is setup to never expire.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't know if that is true in this case.

See things like: https://www.stigviewer.com/stig/windows_server_2019/2020-06-15/finding/V-93209

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is interesting finding, but even in this case the installing administrator doesn't even know the password. Being the finding is speaking about an administrator knowing the password, and in this case the password is completely unknown.

We can definitely add documentation about it for unprivileged installation on Windows. If they choose to change the user account password they will also have to update the password for the service.

Copy link
Member

@pchila pchila left a comment

Choose a reason for hiding this comment

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

Code looks good, no major blockers.
Left a few comments that are mostly for readability/documentation, those don't fundamentally change the implementation

@pierrehilbert pierrehilbert requested a review from leehinman April 3, 2024 08:40
Copy link
Contributor

@leehinman leehinman left a comment

Choose a reason for hiding this comment

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

need to use crypto/rand not math/rand for generating random passwords.

Other than that, LGTM.

@blakerouse
Copy link
Contributor Author

@leehinman Okay I have updated to crypto/rand.

Copy link

Quality Gate failed Quality Gate failed

Failed conditions

7.7% 7.7% Coverage on New Code (required ≥ 40%)

See analysis details on SonarQube

@pierrehilbert
Copy link
Contributor

What can be tested is tested and additional tests will require this to be merged first.
I'm forcing the merge even if the SonarQube requirement is red.

@pierrehilbert pierrehilbert merged commit c923f2e into elastic:main Apr 4, 2024
8 of 9 checks passed
@blakerouse blakerouse deleted the unprivileged-windows branch April 4, 2024 12:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Unprivileged] Enable --unprivileged on Windows
5 participants