Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for creating an HTTPS proxy #2903

Merged
merged 8 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-2903.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: feature
feature:
description: Add support for configuring an HTTPS proxy
links:
- https://github.com/palantir/conjure-java-runtime/pull/2903
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,9 @@ public static ProxySelector createProxySelector(ProxyConfiguration proxyConfig)
InetSocketAddress address = createInetSocketAddress(defaultEnvProxy);
return fixedProxySelectorFor(new Proxy(Proxy.Type.HTTP, address));
case HTTP:
HostAndPort hostAndPort = HostAndPort.fromString(proxyConfig
.hostAndPort()
.orElseThrow(() -> new SafeIllegalArgumentException(
"Expected to find proxy hostAndPort configuration for HTTP proxy")));
InetSocketAddress addr =
// Proxy address must not be resolved, otherwise DNS changes while the application
// is running are ignored by the application.
InetSocketAddress.createUnresolved(hostAndPort.getHost(), hostAndPort.getPort());
return fixedProxySelectorFor(new Proxy(Proxy.Type.HTTP, addr));
return getHttpProxySelector(proxyConfig, false);
case HTTPS:
return getHttpProxySelector(proxyConfig, true);
case MESH:
return ProxySelector.getDefault(); // MESH proxy is not a Java proxy
case SOCKS:
Expand Down Expand Up @@ -222,6 +216,18 @@ private static Optional<HostAndPort> meshProxy(Optional<ProxyConfiguration> prox
}
}

private static ProxySelector getHttpProxySelector(ProxyConfiguration proxyConfig, boolean https) {
HostAndPort httpsHostAndPort = HostAndPort.fromString(proxyConfig
.hostAndPort()
.orElseThrow(() -> new SafeIllegalArgumentException(
"Expected to find proxy hostAndPort configuration for HTTP(S) proxy")));
InetSocketAddress address =
// Proxy address must not be resolved, otherwise DNS changes while the application
// is running are ignored by the application.
InetSocketAddress.createUnresolved(httpsHostAndPort.getHost(), httpsHostAndPort.getPort());
return fixedProxySelectorFor(https ? HttpsProxies.create(address) : new Proxy(Proxy.Type.HTTP, address));
}

private static ProxySelector fixedProxySelectorFor(Proxy proxy) {
return new FixedProxySelector(proxy);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* (c) Copyright 2024 Palantir Technologies Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.palantir.conjure.java.client.config;

import com.palantir.logsafe.Preconditions;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.SocketAddress;

/**
* Utility class for creating and identifying HTTPS proxy instances.
* <p>
* This class provides a way to create proxy instances that represent HTTPS connections, as there is no native support
* for an HTTPS proxy type in the Java {@link Proxy} class. When using a proxy that could have been created
* with this class, consumers should use the provided {@link #isHttps(Proxy)} method
* to determine if the proxy should be connected to using HTTPS.
*/
public final class HttpsProxies {

public static Proxy create(InetSocketAddress address) {
Preconditions.checkNotNull(address, "address is required");
return new HttpsProxy(address);
}

public static boolean isHttps(Proxy proxy) {
return proxy instanceof HttpsProxy;
}

private HttpsProxies() {}

private static final class HttpsProxy extends Proxy {
HttpsProxy(SocketAddress sa) {
super(Proxy.Type.HTTP, sa);
}
}
}
8 changes: 4 additions & 4 deletions versions.lock
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ com.netflix.concurrency-limits:concurrency-limits-core:0.2.2 (1 constraints: 060
com.netflix.feign:feign-core:8.18.0 (2 constraints: 1213df52)
com.netflix.feign:feign-jackson:8.18.0 (1 constraints: 43056b3b)
com.palantir.conjure.java:conjure-lib:8.22.0 (3 constraints: 5e253a4f)
com.palantir.conjure.java.api:errors:2.52.0 (3 constraints: 2f258649)
com.palantir.conjure.java.api:service-config:2.52.0 (3 constraints: a128fee1)
com.palantir.conjure.java.api:ssl-config:2.52.0 (2 constraints: fd174c86)
com.palantir.conjure.java.api:errors:2.53.0 (3 constraints: 3025e649)
com.palantir.conjure.java.api:service-config:2.53.0 (3 constraints: a22868e2)
com.palantir.conjure.java.api:ssl-config:2.53.0 (2 constraints: ff178886)
com.palantir.dialogue:dialogue-apache-hc5-client:3.133.0 (1 constraints: 6c05b240)
com.palantir.dialogue:dialogue-blocking-channels:3.133.0 (2 constraints: 3324bcf3)
com.palantir.dialogue:dialogue-core:3.133.0 (2 constraints: 99193a17)
Expand All @@ -49,7 +49,7 @@ com.palantir.tracing:tracing-jersey-jakarta:6.18.0 (1 constraints: 41055f3b)
com.palantir.tracing:tracing-okhttp3:6.18.0 (1 constraints: 41055f3b)
com.palantir.tritium:tritium-api:0.88.0 (2 constraints: 411f7abe)
com.palantir.tritium:tritium-core:0.88.0 (1 constraints: 481060a2)
com.palantir.tritium:tritium-ids:0.88.0 (1 constraints: d20fb596)
com.palantir.tritium:tritium-ids:0.88.0 (1 constraints: d30fb896)
com.palantir.tritium:tritium-metrics:0.88.0 (5 constraints: 174dbc62)
com.palantir.tritium:tritium-registry:0.88.0 (6 constraints: 5e5de2df)
com.squareup.okhttp3:okhttp:4.12.0 (3 constraints: 3b24fdc0)
Expand Down
2 changes: 1 addition & 1 deletion versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ com.netflix.concurrency-limits:* = 0.2.2
com.netflix.feign:feign-*= 8.18.0
com.palantir.conjure.java:* = 8.22.0
com.palantir.conjure.java:conjure-lib = 8.22.0
com.palantir.conjure.java.api:* = 2.52.0
com.palantir.conjure.java.api:* = 2.53.0
com.palantir.conjure.verification:* = 0.19.0
com.palantir.dialogue:* = 3.133.0
com.palantir.safe-logging:* = 3.7.0
Expand Down