Skip to content

Commit e643e56

Browse files
committed
refactor: use common vertx proxy options factory
BREAKING CHANGE: this version requires APIM in version 3.18 and upper
1 parent e27b04d commit e643e56

File tree

4 files changed

+46
-62
lines changed

4 files changed

+46
-62
lines changed

README.adoc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ processing.
3434
The result of the callout is placed in a variable called `calloutResponse` and is only available during policy
3535
execution. If no variable is configured the result of the callout is no longer available.
3636

37+
== Compatibility with APIM
38+
39+
|===
40+
|Plugin version | APIM version
41+
42+
|2.x and upper | 3.18.x to latest
43+
|1.15.x and upper | 3.15.x to 3.17.x
44+
|1.13.x to 1.14.x | 3.10.x to 3.14.x
45+
|Up to 1.12.x | Up to 3.9.x
46+
|===
47+
3748
== Configuration
3849

3950
|===

pom.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@
3636
<properties>
3737
<gravitee-bom.version>1.0</gravitee-bom.version>
3838
<gravitee-gateway-api.version>1.31.0</gravitee-gateway-api.version>
39+
<gravitee-node.version>1.23.0</gravitee-node.version>
3940
<gravitee-policy-api.version>1.10.0</gravitee-policy-api.version>
40-
<gravitee-common.version>1.25.0</gravitee-common.version>
41+
<gravitee-common.version>1.26.1</gravitee-common.version>
4142
<gravitee-expression-language.version>1.8.0</gravitee-expression-language.version>
4243
<json-schema-generator-maven-plugin.version>1.3.0</json-schema-generator-maven-plugin.version>
4344
<json-schema-generator-maven-plugin.outputDirectory>${project.build.directory}/schemas</json-schema-generator-maven-plugin.outputDirectory>
@@ -69,6 +70,13 @@
6970
<scope>provided</scope>
7071
</dependency>
7172

73+
<dependency>
74+
<groupId>io.gravitee.node</groupId>
75+
<artifactId>gravitee-node-api</artifactId>
76+
<version>${gravitee-node.version}</version>
77+
<scope>provided</scope>
78+
</dependency>
79+
7280
<dependency>
7381
<groupId>io.gravitee.el</groupId>
7482
<artifactId>gravitee-expression-language</artifactId>

src/main/java/io/gravitee/policy/callout/CalloutHttpPolicy.java

Lines changed: 17 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package io.gravitee.policy.callout;
1717

18+
import static io.gravitee.common.util.VertxProxyOptionsUtils.*;
19+
1820
import io.gravitee.el.TemplateEngine;
1921
import io.gravitee.gateway.api.ExecutionContext;
2022
import io.gravitee.gateway.api.Request;
@@ -24,6 +26,7 @@
2426
import io.gravitee.gateway.api.stream.BufferedReadWriteStream;
2527
import io.gravitee.gateway.api.stream.ReadWriteStream;
2628
import io.gravitee.gateway.api.stream.SimpleReadWriteStream;
29+
import io.gravitee.node.api.configuration.Configuration;
2730
import io.gravitee.policy.api.PolicyChain;
2831
import io.gravitee.policy.api.PolicyResult;
2932
import io.gravitee.policy.api.annotations.OnRequest;
@@ -33,18 +36,13 @@
3336
import io.gravitee.policy.callout.configuration.CalloutHttpPolicyConfiguration;
3437
import io.gravitee.policy.callout.configuration.PolicyScope;
3538
import io.vertx.core.Future;
36-
import io.vertx.core.Handler;
3739
import io.vertx.core.Vertx;
3840
import io.vertx.core.buffer.Buffer;
3941
import io.vertx.core.http.*;
40-
import io.vertx.core.net.ProxyOptions;
41-
import io.vertx.core.net.ProxyType;
4242
import java.net.URI;
43-
import java.util.Objects;
4443
import java.util.function.Consumer;
4544
import org.slf4j.Logger;
4645
import org.slf4j.LoggerFactory;
47-
import org.springframework.core.env.Environment;
4846

4947
/**
5048
* @author David BRASSELY (david.brassely at graviteesource.com)
@@ -59,16 +57,16 @@ public class CalloutHttpPolicy {
5957
private static final String CALLOUT_EXIT_ON_ERROR = "CALLOUT_EXIT_ON_ERROR";
6058
private static final String CALLOUT_HTTP_ERROR = "CALLOUT_HTTP_ERROR";
6159

62-
/**
63-
* The associated configuration to this CalloutHttp Policy
64-
*/
65-
private CalloutHttpPolicyConfiguration configuration;
66-
6760
private static final String TEMPLATE_VARIABLE = "calloutResponse";
6861

6962
private static final String REQUEST_TEMPLATE_VARIABLE = "request";
7063
private static final String RESPONSE_TEMPLATE_VARIABLE = "response";
7164

65+
/**
66+
* The associated configuration to this CalloutHttp Policy
67+
*/
68+
private final CalloutHttpPolicyConfiguration configuration;
69+
7270
/**
7371
* Create a new CalloutHttp Policy instance based on its associated configuration
7472
*
@@ -196,8 +194,15 @@ private void doCallout(ExecutionContext context, Consumer<Void> onSuccess, Consu
196194
}
197195

198196
if (configuration.isUseSystemProxy()) {
199-
Environment env = context.getComponent(Environment.class);
200-
options.setProxyOptions(getSystemProxyOptions(env));
197+
Configuration configuration = context.getComponent(Configuration.class);
198+
try {
199+
setSystemProxy(options, configuration);
200+
} catch (IllegalStateException e) {
201+
LOGGER.warn(
202+
"CalloutHttp requires a system proxy to be defined but some configurations are missing or not well defined: {}. Ignoring proxy",
203+
e.getMessage()
204+
);
205+
}
201206
}
202207

203208
HttpClient httpClient = vertx.createHttpClient(options);
@@ -332,43 +337,6 @@ private void handleFailure(Consumer<Void> onSuccess, Consumer<PolicyResult> onEr
332337
httpClient.close();
333338
}
334339

335-
private ProxyOptions getSystemProxyOptions(Environment environment) {
336-
StringBuilder errors = new StringBuilder();
337-
ProxyOptions proxyOptions = new ProxyOptions();
338-
339-
// System proxy must be well configured. Check that this is the case.
340-
if (environment.containsProperty("system.proxy.host")) {
341-
proxyOptions.setHost(environment.getProperty("system.proxy.host"));
342-
} else {
343-
errors.append("'system.proxy.host' ");
344-
}
345-
346-
try {
347-
proxyOptions.setPort(Integer.parseInt(Objects.requireNonNull(environment.getProperty("system.proxy.port"))));
348-
} catch (Exception e) {
349-
errors.append("'system.proxy.port' [").append(environment.getProperty("system.proxy.port")).append("] ");
350-
}
351-
352-
try {
353-
proxyOptions.setType(ProxyType.valueOf(environment.getProperty("system.proxy.type")));
354-
} catch (Exception e) {
355-
errors.append("'system.proxy.type' [").append(environment.getProperty("system.proxy.type")).append("] ");
356-
}
357-
358-
proxyOptions.setUsername(environment.getProperty("system.proxy.username"));
359-
proxyOptions.setPassword(environment.getProperty("system.proxy.password"));
360-
361-
if (errors.length() == 0) {
362-
return proxyOptions;
363-
} else {
364-
LOGGER.warn(
365-
"CalloutHttp requires a system proxy to be defined but some configurations are missing or not well defined: {}. Ignoring proxy",
366-
errors
367-
);
368-
return null;
369-
}
370-
}
371-
372340
private HttpMethod convert(io.gravitee.common.http.HttpMethod httpMethod) {
373341
switch (httpMethod) {
374342
case CONNECT:

src/test/java/io/gravitee/policy/CalloutHttpPolicyTest.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import io.gravitee.gateway.api.Request;
3636
import io.gravitee.gateway.api.RequestWrapper;
3737
import io.gravitee.gateway.api.Response;
38+
import io.gravitee.node.api.configuration.Configuration;
3839
import io.gravitee.policy.api.PolicyChain;
3940
import io.gravitee.policy.api.PolicyResult;
4041
import io.gravitee.policy.callout.CalloutHttpPolicy;
@@ -53,7 +54,6 @@
5354
import org.junit.runner.RunWith;
5455
import org.mockito.Mock;
5556
import org.mockito.junit.MockitoJUnitRunner;
56-
import org.springframework.core.env.Environment;
5757

5858
/**
5959
* @author David BRASSELY (david.brassely at graviteesource.com)
@@ -80,13 +80,13 @@ public class CalloutHttpPolicyTest {
8080
private CalloutHttpPolicyConfiguration configuration;
8181

8282
@Mock
83-
private Environment env;
83+
private Configuration nodeConfiguration;
8484

8585
@Before
8686
public void init() {
87-
reset(configuration, executionContext, request, response, env);
87+
reset(configuration, executionContext, request, response, nodeConfiguration);
8888
when(executionContext.getComponent(Vertx.class)).thenReturn(Vertx.vertx());
89-
when(executionContext.getComponent(Environment.class)).thenReturn(env);
89+
when(executionContext.getComponent(io.gravitee.node.api.configuration.Configuration.class)).thenReturn(nodeConfiguration);
9090
when(executionContext.getTemplateEngine()).thenReturn(new SpelTemplateEngineFactory().templateEngine());
9191

9292
Request request = new RequestWrapper(mock(Request.class)) {
@@ -147,9 +147,6 @@ public void shouldProcessRequest_withProxy() throws Exception {
147147
when(configuration.isUseSystemProxy()).thenReturn(true);
148148
when(configuration.getUrl()).thenReturn("http://localhost:" + wireMockRule.port() + "/");
149149

150-
when(env.containsProperty(anyString())).thenReturn(true);
151-
when(env.getProperty(anyString())).thenReturn("localhost-proxy", "3129", "HTTP", "null", "null");
152-
153150
final CountDownLatch lock = new CountDownLatch(1);
154151
this.policyChain = spy(new CountDownPolicyChain(lock));
155152

@@ -158,11 +155,11 @@ public void shouldProcessRequest_withProxy() throws Exception {
158155
lock.await(1000, TimeUnit.MILLISECONDS);
159156

160157
verify(policyChain).doNext(request, response);
161-
verify(env).getProperty("system.proxy.port");
162-
verify(env).getProperty("system.proxy.type");
163-
verify(env).getProperty("system.proxy.host");
164-
verify(env).getProperty("system.proxy.username");
165-
verify(env).getProperty("system.proxy.password");
158+
verify(nodeConfiguration).getProperty("system.proxy.port");
159+
verify(nodeConfiguration).getProperty("system.proxy.type");
160+
verify(nodeConfiguration).getProperty("system.proxy.host");
161+
verify(nodeConfiguration).getProperty("system.proxy.username");
162+
verify(nodeConfiguration).getProperty("system.proxy.password");
166163
}
167164

168165
@Test

0 commit comments

Comments
 (0)