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

Return actionable error message when enrolling #6144

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

kaanyalti
Copy link
Contributor

@kaanyalti kaanyalti commented Nov 25, 2024

  • Enhancement

What does this PR do?

This PR adds checks to the enroll command to respond with an error message in case the user executing the command and the user that's the owner of the elastic program files don't match. Replaces #6038 based on the following comment

Why is it important?

Currently there are no checks in place to prevent or correct the situation where a user who is not the owner of the program files executes enroll. This leads to a broken state.

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
  • I have added an entry in ./changelog/fragments using the changelog tool
  • I have added an integration test or an E2E test

How to test this PR locally

The testing steps are only for linux and mac

  • Deploy ess, install agent in unprivileged mode and enroll into fleet
  • Unenroll the agent from the fleet ui
  • Run the enroll command as root user and validate the error message

Related issues

Copy link
Contributor

mergify bot commented Nov 25, 2024

This pull request does not have a backport label. Could you fix it @kaanyalti? 🙏
To fixup this pull request, you need to add the backport labels for the needed
branches, such as:

  • backport-./d./d is the label to automatically backport to the 8./d branch. /d is the digit

Copy link
Contributor

mergify bot commented Nov 25, 2024

backport-v8.x has been added to help with the transition to the new branch 8.x.
If you don't need it please use backport-skip label and remove the backport-8.x label.

@mergify mergify bot added the backport-8.x Automated backport to the 8.x branch with mergify label Nov 25, 2024
@pierrehilbert pierrehilbert added the Team:Elastic-Agent-Control-Plane Label for the Agent Control Plane team label Nov 26, 2024
@kaanyalti kaanyalti changed the title Reexec enroll command if the user and file owner don't match Add error message to linux and mac for the enroll command Nov 26, 2024
@kaanyalti kaanyalti force-pushed the enhancement/4889_enroll_match_file_owner branch from 6834e0b to 38be49e Compare November 26, 2024 21:35
@kaanyalti kaanyalti marked this pull request as ready for review November 26, 2024 21:35
@kaanyalti kaanyalti requested a review from a team as a code owner November 26, 2024 21:35
@elasticmachine
Copy link
Contributor

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

@kaanyalti kaanyalti changed the title Add error message to linux and mac for the enroll command Return actionable error message when enrolling Nov 26, 2024
@kaanyalti kaanyalti force-pushed the enhancement/4889_enroll_match_file_owner branch from 5718b15 to 1d02094 Compare November 27, 2024 10:25
return curUser == fileOwner, nil
}

func isEnroll() (bool, error) {
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 think isEnroll is a proper name for this. not sure it captures properly what this func is doing

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 can also split this function into its constituent functions. It would be more readable. All the steps would be clearly outlined. I couldn't come up with a reasonably short function name that describes exactly what's happening.

If I split it, these functions will be called in order in enroll.go

  • getFileOwner
  • getCurrentUser
  • isFileOwner

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree with @michalpristas here, this is not the proper name for this function. It is just verifying that the executing users is the same as the owner of the executable.

To me the better name would be isOwnerExec?

return fmt.Errorf("ran into an error while figuring out if user is allowed to execute the enroll command")
}
if !isEnroll {
return UserOwnerMismatchError
Copy link
Contributor

Choose a reason for hiding this comment

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

On Windows you cannot actually open a shell as the elastic-agent-user so how would they be able to run enroll command on Windows as the elastic-agent-user.

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 can return an error message stating the fact that user cannot execute the command as admin for windows. Linux/mac error message will can still mention running the command as the elastic-agent-user

return curUser == fileOwner, nil
}

func isEnroll() (bool, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I agree with @michalpristas here, this is not the proper name for this function. It is just verifying that the executing users is the same as the owner of the executable.

To me the better name would be isOwnerExec?

Copy link

Quality Gate failed Quality Gate failed

Failed conditions
0.0% Coverage on New Code (required ≥ 40%)

See analysis details on SonarQube

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-8.x Automated backport to the 8.x branch with mergify 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.

Actionable error message when attempting to enroll an unprivileged Agent as a privileged user
5 participants