mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 15:10:50 +08:00 
			
		
		
		
	Make rate-limiting client whitelist dynamic
* Refactor `RateLimitOptions.ClientWhiteList` * Fix typo in variable `enbleRateLimiting` * Fix case in variable `clientIdheader` author Taiwo Otubamowo <totubamowo@deloitte.co.uk>
This commit is contained in:
		@@ -1,4 +1,5 @@
 | 
				
			|||||||
using System.Collections.Generic;
 | 
					using System;
 | 
				
			||||||
 | 
					using System.Collections.Generic;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace Ocelot.Configuration.Builder
 | 
					namespace Ocelot.Configuration.Builder
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
@@ -7,6 +8,7 @@ namespace Ocelot.Configuration.Builder
 | 
				
			|||||||
        private bool _enableRateLimiting;
 | 
					        private bool _enableRateLimiting;
 | 
				
			||||||
        private string _clientIdHeader;
 | 
					        private string _clientIdHeader;
 | 
				
			||||||
        private List<string> _clientWhitelist;
 | 
					        private List<string> _clientWhitelist;
 | 
				
			||||||
 | 
					        private Func<List<string>> _getClientWhitelist;
 | 
				
			||||||
        private bool _disableRateLimitHeaders;
 | 
					        private bool _disableRateLimitHeaders;
 | 
				
			||||||
        private string _quotaExceededMessage;
 | 
					        private string _quotaExceededMessage;
 | 
				
			||||||
        private string _rateLimitCounterPrefix;
 | 
					        private string _rateLimitCounterPrefix;
 | 
				
			||||||
@@ -19,15 +21,15 @@ namespace Ocelot.Configuration.Builder
 | 
				
			|||||||
            return this;
 | 
					            return this;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public RateLimitOptionsBuilder WithClientIdHeader(string clientIdheader)
 | 
					        public RateLimitOptionsBuilder WithClientIdHeader(string clientIdHeader)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            _clientIdHeader = clientIdheader;
 | 
					            _clientIdHeader = clientIdHeader;
 | 
				
			||||||
            return this;
 | 
					            return this;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public RateLimitOptionsBuilder WithClientWhiteList(List<string> clientWhitelist)
 | 
					        public RateLimitOptionsBuilder WithClientWhiteList(Func<List<string>> getClientWhitelist)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            _clientWhitelist = clientWhitelist;
 | 
					            _getClientWhitelist = getClientWhitelist;
 | 
				
			||||||
            return this;
 | 
					            return this;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -63,7 +65,7 @@ namespace Ocelot.Configuration.Builder
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        public RateLimitOptions Build()
 | 
					        public RateLimitOptions Build()
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            return new RateLimitOptions(_enableRateLimiting, _clientIdHeader, _clientWhitelist,
 | 
					            return new RateLimitOptions(_enableRateLimiting, _clientIdHeader, _getClientWhitelist,
 | 
				
			||||||
                _disableRateLimitHeaders, _quotaExceededMessage, _rateLimitCounterPrefix,
 | 
					                _disableRateLimitHeaders, _quotaExceededMessage, _rateLimitCounterPrefix,
 | 
				
			||||||
                _rateLimitRule, _httpStatusCode);
 | 
					                _rateLimitRule, _httpStatusCode);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -11,7 +11,7 @@ namespace Ocelot.Configuration.Creator
 | 
				
			|||||||
            {
 | 
					            {
 | 
				
			||||||
                return new RateLimitOptionsBuilder()
 | 
					                return new RateLimitOptionsBuilder()
 | 
				
			||||||
                    .WithClientIdHeader(globalConfiguration.RateLimitOptions.ClientIdHeader)
 | 
					                    .WithClientIdHeader(globalConfiguration.RateLimitOptions.ClientIdHeader)
 | 
				
			||||||
                    .WithClientWhiteList(fileRateLimitRule.ClientWhitelist)
 | 
					                    .WithClientWhiteList(() => fileRateLimitRule.ClientWhitelist)
 | 
				
			||||||
                    .WithDisableRateLimitHeaders(globalConfiguration.RateLimitOptions.DisableRateLimitHeaders)
 | 
					                    .WithDisableRateLimitHeaders(globalConfiguration.RateLimitOptions.DisableRateLimitHeaders)
 | 
				
			||||||
                    .WithEnableRateLimiting(fileRateLimitRule.EnableRateLimiting)
 | 
					                    .WithEnableRateLimiting(fileRateLimitRule.EnableRateLimiting)
 | 
				
			||||||
                    .WithHttpStatusCode(globalConfiguration.RateLimitOptions.HttpStatusCode)
 | 
					                    .WithHttpStatusCode(globalConfiguration.RateLimitOptions.HttpStatusCode)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,4 +1,5 @@
 | 
				
			|||||||
using System.Collections.Generic;
 | 
					using System;
 | 
				
			||||||
 | 
					using System.Collections.Generic;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace Ocelot.Configuration
 | 
					namespace Ocelot.Configuration
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
@@ -7,12 +8,14 @@ namespace Ocelot.Configuration
 | 
				
			|||||||
    /// </summary>
 | 
					    /// </summary>
 | 
				
			||||||
    public class RateLimitOptions
 | 
					    public class RateLimitOptions
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        public RateLimitOptions(bool enbleRateLimiting, string clientIdHeader, List<string> clientWhitelist, bool disableRateLimitHeaders,
 | 
					        private readonly Func<List<string>> _getClientWhitelist;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        public RateLimitOptions(bool enableRateLimiting, string clientIdHeader, Func<List<string>> getClientWhitelist, bool disableRateLimitHeaders,
 | 
				
			||||||
            string quotaExceededMessage, string rateLimitCounterPrefix, RateLimitRule rateLimitRule, int httpStatusCode)
 | 
					            string quotaExceededMessage, string rateLimitCounterPrefix, RateLimitRule rateLimitRule, int httpStatusCode)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            EnableRateLimiting = enbleRateLimiting;
 | 
					            EnableRateLimiting = enableRateLimiting;
 | 
				
			||||||
            ClientIdHeader = clientIdHeader;
 | 
					            ClientIdHeader = clientIdHeader;
 | 
				
			||||||
            ClientWhitelist = clientWhitelist ?? new List<string>();
 | 
					            _getClientWhitelist = getClientWhitelist;
 | 
				
			||||||
            DisableRateLimitHeaders = disableRateLimitHeaders;
 | 
					            DisableRateLimitHeaders = disableRateLimitHeaders;
 | 
				
			||||||
            QuotaExceededMessage = quotaExceededMessage;
 | 
					            QuotaExceededMessage = quotaExceededMessage;
 | 
				
			||||||
            RateLimitCounterPrefix = rateLimitCounterPrefix;
 | 
					            RateLimitCounterPrefix = rateLimitCounterPrefix;
 | 
				
			||||||
@@ -22,7 +25,10 @@ namespace Ocelot.Configuration
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        public RateLimitRule RateLimitRule { get; private set; }
 | 
					        public RateLimitRule RateLimitRule { get; private set; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public List<string> ClientWhitelist { get; private set; }
 | 
					        /// <summary>
 | 
				
			||||||
 | 
					        /// Gets the list of white listed clients
 | 
				
			||||||
 | 
					        /// </summary>
 | 
				
			||||||
 | 
					        public List<string> ClientWhitelist { get => _getClientWhitelist(); }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        /// <summary>
 | 
					        /// <summary>
 | 
				
			||||||
        /// Gets or sets the HTTP header that holds the client identifier, by default is X-ClientId
 | 
					        /// Gets or sets the HTTP header that holds the client identifier, by default is X-ClientId
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -50,7 +50,7 @@ namespace Ocelot.UnitTests.Configuration
 | 
				
			|||||||
            };
 | 
					            };
 | 
				
			||||||
            var expected = new RateLimitOptionsBuilder()
 | 
					            var expected = new RateLimitOptionsBuilder()
 | 
				
			||||||
                .WithClientIdHeader("ClientIdHeader")
 | 
					                .WithClientIdHeader("ClientIdHeader")
 | 
				
			||||||
                .WithClientWhiteList(fileReRoute.RateLimitOptions.ClientWhitelist)
 | 
					                .WithClientWhiteList(() => fileReRoute.RateLimitOptions.ClientWhitelist)
 | 
				
			||||||
                .WithDisableRateLimitHeaders(true)
 | 
					                .WithDisableRateLimitHeaders(true)
 | 
				
			||||||
                .WithEnableRateLimiting(true)
 | 
					                .WithEnableRateLimiting(true)
 | 
				
			||||||
                .WithHttpStatusCode(200)
 | 
					                .WithHttpStatusCode(200)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -54,7 +54,7 @@ namespace Ocelot.UnitTests.RateLimit
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
            var downstreamReRoute = new DownstreamReRouteBuilder()
 | 
					            var downstreamReRoute = new DownstreamReRouteBuilder()
 | 
				
			||||||
                .WithEnableRateLimiting(true)
 | 
					                .WithEnableRateLimiting(true)
 | 
				
			||||||
                .WithRateLimitOptions(new RateLimitOptions(true, "ClientId", new List<string>(), false, "", "", new RateLimitRule("1s", 100, 3), 429))
 | 
					                .WithRateLimitOptions(new RateLimitOptions(true, "ClientId", () => new List<string>(), false, "", "", new RateLimitRule("1s", 100, 3), 429))
 | 
				
			||||||
                .WithUpstreamHttpMethod(new List<string> { "Get" })
 | 
					                .WithUpstreamHttpMethod(new List<string> { "Get" })
 | 
				
			||||||
                .WithUpstreamPathTemplate(upstreamTemplate)
 | 
					                .WithUpstreamPathTemplate(upstreamTemplate)
 | 
				
			||||||
                .Build();
 | 
					                .Build();
 | 
				
			||||||
@@ -82,7 +82,7 @@ namespace Ocelot.UnitTests.RateLimit
 | 
				
			|||||||
                     .WithDownstreamReRoute(new DownstreamReRouteBuilder()
 | 
					                     .WithDownstreamReRoute(new DownstreamReRouteBuilder()
 | 
				
			||||||
                         .WithEnableRateLimiting(true)
 | 
					                         .WithEnableRateLimiting(true)
 | 
				
			||||||
                         .WithRateLimitOptions(
 | 
					                         .WithRateLimitOptions(
 | 
				
			||||||
                             new Ocelot.Configuration.RateLimitOptions(true, "ClientId", new List<string>() { "ocelotclient2" }, false, "", "", new RateLimitRule("1s", 100, 3), 429))
 | 
					                             new Ocelot.Configuration.RateLimitOptions(true, "ClientId", () => new List<string>() { "ocelotclient2" }, false, "", "", new RateLimitRule("1s", 100, 3), 429))
 | 
				
			||||||
                         .WithUpstreamHttpMethod(new List<string> { "Get" })
 | 
					                         .WithUpstreamHttpMethod(new List<string> { "Get" })
 | 
				
			||||||
                         .Build())
 | 
					                         .Build())
 | 
				
			||||||
                     .WithUpstreamHttpMethod(new List<string> { "Get" })
 | 
					                     .WithUpstreamHttpMethod(new List<string> { "Get" })
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user