Skip to content

Commit 744c7ac

Browse files
author
Chris Wiechmann
authored
Merge pull request #116 from Axway-API-Management-Plus/proxy-support
Adding support for an HTTP-Proxy
2 parents b2e799a + 608926a commit 744c7ac

6 files changed

Lines changed: 144 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1010
- Unicode API-Name was not shown correctly in the Backend-API overview (See issue [#113](https://github.com/Axway-API-Management-Plus/apim-cli/issues/113))
1111
- Application-Subscription not restored, when API is Republished to be updated (See issue [#114](https://github.com/Axway-API-Management-Plus/apim-cli/issues/114))
1212

13+
### Added
14+
- Support to use a proxy for the API-Manager communication (See issue [#109](https://github.com/Axway-API-Management-Plus/apim-cli/issues/109))
15+
1316
## [1.3.0] 2020-11-10
1417
### Added
1518
- Search for APIs based on configured Inbound- and Outbound-Security (See issue [#86](https://github.com/Axway-API-Management-Plus/apim-cli/issues/86))

modules/apim-adapter/src/main/java/com/axway/apim/lib/CoreCLIOptions.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ public Parameters getParams() throws AppException {
3838
// Also support -f for backwards compatibility
3939
if(!params.isForce()) params.setForce(Boolean.parseBoolean(getValue("f")));
4040

41+
params.setProxyHost(getValue("httpProxyHost"));
42+
params.setProxyPort((getValue("httpProxyPort")!=null) ? Integer.valueOf(getValue("httpProxyPort")) : null);
43+
params.setProxyUsername(getValue("httpProxyUsername"));
44+
params.setProxyPassword(getValue("httpProxyPassword"));
45+
4146
return (Parameters) params;
4247
}
4348

@@ -116,6 +121,26 @@ public void addOptions() {
116121
option.setRequired(false);
117122
option.setArgName("true");
118123
cliOptions.addInternalOption(option);
124+
125+
option = new Option("httpProxyHost", true, "Name of the proxy host");
126+
option.setRequired(false);
127+
option.setArgName("true");
128+
cliOptions.addInternalOption(option);
129+
130+
option = new Option("httpProxyPort", true, "The proxy port");
131+
option.setRequired(false);
132+
option.setArgName("true");
133+
cliOptions.addInternalOption(option);
134+
135+
option = new Option("httpProxyUsername", true, "The proxy username");
136+
option.setRequired(false);
137+
option.setArgName("true");
138+
cliOptions.addInternalOption(option);
139+
140+
option = new Option("httpProxyPassword", true, "The proxy username");
141+
option.setRequired(false);
142+
option.setArgName("true");
143+
cliOptions.addInternalOption(option);
119144
}
120145

121146
@Override

modules/apim-adapter/src/main/java/com/axway/apim/lib/CoreParameters.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ public static Mode valueOfDefault(String key) {
9090

9191
private String apimCLIHome;
9292

93+
private String proxyHost;
94+
95+
private Integer proxyPort;
96+
97+
private String proxyUsername;
98+
99+
private String proxyPassword;
100+
93101
public CoreParameters() {
94102
super();
95103
CoreParameters.instance = this;
@@ -358,6 +366,39 @@ public void setApimCLIHome(String apimCLIHome) {
358366
this.apimCLIHome = apimCLIHome;
359367
}
360368

369+
public String getProxyHost() {
370+
return proxyHost;
371+
}
372+
373+
public void setProxyHost(String proxyHost) {
374+
this.proxyHost = proxyHost;
375+
}
376+
377+
public Integer getProxyPort() {
378+
if(proxyPort == null) return -1;
379+
return proxyPort;
380+
}
381+
382+
public void setProxyPort(Integer proxyPort) {
383+
this.proxyPort = proxyPort;
384+
}
385+
386+
public String getProxyUsername() {
387+
return proxyUsername;
388+
}
389+
390+
public void setProxyUsername(String proxyUsername) {
391+
this.proxyUsername = proxyUsername;
392+
}
393+
394+
public String getProxyPassword() {
395+
return proxyPassword;
396+
}
397+
398+
public void setProxyPassword(String proxyPassword) {
399+
this.proxyPassword = proxyPassword;
400+
}
401+
361402
public List<CacheType> clearCaches() {
362403
if(getClearCache()==null) return null;
363404
if(cachesToClear!=null) return cachesToClear;

modules/apim-adapter/src/main/java/com/axway/apim/lib/utils/rest/APIMHttpClient.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,26 @@
55
import java.util.Map;
66

77
import org.apache.http.HttpHost;
8+
import org.apache.http.auth.AuthScope;
9+
import org.apache.http.auth.UsernamePasswordCredentials;
10+
import org.apache.http.client.CredentialsProvider;
811
import org.apache.http.client.HttpClient;
912
import org.apache.http.client.config.CookieSpecs;
1013
import org.apache.http.client.config.RequestConfig;
1114
import org.apache.http.client.protocol.HttpClientContext;
1215
import org.apache.http.config.Registry;
1316
import org.apache.http.config.RegistryBuilder;
1417
import org.apache.http.conn.routing.HttpRoute;
18+
import org.apache.http.conn.routing.HttpRoutePlanner;
1519
import org.apache.http.conn.socket.ConnectionSocketFactory;
1620
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
1721
import org.apache.http.conn.ssl.NoopHostnameVerifier;
1822
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
1923
import org.apache.http.conn.ssl.TrustAllStrategy;
2024
import org.apache.http.impl.client.BasicCookieStore;
25+
import org.apache.http.impl.client.BasicCredentialsProvider;
2126
import org.apache.http.impl.client.HttpClientBuilder;
27+
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
2228
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
2329
import org.apache.http.ssl.SSLContextBuilder;
2430

@@ -104,13 +110,27 @@ private void createConnection(String apiManagerURL) throws AppException {
104110
// We have make sure, that cookies are correclty parsed!
105111
RequestConfig defaultRequestConfig = RequestConfig.custom()
106112
.setCookieSpec(CookieSpecs.STANDARD).build();
107-
108-
this.httpClient = HttpClientBuilder.create()
113+
CoreParameters params = CoreParameters.getInstance();
114+
115+
HttpClientBuilder clientBuilder = HttpClientBuilder.create()
109116
.disableRedirectHandling()
110117
.setConnectionManager(cm)
111118
.useSystemProperties()
112-
.setDefaultRequestConfig(defaultRequestConfig)
113-
.build();
119+
.setDefaultRequestConfig(defaultRequestConfig);
120+
121+
// Check if a proxy is configured
122+
if(params.getProxyHost()!=null) {
123+
HttpHost proxyHost = new HttpHost(params.getProxyHost(), params.getProxyPort());
124+
HttpRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxyHost);
125+
clientBuilder.setRoutePlanner(routePlanner);
126+
if(params.getProxyUsername()!=null) {
127+
CredentialsProvider credentialsPovider = new BasicCredentialsProvider();
128+
credentialsPovider.setCredentials(new AuthScope(params.getProxyHost(), params.getPort()), new UsernamePasswordCredentials(params.getProxyUsername(), params.getProxyPassword()));
129+
clientBuilder.setDefaultCredentialsProvider(credentialsPovider);
130+
}
131+
}
132+
133+
this.httpClient = clientBuilder.build();
114134
} catch (Exception e) {
115135
throw new AppException("Can't create connection to API-Manager.", ErrorCode.API_MANAGER_COMMUNICATION);
116136
}

modules/apim-adapter/src/test/java/com/axway/lib/CoreCLIOptionsTest.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,44 @@ public void testPropertyFileWithoutStage() throws ParseException, AppException {
6060
Assert.assertEquals(params.getHostname(), "localhost");
6161
Assert.assertEquals(params.getProperties().get("myTestVariable"), "resolvedToSomething"); // from env.properties
6262
}
63+
64+
@Test
65+
public void testProxyParametersFromStage() throws ParseException, AppException {
66+
String[] args = {"-s", "stageWithProxy"};
67+
CLIOptions options = SampleCLIOptions.create(args);
68+
CoreParameters params = (CoreParameters) options.getParams();
69+
70+
Assert.assertEquals(params.getProxyHost(), "proxyHost");
71+
Assert.assertTrue(params.getProxyPort() == 8987);
72+
Assert.assertEquals(params.getProxyUsername(), "proxyUser");
73+
Assert.assertEquals(params.getProxyPassword(), "proxyPassword");
74+
}
75+
76+
@Test
77+
public void testProxyParams() throws ParseException, AppException {
78+
String[] args = {"-httpProxyHost", "myProxyHost", "-httpProxyPort", "6767", "-httpProxyUsername", "myProxyUser", "-httpProxyPassword", "myProxyPW"};
79+
CLIOptions options = SampleCLIOptions.create(args);
80+
CoreParameters params = (CoreParameters) options.getParams();
81+
82+
Assert.assertEquals(params.getProxyHost(), "myProxyHost");
83+
Assert.assertTrue(params.getProxyPort() == 6767);
84+
Assert.assertEquals(params.getProxyUsername(), "myProxyUser");
85+
Assert.assertEquals(params.getProxyPassword(), "myProxyPW");
86+
87+
// Test if port is not given
88+
String[] args2 = {"-httpProxyHost", "myProxyHost", "-httpProxyUsername", "myProxyUser", "-httpProxyPassword", "myProxyPW"};
89+
options = SampleCLIOptions.create(args2);
90+
params = (CoreParameters) options.getParams();
91+
Assert.assertTrue(params.getProxyPort() == -1);
92+
93+
// Test if Username / Password is not given
94+
String[] args3 = {"-httpProxyHost", "myProxyHost3", "-httpProxyPort", "1234"};
95+
options = SampleCLIOptions.create(args3);
96+
params = (CoreParameters) options.getParams();
97+
Assert.assertEquals(params.getProxyHost(), "myProxyHost3");
98+
Assert.assertTrue(params.getProxyPort() == 1234);
99+
Assert.assertNull(params.getProxyUsername());
100+
Assert.assertNull(params.getProxyPassword());
101+
102+
}
63103
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
admin_username=yetanotherUser
2+
admin_password=yetanotherPassword
3+
4+
yetAnotherProperty=HellImHere
5+
6+
OS_MAIN_AND_STAGE_ENV_PROPERTY=valueFromAnyOtherStageEnv
7+
8+
httpProxyHost=proxyHost
9+
httpProxyPort=8987
10+
httpProxyUsername=proxyUser
11+
httpProxyPassword=proxyPassword

0 commit comments

Comments
 (0)