Skip to content

Commit

Permalink
[JENKINS-29211] E200015: ISVNAuthentication provider did not provide …
Browse files Browse the repository at this point in the history
…credentials
  • Loading branch information
recena committed Jul 3, 2015
1 parent ad260ce commit 73c3c5c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
27 changes: 25 additions & 2 deletions src/main/java/hudson/scm/PerJobCredentialStore.java
Expand Up @@ -4,18 +4,25 @@
import hudson.model.AbstractProject;
import hudson.model.Saveable;
import hudson.remoting.Channel;
import hudson.scm.SubversionSCM.ModuleLocation;
import hudson.scm.SubversionSCM.DescriptorImpl.Credential;
import hudson.scm.SubversionSCM.DescriptorImpl.RemotableSVNAuthenticationProvider;
import jenkins.model.Jenkins;

import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNURL;

import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.CredentialsStore;
import com.cloudbees.plugins.credentials.common.StandardCredentials;

import java.io.File;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Map;
import java.util.logging.Logger;

import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;

/**
* Persists the credential per job. This object is remotable.
Expand Down Expand Up @@ -105,8 +112,24 @@ private Object writeReplace() {
private static final ThreadLocal<Boolean> IS_SAVING = new ThreadLocal<Boolean>();

/*package*/ void migrateCredentials(SubversionSCM.DescriptorImpl descriptor) throws IOException {
CredentialsStore store = CredentialsProvider.lookupStores(project).iterator().next();
for (Map.Entry<String, Credential> e : credentials.entrySet()) {
descriptor.migrateCredentials(project, e.getKey(), e.getValue());
StandardCredentials credential = descriptor.migrateCredentials(store, e.getKey(), e.getValue());
int i = 0;
boolean found = false;
ModuleLocation[] locations = ((SubversionSCM) project.getScm()).getLocations();
while (i < locations.length && !found) {
try {
if (e.getKey().contains(locations[i].getSVNURL().getHost())) {
locations[i].setCredentialsId(credential.getId());
found = true;
}
i++;
} catch (SVNException ex) {
// Should not happen, but...
LOGGER.log(WARNING, "Repository location with a malformed URL: " + locations[i].remote, ex);
}
}
}
}
}
25 changes: 15 additions & 10 deletions src/main/java/hudson/scm/SubversionSCM.java
Expand Up @@ -1621,8 +1621,9 @@ public void load() {
BulkChange bc = new BulkChange(this);
try {
mayHaveLegacyPerJobCredentials = true;
CredentialsStore store = CredentialsProvider.lookupStores(Jenkins.getInstance()).iterator().next();
for (Map.Entry<String, Credential> e : credentials.entrySet()) {
migrateCredentials(Jenkins.getInstance(), e.getKey(), e.getValue());
migrateCredentials(store, e.getKey(), e.getValue());
}
save();
bc.commit();
Expand All @@ -1648,9 +1649,9 @@ public void load() {
if (jobCredentials.isFile()) {
try {
new PerJobCredentialStore(job).migrateCredentials(this);
job.save();
if (!jobCredentials.delete()) {
LOGGER.log(Level.WARNING, "Could not remove legacy per-job credentials store file: {0}",
jobCredentials);
LOGGER.log(Level.WARNING, "Could not remove legacy per-job credentials store file: {0}", jobCredentials);
allOk = false;
}
} catch (IOException e) {
Expand All @@ -1663,9 +1664,8 @@ public void load() {
save();
}

/*package*/ StandardCredentials migrateCredentials(ModelObject context, String legacyRealm, Credential legacyCredential)
/*package*/ StandardCredentials migrateCredentials(CredentialsStore store, String legacyRealm, Credential legacyCredential)
throws IOException {
CredentialsStore store = CredentialsProvider.lookupStores(context).iterator().next();
StandardCredentials credential = legacyCredential.toCredentials(null, legacyRealm);
if (credential != null) {
return credential;
Expand Down Expand Up @@ -1792,8 +1792,7 @@ public SVNAuthentication createSVNAuthentication(String kind) {

@Override
public StandardCredentials toCredentials(String description) {
return new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, null, description, userName,
getPassword());
return new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, null, description, userName, getPassword());
}

@Override
Expand All @@ -1803,8 +1802,7 @@ public StandardCredentials toCredentials(ModelObject context, String description
findItemGroup(context),
ACL.SYSTEM,
Collections.<DomainRequirement>emptyList())) {
if (userName.equals(c.getUsername())
&& getPassword().equals(c.getPassword().getPlainText())) {
if (userName.equals(c.getUsername()) && getPassword().equals(c.getPassword().getPlainText())) {
return c;
}
}
Expand Down Expand Up @@ -2622,7 +2620,7 @@ public static final class ModuleLocation extends AbstractDescribableImpl<ModuleL
/**
* The credentials to checkout with.
*/
public final String credentialsId;
public String credentialsId;

/**
* Remembers the user-given value.
Expand Down Expand Up @@ -2661,6 +2659,13 @@ public ModuleLocation(String remote, String local) {
this(remote, null, local, null, false);
}

/**
* Sets the credentials identifier.
*/
void setCredentialsId (final String id) {
credentialsId = id;
}

/**
* Constructor to support backwards compatibility.
*/
Expand Down

0 comments on commit 73c3c5c

Please sign in to comment.