Skip to content

Commit

Permalink
feat: Support for HTTP proxy environment variables
Browse files Browse the repository at this point in the history
Fixes #368
  • Loading branch information
Jan Wedel authored and mthmulders committed May 7, 2024
1 parent 4b84a0c commit 7c2280f
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 3 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 73,10 @@ Next, point MCS to that trust store like so
mcs -Djavax.net.ssl.trustStore=/path/to/keystore search something
```
### Usage Behind a Proxy
If you are running behind a proxy, MCS will respect the `HTTP_PROXY` and `HTTPS_PROXY` environment variables.
## Configuring MCS
Some configuration for MCS is passed through system properties.
You can do this every time you invoke MCS by adding `-Dxxx=yyy`.
Expand Down Expand Up @@ -104,4 108,4 @@ If you have a new idea, feel free to bring it up using the [discussions](https:/
* [Andres Almiray](https://andresalmiray.com/) did a great job helping me set up [JReleaser](https://jreleaser.org/) to make releasing **mcs** a real breeze!
* [Martin Goldhahn](https://github.com/maddingo) shared the idea of searching on class name, and provided some initial API and design ideas for this great feature.
* [Willem van Lent](https://github.com/Ocirina) contributed a fix for a parameter whose name didn't match it's behavior.
* [Hanno Embregts](https://hanno.codes/) contributed a fix that makes it more clear why the results are truncated.
* [Hanno Embregts](https://hanno.codes/) contributed a fix that makes it more clear why the results are truncated.
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 116,7 @@
</dependency>
</dependencies>
<configuration>
<argLine>--add-opens java.base/java.util=ALL-UNNAMED --add-opens java.base/java.lang=ALL-UNNAMED</argLine>
<outputFormats>
<format>stryker-dashboard</format>
</outputFormats>
Expand Down Expand Up @@ -177,6 178,13 @@
</goals>
</execution>
</executions>
<configuration>
<!-- This is necessary to change environment variables in tests in JDK 17 -->
<argLine>
--add-opens java.base/java.util=ALL-UNNAMED
--add-opens java.base/java.lang=ALL-UNNAMED
</argLine>
</configuration>
</plugin>

<plugin>
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/it/mulders/mcs/App.java
Original file line number Diff line number Diff line change
@@ -1,5 1,8 @@
package it.mulders.mcs;

import java.net.URI;
import java.net.URISyntaxException;

import it.mulders.mcs.cli.Cli;
import it.mulders.mcs.cli.CommandClassFactory;
import it.mulders.mcs.cli.SystemPropertyLoader;
Expand All @@ -18,6 21,7 @@ static int doMain(final String... originalArgs) {

static int doMain(final Cli cli, final SystemPropertyLoader systemPropertyLoader, final String... originalArgs) {
System.setProperties(systemPropertyLoader.getProperties());
setUpProxy();
var program = new CommandLine(cli, new CommandClassFactory(cli))
.setExecutionExceptionHandler(new McsExecutionExceptionHandler());

Expand All @@ -28,6 32,28 @@ static int doMain(final Cli cli, final SystemPropertyLoader systemPropertyLoader
return program.execute(args);
}

private static void setUpProxy() {
var httpProxy = System.getenv( "HTTP_PROXY" );
var httpsProxy = System.getenv( "HTTPS_PROXY" );

try {
if ( httpProxy != null && !httpProxy.isEmpty() ) {
final URI uri = new URI( httpProxy );

System.setProperty( "http.proxyHost", uri.getHost() );
System.setProperty( "http.proxyPort", Integer.toString( uri.getPort() ) );
}

if ( httpsProxy != null && !httpsProxy.isEmpty() ) {
final URI uri = new URI( httpsProxy );
System.setProperty( "https.proxyHost", uri.getHost() );
System.setProperty( "https.proxyPort", Integer.toString( uri.getPort() ) );
}
} catch ( URISyntaxException e ) {
System.err.println("Error while setting up proxy from environment: HTTP_PROXY=[%s], HTTPS_PROXY=[%s]".formatted( httpProxy, httpsProxy ) );
}
}

static boolean isInvocationWithoutSearchCommand(CommandLine program, String... args) {
try {
program.parseArgs(args);
Expand Down
49 changes: 47 additions & 2 deletions src/test/java/it/mulders/mcs/AppIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 3,20 @@
import it.mulders.mcs.cli.Cli;
import it.mulders.mcs.cli.SystemPropertyLoader;
import org.assertj.core.api.WithAssertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Properties;

import static com.github.stefanbirkner.systemlambda.SystemLambda.tapSystemOut;
import static com.github.stefanbirkner.systemlambda.SystemLambda.withEnvironmentVariable;
import static java.util.Arrays.asList;

@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
class AppIT implements WithAssertions {
class AppIT implements WithAssertions {
private final Cli command = new Cli() {
@Override
public SearchCommand createSearchCommand() {
Expand All @@ -32,6 36,15 @@ public Integer call() {
};
}
};

@BeforeEach
void clearProxyProperties() {
System.clearProperty("http.proxyHost");
System.clearProperty("http.proxyPort");
System.clearProperty("https.proxyHost");
System.clearProperty("https.proxyPort");
}

@Test
void should_show_version() throws Exception {
var output = tapSystemOut(() -> App.doMain("-V"));
Expand Down Expand Up @@ -68,4 81,36 @@ public Properties getProperties() {

assertThat(System.getProperty("example")).isEqualTo("value");
}
}

@Test
void should_not_set_proxy_system_properties_when_no_env_variable_is_present() throws Exception {
List<String> values = withEnvironmentVariable("HTTP_PROXY", null)
.and("HTTPS_PROXY", null)
.execute(() -> {
App.doMain(command, new SystemPropertyLoader(), "info.picocli:picocli");

return asList(System.getProperty("http.proxyHost"),
System.getProperty("http.proxyPort"),
System.getProperty("https.proxyHost"),
System.getProperty("https.proxyPort"));
});

assertThat(values).isEqualTo(asList(null, null, null, null));
}

@Test
void should_set_proxy_system_properties_when_env_variables_are_present() throws Exception {
List<String> values = withEnvironmentVariable("HTTP_PROXY", "http://http.proxy.example.com:8080")
.and("HTTPS_PROXY", "http://https.proxy.example.com:8484")
.execute(() -> {
App.doMain(command, new SystemPropertyLoader(), "info.picocli:picocli");

return asList(System.getProperty("http.proxyHost"),
System.getProperty("http.proxyPort"),
System.getProperty("https.proxyHost"),
System.getProperty("https.proxyPort"));
});

assertThat(values).isEqualTo(asList("http.proxy.example.com", "8080", "https.proxy.example.com", "8484"));
}
}

0 comments on commit 7c2280f

Please sign in to comment.