Skip to content

Commit

Permalink
Use jenkins.util.SetContextClassLoader where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
basil committed Nov 5, 2024
1 parent b455e93 commit 1ce4414
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 53 deletions.
8 changes: 4 additions & 4 deletions src/main/java/hudson/security/LDAPSecurityRealm.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
import jenkins.security.plugins.ldap.LDAPGroupMembershipStrategy;
import jenkins.security.plugins.ldap.LDAPExtendedTemplate;
import jenkins.security.plugins.ldap.LdapEntryMapper;
import jenkins.security.plugins.ldap.SetContextClassLoader;
import jenkins.util.SetContextClassLoader;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.ldap.core.ContextSource;
Expand Down Expand Up @@ -986,7 +986,7 @@ private void addDelegate(AuthenticationManager delegate, String configurationId,

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
try (SetContextClassLoader sccl = new SetContextClassLoader()) {
try (SetContextClassLoader sccl = new SetContextClassLoader(LDAPAuthenticationManager.class)) {
String password = authentication.getCredentials() instanceof String ? (String) authentication.getCredentials() : null;
if(FIPS140.useCompliantAlgorithms() && (StringUtils.isBlank(password) || password.length() < 14)) {
final String message = "When running in FIPS compliance mode, the password must be at least 14 characters long";
Expand Down Expand Up @@ -1173,7 +1173,7 @@ public static Attributes getAttributes(LdapUserDetails details, @CheckForNull Ld
return ((DelegatedLdapUserDetails) details).attributes;
} else {
if (ldapUserSearch != null) {
try (SetContextClassLoader sccl = new SetContextClassLoader()) {
try (SetContextClassLoader sccl = new SetContextClassLoader(DelegatedLdapUserDetails.class)) {
return ldapUserSearch.searchForUser(details.getUsername()).getAttributes();
} catch (UsernameNotFoundException x) {
// ignore
Expand Down Expand Up @@ -1276,7 +1276,7 @@ public static class LDAPUserDetailsService implements UserDetailsService {
@Override
public DelegatedLdapUserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
username = fixUsername(username);
try (SetContextClassLoader sccl = new SetContextClassLoader()) {
try (SetContextClassLoader sccl = new SetContextClassLoader(LDAPUserDetailsService.class)) {
final SecurityRealm securityRealm = Jenkins.get().getSecurityRealm();
if (securityRealm instanceof LDAPSecurityRealm
&& (securityRealm.getSecurityComponents().userDetails2 == this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import jenkins.util.SetContextClassLoader;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
Expand Down Expand Up @@ -62,7 +63,7 @@ public LDAPExtendedTemplate(ContextSource dirContextFactory) {
*/
public @CheckForNull <T> T searchForFirstEntry(@NonNull final String base, @NonNull final String filter,
final Object[] filterArgs, final String[] attributeNames, @NonNull final LdapEntryMapper<T> mapper) {
try (SetContextClassLoader sccl = new SetContextClassLoader();
try (SetContextClassLoader sccl = new SetContextClassLoader(LDAPExtendedTemplate.class);
SearchResultEnumeration<T> searchEnum = searchForAllEntriesEnum(base, filter, filterArgs, attributeNames, mapper)) {
return searchEnum.hasMore() ? searchEnum.next() : null;
} catch (NamingException e) {
Expand All @@ -85,7 +86,7 @@ public LDAPExtendedTemplate(ContextSource dirContextFactory) {
public @NonNull <T> List<? extends T> searchForAllEntries(@NonNull final String base, @NonNull final String filter,
final Object[] filterArgs, final String[] attributeNames, @NonNull final LdapEntryMapper<T> mapper) {
List<T> results = new ArrayList<>();
try (SetContextClassLoader sccl = new SetContextClassLoader();
try (SetContextClassLoader sccl = new SetContextClassLoader(LDAPExtendedTemplate.class);
SearchResultEnumeration<T> searchEnum = searchForAllEntriesEnum(base, filter, filterArgs, attributeNames, mapper)) {
while (searchEnum.hasMore()) {
results.add(searchEnum.next());
Expand Down

This file was deleted.

0 comments on commit 1ce4414

Please sign in to comment.