Skip to content

Commit

Permalink
Migrate from Acegi to Spring Security (#209)
Browse files Browse the repository at this point in the history
  • Loading branch information
basil authored Feb 10, 2025
1 parent 3c73d3c commit 2bd33c4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.model.User;
import jenkins.security.SecurityListener;
import org.acegisecurity.AuthenticationException;
import org.acegisecurity.userdetails.UserDetails;
import org.acegisecurity.userdetails.UsernameNotFoundException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.apache.sshd.server.auth.pubkey.PublickeyAuthenticator;
import org.apache.sshd.server.session.ServerSession;
import org.jenkinsci.main.modules.cli.auth.ssh.PublicKeySignatureWriter;
Expand Down Expand Up @@ -41,7 +41,7 @@ public boolean authenticate(String username, PublicKey key, ServerSession sessio
return false;
}

SecurityListener.fireAuthenticated(user.getUserDetailsForImpersonation());
SecurityListener.fireAuthenticated2(user.getUserDetailsForImpersonation2());
return true;
}

Expand Down Expand Up @@ -70,7 +70,7 @@ public boolean authenticate(String username, PublicKey key, ServerSession sessio

private @CheckForNull UserDetails verifyUserUsingSecurityRealm(@NonNull User user) {
try {
return user.getUserDetailsForImpersonation();
return user.getUserDetailsForImpersonation2();
} catch (UsernameNotFoundException e) {
LOGGER.log(Level.FINE, e, () -> user.getId() + " is not a real user according to SecurityRealm");
return null;
Expand Down
28 changes: 13 additions & 15 deletions src/test/java/org/jenkinsci/main/modules/sshd/SSHDTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
import hudson.Functions;
import hudson.security.AbstractPasswordBasedSecurityRealm;
import hudson.security.GroupDetails;
import org.acegisecurity.AccountExpiredException;
import org.acegisecurity.AuthenticationException;
import org.acegisecurity.CredentialsExpiredException;
import org.acegisecurity.DisabledException;
import org.acegisecurity.GrantedAuthority;
import org.acegisecurity.LockedException;
import org.acegisecurity.userdetails.User;
import org.acegisecurity.userdetails.UserDetails;
import org.acegisecurity.userdetails.UsernameNotFoundException;
import org.springframework.security.authentication.AccountExpiredException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.authentication.CredentialsExpiredException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.LockedException;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.apache.sshd.client.SshClient;
import org.apache.sshd.client.future.ConnectFuture;
import org.apache.sshd.client.keyverifier.AcceptAllServerKeyVerifier;
Expand All @@ -26,7 +25,6 @@
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.springframework.dao.DataAccessException;

import java.io.IOException;
import java.net.InetSocketAddress;
Expand Down Expand Up @@ -154,12 +152,12 @@ private static KeyPair generateKeys(hudson.model.User user) throws NoSuchAlgorit
@Issue("JENKINS-55813")
private static class InvalidUserTypesRealm extends AbstractPasswordBasedSecurityRealm {
@Override
protected UserDetails authenticate(String user, String pass) throws AuthenticationException {
return loadUserByUsername(user);
protected UserDetails authenticate2(String user, String pass) throws AuthenticationException {
return loadUserByUsername2(user);
}

@Override
public UserDetails loadUserByUsername(String user) throws UsernameNotFoundException, DataAccessException {
public UserDetails loadUserByUsername2(String user) throws UsernameNotFoundException {
switch (user) {
case "disabled":
throw new DisabledException(user);
Expand All @@ -174,12 +172,12 @@ public UserDetails loadUserByUsername(String user) throws UsernameNotFoundExcept
throw new LockedException(user);

default:
return new User(user, "", true, true, true, true, new GrantedAuthority[0]);
return new User(user, "", true, true, true, true, List.of());
}
}

@Override
public GroupDetails loadGroupByGroupname(String group) throws UsernameNotFoundException, DataAccessException {
public GroupDetails loadGroupByGroupname2(String groupname, boolean fetchMembers) throws UsernameNotFoundException {
throw new UnsupportedOperationException();
}
}
Expand Down

0 comments on commit 2bd33c4

Please sign in to comment.