spring-cloud / spring-cloud-config

External configuration (server and client) for Spring Cloud
Apache License 2.0
1.96k stars 1.29k forks source link

Proxy configuration not working in Spring Cloud Config #2608

Open moiMeme opened 1 week ago

moiMeme commented 1 week ago

Describe the bug When configuring a proxy in Spring Cloud Config, HTTP requests to the config server do not route through the specified proxy. Despite setting http.proxyHost and http.proxyPort (or equivalent settings in application.yml), the connection bypasses the proxy and connects directly. This issue impacts users who require proxy routing for secure or restricted network environments.

Step to reproduce

  1. Set up a Spring Boot project with the following dependencies:

    <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    </dependencies>
  2. Configure the proxy settings in application.yml:

    spring:
    cloud:
    config:
      server:
        defaultLabel: master
        git:
          deleteUntrackedBranches: true
          clone-on-start: true
          skipSslValidation: true
          searchPaths:
            - "{application}"
            - "{application}/{profile}"
          uri: "https://config-server-url"  # Replace with your config server URL
          proxy:
            host: "proxy.example.com"       # Replace with your proxy hostname
            port: 8080                      # Replace with your proxy port
  3. Start the Spring Boot application. Verify if requests to the config server route through the specified proxy (e.g., by checking proxy logs)

Expected Behavior Requests to the config server should be routed through the configured proxy (proxy.example.com:8080).

Actual Behavior Requests to the config server bypass the configured proxy and connect directly. Proxy logs show no incoming requests, confirming that the proxy is not being used.

Environment

Additional Context No related exceptions or errors appear in the logs. The following configurations were tested without success:

Workaround Solution To route requests through the proxy despite the original configuration issue, I extended HttpClientConfigurableHttpConnectionFactory and modified the create method to ensure a new proxy is always created if none is provided or if the proxy type is set to DIRECT. This approach successfully routed requests through the proxy as expected. (not a good solution)

Here’s a summary of the code:

public class CustomHttpConnectionFactory extends HttpClientConfigurableHttpConnectionFactory {

    @Override
    public HttpConnection create(URL url, Proxy proxy) throws IOException {
        // Check if the proxy is null or has a direct type
        Proxy newProxy = Optional.ofNullable(proxy)
                      .filter(pp -> !Proxy.Type.DIRECT.equals(pp.type()))
                      .orElse(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.example.com", 3128)));
        return super.create(url, newProxy );
    }
}

In this method:

If the provided proxy is null or of type DIRECT, it creates a new Proxy instance with Proxy.Type.HTTP and the desired proxy host and port. This successfully routes the request via the specified proxy.(for test purpose)

ryanjbaxter commented 5 days ago

It sounds like there is a bit of a confusion here.

The properties under spring.cloud.config.server.git.proxy are used by the CONFIG SERVER to access the Git server through a proxy, it has no effect on the CONFIG CLIENT. Based on your issue it sounds like you are trying to configure the config client to use a proxy to access the config server.

The config client uses RestTemplate to make requests to the config server so you should be able to follow the Spring Boot documentation for configuring a proxy for the client.
https://docs.spring.io/spring-boot/reference/io/rest-client.html#io.rest-client.resttemplate.customization

moiMeme commented 4 days ago

I am setting up Spring Cloud Config Server to connect to a Git server through a proxy. The issue arises when using a token for authentication. Upon debugging, I found that the private lookupHttpClientBuilder(url) method fails to locate the HttpClientBuilder by URL.

During configuration, HttpClientConfigurableHttpConnectionFactory creates an HttpClientBuilder and stores it in a Map<String, HttpClientBuilder> httpClientBuildersByUri, where the key is the URL (including the token) from the properties file. However, the lookupHttpClientBuilder method is called with a URL that lacks the token, leading to a lookup failure.

ryanjbaxter commented 4 days ago

Can you provide a complete, minimal, verifiable sample that reproduces the problem? It should be available as a GitHub (or similar) project or attached to this issue as a zip file.