Skip to content

Commit

Permalink
#166 Jenkins proxy settings support
Browse files Browse the repository at this point in the history
  • Loading branch information
Artem Stasiuk committed Jan 21, 2020
1 parent b77ce1b commit 26ec7ff
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 5 deletions.
41 changes: 40 additions & 1 deletion src/main/java/com/amazon/jenkins/ec2fleet/AWSUtils.java
Original file line number Diff line number Diff line change
@@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/amazon/jenkins/ec2fleet/EC2Api.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public void tagInstances(final AmazonEC2 ec2, final Set<String> 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 ?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public Map<String, FleetStateStats> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ public abstract class IntegrationTest {

protected static final long JOB_SLEEP_TIME = 30;


@ClassRule
public static BuildWatcher bw = new BuildWatcher();

Expand Down

0 comments on commit 26ec7ff

Please sign in to comment.