Skip to content

Commit 70ee9ae

Browse files
Transparent proxy: TP Loader Cloud SDK native integration
1 parent 48616ac commit 70ee9ae

File tree

4 files changed

+714
-0
lines changed

4 files changed

+714
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.sap.cloud.sdk.cloudplatform.connectivity;
2+
3+
import java.io.IOException;
4+
import java.net.InetSocketAddress;
5+
import java.net.Socket;
6+
import java.net.UnknownHostException;
7+
8+
import javax.annotation.Nonnull;
9+
10+
import com.sap.cloud.sdk.cloudplatform.connectivity.exception.DestinationAccessException;
11+
12+
/**
13+
* Default implementation of {@link NetworkValidator} that performs TCP socket-based connectivity validation.
14+
*
15+
* <p>
16+
* This implementation uses standard Java socket connections to validate that a remote host and port combination is
17+
* reachable and accepting connections. It establishes a temporary TCP connection to the target endpoint and immediately
18+
* closes it upon successful establishment.
19+
*
20+
* <p>
21+
* <strong>Validation Process:</strong>
22+
* <ol>
23+
* <li>Creates a new TCP socket</li>
24+
* <li>Attempts to connect to the specified host and port with a timeout</li>
25+
* <li>Immediately closes the connection if successful</li>
26+
* <li>Throws appropriate exceptions for failures</li>
27+
* </ol>
28+
*
29+
* <p>
30+
* <strong>Timeout Configuration:</strong> The validation uses a fixed timeout of {@value #HOST_REACH_TIMEOUT}
31+
* milliseconds to prevent indefinite blocking on unreachable endpoints.
32+
*
33+
* <p>
34+
* <strong>Error Handling:</strong>
35+
* <ul>
36+
* <li>{@link UnknownHostException} - Host cannot be resolved to an IP address</li>
37+
* <li>{@link IOException} - Network connectivity issues or connection refused</li>
38+
* </ul>
39+
*
40+
* @see NetworkValidator
41+
* @see TransparentProxy
42+
* @since 5.24.0
43+
*/
44+
class DefaultNetworkValidator implements NetworkValidator
45+
{
46+
private static final int HOST_REACH_TIMEOUT = 5000;
47+
48+
/**
49+
* {@inheritDoc}
50+
*
51+
* <p>
52+
* This implementation creates a TCP socket connection to the specified host and port to verify connectivity. The
53+
* connection is immediately closed after successful establishment.
54+
*
55+
* @param host
56+
* {@inheritDoc}
57+
* @param port
58+
* {@inheritDoc}
59+
* @throws DestinationAccessException
60+
* {@inheritDoc}
61+
* <p>
62+
* Specific error conditions:
63+
* <ul>
64+
* <li>Host resolution failure - when DNS lookup fails</li>
65+
* <li>Connection failure - when host is unreachable or port is closed</li>
66+
* <li>Network timeouts - when connection attempt exceeds timeout</li>
67+
* </ul>
68+
*/
69+
@Override
70+
public void verifyHostConnectivity( @Nonnull final String host, final int port )
71+
throws DestinationAccessException
72+
{
73+
try( Socket socket = new Socket() ) {
74+
socket.connect(new InetSocketAddress(host, port), HOST_REACH_TIMEOUT);
75+
}
76+
catch( final UnknownHostException e ) {
77+
throw new DestinationAccessException(String.format("Host [%s] could not be resolved", host), e);
78+
}
79+
catch( final IOException e ) {
80+
throw new DestinationAccessException(
81+
String.format("Host [%s] on port [%d] is not reachable", host, port),
82+
e);
83+
}
84+
}
85+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.sap.cloud.sdk.cloudplatform.connectivity;
2+
3+
import javax.annotation.Nonnull;
4+
5+
import com.sap.cloud.sdk.cloudplatform.connectivity.exception.DestinationAccessException;
6+
7+
/**
8+
* Interface for validating network connectivity to remote hosts and ports.
9+
*
10+
* <p>
11+
* This interface defines the contract for network validation implementations used by transparent proxy components to
12+
* ensure that registered hosts are reachable before completing the registration process.
13+
*
14+
* <p>
15+
* Implementations should perform actual network connectivity tests to verify that the specified host and port
16+
* combination is accessible from the current environment. This validation helps prevent registration of unreachable
17+
* endpoints that would cause runtime failures.
18+
*
19+
* <p>
20+
* <strong>Usage Context:</strong> This interface is primarily used by {@link TransparentProxy} during host registration
21+
* to validate network connectivity before accepting the registration.
22+
*
23+
* @see DefaultNetworkValidator
24+
* @see TransparentProxy
25+
* @since 5.24.0
26+
*/
27+
interface NetworkValidator
28+
{
29+
/**
30+
* Validates connectivity to the specified host and port combination.
31+
*
32+
* <p>
33+
* This method should attempt to establish a network connection to the given host and port to verify that the
34+
* endpoint is reachable and accepting connections. The validation should complete within a reasonable timeout
35+
* period.
36+
*
37+
* <p>
38+
* If the host cannot be resolved or the connection cannot be established, this method should throw a
39+
* {@link DestinationAccessException} with descriptive error information.
40+
*
41+
* @param host
42+
* the hostname or IP address to validate connectivity to. Must not be null or empty
43+
* @param port
44+
* the port number to test connectivity on. Should be a valid port number (1-65535)
45+
* @throws DestinationAccessException
46+
* if the host cannot be resolved, the connection cannot be established, or any other network-related
47+
* validation failure occurs
48+
* @throws IllegalArgumentException
49+
* if host is null or port is invalid
50+
*/
51+
void verifyHostConnectivity( @Nonnull String host, int port )
52+
throws DestinationAccessException;
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
package com.sap.cloud.sdk.cloudplatform.connectivity;
2+
3+
import java.net.URI;
4+
import java.net.URISyntaxException;
5+
6+
import javax.annotation.Nonnull;
7+
8+
import com.sap.cloud.sdk.cloudplatform.connectivity.exception.DestinationAccessException;
9+
10+
import io.vavr.control.Try;
11+
12+
/**
13+
* A transparent proxy loader that enables routing traffic through a single registered gateway host.
14+
*
15+
* <p>
16+
* This class provides a mechanism to register a proxy gateway that will handle all destination requests transparently.
17+
* Once registered, all destination lookups will be routed through the configured gateway host and port.
18+
*
19+
* <p>
20+
* <strong>Key Features:</strong>
21+
* <ul>
22+
* <li>Single gateway registration - only one proxy can be registered at a time</li>
23+
* <li>Host validation - ensures hosts don't contain paths and are reachable</li>
24+
* <li>Automatic scheme normalization - defaults to HTTP if no scheme provided</li>
25+
* <li>Network connectivity validation before registration</li>
26+
* </ul>
27+
*
28+
* <p>
29+
* <strong>Usage Example:</strong>
30+
*
31+
* <pre>{@code
32+
* // Register with default port 80
33+
* TransparentProxy.register("gateway.svc.cluster.local");
34+
*
35+
* // Register with custom port
36+
* TransparentProxy.register("http://gateway.svc.cluster.local", 8080);
37+
* }</pre>
38+
*
39+
* <p>
40+
* <strong>Thread Safety:</strong> This class uses static state and is not thread-safe. Registration should be performed
41+
* during application initialization.
42+
*
43+
* @since 5.24.0
44+
*/
45+
public class TransparentProxy implements DestinationLoader
46+
{
47+
private static final Integer DEFAULT_PORT = 80;
48+
private static final String SCHEME_SEPARATOR = "://";
49+
private static final String HTTP_SCHEME = org.apache.http.HttpHost.DEFAULT_SCHEME_NAME + SCHEME_SEPARATOR;
50+
private static final String PATH_SEPARATOR = "/";
51+
private static final String PORT_SEPARATOR = ":";
52+
private static final String HOST_CONTAINS_PATH_ERROR_MESSAGE_TEMPLATE =
53+
"Host '%s' contains a path '%s'. Paths are not allowed in host registration.";
54+
static String uri;
55+
static NetworkValidator networkValidator = new DefaultNetworkValidator();
56+
57+
/**
58+
* Registers a transparent proxy gateway using the default port 80.
59+
*
60+
* <p>
61+
* This method registers the specified host as a transparent proxy gateway that will handle all subsequent
62+
* destination requests. The host will be validated for reachability and must not contain any path components.
63+
*
64+
* <p>
65+
* If no scheme is provided, HTTP will be used by default. The final URI will be constructed as:
66+
* {@code <normalized-host>:80}
67+
*
68+
* @param host
69+
* the gateway host to register (e.g., "gateway.svc.cluster.local") Must not contain paths or be null
70+
* @throws DestinationAccessException
71+
* if the proxy is already registered, the host contains a path, or the host is not reachable on port 80
72+
* @throws IllegalArgumentException
73+
* if host is null
74+
* @see #register(String, Integer)
75+
*/
76+
public static void register( @Nonnull final String host )
77+
{
78+
registerLoader(host, DEFAULT_PORT);
79+
}
80+
81+
/**
82+
* Registers a transparent proxy gateway with a specified port.
83+
*
84+
* <p>
85+
* This method registers the specified host and port as a transparent proxy gateway that will handle all subsequent
86+
* destination requests. The host will be validated for reachability on the specified port and must not contain any
87+
* path components.
88+
*
89+
* <p>
90+
* If no scheme is provided, HTTP will be used by default. The final URI will be constructed as:
91+
* {@code <normalized-host>:<port>}
92+
*
93+
* @param host
94+
* the gateway host to register (e.g., "gateway" or "<a href="http://gateway">...</a>") Must not contain
95+
* paths or be null
96+
* @param port
97+
* the port number to use for the gateway connection. Must not be null and should be a valid port number
98+
* (1-65535)
99+
* @throws DestinationAccessException
100+
* if the proxy is already registered, the host contains a path, or the host is not reachable on the
101+
* specified port
102+
* @throws IllegalArgumentException
103+
* if host or port is null
104+
* @see #register(String)
105+
*/
106+
public static void register( @Nonnull final String host, @Nonnull final Integer port )
107+
{
108+
registerLoader(host, port);
109+
}
110+
111+
private static void registerLoader( @Nonnull final String host, final Integer port )
112+
{
113+
if( uri != null ) {
114+
throw new DestinationAccessException(
115+
"TransparentProxy is already registered. Only one registration is allowed.");
116+
}
117+
118+
validateHostHasNoPath(host);
119+
120+
final String normalizedHost = normalizeHostWithScheme(host);
121+
final String hostForValidation = extractHostForValidation(normalizedHost);
122+
123+
validateHostConnectivity(hostForValidation, port);
124+
125+
uri = String.format("%s%s%d", normalizedHost, PORT_SEPARATOR, port);
126+
DestinationAccessor.prependDestinationLoader(new TransparentProxy());
127+
}
128+
129+
@Nonnull
130+
private static String normalizeHostWithScheme( @Nonnull final String host )
131+
{
132+
if( host.contains(SCHEME_SEPARATOR) ) {
133+
return host;
134+
}
135+
return HTTP_SCHEME + host;
136+
}
137+
138+
private static String extractHostForValidation( @Nonnull final String normalizedHost )
139+
{
140+
try {
141+
final URI uri = new URI(normalizedHost);
142+
return uri.getHost();
143+
}
144+
catch( final URISyntaxException e ) {
145+
String host = normalizedHost;
146+
if( host.contains(SCHEME_SEPARATOR) ) {
147+
host = host.substring(host.indexOf(SCHEME_SEPARATOR) + 3);
148+
}
149+
if( host.contains(PORT_SEPARATOR) ) {
150+
host = host.substring(0, host.indexOf(PORT_SEPARATOR));
151+
}
152+
return host;
153+
}
154+
}
155+
156+
private static void validateHostConnectivity( @Nonnull final String host, final int port )
157+
{
158+
networkValidator.verifyHostConnectivity(host, port);
159+
}
160+
161+
private static void validateHostHasNoPath( @Nonnull final String host )
162+
{
163+
try {
164+
final URI uri = new URI(host.contains(SCHEME_SEPARATOR) ? host : HTTP_SCHEME + host);
165+
final String path = uri.getPath();
166+
if( path != null && !path.isEmpty() ) {
167+
throw new DestinationAccessException(
168+
String.format(HOST_CONTAINS_PATH_ERROR_MESSAGE_TEMPLATE, host, path));
169+
}
170+
}
171+
catch( final URISyntaxException e ) {
172+
final String hostToCheck =
173+
host.contains(SCHEME_SEPARATOR) ? host.substring(host.indexOf(SCHEME_SEPARATOR) + 3) : host;
174+
if( hostToCheck.contains(PATH_SEPARATOR) ) {
175+
final String pathPart = hostToCheck.substring(hostToCheck.indexOf(PATH_SEPARATOR));
176+
throw new DestinationAccessException(
177+
String.format(HOST_CONTAINS_PATH_ERROR_MESSAGE_TEMPLATE, host, pathPart),
178+
e);
179+
}
180+
}
181+
}
182+
183+
@Nonnull
184+
@Override
185+
public Try<Destination> tryGetDestination( @Nonnull final String destinationName )
186+
{
187+
return Try.success(TransparentProxyDestination.gateway(destinationName, uri).build());
188+
}
189+
190+
@Nonnull
191+
@Override
192+
public
193+
Try<Destination>
194+
tryGetDestination( @Nonnull final String destinationName, @Nonnull DestinationOptions options )
195+
{
196+
return Try.success(TransparentProxyDestination.gateway(destinationName, uri).build());
197+
}
198+
199+
}

0 commit comments

Comments
 (0)