ThreeMammals / Ocelot

.NET API Gateway
https://www.nuget.org/packages/Ocelot
MIT License
8.38k stars 1.64k forks source link

How to configure Ocelot to prevent it from adding the port to my downstream URL? #1692

Closed walissonde closed 1 year ago

walissonde commented 1 year ago

Hello, I am currently working on a Proof of Concept (POC) using Ocelot, but I have encountered an issue when making a URL call from localhost to route traffic to my API. Is that correct when missing downstream port is replaced with an upstream port?

Actual Behavior

The base URL of my project is:

I also have a downstream URL, for instance:

However, when I make a call to my URL through the Ocelot gateway, it adds a port to my URL, causing my external API to malfunction because it doesn't expect a port.

Here's what happens:

I call my upstream URL as follows: localhost:5021/gateway/my-api Ocelot attempts to call my downstream and adds a port: my-external-api.com:5021

Expected Behavior

Now, I'm looking for a way to configure Ocelot to ignore this added port (e.g., 5021) when making calls to my API. I've already removed the port from my downstream configuration.

How can I configure Ocelot to resolve this issue and prevent it from adding the port to my downstream URL?

Specifications

raman-m commented 1 year ago

Hi Walisson! Thanks for your interest in Ocelot!

As far as I understood, Ocelot substitutes downstream port by upstream port when there is no downstream port. Correct? If Yes, it can be a bug. But this scenario should be reproduced and proved. Also, you need to check Ocelot logs for a warnings & messages of configuration validation. Could you check your logs for messages please?

On my opinion, when there is no specified downstream port, so a warning should be logged. Moreover, as a default feature, missing port must be initialized by default port of current scheme (protocol) like:

raman-m commented 1 year ago

How can I configure Ocelot to resolve this issue and prevent it from adding the port to my downstream URL?

Just define the downstream port for all routes! For example:

{
  "Routes": [
    {
      // Route 1
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "route1.com",
          "Port": 80
        }
      ],
      // ...
    },
    {
      // Route 2
      "DownstreamScheme": "https",
      "DownstreamHostAndPorts": [
        {
          "Host": "route2.com",
          "Port": 443
        }
      ],
      // ...
    },
  ],
  "GlobalConfiguration": {
  }
}

Let me know testing results please!

raman-m commented 1 year ago

@walissonde commented on Sep 13

Answering your question

How can I configure Ocelot to resolve this issue and prevent it from adding the port to my downstream URL?

I suggested you just specify Port value explicitly as is done for all routes now as the default behavior.

Regarding potential bug of port substitution from upstream... Still need to check it... Haven't you received validation errors 7 warning in the log?

Also, I've looked into the code, the DownstreamRequestCreator class, and it has no extra logic related to Port property. The Port property has no implicit initializations... Historically Port should be initialized explicitly in configuration of the Route, because downstream services have deployed behind firewalls which open access with specific ports.


Have I answered on your question? Would you like to convert your question to bug/feature type?

raman-m commented 1 year ago

@walissonde It could be a bug, it can be a feature, I'm not sure now. Historically all routes should have specified port. So, moving this thread to discussions...