Ocelot icon indicating copy to clipboard operation
Ocelot copied to clipboard

Issue with IP Blocking and Allowing in global configuration

Open CavidH opened this issue 1 year ago • 7 comments

I configured IP blocking and allowing in Ocelot using SecurityOptions, but it's not working.

{
  "GlobalConfiguration": {
    "BaseUrl": "http://localhost:5000",
    "SecurityOptions": {
      "IPBlockedList": ["192.168.0.23"]
    }
  }
}


The IP blocking configuration is not working as expected.

CavidH avatar Oct 07 '24 11:10 CavidH

Hello, Cavid! It seems we lack support for global settings. The potential solutions could be:

  1. Solely using ocelot.json. Define the options for each route individually.
  2. Utilizing C# coding. Replace the ISecurityOptionsCreator service in the DI container by redeveloping the SecurityOptionsCreator class to consider only the global settings. https://github.com/ThreeMammals/Ocelot/blob/6088515173b70abd52798e544e5ded409680dbdb/src/Ocelot/DependencyInjection/OcelotBuilder.cs#L141

Which solution would be more convenient for you?

raman-m avatar Oct 08 '24 09:10 raman-m

Hello @Fabman08, The absence of global settings support is a significant issue. Here's the current usage of SecurityOptionsCreator:

https://github.com/ThreeMammals/Ocelot/blob/6088515173b70abd52798e544e5ded409680dbdb/src/Ocelot/Configuration/Creator/RoutesCreator.cs#L111

Consequently, the method should accept two arguments, including global settings: https://github.com/ThreeMammals/Ocelot/blob/6088515173b70abd52798e544e5ded409680dbdb/src/Ocelot/Configuration/Creator/SecurityOptionsCreator.cs#L8 Would you be able to allocate some time to address this?

raman-m avatar Oct 08 '24 09:10 raman-m

Hi @raman-m! Sure, I'll be able to fix the issue this or next week. ☺️

@CavidH thank you for reporting this issue! 👍

Fabman08 avatar Oct 08 '24 10:10 Fabman08

Thank you very much for your help. We will eagerly await the new version.

CavidH avatar Oct 08 '24 14:10 CavidH

Hello, Cavid! It seems we lack support for global settings. The potential solutions could be:

  1. Solely using ocelot.json. Define the options for each route individually.
  2. Utilizing C# coding. Replace the ISecurityOptionsCreator service in the DI container by redeveloping the SecurityOptionsCreator class to consider only the global settings. https://github.com/ThreeMammals/Ocelot/blob/6088515173b70abd52798e544e5ded409680dbdb/src/Ocelot/DependencyInjection/OcelotBuilder.cs#L141

Which solution would be more convenient for you?

I had thought of the first option, but since I have too many services, it will be difficult to control this

CavidH avatar Oct 08 '24 14:10 CavidH

@raman-m just a little question about Global Settings behaviours.

I've found three ways to fix the issue:

  1. Every properties in Route FileSecurityOptions overrides their Global Settings values eg.
  • Global: Allowed: B,C Blocked: E ExcludeAllowedFromBlocked: true
  • Route: Allowed: A,B,C Blocked: D,E,F ExcludeAllowedFromBlocked: false
  • Final: Allowed: A,B,C Blocked: D,E,F ExcludeAllowedFromBlocked: false
  1. Only the existing properties in Route FileSecurityOptions overrides their Global Settings values eg.
  • Global: Allowed: B,C Blocked: E ExcludeAllowedFromBlocked: true
  • Route: Allowed: A,B,C Blocked: null ExcludeAllowedFromBlocked: null
  • Final: Allowed: A,B,C Blocked: E ExcludeAllowedFromBlocked: true
  1. The Route FileSecurityOptions values will be merged to Global Settings values eg.
  • Global: Allowed: A,B,C Blocked: D,E ExcludeAllowedFromBlocked: true
  • Route: Allowed: F,G,H Blocked: I,K ExcludeAllowedFromBlocked: false
  • Final: Allowed: A,B,C,F,G,H Blocked: D,E,I,K ExcludeAllowedFromBlocked: false

Which one is the best?

Fabman08 avatar Oct 10 '24 08:10 Fabman08

@Fabman08 Your research is too complicated!

Which one is the best?

It is best to prioritize the route-specific FileSecurityOptions object over the global settings. Therefore, if route settings are defined, all global settings should be disregarded. In practice, if the route object is null, then the global settings should be utilized.

Every properties in Route FileSecurityOptions overrides their Global Settings values

Yes, if route options are defined, all global ones should be ignored. We will highlight this in the documentation. There should be no merging whatsoever. This approach is the simplest and most correct solution, as merging properties would be erroneous due to potential conflicts within the algorithm. Therefore, merging is identified as a source of bugs.

raman-m avatar Oct 10 '24 12:10 raman-m