diff --git a/pom.xml b/pom.xml index cf3660d..8d7abca 100644 --- a/pom.xml +++ b/pom.xml @@ -19,7 +19,7 @@ org.jenkins-ci.plugins plugin - 3.19 + 4.37 @@ -32,8 +32,10 @@ https://wiki.jenkins-ci.org/display/JENKINS/AWSEB+Deployment+Plugin - 7 - 2.121.3 + 2.289.3 + 8 + 1.8 + 1.8 @@ -91,20 +93,13 @@ apache_v2 - - maven-compiler-plugin - 3.2 - - 1.7 - 1.7 - - org.jenkins-ci.tools maven-hpi-plugin true - 0.0.4 + 1.45 + 8 @@ -114,24 +109,29 @@ scm:git:ssh://github.com/ingenieux/awseb-deployment-plugin.git scm:git:ssh://git@github.com/ingenieux/awseb-deployment-plugin.git https://wiki.jenkins-ci.org/display/JENKINS/AWSEB+Deployment+Plugin - awseb-deployment-plugin-0.3.8 + awseb-deployment-plugin-0.3.22 org.jenkins-ci.plugins aws-credentials - 1.23 + 189.v3551d5642995 org.jenkins-ci.plugins aws-java-sdk - 1.11.341 + 1.12.70 org.jenkins-ci.plugins token-macro - 2.1 + 2.15 + + + org.jenkins-ci + symbol-annotation + 1.23 org.jenkins-ci.main @@ -141,7 +141,7 @@ org.projectlombok lombok - 1.16.6 + 1.18.24 provided diff --git a/src/main/java/br/com/ingenieux/jenkins/plugins/awsebdeployment/AWSClientFactory.java b/src/main/java/br/com/ingenieux/jenkins/plugins/awsebdeployment/AWSClientFactory.java index 3fd9f18..0b0b81e 100644 --- a/src/main/java/br/com/ingenieux/jenkins/plugins/awsebdeployment/AWSClientFactory.java +++ b/src/main/java/br/com/ingenieux/jenkins/plugins/awsebdeployment/AWSClientFactory.java @@ -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(); diff --git a/src/main/java/br/com/ingenieux/jenkins/plugins/awsebdeployment/AWSEBDeploymentBuilder.java b/src/main/java/br/com/ingenieux/jenkins/plugins/awsebdeployment/AWSEBDeploymentBuilder.java index d077ba6..d8566f8 100644 --- a/src/main/java/br/com/ingenieux/jenkins/plugins/awsebdeployment/AWSEBDeploymentBuilder.java +++ b/src/main/java/br/com/ingenieux/jenkins/plugins/awsebdeployment/AWSEBDeploymentBuilder.java @@ -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) { diff --git a/src/main/java/br/com/ingenieux/jenkins/plugins/awsebdeployment/AWSEBDeploymentCredentials.java b/src/main/java/br/com/ingenieux/jenkins/plugins/awsebdeployment/AWSEBDeploymentCredentials.java new file mode 100644 index 0000000..c5cd4ea --- /dev/null +++ b/src/main/java/br/com/ingenieux/jenkins/plugins/awsebdeployment/AWSEBDeploymentCredentials.java @@ -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); + } +} diff --git a/src/main/java/br/com/ingenieux/jenkins/plugins/awsebdeployment/DeployerRunner.java b/src/main/java/br/com/ingenieux/jenkins/plugins/awsebdeployment/DeployerRunner.java index ba25418..3893a09 100644 --- a/src/main/java/br/com/ingenieux/jenkins/plugins/awsebdeployment/DeployerRunner.java +++ b/src/main/java/br/com/ingenieux/jenkins/plugins/awsebdeployment/DeployerRunner.java @@ -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(); diff --git a/src/main/java/br/com/ingenieux/jenkins/plugins/awsebdeployment/Utils.java b/src/main/java/br/com/ingenieux/jenkins/plugins/awsebdeployment/Utils.java index ad8722f..602bd8e 100644 --- a/src/main/java/br/com/ingenieux/jenkins/plugins/awsebdeployment/Utils.java +++ b/src/main/java/br/com/ingenieux/jenkins/plugins/awsebdeployment/Utils.java @@ -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; diff --git a/src/main/java/br/com/ingenieux/jenkins/plugins/awsebdeployment/cmd/DeployerCommand.java b/src/main/java/br/com/ingenieux/jenkins/plugins/awsebdeployment/cmd/DeployerCommand.java index c79486f..289c122 100644 --- a/src/main/java/br/com/ingenieux/jenkins/plugins/awsebdeployment/cmd/DeployerCommand.java +++ b/src/main/java/br/com/ingenieux/jenkins/plugins/awsebdeployment/cmd/DeployerCommand.java @@ -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)); diff --git a/src/main/java/br/com/ingenieux/jenkins/plugins/awsebdeployment/cmd/DeployerContext.java b/src/main/java/br/com/ingenieux/jenkins/plugins/awsebdeployment/cmd/DeployerContext.java index 1d09aac..7ab70d6 100644 --- a/src/main/java/br/com/ingenieux/jenkins/plugins/awsebdeployment/cmd/DeployerContext.java +++ b/src/main/java/br/com/ingenieux/jenkins/plugins/awsebdeployment/cmd/DeployerContext.java @@ -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; + /** *

* Environment Id