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

enhancement(4889): added check to abort enrolling if user is privileg… #6038

Conversation

kaanyalti
Copy link
Contributor

@kaanyalti kaanyalti commented Nov 14, 2024

  • Enhancement

What does this PR do?

This PR updates the enroll command so that it provides a more descriptive error message to the user in the case a root user tries to enroll an unprivileged agent.

Why is it important?

Provides users with clearer information.

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

  • Create an agent policy, install and enroll and agent in unprivileged mode
  • Unenroll the agent
  • Try to enroll the agent with root user, validate the error message

Related issues

Copy link
Contributor

mergify bot commented Nov 14, 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 14, 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 14, 2024
@kaanyalti kaanyalti force-pushed the enhancement/4889_add_actionable_error_message_enroll_unprivileged branch 2 times, most recently from fc7a200 to 0e77313 Compare November 18, 2024 21:32
@kaanyalti kaanyalti marked this pull request as ready for review November 18, 2024 21:32
@kaanyalti kaanyalti requested a review from a team as a code owner November 18, 2024 21:32
@kaanyalti kaanyalti force-pushed the enhancement/4889_add_actionable_error_message_enroll_unprivileged branch 5 times, most recently from 7f3bf53 to 9bbb3c0 Compare November 20, 2024 05:30
@pierrehilbert pierrehilbert added the Team:Elastic-Agent-Control-Plane Label for the Agent Control Plane team label Nov 20, 2024
@elasticmachine
Copy link
Contributor

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

if hasRoot && as.Info.Unprivileged && !c.options.FromInstall {
return unprivilegedAgentRootUserError
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I did not see the original filled issue for this and I would have made a comment there. I know this is coming late, but I believe this is the wrong approach.

If you call enroll as root, you can easily tell if Elastic Agent is installed without communicating with the daemon. You can read the user/group from the executing binary from the filesystem, because you are root.

You can then just ReExec the enroll command directly using the user you retrieved from reading from the disk. You need to read user from the filesystem as @michalpristas is adding the ability to use a custom user, so you cannot rely on the idea that the user is always the same.

This change also will allow a root user to call enroll and have the enroll work correctly as the unprivileged user.

Copy link
Contributor Author

@kaanyalti kaanyalti Nov 20, 2024

Choose a reason for hiding this comment

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

Would you be ok with implementing your suggestion in another issue? The goal of this implementation is just to provide a descriptive error message back to the user. Would this implementation break anything for our users? I do agree what you're suggesting is a better solution.

Copy link
Contributor

Choose a reason for hiding this comment

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

I just think the implementation suggestion I provided would replace this work in the PR, not really making this work required.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes it would, and I am now thinking that the current implementation is tricky to get working with delayed enrolls. I started looking into your suggestion and will be replacing this PR.

internal/pkg/agent/cmd/enroll_cmd.go Outdated Show resolved Hide resolved
@kaanyalti kaanyalti force-pushed the enhancement/4889_add_actionable_error_message_enroll_unprivileged branch from 9bbb3c0 to b582d40 Compare November 20, 2024 16:36
}, nil
}

var unprivilegedAgentRootUserError = errors.New("cannot execute this command as root, agent is unprivileged. either execute the command as the elastic-agent-user or add your user to the elastic-agent group")
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 it works the same even if the user is in that group. Because the file ownership will be incorrect.

Providing specific error message based on the platform would be a better approach.

if hasRoot && as.Info.Unprivileged && !c.options.FromInstall {
return unprivilegedAgentRootUserError
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I just think the implementation suggestion I provided would replace this work in the PR, not really making this work required.

@kaanyalti kaanyalti force-pushed the enhancement/4889_add_actionable_error_message_enroll_unprivileged branch from 70bccd2 to e32ddc9 Compare November 24, 2024 13:59
@kaanyalti kaanyalti closed this Nov 24, 2024
@kaanyalti kaanyalti force-pushed the enhancement/4889_add_actionable_error_message_enroll_unprivileged branch from e32ddc9 to e983650 Compare November 24, 2024 14:47
@kaanyalti
Copy link
Contributor Author

Closed in favor of #6144

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