diff --git a/src/main/java/com/amazon/jenkins/ec2fleet/AWSUtils.java b/src/main/java/com/amazon/jenkins/ec2fleet/AWSUtils.java index 96abbb1a..e945b40e 100644 --- a/src/main/java/com/amazon/jenkins/ec2fleet/AWSUtils.java +++ b/src/main/java/com/amazon/jenkins/ec2fleet/AWSUtils.java @@ -1,14 +1,53 @@ package com.amazon.jenkins.ec2fleet; import com.amazonaws.ClientConfiguration; +import hudson.ProxyConfiguration; +import jenkins.model.Jenkins; + +import java.net.InetSocketAddress; +import java.net.MalformedURLException; +import java.net.Proxy; +import java.net.URL; public final class AWSUtils { private static final String USER_AGENT_PREFIX = "ec2-fleet-plugin"; - public static ClientConfiguration getClientConfiguration() { + /** + * Create {@link ClientConfiguration} for AWS-SDK with proper inited + * {@link ClientConfiguration#getUserAgentPrefix()} and proxy if + * Jenkins configured to use proxy + * + * @param endpoint real endpoint which need to be called, + * required to find if proxy configured to bypass some of hosts + * and real host in that whitelist + * @return client configuration + */ + public static ClientConfiguration getClientConfiguration(final String endpoint) { final ClientConfiguration clientConfiguration = new ClientConfiguration(); clientConfiguration.setUserAgentPrefix(USER_AGENT_PREFIX); + + final ProxyConfiguration proxyConfig = Jenkins.getActiveInstance().proxy; + if (proxyConfig != null) { + Proxy proxy; + try { + proxy = proxyConfig.createProxy(new URL(endpoint).getHost()); + } catch (MalformedURLException e) { + // no to fix it here, so just skip + proxy = proxyConfig.createProxy(endpoint); + } + + if (!proxy.equals(Proxy.NO_PROXY) && proxy.address() instanceof InetSocketAddress) { + InetSocketAddress address = (InetSocketAddress) proxy.address(); + clientConfiguration.setProxyHost(address.getHostName()); + clientConfiguration.setProxyPort(address.getPort()); + if (null != proxyConfig.getUserName()) { + clientConfiguration.setProxyUsername(proxyConfig.getUserName()); + clientConfiguration.setProxyPassword(proxyConfig.getPassword()); + } + } + } + return clientConfiguration; } diff --git a/src/main/java/com/amazon/jenkins/ec2fleet/CloudFormationApi.java b/src/main/java/com/amazon/jenkins/ec2fleet/CloudFormationApi.java index 08d2c888..35276c50 100644 --- a/src/main/java/com/amazon/jenkins/ec2fleet/CloudFormationApi.java +++ b/src/main/java/com/amazon/jenkins/ec2fleet/CloudFormationApi.java @@ -27,7 +27,7 @@ public class CloudFormationApi { public AmazonCloudFormation connect(final String awsCredentialsId, final String regionName, final String endpoint) { - final ClientConfiguration clientConfiguration = AWSUtils.getClientConfiguration(); + final ClientConfiguration clientConfiguration = AWSUtils.getClientConfiguration(endpoint); final AmazonWebServicesCredentials credentials = AWSCredentialsHelper.getCredentials(awsCredentialsId, Jenkins.getInstance()); final AmazonCloudFormation client = credentials != null ? diff --git a/src/main/java/com/amazon/jenkins/ec2fleet/EC2Api.java b/src/main/java/com/amazon/jenkins/ec2fleet/EC2Api.java index 50451535..c57aaa4e 100644 --- a/src/main/java/com/amazon/jenkins/ec2fleet/EC2Api.java +++ b/src/main/java/com/amazon/jenkins/ec2fleet/EC2Api.java @@ -162,7 +162,7 @@ public void tagInstances(final AmazonEC2 ec2, final Set instanceIds, fin } public AmazonEC2 connect(final String awsCredentialsId, final String regionName, final String endpoint) { - final ClientConfiguration clientConfiguration = AWSUtils.getClientConfiguration(); + final ClientConfiguration clientConfiguration = AWSUtils.getClientConfiguration(endpoint); final AmazonWebServicesCredentials credentials = AWSCredentialsHelper.getCredentials(awsCredentialsId, Jenkins.getInstance()); final AmazonEC2Client client = credentials != null ? diff --git a/src/main/java/com/amazon/jenkins/ec2fleet/fleet/AutoScalingGroupFleet.java b/src/main/java/com/amazon/jenkins/ec2fleet/fleet/AutoScalingGroupFleet.java index f8c8c1de..41087cc6 100644 --- a/src/main/java/com/amazon/jenkins/ec2fleet/fleet/AutoScalingGroupFleet.java +++ b/src/main/java/com/amazon/jenkins/ec2fleet/fleet/AutoScalingGroupFleet.java @@ -102,7 +102,7 @@ public Map getStateBatch(String awsCredentialsId, Strin private AmazonAutoScalingClient createClient( final String awsCredentialsId, final String regionName, final String endpoint) { final AmazonWebServicesCredentials credentials = AWSCredentialsHelper.getCredentials(awsCredentialsId, Jenkins.getInstance()); - final ClientConfiguration clientConfiguration = AWSUtils.getClientConfiguration(); + final ClientConfiguration clientConfiguration = AWSUtils.getClientConfiguration(endpoint); final AmazonAutoScalingClient client = new AmazonAutoScalingClient(credentials, clientConfiguration); final String effectiveEndpoint = getEndpoint(regionName, endpoint); if (effectiveEndpoint != null) client.setEndpoint(effectiveEndpoint); diff --git a/src/test/java/com/amazon/jenkins/ec2fleet/AWSUtilsIntegrationTest.java b/src/test/java/com/amazon/jenkins/ec2fleet/AWSUtilsIntegrationTest.java new file mode 100644 index 00000000..ad883716 --- /dev/null +++ b/src/test/java/com/amazon/jenkins/ec2fleet/AWSUtilsIntegrationTest.java @@ -0,0 +1,53 @@ +package com.amazon.jenkins.ec2fleet; + +import com.amazonaws.ClientConfiguration; +import hudson.ProxyConfiguration; +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.jvnet.hudson.test.JenkinsRule; + +public class AWSUtilsIntegrationTest { + + private static final int PROXY_PORT = 8888; + private static final String PROXY_HOST = "localhost"; + + @Rule + public JenkinsRule j = new JenkinsRule(); + + @Test + public void getClientConfiguration_when_no_proxy_returns_configuration_without_proxy() { + j.jenkins.proxy = null; + ClientConfiguration clientConfiguration = AWSUtils.getClientConfiguration("somehost"); + Assert.assertNull(clientConfiguration.getProxyHost()); + } + + @Test + public void getClientConfiguration_when_proxy_returns_configuration_with_proxy() { + j.jenkins.proxy = new ProxyConfiguration(PROXY_HOST, PROXY_PORT); + ClientConfiguration clientConfiguration = AWSUtils.getClientConfiguration("somehost"); + Assert.assertEquals(PROXY_HOST, clientConfiguration.getProxyHost()); + Assert.assertEquals(PROXY_PORT, clientConfiguration.getProxyPort()); + Assert.assertNull(clientConfiguration.getProxyUsername()); + Assert.assertNull(clientConfiguration.getProxyPassword()); + } + + @Test + public void getClientConfiguration_when_proxy_with_credentials_returns_configuration_with_proxy() { + j.jenkins.proxy = new ProxyConfiguration(PROXY_HOST, PROXY_PORT, "a", "b"); + ClientConfiguration clientConfiguration = AWSUtils.getClientConfiguration("somehost"); + Assert.assertEquals(PROXY_HOST, clientConfiguration.getProxyHost()); + Assert.assertEquals(PROXY_PORT, clientConfiguration.getProxyPort()); + Assert.assertEquals("a", clientConfiguration.getProxyUsername()); + Assert.assertEquals("b", clientConfiguration.getProxyPassword()); + } + + @Test + public void getClientConfiguration_when_endpoint_is_invalid_url_use_it_as_is() { + j.jenkins.proxy = new ProxyConfiguration(PROXY_HOST, PROXY_PORT); + ClientConfiguration clientConfiguration = AWSUtils.getClientConfiguration("rumba"); + Assert.assertEquals(PROXY_HOST, clientConfiguration.getProxyHost()); + Assert.assertEquals(PROXY_PORT, clientConfiguration.getProxyPort()); + } + +} diff --git a/src/test/java/com/amazon/jenkins/ec2fleet/IntegrationTest.java b/src/test/java/com/amazon/jenkins/ec2fleet/IntegrationTest.java index 203d368e..36d469c5 100644 --- a/src/test/java/com/amazon/jenkins/ec2fleet/IntegrationTest.java +++ b/src/test/java/com/amazon/jenkins/ec2fleet/IntegrationTest.java @@ -88,7 +88,6 @@ public abstract class IntegrationTest { protected static final long JOB_SLEEP_TIME = 30; - @ClassRule public static BuildWatcher bw = new BuildWatcher();