Skip to content

Commit

Permalink
adding proxy setting support from env to be more like other cli's
Browse files Browse the repository at this point in the history
didn't added authenticated proxy support since that would be a much more intrusive change..
  • Loading branch information
Damick committed Aug 4, 2015
1 parent 60aef58 commit 5d55bb6
Showing 1 changed file with 64 additions and 25 deletions.
89 changes: 64 additions & 25 deletions cli/src/main/java/denominator/cli/Denominator.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,6 @@
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;

import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.jce.provider.X509CertParser;
import org.bouncycastle.openssl.PEMKeyPair;
import org.bouncycastle.openssl.PEMParser;
import org.yaml.snakeyaml.Yaml;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.lang.reflect.Type;
import java.security.KeyFactory;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.inject.Singleton;

import dagger.ObjectGraph;
import dagger.Provides;
import denominator.Credentials;
Expand Down Expand Up @@ -86,6 +61,30 @@
import io.airlift.airline.Help;
import io.airlift.airline.Option;
import io.airlift.airline.OptionType;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.jce.provider.X509CertParser;
import org.bouncycastle.openssl.PEMKeyPair;
import org.bouncycastle.openssl.PEMParser;
import org.yaml.snakeyaml.Yaml;

import javax.inject.Singleton;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.lang.reflect.Type;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.KeyFactory;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import static com.google.common.base.Preconditions.checkArgument;
import static denominator.CredentialsConfiguration.credentials;
Expand Down Expand Up @@ -274,6 +273,8 @@ public static abstract class DenominatorCommand implements Runnable {

@SuppressWarnings("unchecked")
public void run() {
setProxyFromEnv();

if (providerName != null && credentialArgs != null) {
credentials = ListCredentials.from(Lists.transform(credentialArgs, decodeAnyPems));
} else if (providerConfigurationName != null) {
Expand Down Expand Up @@ -426,6 +427,44 @@ void overrideFromEnv(Map<String, String> env) {
}
}

static void setProxyFromEnv() {
String envHttpProxy = System.getenv("HTTP_PROXY");
if (envHttpProxy != null && !envHttpProxy.isEmpty()) {
try {
URL httpProxyUrl = new URL(envHttpProxy);

String httpProxyHost = System.getProperty("http.proxyHost");
if ((httpProxyHost == null || httpProxyHost.isEmpty())) {
System.setProperty("http.proxyHost", httpProxyUrl.getHost());
System.setProperty("http.proxyPort",
Integer.toString(
httpProxyUrl.getPort() == -1 ? httpProxyUrl.getDefaultPort() : httpProxyUrl.getPort()));
}
} catch (MalformedURLException e) {
System.err.println("invalid http proxy configuration: " + e.getMessage());
System.exit(1);
}
}

String envHttpsProxy = System.getenv("HTTPS_PROXY");
if (envHttpsProxy != null && !envHttpsProxy.isEmpty()) {
try {
URL httpsProxyUrl = new URL(envHttpsProxy);

String httpsProxyHost = System.getProperty("https.proxyHost");
if ((httpsProxyHost == null || httpsProxyHost.isEmpty())) {
System.setProperty("https.proxyHost", httpsProxyUrl.getHost());
System.setProperty("https.proxyPort",
Integer.toString(
httpsProxyUrl.getPort() == -1 ? httpsProxyUrl.getDefaultPort() : httpsProxyUrl.getPort()));
}
} catch (MalformedURLException e) {
System.err.println("invalid https proxy configuration: " + e.getMessage());
System.exit(1);
}
}
}

/**
* return a lazy iterator where possible to improve the perceived responsiveness of the cli
*/
Expand Down

0 comments on commit 5d55bb6

Please sign in to comment.