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

[JENKINS-67981] handle any AuthenticationException #1322

Merged
merged 1 commit into from
Sep 3, 2022

Conversation

hashar
Copy link
Contributor

@hashar hashar commented Sep 1, 2022

JENKINS-67981 - handle any AuthenticationException

When processing the changelog, if a looked user is disabled in LDAP the git plugin would throw an UncheckedExecutionException with the cause being DisabledException. The unhandled exception would cause a build
failure whenever processing a commit that is determinated to be from a disabled user.

Jenkins UserDetailsCache.loadUserByUsername() receives an ExecutionException which wraps the AuthenticationException. The method inspects the cause, handles instances of UsernameNotFoundException which it throws but ignores any other AuthenticationException.

This introduces a User getter to handle any AuthenticationException and throws it instead of the ExecutionException. Callers can thus:

  try {
    user = getUser("someOne");
  } catch (AuthenticationException e) {
    return User.getUnknown();
  }
  // Do something with "user"

I have added a test to ensure a DisabledException does yield an unknown user. It only test a single code path though.

This pull request supersedes #1233 on which I analyzed the AuthenticationException is wrapped in an ExecutionException. Compared to that other pull request, I introduce a getter to avoid copy pasting try/catch statements.

References:

Checklist

  • I have read the CONTRIBUTING doc
  • I have referenced the Jira issue related to my changes in one or more commit messages
  • I have added tests that verify my changes
  • Unit tests pass locally with my changes
  • I have added documentation as necessary
  • No Javadoc warnings were introduced with my changes
  • No spotbugs warnings were introduced with my changes
  • Documentation in README has been updated as necessary
  • Online help has been added and reviewed for any new or modified fields
  • I have interactively tested my changes (though my Jenkins instance script console)
  • Any dependent changes have been merged and published in upstream modules (like git-client-plugin)

Types of changes

  • Bug fix (non-breaking change which fixes an issue

When processing the changelog, if a looked user is disabled in LDAP the
git plugin would throw an `UncheckedExecutionException` with the cause
being `DisabledException`. The unhandled exception would cause a build
failure whenever processing a commit that is determinated to be from a
disabled user.

Jenkins `UserDetailsCache.loadUserByUsername()` receives an
`ExecutionException` which wraps the `AuthenticationException`. The
method inspects the cause, handles instances of
`UsernameNotFoundException` which it throws but ignores any other
`AuthenticationException`.

This introduces a `User` getter to handle any `AuthenticationException`
and throws it instead of the `ExecutionException`. Callers can thus:

  try {
    user = getUser("someOne");
  } catch (AuthenticationException e) {
    return User.getUnknown();
  }
  // Do something with "user"

I have added a test to ensure a `DisabledException` does yield an
unknown user. It only test a single code path though.

Reference: https://phabricator.wikimedia.org/T315897
@MarkEWaite
Copy link
Contributor

MarkEWaite commented Sep 1, 2022

I'd like to have confirmation from users that the change resolves their issue. I can't test it.

https://ci.jenkins.io/job/Plugins/job/git-plugin/job/PR-1322/lastSuccessfulBuild/ shows the most recent build. Copy the URL of the HPI file from that page and paste it into the "Advanced" tab of the Jenkins plugin manager.

Thanks!

Copy link
Contributor

@MarkEWaite MarkEWaite left a comment

Choose a reason for hiding this comment

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

This looks good to me. Thanks for the investigation and for the contribution.

The Spring security AuthenticationException shows it has subclasses like:

Those all seem very reasonable things that should result in User.getUnknown() for these types of cases where the result from the call is used to present informational content. None of these uses are for authentication, so returning "unkwown" is much better than failing with an exception.

@hashar
Copy link
Contributor Author

hashar commented Sep 2, 2022

Those all seem very reasonable things that should result in User.getUnknown() for these types of cases where the result from the call is used to present informational content. None of these uses are for authentication, so returning "unkwown" is much better than failing with an exception.

Thank you for investigating and confirming we could return an unknown user for any kind of AuthenticationException. I wasn't really sure it was the proper thing to do. There might be a way in Jenkins to represent a user even when it is disabled/expired/locked, then it is probably for a future change (if at all needed). Thanks!

I have build the plugin at 13a61a0 + this pull request and deployed it on our affected instance. That has fixed the issue and the affected job completed without errors. Hurrah!

Ref: https://phabricator.wikimedia.org/T315897#8205675

@MarkEWaite MarkEWaite added bugfix Fixes a bug - used by Release Drafter and removed test labels Sep 3, 2022
@MarkEWaite
Copy link
Contributor

I'm proceeding with the merge in hopes that others will test it before release. Even if not, we have the report from @hashar that the changes resolves the issue for his case.

@MarkEWaite MarkEWaite merged commit a26c735 into jenkinsci:master Sep 3, 2022
@SaschaSchwarz
Copy link

* [ ]  @SaschaSchwarz can you test it and report your results?

I tried Build 1015 and was not having any issues. 👍

@MarkEWaite
Copy link
Contributor

Thanks @SaschaSchwarz ! The change is now merged to the master branch. It will be included in the next release of the git plugin.

@hashar
Copy link
Contributor Author

hashar commented Sep 5, 2022

Thank you @SaschaSchwarz for the verification and thank you @MarkEWaite for the review/merge!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix Fixes a bug - used by Release Drafter
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants