Skip to content

Commit 76c9769

Browse files
committed
Fix migrating settings from plugin 0.20 to 0.21+
fixes [JENKINS-29473]
1 parent 9f73b6b commit 76c9769

File tree

1 file changed

+41
-17
lines changed

1 file changed

+41
-17
lines changed

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

+41-17
Original file line numberDiff line numberDiff line change
@@ -33,30 +33,40 @@ of this software and associated documentation files (the "Software"), to deal
3333
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
3434
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
3535
import hudson.Extension;
36-
import hudson.ProxyConfiguration;
37-
import hudson.Util;
3836
import hudson.model.Descriptor;
37+
import hudson.model.listeners.ItemListener;
3938
import hudson.model.User;
39+
import hudson.ProxyConfiguration;
4040
import hudson.security.GroupDetails;
4141
import hudson.security.SecurityRealm;
4242
import hudson.security.UserMayOrMayNotExistException;
4343
import hudson.tasks.Mailer;
44+
import hudson.Util;
45+
import java.io.IOException;
46+
import java.lang.reflect.InvocationTargetException;
47+
import java.lang.reflect.Method;
48+
import java.net.InetSocketAddress;
49+
import java.net.Proxy;
50+
import java.util.HashSet;
51+
import java.util.logging.Level;
52+
import java.util.logging.Logger;
53+
import java.util.Set;
4454
import jenkins.model.Jenkins;
4555
import org.acegisecurity.Authentication;
4656
import org.acegisecurity.AuthenticationException;
4757
import org.acegisecurity.AuthenticationManager;
4858
import org.acegisecurity.BadCredentialsException;
4959
import org.acegisecurity.context.SecurityContextHolder;
60+
import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
5061
import org.acegisecurity.userdetails.UserDetails;
5162
import org.acegisecurity.userdetails.UserDetailsService;
5263
import org.acegisecurity.userdetails.UsernameNotFoundException;
53-
import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
5464
import org.apache.commons.httpclient.URIException;
55-
import org.apache.http.HttpEntity;
56-
import org.apache.http.HttpHost;
5765
import org.apache.http.client.methods.HttpPost;
5866
import org.apache.http.client.methods.HttpUriRequest;
5967
import org.apache.http.conn.params.ConnRoutePNames;
68+
import org.apache.http.HttpEntity;
69+
import org.apache.http.HttpHost;
6070
import org.apache.http.impl.client.DefaultHttpClient;
6171
import org.apache.http.util.EntityUtils;
6272
import org.jfree.util.Log;
@@ -72,17 +82,6 @@ of this software and associated documentation files (the "Software"), to deal
7282
import org.springframework.dao.DataAccessException;
7383
import org.springframework.dao.DataRetrievalFailureException;
7484

75-
import java.io.IOException;
76-
import java.lang.reflect.InvocationTargetException;
77-
import java.lang.reflect.Method;
78-
import java.net.InetSocketAddress;
79-
import java.net.Proxy;
80-
import java.util.HashSet;
81-
import java.util.Set;
82-
import java.util.logging.Logger;
83-
84-
import static java.util.logging.Level.*;
85-
8685
/**
8786
*
8887
* Implementation of the AbstractPasswordBasedSecurityRealm that uses github
@@ -479,7 +478,7 @@ private void fireAuthenticated(UserDetails details) {
479478
} catch (IllegalAccessException e) {
480479
throw (Error)new IllegalAccessError(e.getMessage()).initCause(e);
481480
} catch (InvocationTargetException e) {
482-
LOGGER.log(WARNING, "Failed to invoke fireAuthenticated", e);
481+
LOGGER.log(Level.WARNING, "Failed to invoke fireAuthenticated", e);
483482
}
484483
}
485484

@@ -663,6 +662,31 @@ public GroupDetails loadGroupByGroupname(String groupName)
663662
}
664663
}
665664

665+
/*
666+
Migrate settings from 0.20 to 0.21+
667+
*/
668+
@Extension
669+
public static final class Migrator extends ItemListener {
670+
@SuppressWarnings("deprecation")
671+
@Override
672+
public void onLoaded() {
673+
try {
674+
Jenkins instance = Jenkins.getInstance();
675+
if(instance.getSecurityRealm() instanceof GithubSecurityRealm) {
676+
GithubSecurityRealm myRealm = (GithubSecurityRealm) instance.getSecurityRealm();
677+
if(myRealm.getOauthScopes() == null) {
678+
GithubSecurityRealm newRealm = new GithubSecurityRealm(myRealm.getGithubWebUri(), myRealm.getGithubApiUri(), myRealm.getClientID(), myRealm.getClientSecret());
679+
instance.setSecurityRealm(newRealm);
680+
instance.save();
681+
}
682+
}
683+
}
684+
catch(IOException e) {
685+
LOGGER.log(Level.WARNING, "could not migrate GithubSecurityRealm", e);
686+
}
687+
}
688+
}
689+
666690
/**
667691
* Logger for debugging purposes.
668692
*/

0 commit comments

Comments
 (0)