Burgyn / MMLib.SwaggerForOcelot

This repo contains swagger extension for ocelot.
MIT License
351 stars 93 forks source link

Server URL is HTTP causing the Swagger UI to be unusable from HTTPS #246

Closed royberris closed 1 year ago

royberris commented 1 year ago

Describe the bug This is not really a bug, but it might still be something to think about. Mainly I need support on this.

We use Azure Container Apps and internal communications between services is on http. The ingress uses https. So Swagger UI is served over HTTPS. The problem is that the Open API document has the server URL in HTTP format, not in HTTPS. Swagger UI will try to make a request over HTTP, not HTTPS. This is something that the browser will not allow. When importing this into Postman it will give me the HTTP server url, but it required HTTPS.

How do I update the Server URL to force HTTPS instead of the HTTP it thinks it is? It is not an option to set ASP.NET to HTTPS because there is no certificate.

royberris commented 1 year ago

Looking at the source code the Scheme is determined through the HttpContext. I also noticed that there is a HostOverride I can set. This is a nice work-around for now. But it would be nice to be able to do this globally, instead of having to set my custom domain (which the project shouldn't know about) and add it to the environment ocelot config.

Burgyn commented 1 year ago

Hi @royberris, Unfortunately, there is currently no direct way to do it globally. However, it is possible to do a custom post-process on the documentation before it is displayed:

public string AlterUpstreamSwaggerJson(HttpContext context, string swaggerJson)
{
  var swagger = JObject.Parse(swaggerJson);
  // ... alter upstream json
  return swagger.ToString(Formatting.Indented);
}

app.UseSwaggerForOcelotUI(opt => {
  opt.ReConfigureUpstreamSwaggerJson = AlterUpstreamSwaggerJson;
})
royberris commented 1 year ago

Hi @Burgyn, thanks.

I managed to get this:

opt.ReConfigureUpstreamSwaggerJson = (HttpContext context, string swaggerJson) =>
{
    var swagger = JObject.Parse(swaggerJson);

    foreach (var token in swagger.SelectTokens("$.servers[*].url"))
    {
        var serverUrl = token.ToString();
        if (serverUrl.StartsWith("http://", StringComparison.OrdinalIgnoreCase))
        {
            token.Replace(serverUrl.Replace("http://", "https://", StringComparison.OrdinalIgnoreCase));
        }
    }

    return swagger.ToString(Formatting.Indented);
};

For anyone who has the same problem in this hosting situation. I'll close the issue