Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lookup credentials on server in case job runs on remote node #104

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>3.19</version>
<version>4.37</version>
<relativePath />
</parent>

@@ -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>
@@ -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>
@@ -114,24 +109,29 @@
<connection>scm:git:ssh://github.com/ingenieux/awseb-deployment-plugin.git</connection>
<developerConnection>scm:git:ssh://git@github.com/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>
@@ -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>
Original file line number Diff line number Diff line change
@@ -56,28 +56,36 @@ 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,
ProxyConfiguration proxy) {
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 (proxy != null && proxy.getName() != null) {
clientConfig.setProxyHost(proxy.getName());
clientConfig.setProxyPort(proxy.getPort());
if (proxy.getUserName() != null) {
clientConfig.setProxyUsername(proxy.getUserName());
}
if(proxy.getSecretPassword() != null) {
clientConfig.setProxyPassword(proxy.getSecretPassword().getPlainText());
}
}

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();

@@ -88,7 +96,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();

Original file line number Diff line number Diff line change
@@ -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;
@@ -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;
@@ -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) {
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package br.com.ingenieux.jenkins.plugins.awsebdeployment;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import lombok.Data;

import java.io.Serializable;

@Data
public class AWSEBDeploymentCredentials implements Serializable {
private static final long serialVersionUID = 1L;

public AWSEBDeploymentCredentials(String awsAccessKeyId, String awsSecretKey) {
this.awsAccessKeyId = awsAccessKeyId;
this.awsSecretKey = awsSecretKey;
}

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

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

public AWSCredentials toAWSCredentials() {
return new BasicAWSCredentials(awsAccessKeyId, awsSecretKey);
}
}
Original file line number Diff line number Diff line change
@@ -17,19 +17,24 @@
package br.com.ingenieux.jenkins.plugins.awsebdeployment;

import br.com.ingenieux.jenkins.plugins.awsebdeployment.cmd.DeployerContext;
import com.amazonaws.auth.AWSCredentials;
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;
@@ -38,8 +43,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;
@@ -49,8 +53,21 @@ 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);
}

AWSCredentials awsCredentials = provider.getCredentials();
if(awsCredentials == null) {
throw new IllegalStateException("Could not determine AWS credentials.");
}
AWSEBDeploymentCredentials credentials = new AWSEBDeploymentCredentials(awsCredentials.getAWSAccessKeyId(), awsCredentials.getAWSSecretKey());

ProxyConfiguration proxy = Jenkins.get().getProxy();
DeployerContext deployerContext = new DeployerContext(config, rootFileObject, listener, credentials, proxy);

final VirtualChannel channel = launcher.getChannel();

Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -19,12 +19,16 @@
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.AWSCredentials;
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;
import com.amazonaws.util.VersionInfoUtils;
import com.google.common.collect.Sets;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.ProxyConfiguration;
import hudson.Util;
import lombok.Data;
import lombok.experimental.Delegate;
@@ -138,11 +142,13 @@ public boolean perform() throws Exception {
public static class InitAWS extends DeployerCommand {
@Override
public boolean perform() throws Exception {
AWSClientFactory factory;
AWSCredentials credentials = c.getCredentials().toAWSCredentials();
AWSStaticCredentialsProvider provider = new AWSStaticCredentialsProvider(credentials);

factory = AWSClientFactory.getClientFactory(getConfig().getCredentialId(), getConfig().getAwsRegion());
String region = getConfig().getAwsRegion();
AWSClientFactory factory = AWSClientFactory.getClientFactory(provider, region, c.getProxy());

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

setS3(factory.getService(AmazonS3Client.class));
setAwseb(factory.getService(AWSElasticBeanstalkClient.class));
Original file line number Diff line number Diff line change
@@ -17,10 +17,12 @@
package br.com.ingenieux.jenkins.plugins.awsebdeployment.cmd;

import br.com.ingenieux.jenkins.plugins.awsebdeployment.AWSEBDeploymentConfig;
import br.com.ingenieux.jenkins.plugins.awsebdeployment.AWSEBDeploymentCredentials;
import br.com.ingenieux.jenkins.plugins.awsebdeployment.Constants;
import com.amazonaws.services.elasticbeanstalk.AWSElasticBeanstalk;
import com.amazonaws.services.s3.AmazonS3;
import hudson.FilePath;
import hudson.ProxyConfiguration;
import hudson.model.TaskListener;
import lombok.Data;

@@ -36,6 +38,11 @@ public class DeployerContext implements Constants, Serializable {
*/
final AWSEBDeploymentConfig config;

/**
* Deployer Credentials
*/
final AWSEBDeploymentCredentials credentials;

/**
* Root File Object
*/
@@ -46,10 +53,16 @@ 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,
AWSEBDeploymentCredentials credentials,
ProxyConfiguration proxy) {
this.config = config;
this.rootFileObject = rootFileObject;
this.listener = listener;
this.credentials = credentials;
this.proxy = proxy;
}

/**
@@ -67,6 +80,11 @@ public DeployerContext(AWSEBDeploymentConfig config, FilePath rootFileObject, Ta
*/
transient PrintStream logger;

/**
* Proxy Configuration
*/
ProxyConfiguration proxy;

/**
* <p>
* Environment Id