Uploaded image for project: 'Camel'
  1. Camel
  2. CAMEL-12855

camel-swagger-java not honoring the x-forwarded-[host,proto,prefix] headers.

    XMLWordPrintableJSON

Details

    • New Feature
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 2.22.1
    • 2.23.0
    • camel-swagger
    • None
    • Unknown

    Description

      We have a camel spring boot application sitting behind an api gateway with a Swagger page for accessing the documentation for all of our services (created with Spring Boot and Zuul Proxy, Consul as service discovery). The Swagger gateway sends three headers to the camel application to retrieve the swagger docs, but camel-swagger-java is not honoring those headers, thus the attributes host, basePath and schemes remain the same as if the swagger documentation was being acessed directly.

      Example, when trying to get the swagger docs for our customer-service camel application:

      https://api-gateway.dev-internal.io/api-gateway/customer-service/v2/api-docs

       

      x-forwarded-prefix: /api-gateway/
      x-forwarded-host: api-gateway.dev-internal.io
      x-forwarded-proto: https,http

       

      The expected results should be

      {
          "swagger": "2.0",
          ...
          "host": "api-gateway.dev-internal.io",
          "basePath": "/api-gateway/customer-service",
          "schemes": [ "https", "http" ]
          ...
      }

      Current results:

      {
          "swagger": "2.0",
          ...
          "host": "",
          "basePath": "/customer-service",
          "schemes": [ "http" ]
          ...
      }

       

      we had a simple solution, which might not be the correct one. We changed the RestSwaggerSupport.java:

       

      public class RestSwaggerSupport {
      
          private static final String X_FORWARDED_PREFIX = "X-Forwarded-Prefix";     
          private static final String X_FORWARDED_HOST = "X-Forwarded-Host";
      
          public void renderResourceListing(RestApiResponseAdapter response, BeanConfig swaggerConfig, String contextId, String route, boolean json, boolean yaml, Exchange exchange, ClassResolver classResolver, RestConfiguration configuration, String prefix) throws Exception {
      
          ...
              Swagger swagger = reader.read(rests, route, swaggerConfig, contextId, exchange == null ? classResolver : exchange.getContext().getClassResolver());   
              setupXForwardedHeaders(swagger, exchange);
          ...
      
          }
      
          private void setupXForwardedHeaders(Swagger swagger, Exchange exchange) {
      
              if (exchange.getIn().getHeaders().containsKey(X_FORWARDED_PREFIX)) {
                  String prefix = exchange.getIn().getHeader(X_FORWARDED_PREFIX, String.class);
                  prefix = prefix.replace("/", "");
                  if (prefix != null) {
                      String path = swagger.getBasePath();
                      path = "/" + prefix + path;
                      swagger.setBasePath(path);
                  }
              }
      
              if(exchange.getIn().getHeaders().containsKey(X_FORWARDED_HOST)) {
                  String host = exchange.getIn().getHeader(X_FORWARDED_HOST, String.class);
                  if(host != null && host.length() > 0) {
                      swagger.setHost(host);
                  }
              }
          ...
      }
      

       

      If this is a valid approach, I will submit a PR. 

       

       

      Attachments

        Activity

          People

            zregvart Zoran Regvart
            thiagolocatelli Thiago Locatelli
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: