Azure / azure-storage-java

Microsoft Azure Storage Library for Java
https://docs.microsoft.com/en-us/java/api/overview/azure/storage
MIT License
189 stars 165 forks source link

How to use an authenticated proxy with azure-storage-blob 11.0.1. #555

Open sesu-bio opened 3 years ago

sesu-bio commented 3 years ago

Which service(blob, file, queue, table) does this issue concern?

blob

Which version of the SDK was used?

I am working on a project which uses azure-storage-blob 11.0.1. Upgrading should be avoided if possible.

What problem was encountered?

The project needs to have proxy support added including proxy authentication. I managed to get an unauthenticated proxy server working. However, it is unclear to me how to supply credentials. I have seen #307 which mentions a default authenticator but it doesn't seem to work for the version I am working with. #307 mentions OperationContext for configuring the proxy which doesn't work for me either. Here is my code which includes the things I tried that didn't work.

/*
// configures the proxy for Javas HttpURLConnection but not Azure
System.setProperty("https.proxyHost", PROXY_HOST);
System.setProperty("https.proxyPort", PROXY_PORT);

// no effect, no option for authentication
OperationContext.setDefaultProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(PROXY_HOST, PROXY_PORT_INT)));

// no effect
System.setProperty("https.proxyUser", PROXY_USER);
System.setProperty("https.proxyPassword", PROXY_PASS);
System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");

// authentication which works for Javas HttpURLConnection but not Azure
Authenticator.setDefault(new Authenticator() {
    @Override
    public PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(PROXY_USER, PROXY_PASS.toCharArray());
    }
});
*/

// works for configuring the proxy for Azure but no option for authentication
PipelineOptions pipeLineOptions = new PipelineOptions();
pipeLineOptions.withClient(HttpClient.createDefault(new HttpClientConfiguration(
        new Proxy(Proxy.Type.HTTP, new InetSocketAddress(PROXY_HOST, PROXY_PORT_INT)))));

// try to download the file
BlockBlobURL blockBlobUrl = new BlockBlobURL(url, BlockBlobURL.createPipeline(pipeLineOptions));
blockBlobUrl.download().flatMapCompletable(response -> {
    AsynchronousFileChannel channel = AsynchronousFileChannel.open(Paths.get("./file.zip"),
            StandardOpenOption.CREATE, StandardOpenOption.WRITE);
    return FlowableUtil.writeFile(response.body(null), channel);
}).doOnComplete(() -> System.out.println("The blob was downloaded to ./file.zip")).blockingAwait();

Which results in this error when the proxy requires authentication:

Okt 22, 2020 2:02:44 PM com.microsoft.azure.storage.blob.LoggingFactory$LoggingPolicy log
SCHWERWIEGEND: Unexpected failure attempting to make request.
Error message:'http, none, /[removed proxy ip and port] => [mystorage].blob.core.windows.net/20.38.118.132:443, status: 407 Proxy Authentication Required'