Skip to content

Commit

Permalink
Lookup credentials on server in case job runs on remote node
Browse files Browse the repository at this point in the history
On a remote node the configuration or credential store
of Jenkins is not available. This gathers all values
before hand and provides them via the context.
  • Loading branch information
Christian Mötzing committed Apr 29, 2022
1 parent ca7814e commit 80e9d4c
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 49 deletions.
34 changes: 17 additions & 17 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>3.19</version>
<version>4.37</version>
<relativePath />
</parent>

Expand All @@ -32,8 +32,10 @@
<url>https://wiki.jenkins-ci.org/display/JENKINS/AWSEB+Deployment+Plugin</url>

<properties>
<java.level>7</java.level>
<jenkins.version>2.121.3</jenkins.version>
<jenkins.version>2.289.3</jenkins.version>
<java.level>8</java.level>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<licenses>
Expand Down Expand Up @@ -91,20 +93,13 @@
<licenseName>apache_v2</licenseName>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.jenkins-ci.tools</groupId>
<artifactId>maven-hpi-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<compatibleSinceVersion>0.0.4</compatibleSinceVersion>
<compatibleSinceVersion>1.45</compatibleSinceVersion>
<minimumJavaVersion>8</minimumJavaVersion>
</configuration>
</plugin>
</plugins>
Expand All @@ -114,24 +109,29 @@
<connection>scm:git:ssh://github.com/ingenieux/awseb-deployment-plugin.git</connection>
<developerConnection>scm:git:ssh://[email protected]/ingenieux/awseb-deployment-plugin.git</developerConnection>
<url>https://wiki.jenkins-ci.org/display/JENKINS/AWSEB+Deployment+Plugin</url>
<tag>awseb-deployment-plugin-0.3.8</tag>
<tag>awseb-deployment-plugin-0.3.22</tag>
</scm>

<dependencies>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>aws-credentials</artifactId>
<version>1.23</version>
<version>189.v3551d5642995</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.11.341</version>
<version>1.12.70</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>token-macro</artifactId>
<version>2.1</version>
<version>2.15</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci</groupId>
<artifactId>symbol-annotation</artifactId>
<version>1.23</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.main</groupId>
Expand All @@ -141,7 +141,7 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.6</version>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.cloudbees.plugins.credentials.CredentialsMatchers;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.domains.DomainRequirement;
import hudson.ProxyConfiguration;
import hudson.security.ACL;
import jenkins.model.Jenkins;
import org.apache.commons.lang.reflect.ConstructorUtils;
Expand Down Expand Up @@ -56,28 +55,34 @@ private AWSClientFactory(AWSCredentialsProvider provider, ClientConfiguration cl
this.region = region.toLowerCase();
}

private static AWSClientFactory getClientFactory(AWSCredentialsProvider provider,
String awsRegion) {
public static AWSClientFactory getClientFactory(AWSCredentialsProvider provider, String awsRegion,
String proxyHost, int proxyPort,
String proxyUser, String proxyPassword) {
ClientConfiguration clientConfig = new ClientConfiguration();

Jenkins jenkins = Jenkins.get();

if (jenkins.proxy != null) {
ProxyConfiguration proxyConfig = jenkins.proxy;
clientConfig.setProxyHost(proxyConfig.name);
clientConfig.setProxyPort(proxyConfig.port);
if (proxyConfig.getUserName() != null) {
clientConfig.setProxyUsername(proxyConfig.getUserName());
clientConfig.setProxyPassword(proxyConfig.getPassword());
if (proxyHost != null) {
clientConfig.setProxyHost(proxyHost);
clientConfig.setProxyPort(proxyPort);
if (proxyUser != null) {
clientConfig.setProxyUsername(proxyUser);
clientConfig.setProxyPassword(proxyPassword);
}
}

return getClientFactory(provider,awsRegion, clientConfig);
}

public static AWSClientFactory getClientFactory(AWSCredentialsProvider provider, String awsRegion) {
return getClientFactory(provider,awsRegion, new ClientConfiguration());
}

private static AWSClientFactory getClientFactory(AWSCredentialsProvider provider, String awsRegion, ClientConfiguration clientConfig) {
clientConfig.setUserAgentPrefix("ingenieux CloudButler/" + Utils.getVersion());

return new AWSClientFactory(provider, clientConfig, awsRegion);
}

public static AWSClientFactory getClientFactory(String credentialsId, String awsRegion)
protected static AWSClientFactory getClientFactory(String credentialsId, String awsRegion)
throws CredentialNotFoundException {
AWSCredentialsProvider provider = new DefaultAWSCredentialsProviderChain();

Expand All @@ -88,7 +93,7 @@ public static AWSClientFactory getClientFactory(String credentialsId, String aws
return getClientFactory(provider, awsRegion);
}

private static AmazonWebServicesCredentials lookupNamedCredential(String credentialsId)
protected static AmazonWebServicesCredentials lookupNamedCredential(String credentialsId)
throws CredentialNotFoundException {
final Jenkins jenkins = Jenkins.getInstanceOrNull();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
Expand All @@ -69,7 +66,6 @@
*/
@SuppressWarnings({"unchecked", "deprecation"})
public class AWSEBDeploymentBuilder extends Builder implements SimpleBuildStep {
private static final Logger LOGGER = LoggerFactory.getLogger(AWSEBDeploymentBuilder.class);

@Getter
private AWSEBDeploymentConfig config;
Expand Down Expand Up @@ -237,8 +233,7 @@ public String getCredentialId() {
}

@Override
public void perform(@Nonnull Run<?, ?> build, @Nonnull FilePath ws, @Nonnull Launcher launcher,
@Nonnull TaskListener listener) throws IOException {
public void perform(Run<?, ?> build, FilePath ws, Launcher launcher, TaskListener listener) throws IOException {
try {
new DeployerRunner(build, ws, launcher, listener, this).perform();
} catch (Exception exc) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,23 @@
package br.com.ingenieux.jenkins.plugins.awsebdeployment;

import br.com.ingenieux.jenkins.plugins.awsebdeployment.cmd.DeployerContext;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
import hudson.FilePath;
import hudson.Launcher;
import hudson.ProxyConfiguration;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.remoting.Future;
import hudson.remoting.VirtualChannel;
import jenkins.model.Jenkins;
import org.jenkinsci.plugins.tokenmacro.MacroEvaluationException;

import java.io.IOException;

public class DeployerRunner {
private final Run<?, ?> build;
import static org.apache.commons.lang.StringUtils.isNotBlank;

public class DeployerRunner {
private final Launcher launcher;

private final TaskListener listener;
Expand All @@ -38,8 +42,7 @@ public class DeployerRunner {

private final AWSEBDeploymentConfig config;

DeployerRunner(Run<?, ?> build, FilePath ws, Launcher launcher, TaskListener listener, AWSEBDeploymentBuilder deploymentBuilder) throws InterruptedException, MacroEvaluationException, IOException {
this.build = build;
DeployerRunner(Run<?, ?> build, FilePath ws, Launcher launcher, TaskListener listener, AWSEBDeploymentBuilder deploymentBuilder) throws InterruptedException, IOException, MacroEvaluationException {
this.launcher = launcher;
this.listener = listener;
this.workspace = ws;
Expand All @@ -49,8 +52,24 @@ public class DeployerRunner {
public boolean perform() throws Exception {
FilePath rootFileObject = new FilePath(this.workspace, config.getRootObject());

final DeployerContext
deployerContext = new DeployerContext(config, rootFileObject, listener);
AWSCredentialsProvider provider = new DefaultAWSCredentialsProviderChain();

String credentialsId = config.getCredentialId();
if (isNotBlank(credentialsId)) {
provider = AWSClientFactory.lookupNamedCredential(credentialsId);
}

String keyId=provider.getCredentials().getAWSAccessKeyId();
String secretKey=provider.getCredentials().getAWSSecretKey();

DeployerContext deployerContext;
ProxyConfiguration proxy = Jenkins.get().getProxy();
if(proxy == null) {
deployerContext = new DeployerContext(config, rootFileObject, listener, keyId, secretKey);
} else {
deployerContext = new DeployerContext(config, rootFileObject, listener, keyId, secretKey,
proxy.getName(), proxy.getPort(), proxy.getUserName(), proxy.getSecretPassword().getPlainText());
}

final VirtualChannel channel = launcher.getChannel();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@
package br.com.ingenieux.jenkins.plugins.awsebdeployment;

import hudson.FilePath;
import hudson.model.AbstractBuild;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.tokenmacro.MacroEvaluationException;
import org.jenkinsci.plugins.tokenmacro.TokenMacro;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import br.com.ingenieux.jenkins.plugins.awsebdeployment.AWSClientFactory;
import br.com.ingenieux.jenkins.plugins.awsebdeployment.Constants;
import br.com.ingenieux.jenkins.plugins.awsebdeployment.Utils;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.elasticbeanstalk.AWSElasticBeanstalkClient;
import com.amazonaws.services.elasticbeanstalk.model.*;
import com.amazonaws.services.s3.AmazonS3Client;
Expand Down Expand Up @@ -138,11 +140,14 @@ public boolean perform() throws Exception {
public static class InitAWS extends DeployerCommand {
@Override
public boolean perform() throws Exception {
AWSClientFactory factory;
BasicAWSCredentials credentials = new BasicAWSCredentials(c.getAwsAccessKeyId(), c.getAwsSecretKey());
AWSStaticCredentialsProvider provider = new AWSStaticCredentialsProvider(credentials);

factory = AWSClientFactory.getClientFactory(getConfig().getCredentialId(), getConfig().getAwsRegion());
String region = getConfig().getAwsRegion();
AWSClientFactory factory = AWSClientFactory.getClientFactory(provider, region,
c.getProxyHost(), c.getProxyPort(), c.getProxyUsername(), c.getProxyPassword());

log("Using region: '%s'", getConfig().getAwsRegion());
log("Using region: '%s'", region);

setS3(factory.getService(AmazonS3Client.class));
setAwseb(factory.getService(AWSElasticBeanstalkClient.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,21 @@ public class DeployerContext implements Constants, Serializable {
*/
final TaskListener listener;

public DeployerContext(AWSEBDeploymentConfig config, FilePath rootFileObject, TaskListener listener) {
public DeployerContext(AWSEBDeploymentConfig config, FilePath rootFileObject, TaskListener listener, String awsAccessKeyId, String awsSecretKey) {
this.config = config;
this.rootFileObject = rootFileObject;
this.listener = listener;
this.awsAccessKeyId =awsAccessKeyId;
this.awsSecretKey=awsSecretKey;
}

public DeployerContext(AWSEBDeploymentConfig config, FilePath rootFileObject, TaskListener listener, String awsAccessKeyId, String awsSecretKey,
String proxyHost, int proxyPort, String proxyUsername, String proxyPassword) {
this(config, rootFileObject, listener, awsAccessKeyId, awsSecretKey);
this.proxyHost=proxyHost;
this.proxyPort=proxyPort;
this.proxyUsername =proxyUsername;
this.proxyPassword=proxyPassword;
}

/**
Expand Down Expand Up @@ -93,6 +104,36 @@ public DeployerContext(AWSEBDeploymentConfig config, FilePath rootFileObject, Ta
*/
String versionDescription;

/**
* Access Key ID of credential
*/
String awsAccessKeyId;

/**
* Secret Key of credential
*/
String awsSecretKey;

/**
* Proxy host name
*/
String proxyHost;

/**
* Proxy port
*/
int proxyPort;

/**
* Proxy auth username
*/
String proxyUsername;

/**
* Proxy auth password
*/
String proxyPassword;

/**
* Successfull?
*/
Expand Down

0 comments on commit 80e9d4c

Please sign in to comment.