stefanprodan / AspNetCoreRateLimit

ASP.NET Core rate limiting middleware
MIT License
3.12k stars 445 forks source link

Cannot use this in Azure App Service as App Service Configuration requires all settings to be string type #153

Open chandde opened 4 years ago

chandde commented 4 years ago

When I locally tested it and it worked pretty well, however, I have an Asp.Net Core App Service running in Azure, I use App Service Configuration to set the configs, there is a requirement that all settings key and value must be string type (i.e. quoted, here's a link to Azure Docs https://docs.microsoft.com/en-us/azure/app-service/configure-common), however package uses a few boolean and integer setting values, how should I make it work? see below snapshot, is there a way to work around this?

Do I have to load the string version of config, then build another config with manual conversion ("true" -> true, "20" -> 20) then feed the component?

image

"IpRateLimiting": {
  "EnableEndpointRateLimiting": true, <- non string
  "StackBlockedRequests": false, <- non string
  "RealIPHeader": "X-Real-IP",
  "ClientIdHeader": "X-ClientId",
  "HttpStatusCode": 429, <- non string
  "GeneralRules": [
    {
      "Endpoint": "*",
      "Period": "10m",
      "Limit": 60 <- non string
    },
    {
      "Endpoint": "*:/sendemail",
      "Period": "5m",
      "Limit": 5 <- non string
    }
  ]
}
chandde commented 4 years ago

I resolved this issue by converting the entire IpRateLimiting configuration into a json, and put the json string in app settings (or Azure App Service Configuration),

{
    "otherconfigurations": "othervalues",
    "IpRateLimiting": "{\"EnableEndpointRateLimiting\":true,\"StackBlockedRequests\":false,\"RealIPHeader\":\"X-Real-IP\",\"ClientIdHeader\":\"X-ClientId\",\"HttpStatusCode\":429,\"GeneralRules\":[{\"Endpoint\":\"*\",\"Period\":\"10m\",\"Limit\":60},{\"Endpoint\":\"*:/sendemail\",\"Period\":\"5m\",\"Limit\":5}]}"
  }
}

At runtime, read the configuration value string, build a new configuration by the json stream

var ipRateLimitingStr = config.GetSection("IpRateLimiting");
var ipRateLimitingConfiguration = new ConfigurationBuilder().AddJsonStream(new MemoryStream(Encoding.ASCII.GetBytes(ipRateLimitingStr.Value))).Build();
services.Configure<IpRateLimitOptions>(ipRateLimitingConfiguration);
aeslinger0 commented 11 months ago

I use Azure App Configuration and I can set these values like "True" and they get converted into an actual boolean value when IOptionsSnapshot<IPRateLimitOptions> or IOptionsSnapshot<ClientRateLimitOptions> is used. You can test this yourself by injecting one of those into your controller or service and observing the property in your debugger.

image

I would assume it would be the same with Azure App Service Configuration since it's just another configuration source.