mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 20: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
 | 
			
		||||
{
 | 
			
		||||
@@ -7,6 +8,7 @@ namespace Ocelot.Configuration.Builder
 | 
			
		||||
        private bool _enableRateLimiting;
 | 
			
		||||
        private string _clientIdHeader;
 | 
			
		||||
        private List<string> _clientWhitelist;
 | 
			
		||||
        private Func<List<string>> _getClientWhitelist;
 | 
			
		||||
        private bool _disableRateLimitHeaders;
 | 
			
		||||
        private string _quotaExceededMessage;
 | 
			
		||||
        private string _rateLimitCounterPrefix;
 | 
			
		||||
@@ -19,15 +21,15 @@ namespace Ocelot.Configuration.Builder
 | 
			
		||||
            return this;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public RateLimitOptionsBuilder WithClientIdHeader(string clientIdheader)
 | 
			
		||||
        public RateLimitOptionsBuilder WithClientIdHeader(string clientIdHeader)
 | 
			
		||||
        {
 | 
			
		||||
            _clientIdHeader = clientIdheader;
 | 
			
		||||
            _clientIdHeader = clientIdHeader;
 | 
			
		||||
            return this;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public RateLimitOptionsBuilder WithClientWhiteList(List<string> clientWhitelist)
 | 
			
		||||
        public RateLimitOptionsBuilder WithClientWhiteList(Func<List<string>> getClientWhitelist)
 | 
			
		||||
        {
 | 
			
		||||
            _clientWhitelist = clientWhitelist;
 | 
			
		||||
            _getClientWhitelist = getClientWhitelist;
 | 
			
		||||
            return this;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -63,9 +65,9 @@ namespace Ocelot.Configuration.Builder
 | 
			
		||||
 | 
			
		||||
        public RateLimitOptions Build()
 | 
			
		||||
        {
 | 
			
		||||
            return new RateLimitOptions(_enableRateLimiting, _clientIdHeader, _clientWhitelist,
 | 
			
		||||
                _disableRateLimitHeaders, _quotaExceededMessage, _rateLimitCounterPrefix,
 | 
			
		||||
            return new RateLimitOptions(_enableRateLimiting, _clientIdHeader, _getClientWhitelist,
 | 
			
		||||
                _disableRateLimitHeaders, _quotaExceededMessage, _rateLimitCounterPrefix,
 | 
			
		||||
                _rateLimitRule, _httpStatusCode);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -11,7 +11,7 @@ namespace Ocelot.Configuration.Creator
 | 
			
		||||
            {
 | 
			
		||||
                return new RateLimitOptionsBuilder()
 | 
			
		||||
                    .WithClientIdHeader(globalConfiguration.RateLimitOptions.ClientIdHeader)
 | 
			
		||||
                    .WithClientWhiteList(fileRateLimitRule.ClientWhitelist)
 | 
			
		||||
                    .WithClientWhiteList(() => fileRateLimitRule.ClientWhitelist)
 | 
			
		||||
                    .WithDisableRateLimitHeaders(globalConfiguration.RateLimitOptions.DisableRateLimitHeaders)
 | 
			
		||||
                    .WithEnableRateLimiting(fileRateLimitRule.EnableRateLimiting)
 | 
			
		||||
                    .WithHttpStatusCode(globalConfiguration.RateLimitOptions.HttpStatusCode)
 | 
			
		||||
 
 | 
			
		||||
@@ -1,4 +1,5 @@
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Configuration
 | 
			
		||||
{
 | 
			
		||||
@@ -7,12 +8,14 @@ namespace Ocelot.Configuration
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    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)
 | 
			
		||||
        {
 | 
			
		||||
            EnableRateLimiting = enbleRateLimiting;
 | 
			
		||||
            EnableRateLimiting = enableRateLimiting;
 | 
			
		||||
            ClientIdHeader = clientIdHeader;
 | 
			
		||||
            ClientWhitelist = clientWhitelist ?? new List<string>();
 | 
			
		||||
            _getClientWhitelist = getClientWhitelist;
 | 
			
		||||
            DisableRateLimitHeaders = disableRateLimitHeaders;
 | 
			
		||||
            QuotaExceededMessage = quotaExceededMessage;
 | 
			
		||||
            RateLimitCounterPrefix = rateLimitCounterPrefix;
 | 
			
		||||
@@ -22,18 +25,21 @@ namespace Ocelot.Configuration
 | 
			
		||||
 | 
			
		||||
        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>
 | 
			
		||||
        /// Gets or sets the HTTP header that holds the client identifier, by default is X-ClientId
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        public string ClientIdHeader { get; private set; }
 | 
			
		||||
 | 
			
		||||
        public string ClientIdHeader { get; private set; }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Gets or sets the HTTP Status code returned when rate limiting occurs, by default value is set to 429 (Too Many Requests)
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        public int HttpStatusCode { get; private set; }
 | 
			
		||||
 | 
			
		||||
        public int HttpStatusCode { get; private set; }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Gets or sets a value that will be used as a formatter for the QuotaExceeded response message.
 | 
			
		||||
        /// If none specified the default will be:
 | 
			
		||||
@@ -44,8 +50,8 @@ namespace Ocelot.Configuration
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Gets or sets the counter prefix, used to compose the rate limit counter cache key
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        public string RateLimitCounterPrefix { get; private set; }
 | 
			
		||||
 | 
			
		||||
        public string RateLimitCounterPrefix { get; private set; }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Enables endpoint rate limiting based URL path and HTTP verb
 | 
			
		||||
        /// </summary>
 | 
			
		||||
@@ -56,4 +62,4 @@ namespace Ocelot.Configuration
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        public bool DisableRateLimitHeaders { get; private set; }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user