|
| 1 | +/* |
| 2 | + * (c) 2021 Open Source Geospatial Foundation - all rights reserved This code is licensed under the |
| 3 | + * GPL 2.0 license, available at the root application directory. |
| 4 | + */ |
| 5 | +package org.geotools.autoconfigure.httpclient; |
| 6 | + |
| 7 | +import lombok.extern.slf4j.Slf4j; |
| 8 | +import org.geotools.autoconfigure.httpclient.ProxyConfig.ProxyHostConfig; |
| 9 | +import org.geotools.http.HTTPClientFactory; |
| 10 | +import org.springframework.beans.factory.annotation.Autowired; |
| 11 | +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
| 12 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 13 | +import org.springframework.boot.context.properties.ConfigurationProperties; |
| 14 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 15 | +import org.springframework.context.annotation.Bean; |
| 16 | +import org.springframework.context.annotation.Configuration; |
| 17 | + |
| 18 | +/** |
| 19 | + * {@link EnableAutoConfiguration @EnableAutoConfiguration} auto configuration for a GeoTools {@link |
| 20 | + * HTTPClientFactory} that can be configured through spring-boot externalized properties and only |
| 21 | + * affects GeoTools http clients instead of the whole JVM. |
| 22 | + * |
| 23 | + * <p>The usual way to set an http proxy is through the {@literal http.proxyHost}, {@literal |
| 24 | + * http.proxyPort}, {@literal http.proxyUser}, {@literal http.proxyPassword} Java System Properties. |
| 25 | + * |
| 26 | + * <p>In the context of Cloud Native GeoServer containerized applications, this has a number of |
| 27 | + * drawbacks: |
| 28 | + * |
| 29 | + * <ul> |
| 30 | + * <li>Standard java proxy parameters only work with System properties, not env variables (at |
| 31 | + * least with the apache http client), and setting system properties is more cumbersome than |
| 32 | + * env variables (you have to modify the container run command) |
| 33 | + * <li>{@literal http.proxyUser/Password} are not standard properties, though commonly used, it's |
| 34 | + * kind of JDK implementation dependent. |
| 35 | + * <li>Setting {@literal -Dhtt.proxy*} System properties affects all HTTP clients in the |
| 36 | + * container, meaning requests to the {@literal config-service}, {@literal discovery-service}, |
| 37 | + * etc., will also try to go through the proxy, or you need to go through the extra burden of |
| 38 | + * figuring out how to ignore them. |
| 39 | + * <li>If the proxy is secured, and since the http client used may not respect the {@literal |
| 40 | + * http.proxyUser/Password} parameters, the apps won't start since they'll get HTTP 407 "Proxy |
| 41 | + * Authentication Required". |
| 42 | + * </ul> |
| 43 | + * |
| 44 | + * <p>The following externalized configuration properties apply: |
| 45 | + * |
| 46 | + * <pre> |
| 47 | + * <code> |
| 48 | + * geotools: |
| 49 | + * httpclient: |
| 50 | + * proxy: |
| 51 | + * # defaults to true, false disables the autoconfiguration and falls back to standard GeoServer behavior |
| 52 | + * enabled: true |
| 53 | + * http: |
| 54 | + * host: |
| 55 | + * port: |
| 56 | + * user: |
| 57 | + * password: |
| 58 | + * nonProxyHosts: |
| 59 | + * # comma separated list of Java regular expressions, e.g.: nonProxyHosts: localhost, example.* |
| 60 | + * https: |
| 61 | + * host: |
| 62 | + * port: |
| 63 | + * user: |
| 64 | + * password: |
| 65 | + * nonProxyHosts: |
| 66 | + * </code> |
| 67 | + * </pre> |
| 68 | + */ |
| 69 | +@Configuration(proxyBeanMethods = false) |
| 70 | +@EnableConfigurationProperties |
| 71 | +@ConditionalOnProperty( |
| 72 | + prefix = "geotools.httpclient.proxy", |
| 73 | + name = "enabled", |
| 74 | + havingValue = "true", |
| 75 | + matchIfMissing = true |
| 76 | +) |
| 77 | +@Slf4j(topic = "org.geotools.autoconfigure.httpclient") |
| 78 | +public class GeoToolsHttpClientAutoConfiguration { |
| 79 | + |
| 80 | + @ConfigurationProperties(prefix = "geotools.httpclient.proxy") |
| 81 | + public @Bean ProxyConfig geoToolsHttpProxyConfiguration() { |
| 82 | + System.setProperty( |
| 83 | + "HTTP_CLIENT_FACTORY", |
| 84 | + SpringEnvironmentAwareGeoToolsHttpClientFactory.class.getCanonicalName()); |
| 85 | + return new ProxyConfig(); |
| 86 | + } |
| 87 | + |
| 88 | + public @Bean SpringEnvironmentAwareGeoToolsHttpClientFactory |
| 89 | + springEnvironmentAwareGeoToolsHttpClientFactory(@Autowired ProxyConfig proxyConfig) { |
| 90 | + |
| 91 | + log.info("Using spring environment aware GeoTools HTTPClientFactory"); |
| 92 | + log(proxyConfig.getHttp(), "HTTP"); |
| 93 | + log(proxyConfig.getHttps(), "HTTPS"); |
| 94 | + SpringEnvironmentAwareGeoToolsHttpClientFactory.setProxyConfig(proxyConfig); |
| 95 | + |
| 96 | + return new SpringEnvironmentAwareGeoToolsHttpClientFactory(); |
| 97 | + } |
| 98 | + |
| 99 | + private void log(ProxyHostConfig config, String protocol) { |
| 100 | + config.host() |
| 101 | + .ifPresentOrElse( |
| 102 | + host -> |
| 103 | + log.info( |
| 104 | + "{} proxy configured for GeoTools cascaded OWS stores: {}:{}, secured: {}", |
| 105 | + protocol, |
| 106 | + host, |
| 107 | + config.port(), |
| 108 | + config.isSecured()), |
| 109 | + () -> |
| 110 | + log.info( |
| 111 | + "No {} proxy configured for GeoTools cascaded OWS stores", |
| 112 | + protocol)); |
| 113 | + } |
| 114 | +} |
0 commit comments