0

I want to run my Java code to read Azure KeyVault with proxy in Windows server.

I've gone through many posts but could find any working solution. Mostly given for c# but I want for Java. My code is working fine in my local machine but when I'm trying to run same code in Pre-Prod Windows server where I need to set Proxy is not working.

AuthenticationContext context = null;
        AuthenticationResult result = null;
        ExecutorService service = null;
        try {

            service = Executors.newFixedThreadPool(1);
            context = new AuthenticationContext(authorization, false, service);
        //added below 2 lines but don't see any effect
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.server.com", 80));
        context.setProxy(proxy);

            ClientCredential credentials = new ClientCredential(clientId, clientKey);
            Future<AuthenticationResult> future = context.acquireToken(
                    resource, credentials, null);
            result = future.get();

When I'm running the code in my local machine it is running fine with and without proxy setting but in that Windows server it say "Unknown host" exception.

San4musa
  • 277
  • 2
  • 12
  • This is a very strange phenomenon. Could you please check in the windows server to see if you can get any reply from `proxy.server.com`? – Jack Jia Nov 07 '19 at 09:01
  • that proxy server is working fine for other Rest API call like below `String urlString = "https://myapiurl/api"; Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.server.com", 80)); HttpURLConnection conn = (HttpURLConnection) new URL(urlString).openConnection(proxy); ` Without proxy above code is not working from that windows server. So Azure Keyvault read java code should have worked same way but alas! – San4musa Nov 07 '19 at 15:24

1 Answers1

0

I am not sure if the following would help, but you can have a try.

You can try to find the direct IP of the proxy, and use it in your code:

InetAddress[] allByName = InetAddress.getAllByName("proxy.server.com");
for (InetAddress address : allByName) {
    System.out.println(address.getHostAddress());
}

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(allByName[0].getHostAddress(),80););

Or

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(InetAddress.getByName("proxy.server.com"),80));

Maybe the direct IP may work.

Jack Jia
  • 5,268
  • 1
  • 12
  • 14
  • I just checked Azure code is not even reaching that proxy code. Before reaching there, it is failing with "unknown host" from that windows server but in my local machine it is working with and without proxy. Not sure how to provide proxy info to Azure connection code before it reach to context creation portion where I'm adding proxy details. – San4musa Nov 08 '19 at 18:16