|
| 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 | +import lombok.extern.slf4j.Slf4j; |
| 12 | + |
| 13 | +/** |
| 14 | + * A transparent proxy loader that enables routing traffic through a single registered gateway host. |
| 15 | + * |
| 16 | + * <p> |
| 17 | + * This class provides a mechanism to register a proxy gateway that will handle all destination requests transparently. |
| 18 | + * Once registered, all destination lookups will be routed through the configured gateway host and port. |
| 19 | + * |
| 20 | + * <p> |
| 21 | + * <strong>Key Features:</strong> |
| 22 | + * <ul> |
| 23 | + * <li>Single gateway registration - only one proxy can be registered at a time</li> |
| 24 | + * <li>Host validation - ensures hosts don't contain paths and are reachable</li> |
| 25 | + * <li>Automatic scheme normalization - defaults to HTTP if no scheme provided</li> |
| 26 | + * <li>Network connectivity validation before registration</li> |
| 27 | + * </ul> |
| 28 | + * |
| 29 | + * <p> |
| 30 | + * <strong>Usage Example:</strong> |
| 31 | + * |
| 32 | + * <pre>{@code |
| 33 | + * // Register with default port 80 |
| 34 | + * TransparentProxy.register("gateway.svc.cluster.local"); |
| 35 | + * |
| 36 | + * // Register with custom port |
| 37 | + * TransparentProxy.register("http://gateway.svc.cluster.local", 8080); |
| 38 | + * }</pre> |
| 39 | + * |
| 40 | + * <p> |
| 41 | + * <strong>Thread Safety:</strong> This class uses static state and is not thread-safe. Registration should be performed |
| 42 | + * during application initialization. |
| 43 | + * |
| 44 | + * @since 5.24.0 |
| 45 | + */ |
| 46 | +@Slf4j |
| 47 | +public class TransparentProxy implements DestinationLoader { |
| 48 | + private static final Integer DEFAULT_PORT = 80; |
| 49 | + private static final String SCHEME_SEPARATOR = "://"; |
| 50 | + private static final String HTTP_SCHEME = org.apache.http.HttpHost.DEFAULT_SCHEME_NAME + SCHEME_SEPARATOR; |
| 51 | + private static final String PATH_SEPARATOR = "/"; |
| 52 | + private static final String PORT_SEPARATOR = ":"; |
| 53 | + private static final String HOST_CONTAINS_PATH_ERROR_MESSAGE_TEMPLATE = |
| 54 | + "Host '%s' contains a path '%s'. Paths are not allowed in host registration."; |
| 55 | + static String uri; |
| 56 | + static NetworkValidator networkValidator = new DefaultNetworkValidator(); |
| 57 | + |
| 58 | + /** |
| 59 | + * Registers a transparent proxy gateway using the default port 80. |
| 60 | + * |
| 61 | + * <p> |
| 62 | + * This method registers the specified host as a transparent proxy gateway that will handle all subsequent |
| 63 | + * destination requests. The host will be validated for reachability and must not contain any path components. |
| 64 | + * |
| 65 | + * <p> |
| 66 | + * If no scheme is provided, HTTP will be used by default. The final URI will be constructed as: |
| 67 | + * {@code <normalized-host>:80} |
| 68 | + * |
| 69 | + * @param host the gateway host to register (e.g., "gateway.svc.cluster.local") Must not contain paths or be null |
| 70 | + * @throws DestinationAccessException if the proxy is already registered, the host contains a path, or the host is not reachable on port 80 |
| 71 | + * @throws IllegalArgumentException if host is null |
| 72 | + * @see #register(String, Integer) |
| 73 | + */ |
| 74 | + public static void register(@Nonnull final String host) { |
| 75 | + registerLoader(host, DEFAULT_PORT); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Registers a transparent proxy gateway with a specified port. |
| 80 | + * |
| 81 | + * <p> |
| 82 | + * This method registers the specified host and port as a transparent proxy gateway that will handle all subsequent |
| 83 | + * destination requests. The host will be validated for reachability on the specified port and must not contain any |
| 84 | + * path components. |
| 85 | + * |
| 86 | + * <p> |
| 87 | + * If no scheme is provided, HTTP will be used by default. The final URI will be constructed as: |
| 88 | + * {@code <normalized-host>:<port>} |
| 89 | + * |
| 90 | + * @param host the gateway host to register (e.g., "gateway" or "<a href="http://gateway">...</a>") Must not contain |
| 91 | + * paths or be null |
| 92 | + * @param port the port number to use for the gateway connection. Must not be null and should be a valid port number |
| 93 | + * (1-65535) |
| 94 | + * @throws DestinationAccessException if the proxy is already registered, the host contains a path, or the host is not reachable on the |
| 95 | + * specified port |
| 96 | + * @throws IllegalArgumentException if host or port is null |
| 97 | + * @see #register(String) |
| 98 | + */ |
| 99 | + public static void register(@Nonnull final String host, @Nonnull final Integer port) { |
| 100 | + registerLoader(host, port); |
| 101 | + } |
| 102 | + |
| 103 | + private static void registerLoader(@Nonnull final String host, final Integer port) { |
| 104 | + if (uri != null) { |
| 105 | + throw new DestinationAccessException( |
| 106 | + "TransparentProxy is already registered. Only one registration is allowed."); |
| 107 | + } |
| 108 | + |
| 109 | + try { |
| 110 | + final String normalizedHost = normalizeHostWithScheme(host); |
| 111 | + final String hostForVerification = getHostForVerification(host, normalizedHost); |
| 112 | + |
| 113 | + verifyHostConnectivity(hostForVerification, port); |
| 114 | + |
| 115 | + uri = String.format("%s%s%d", normalizedHost, PORT_SEPARATOR, port); |
| 116 | + DestinationAccessor.prependDestinationLoader(new TransparentProxy()); |
| 117 | + |
| 118 | + } catch (final URISyntaxException e) { |
| 119 | + throw new DestinationAccessException("Invalid host format: " + host, e); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + @Nonnull |
| 124 | + private static String getHostForVerification(@Nonnull String host, String normalizedHost) throws URISyntaxException { |
| 125 | + final URI parsedUri = new URI(normalizedHost); |
| 126 | + |
| 127 | + final String path = parsedUri.getPath(); |
| 128 | + if (path != null && !path.isEmpty()) { |
| 129 | + throw new DestinationAccessException( |
| 130 | + String.format(HOST_CONTAINS_PATH_ERROR_MESSAGE_TEMPLATE, host, path)); |
| 131 | + } |
| 132 | + |
| 133 | + final String hostForVerification = parsedUri.getHost(); |
| 134 | + if (hostForVerification == null) { |
| 135 | + throw new DestinationAccessException("Invalid host format: " + host); |
| 136 | + } |
| 137 | + return hostForVerification; |
| 138 | + } |
| 139 | + |
| 140 | + @Nonnull |
| 141 | + private static String normalizeHostWithScheme(@Nonnull final String host) { |
| 142 | + if (host.contains(SCHEME_SEPARATOR)) { |
| 143 | + return host; |
| 144 | + } |
| 145 | + return HTTP_SCHEME + host; |
| 146 | + } |
| 147 | + |
| 148 | + private static void verifyHostConnectivity(@Nonnull final String host, final int port) { |
| 149 | + networkValidator.verifyHostConnectivity(host, port); |
| 150 | + } |
| 151 | + |
| 152 | + @Nonnull |
| 153 | + @Override |
| 154 | + public Try<Destination> tryGetDestination(@Nonnull final String destinationName) { |
| 155 | + return Try.success(TransparentProxyDestination.gateway(destinationName, uri).build()); |
| 156 | + } |
| 157 | + |
| 158 | + @Nonnull |
| 159 | + @Override |
| 160 | + public Try<Destination> |
| 161 | + tryGetDestination(@Nonnull final String destinationName, @Nonnull DestinationOptions options) { |
| 162 | + return Try.success(TransparentProxyDestination.gateway(destinationName, uri).build()); |
| 163 | + } |
| 164 | + |
| 165 | +} |
0 commit comments