4

I have bunch of Java apps running in Kubernetes. This is mainly legacy code with very big code base. The problem is almost all of the apps uses some third party libraries. It causes Java (or those libraries to be exact) to ignore http_proxy and https_proxy environment variables in Docker container. It ignores Java options as well.

Because of big code base and multiple dependencies it's miserable task to rewrite it to use http proxy properly. We reconfigured entire Jsoup related code in one app. That was easy enough to do but we have much more complex apps and even some without source code at all.

So, is there any way to make http proxy system wide AKA transparent to Java? Just force any http/s traffic via proxy.

I've tried numerous tricks and tweaks but nothing. Keep in mind I can't set global rule on k8s node. There are multiple apps which I don't want to use http proxy with.

Any help appreciated.

UPDATE:

Running Java with:

-Dhttp.proxyHost=my_proxy_ip -Dhttp.proxyPort=my_proxy_port

or:

-Djava.net.useSystemProxies=true

unfortunately doesn't work. Hardcoding like so:

System.setProperty("http.proxyHost", "someUrl");
System.setProperty("http.proxyPort", "somePort");

also fails.

This is because there are third party libraries like Jsoup. Unless we configure this library to use proxy it doesn't. Only Java-native http tools are using proxy properly with such settings.

UPDATE 2:

Even not desired I've tried hardcoding proxy in docker demon. That was just desperation and somebody suggested that so I did it just for testing purposes. Apparently all it does is... just setting up http_proxy environment variable inside each container. So back to beginning unfortunately.

This is clearly wrote in point 2:

2. When you create or start new containers, the environment variables are set automatically within the container.

here: https://docs.docker.com/network/proxy/ but in a hurry I lost half hour to learn this hard way.

Any ideas?

Grzegorz Pudłowski
  • 477
  • 1
  • 7
  • 14
  • 1
    Have you tried this? [How do I set the proxy to be used by the JVM](https://stackoverflow.com/questions/120797/how-do-i-set-the-proxy-to-be-used-by-the-jvm) – rustyx May 07 '20 at 13:29
  • 1
    @rustyx proposition is probably right. Java is a pain in the neck to use env vars. Sometimes it works, sometimes it does not. The only systematic way I figured out is to always specify them in the command line call. – A. Ocannaille May 07 '20 at 13:38
  • Thanks guys. Unfortunately I've tried everything from this post already and that's not the case. As I mentioned in the edit Java-nativ http tools use proxy correctly but third party libraries doesn't. Have you any idea why? – Grzegorz Pudłowski May 07 '20 at 14:01
  • Your update hardcoding seems strange, I would expect something like `System.setProperty("http.proxyHost", "someUrl");` – A. Ocannaille May 07 '20 at 14:08
  • Yep, I'm not a Java programmer. I've pasted the wrong code. I corrected myself. – Grzegorz Pudłowski May 07 '20 at 15:22
  • Did you set also **`https.proxyHost`** and port? – rustyx May 07 '20 at 15:39
  • 1
    Yes I did. The strange thing is when Java standard library http tools are used then all three methods works as expected. We have wrote super simple Java app. The only thing it does is to fetch http://ifconfig.co/ip to check if it's using proxy or not (proxy is on different public IP). When `java.net` is used it works but if we use e.g. `apache httpclient` or whatever else then it just ignores the config in any shape or form. – Grzegorz Pudłowski May 07 '20 at 16:04
  • @GrzegorzPudłowski have you found solution and made jvm work from containers behind proxy? – sergey.radov Dec 08 '20 at 18:22
  • Unfortunately no progress. Added support in Java code as walk-around. I haven't tried since then though. – Grzegorz Pudłowski Dec 09 '20 at 19:46

0 Answers0