Skip to content

Commit

Permalink
[JENKINS-21494] Added server credentials also in the settings provide…
Browse files Browse the repository at this point in the history
…d via environment variables.
  • Loading branch information
obillard committed Jan 27, 2014
1 parent 7ecd9f9 commit 33a81d3
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
Expand Up @@ -39,9 +39,14 @@ of this software and associated documentation files (the "Software"), to deal
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.lib.configprovider.ConfigProvider;
import org.jenkinsci.lib.configprovider.model.Config;
import org.jenkinsci.plugins.configfiles.maven.GlobalMavenSettingsConfig;
import org.jenkinsci.plugins.configfiles.maven.security.CredentialsHelper;
import org.jenkinsci.plugins.configfiles.maven.security.HasServerCredentialMappings;
import org.jenkinsci.plugins.tokenmacro.MacroEvaluationException;
import org.jenkinsci.plugins.tokenmacro.TokenMacro;

import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;

public class ManagedFileUtil {

/**
Expand Down Expand Up @@ -109,16 +114,39 @@ public static Map<ManagedFile, FilePath> provisionConfigFiles(List<ManagedFile>
}
target = new FilePath(build.getWorkspace(), expandedTargetLocation);
}

// Inserts Maven server credentials if config files are Maven settings
String fileContent = insertCredentialsInSettings(build, configFile);


listener.getLogger().println(Messages.console_output(configFile.name, target.toURI()));
ByteArrayInputStream bs = new ByteArrayInputStream(configFile.content.getBytes());
ByteArrayInputStream bs = new ByteArrayInputStream(fileContent.getBytes());
target.copyFrom(bs);
file2Path.put(managedFile, target);
}

return file2Path;
}

private static String insertCredentialsInSettings(AbstractBuild<?, ?> build, Config configFile) throws IOException {
String fileContent = configFile.content;

if (configFile instanceof HasServerCredentialMappings) {
HasServerCredentialMappings settings = (HasServerCredentialMappings) configFile;
final Map<String, StandardUsernameCredentials> resolvedCredentials = CredentialsHelper.resolveCredentials(build.getProject(), settings.getServerCredentialMappings());

if (!resolvedCredentials.isEmpty()) {
try {
fileContent = CredentialsHelper.fillAuthentication(fileContent, resolvedCredentials);
} catch (Exception exception) {
throw new IOException("[ERROR] could not insert credentials into the settings file", exception);
}
}
}

return fileContent;
}

private static ConfigProvider getProviderForConfigId(String id) {
if (!StringUtils.isBlank(id)) {
for (ConfigProvider provider : ConfigProvider.all()) {
Expand Down
Expand Up @@ -33,10 +33,11 @@ of this software and associated documentation files (the "Software"), to deal
import org.jenkinsci.lib.configprovider.model.Config;
import org.jenkinsci.lib.configprovider.model.ContentType;
import org.jenkinsci.plugins.configfiles.Messages;
import org.jenkinsci.plugins.configfiles.maven.security.HasServerCredentialMappings;
import org.jenkinsci.plugins.configfiles.maven.security.ServerCredentialMapping;
import org.kohsuke.stapler.DataBoundConstructor;

public class GlobalMavenSettingsConfig extends Config {
public class GlobalMavenSettingsConfig extends Config implements HasServerCredentialMappings {
private static final long serialVersionUID = 1L;

private List<ServerCredentialMapping> serverCredentialMappings;
Expand Down
Expand Up @@ -34,10 +34,11 @@ of this software and associated documentation files (the "Software"), to deal
import org.jenkinsci.lib.configprovider.model.Config;
import org.jenkinsci.lib.configprovider.model.ContentType;
import org.jenkinsci.plugins.configfiles.Messages;
import org.jenkinsci.plugins.configfiles.maven.security.HasServerCredentialMappings;
import org.jenkinsci.plugins.configfiles.maven.security.ServerCredentialMapping;
import org.kohsuke.stapler.DataBoundConstructor;

public class MavenSettingsConfig extends Config {
public class MavenSettingsConfig extends Config implements HasServerCredentialMappings {
private static final long serialVersionUID = 1L;

private List<ServerCredentialMapping> serverCredentialMappings;
Expand Down
@@ -0,0 +1,13 @@
package org.jenkinsci.plugins.configfiles.maven.security;

import java.util.List;

/**
* Implemented by {@code Config} instances that contain server credential
* mappings (Maven settings).
*/
public interface HasServerCredentialMappings {

public abstract List<ServerCredentialMapping> getServerCredentialMappings();

}

0 comments on commit 33a81d3

Please sign in to comment.