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

Unauthorized keys return 401 #3135

Merged
merged 3 commits into from
Dec 1, 2023

Conversation

michel-laterman
Copy link
Contributor

What is the problem this PR solves?

Any issues with the API key result in a 400 status code

How does this PR solve the problem?

Issues with API keys will result in a 401, or a 403 if the key does not match the agent.

Checklist

  • 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

Related issues

@michel-laterman michel-laterman added enhancement New feature or request Team:Fleet Label for the Fleet team labels Nov 30, 2023
@michel-laterman michel-laterman requested a review from a team as a code owner November 30, 2023 22:09
@@ -167,7 +167,7 @@ func NewHTTPErrResp(err error) HTTPErrResp {
{
ErrAgentIdentity,
HTTPErrResp{
http.StatusBadRequest,
http.StatusForbidden,
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'm pretty sure I changed all the relevant auth errors to return a 401 here.

However I think it makes sense to use a 403 instead of a 401 in this case where the API key does not match the requested agent. I've altered the openapi spec to include it as a return for the relevant endpoints

cc @joshdover

Copy link
Contributor

Choose a reason for hiding this comment

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

Makes sense to me.
So IIUC if the key doesn't match it will be a 403 everything else will be 401.

If the key is not found, shouldn't we also return a 403 instead of a 401?

Copy link
Member

Choose a reason for hiding this comment

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

If the key is not found, shouldn't we also return a 403 instead of a 401?

This would be an unauthorized case, right? So 401 would be fine.

@@ -167,7 +167,7 @@ func NewHTTPErrResp(err error) HTTPErrResp {
{
ErrAgentIdentity,
HTTPErrResp{
http.StatusBadRequest,
http.StatusForbidden,
Copy link
Member

Choose a reason for hiding this comment

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

If the key is not found, shouldn't we also return a 403 instead of a 401?

This would be an unauthorized case, right? So 401 would be fine.

model/openapi.yml Outdated Show resolved Hide resolved
internal/pkg/api/error.go Show resolved Hide resolved
Co-authored-by: Jaime Soriano Pastor <[email protected]>
Copy link
Member

@jsoriano jsoriano left a comment

Choose a reason for hiding this comment

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

Change makes sense to me 👍

Do we need to change anything in how the Agent handles authorization errors to completely close #2861?

@michel-laterman
Copy link
Contributor Author

I don't think so, the isUnauth check for the checkin would trigger based on the response code the agent gets, so the agent would unenroll iteself after too many failures.

Copy link

@michel-laterman michel-laterman merged commit 85d6d8c into elastic:main Dec 1, 2023
5 checks passed
@michel-laterman michel-laterman deleted the unauthorized-status branch December 1, 2023 19:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request Team:Fleet Label for the Fleet team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Check enrollment status before authenticating agents
3 participants