Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
royberris opened this issue Nov 29, 2022 · 3 comments
Closed
Assignees
Labels
waiting-8-days Closing after 8 days of waiting for the additional info requested.

Comments

@royberris
Copy link

royberris commented Nov 29, 2022

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
Copy link
Author

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
Copy link
Owner

Burgyn commented Nov 29, 2022

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;
})

@Burgyn Burgyn added the waiting-8-days Closing after 8 days of waiting for the additional info requested. label Nov 29, 2022
@royberris
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting-8-days Closing after 8 days of waiting for the additional info requested.
Projects
None yet
Development

No branches or pull requests

2 participants