Skip to content

Commit 5595262

Browse files
authored
Migrate from Acegi compatibility layer to Spring Security 6.x (#169)
1 parent 8d2c859 commit 5595262

7 files changed

+51
-55
lines changed

pom.xml

+3-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<groupId>org.jenkins-ci.plugins</groupId>
66
<artifactId>plugin</artifactId>
7-
<version>4.88</version>
7+
<version>5.6</version>
88
<relativePath />
99
</parent>
1010
<artifactId>gitlab-oauth</artifactId>
@@ -13,13 +13,11 @@
1313
<properties>
1414
<revision>1.20</revision>
1515
<changelist>-SNAPSHOT</changelist>
16-
<jenkins.baseline>2.452</jenkins.baseline>
17-
<jenkins.version>${jenkins.baseline}.4</jenkins.version>
16+
<jenkins.baseline>2.479</jenkins.baseline>
17+
<jenkins.version>${jenkins.baseline}.1</jenkins.version>
1818
<spotbugs.effort>Max</spotbugs.effort>
1919
<spotbugs.threshold>Low</spotbugs.threshold>
2020
<gitHubRepo>jenkinsci/${project.artifactId}-plugin</gitHubRepo>
21-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
22-
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
2321
</properties>
2422
<name>GitLab Authentication plugin</name>
2523
<description>A Jenkins authentication plugin that delegates to GitLab. We also implement an Authorization Strategy that users the acquired OAuth token to interact with the GitLab API to determine a users level of access to Jenkins.</description>

src/main/java/org/jenkinsci/plugins/GitLabAuthenticationException.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ of this software and associated documentation files (the "Software"), to deal
3333

3434
package org.jenkinsci.plugins;
3535

36-
import org.acegisecurity.AuthenticationException;
36+
import org.springframework.security.core.AuthenticationException;
3737

3838
/**
3939
*

src/main/java/org/jenkinsci/plugins/GitLabAuthenticationToken.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ of this software and associated documentation files (the "Software"), to deal
4141
import java.util.logging.Level;
4242
import java.util.logging.Logger;
4343
import jenkins.model.Jenkins;
44-
import org.acegisecurity.GrantedAuthority;
45-
import org.acegisecurity.GrantedAuthorityImpl;
46-
import org.acegisecurity.providers.AbstractAuthenticationToken;
4744
import org.apache.commons.collections.CollectionUtils;
4845
import org.apache.commons.lang.StringUtils;
4946
import org.gitlab4j.api.Constants.TokenType;
@@ -52,6 +49,9 @@ of this software and associated documentation files (the "Software"), to deal
5249
import org.gitlab4j.api.models.Group;
5350
import org.gitlab4j.api.models.Project;
5451
import org.gitlab4j.api.models.User;
52+
import org.springframework.security.authentication.AbstractAuthenticationToken;
53+
import org.springframework.security.core.GrantedAuthority;
54+
import org.springframework.security.core.authority.SimpleGrantedAuthority;
5555

5656
/**
5757
* @author mocleiri
@@ -93,7 +93,7 @@ public class GitLabAuthenticationToken extends AbstractAuthenticationToken {
9393
private final List<GrantedAuthority> authorities = new ArrayList<>();
9494

9595
public GitLabAuthenticationToken(String accessToken, String gitlabServer, TokenType tokenType) throws GitLabApiException {
96-
super(new GrantedAuthority[] {});
96+
super(List.of());
9797

9898
this.accessToken = accessToken;
9999
this.gitLabAPI = new GitLabApi(gitlabServer, tokenType, accessToken);
@@ -103,7 +103,7 @@ public GitLabAuthenticationToken(String accessToken, String gitlabServer, TokenT
103103
setAuthenticated(true);
104104

105105
this.userName = this.me.getUsername();
106-
authorities.add(SecurityRealm.AUTHENTICATED_AUTHORITY);
106+
authorities.add(SecurityRealm.AUTHENTICATED_AUTHORITY2);
107107
Jenkins jenkins = Jenkins.getInstanceOrNull();
108108
if (jenkins != null && jenkins.getSecurityRealm() instanceof GitLabSecurityRealm) {
109109

@@ -147,8 +147,8 @@ public GitLabApi getGitLabAPI() {
147147
}
148148

149149
@Override
150-
public GrantedAuthority[] getAuthorities() {
151-
return authorities.toArray(new GrantedAuthority[0]);
150+
public Collection<GrantedAuthority> getAuthorities() {
151+
return authorities;
152152
}
153153

154154
@Override
@@ -318,12 +318,12 @@ public GitLabOAuthUserDetails getUserDetails(String username) {
318318
try {
319319
List<Group> gitLabGroups = gitLabAPI.getGroupApi().getGroups();
320320
for (Group gitlabGroup : gitLabGroups) {
321-
groups.add(new GrantedAuthorityImpl(gitlabGroup.getName()));
321+
groups.add(new SimpleGrantedAuthority(gitlabGroup.getName()));
322322
}
323323
} catch (GitLabApiException e) {
324324
LOGGER.log(Level.FINE, e.getMessage(), e);
325325
}
326-
return new GitLabOAuthUserDetails(user, groups.toArray(new GrantedAuthority[0]));
326+
return new GitLabOAuthUserDetails(user, groups);
327327
}
328328
return null;
329329
}

src/main/java/org/jenkinsci/plugins/GitLabOAuthGroupDetails.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
package org.jenkinsci.plugins;
66

77
import hudson.security.GroupDetails;
8-
import org.acegisecurity.GrantedAuthority;
9-
import org.acegisecurity.GrantedAuthorityImpl;
108
import org.gitlab4j.api.models.Group;
9+
import org.springframework.security.core.GrantedAuthority;
10+
import org.springframework.security.core.authority.SimpleGrantedAuthority;
1111

1212
/**
1313
* Represent a group from GitLab as a group in Jenkins terms.
@@ -60,6 +60,6 @@ public String toString() {
6060
}
6161

6262
public GrantedAuthority getAuth() {
63-
return new GrantedAuthorityImpl(getName());
63+
return new SimpleGrantedAuthority(getName());
6464
}
6565
}

src/main/java/org/jenkinsci/plugins/GitLabOAuthUserDetails.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package org.jenkinsci.plugins;
22

3-
import org.acegisecurity.GrantedAuthority;
4-
import org.acegisecurity.userdetails.User;
3+
import java.util.Collection;
4+
import org.springframework.security.core.GrantedAuthority;
5+
import org.springframework.security.core.userdetails.User;
56

67
/**
78
* @author Mike
@@ -11,7 +12,7 @@ public class GitLabOAuthUserDetails extends User {
1112

1213
private static final long serialVersionUID = 1709511212188366292L;
1314

14-
public GitLabOAuthUserDetails(org.gitlab4j.api.models.User user, GrantedAuthority[] authorities) {
15+
public GitLabOAuthUserDetails(org.gitlab4j.api.models.User user, Collection<? extends GrantedAuthority> authorities) {
1516
super(user.getUsername(), "", true, true, true, true, authorities);
1617
}
1718

src/main/java/org/jenkinsci/plugins/GitLabRequireOrganizationMembershipACL.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ of this software and associated documentation files (the "Software"), to deal
4141
import java.util.List;
4242
import java.util.logging.Logger;
4343
import jenkins.model.Jenkins;
44-
import org.acegisecurity.Authentication;
4544
import org.kohsuke.stapler.Stapler;
46-
import org.kohsuke.stapler.StaplerRequest;
45+
import org.kohsuke.stapler.StaplerRequest2;
46+
import org.springframework.security.core.Authentication;
4747

4848
/**
4949
* @author Mike
@@ -70,11 +70,11 @@ public class GitLabRequireOrganizationMembershipACL extends ACL {
7070
/*
7171
* (non-Javadoc)
7272
*
73-
* @see hudson.security.ACL#hasPermission(org.acegisecurity.Authentication,
73+
* @see hudson.security.ACL#hasPermission(org.springframework.security.core.Authentication,
7474
* hudson.security.Permission)
7575
*/
7676
@Override
77-
public boolean hasPermission(Authentication a, Permission permission) {
77+
public boolean hasPermission2(Authentication a, Permission permission) {
7878
if (a != null && a instanceof GitLabAuthenticationToken) {
7979
if (!a.isAuthenticated()) {
8080
return false;
@@ -154,7 +154,7 @@ public boolean hasPermission(Authentication a, Permission permission) {
154154
} else {
155155
String authenticatedUserName = a.getName();
156156

157-
if (authenticatedUserName.equals(SYSTEM.getPrincipal())) {
157+
if (authenticatedUserName.equals(SYSTEM2.getPrincipal())) {
158158
// give system user full access
159159
log.finest("Granting Full rights to SYSTEM user.");
160160
return true;
@@ -223,7 +223,7 @@ private boolean currentUriPathEquals(String specificPath) {
223223
}
224224

225225
private String requestURI() {
226-
StaplerRequest currentRequest = Stapler.getCurrentRequest();
226+
StaplerRequest2 currentRequest = Stapler.getCurrentRequest2();
227227
return (currentRequest == null) ? null : currentRequest.getOriginalRequestURI();
228228
}
229229

src/main/java/org/jenkinsci/plugins/GitLabSecurityRealm.java

+25-28
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@
4141
import hudson.model.User;
4242
import hudson.security.GroupDetails;
4343
import hudson.security.SecurityRealm;
44-
import hudson.security.UserMayOrMayNotExistException;
44+
import hudson.security.UserMayOrMayNotExistException2;
4545
import hudson.tasks.Mailer;
4646
import hudson.util.Secret;
47+
import jakarta.servlet.http.HttpSession;
4748
import java.io.IOException;
4849
import java.net.InetSocketAddress;
4950
import java.net.MalformedURLException;
@@ -55,18 +56,8 @@
5556
import java.util.ArrayList;
5657
import java.util.List;
5758
import java.util.logging.Logger;
58-
import javax.servlet.http.HttpSession;
5959
import jenkins.model.Jenkins;
6060
import jenkins.security.SecurityListener;
61-
import org.acegisecurity.Authentication;
62-
import org.acegisecurity.AuthenticationException;
63-
import org.acegisecurity.AuthenticationManager;
64-
import org.acegisecurity.BadCredentialsException;
65-
import org.acegisecurity.context.SecurityContextHolder;
66-
import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
67-
import org.acegisecurity.userdetails.UserDetails;
68-
import org.acegisecurity.userdetails.UserDetailsService;
69-
import org.acegisecurity.userdetails.UsernameNotFoundException;
7061
import org.apache.commons.lang.StringUtils;
7162
import org.apache.commons.lang.builder.HashCodeBuilder;
7263
import org.apache.http.HttpEntity;
@@ -91,9 +82,17 @@
9182
import org.kohsuke.stapler.HttpResponse;
9283
import org.kohsuke.stapler.HttpResponses;
9384
import org.kohsuke.stapler.QueryParameter;
94-
import org.kohsuke.stapler.StaplerRequest;
95-
import org.springframework.dao.DataAccessException;
96-
import org.springframework.dao.DataRetrievalFailureException;
85+
import org.kohsuke.stapler.StaplerRequest2;
86+
import org.springframework.security.authentication.AuthenticationManager;
87+
import org.springframework.security.authentication.AuthenticationServiceException;
88+
import org.springframework.security.authentication.BadCredentialsException;
89+
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
90+
import org.springframework.security.core.Authentication;
91+
import org.springframework.security.core.AuthenticationException;
92+
import org.springframework.security.core.context.SecurityContextHolder;
93+
import org.springframework.security.core.userdetails.UserDetails;
94+
import org.springframework.security.core.userdetails.UserDetailsService;
95+
import org.springframework.security.core.userdetails.UsernameNotFoundException;
9796

9897
/**
9998
*
@@ -103,7 +102,7 @@
103102
* This is based on the GitLabSecurityRealm from the gitlab-auth-plugin written
104103
* by Alex Ackerman.
105104
*/
106-
public class GitLabSecurityRealm extends SecurityRealm implements UserDetailsService {
105+
public class GitLabSecurityRealm extends SecurityRealm {
107106
private String gitlabWebUri;
108107
private String gitlabApiUri;
109108
private String clientID;
@@ -266,7 +265,7 @@ public Secret getClientSecret() {
266265

267266
// "from" is coming from SecurityRealm/loginLink.jelly
268267
public HttpResponse doCommenceLogin(
269-
StaplerRequest request, @QueryParameter String from, @Header("Referer") final String referer)
268+
StaplerRequest2 request, @QueryParameter String from, @Header("Referer") final String referer)
270269
throws IOException {
271270
// 2. Requesting authorization :
272271
// http://doc.gitlab.com/ce/api/oauth2.html
@@ -298,7 +297,7 @@ public HttpResponse doCommenceLogin(
298297
gitlabWebUri + "/oauth/authorize?" + URLEncodedUtils.format(parameters, StandardCharsets.UTF_8));
299298
}
300299

301-
private String buildRedirectUrl(StaplerRequest request) throws MalformedURLException {
300+
private String buildRedirectUrl(StaplerRequest2 request) throws MalformedURLException {
302301
URL currentUrl = new URL(Jenkins.get().getRootUrl());
303302

304303
URL redirect_uri = new URL(
@@ -313,7 +312,7 @@ private String buildRedirectUrl(StaplerRequest request) throws MalformedURLExcep
313312
* This is where the user comes back to at the end of the OpenID redirect
314313
* ping-pong.
315314
*/
316-
public HttpResponse doFinishLogin(StaplerRequest request) throws IOException {
315+
public HttpResponse doFinishLogin(StaplerRequest2 request) throws IOException {
317316
String code = request.getParameter("code");
318317
String state = request.getParameter(STATE_ATTRIBUTE);
319318
String expectedState = (String) request.getSession().getAttribute(STATE_ATTRIBUTE);
@@ -394,7 +393,7 @@ public HttpResponse doFinishLogin(StaplerRequest request) throws IOException {
394393
new Mailer.UserProperty(auth.getMyself().getEmail()));
395394
}
396395
}
397-
SecurityListener.fireAuthenticated(new GitLabOAuthUserDetails(self, auth.getAuthorities()));
396+
SecurityListener.fireAuthenticated2(new GitLabOAuthUserDetails(self, auth.getAuthorities()));
398397
} catch (GitLabApiException e) {
399398
throw new RuntimeException(e);
400399
}
@@ -489,8 +488,8 @@ public Authentication authenticate(Authentication authentication) throws Authent
489488
new UserDetailsService() {
490489
@Override
491490
public UserDetails loadUserByUsername(String username)
492-
throws UsernameNotFoundException, DataAccessException {
493-
return GitLabSecurityRealm.this.loadUserByUsername(username);
491+
throws UsernameNotFoundException {
492+
return GitLabSecurityRealm.this.loadUserByUsername2(username);
494493
}
495494
});
496495
}
@@ -501,7 +500,7 @@ public String getLoginUrl() {
501500
}
502501

503502
@Override
504-
protected String getPostLogOutUrl(StaplerRequest req, Authentication auth) {
503+
protected String getPostLogOutUrl2(StaplerRequest2 req, Authentication auth) {
505504
// if we just redirect to the root and anonymous does not have Overall read then we will start a login all over
506505
// again.
507506
// we are actually anonymous here as the security context has been cleared
@@ -546,16 +545,15 @@ public DescriptorImpl getDescriptor() {
546545
/**
547546
* @param username
548547
* @throws UsernameNotFoundException
549-
* @throws DataAccessException
550548
*/
551549
@Override
552-
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
550+
public UserDetails loadUserByUsername2(String username) throws UsernameNotFoundException {
553551
GitLabAuthenticationToken authToken;
554552
if (SecurityContextHolder.getContext().getAuthentication() instanceof GitLabAuthenticationToken) {
555553
authToken = (GitLabAuthenticationToken)
556554
SecurityContextHolder.getContext().getAuthentication();
557555
} else {
558-
throw new UserMayOrMayNotExistException("Could not get auth token.");
556+
throw new UserMayOrMayNotExistException2("Could not get auth token.");
559557
}
560558

561559
try {
@@ -572,7 +570,7 @@ public UserDetails loadUserByUsername(String username) throws UsernameNotFoundEx
572570

573571
return userDetails;
574572
} catch (Error e) {
575-
throw new DataRetrievalFailureException("loadUserByUsername (username=" + username + ")", e);
573+
throw new AuthenticationServiceException("loadUserByUsername (username=" + username + ")", e);
576574
}
577575
}
578576

@@ -604,10 +602,9 @@ public int hashCode() {
604602
/**
605603
* @param groupName
606604
* @throws UsernameNotFoundException
607-
* @throws DataAccessException
608605
*/
609606
@Override
610-
public GroupDetails loadGroupByGroupname(String groupName) throws UsernameNotFoundException, DataAccessException {
607+
public GroupDetails loadGroupByGroupname2(String groupName, boolean fetchMembers) throws UsernameNotFoundException {
611608

612609
GitLabAuthenticationToken authToken =
613610
(GitLabAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();

0 commit comments

Comments
 (0)