mirror of
https://github.com/bitwarden/server
synced 2025-12-25 04:33:26 +00:00
[PS-93] Distributed Ip rate limiting (#2060)
* Upgrade AspNetCoreRateLimiter and enable redis distributed cache for rate limiting. - Upgrades AspNetCoreRateLimiter to 4.0.2, which required updating NewtonSoft.Json to 13.0.1. - Replaces Microsoft.Extensions.Caching.Redis with Microsoft.Extensions.Caching.StackExchangeRedis as the original was deprecated and conflicted with the latest AspNetCoreRateLimiter - Adds startup task to Program.cs for Api/Identity projects to support AspNetCoreRateLimiters breaking changes for seeding its stores. - Adds a Redis connection string option to GlobalSettings Signed-off-by: Shane Melton <smelton@bitwarden.com> * Cleanup Redis distributed cache registration - Add new AddDistributedCache service collection extension to add either a Memory or Redis distributed cache. - Remove distributed cache registration from Identity service collection extension. - Add IpRateLimitSeedStartupService.cs to run at application startup to seed the Ip rate limiting policies. Signed-off-by: Shane Melton <smelton@bitwarden.com> * Add caching configuration to SSO Startup.cs Signed-off-by: Shane Melton <smelton@bitwarden.com> * Add ProjectName as an instance name for Redis options Signed-off-by: Shane Melton <smelton@bitwarden.com> * Use distributed cache in CustomIpRateLimitMiddleware.cs Signed-off-by: Shane Melton <smelton@bitwarden.com> * Undo changes to Program.cs and launchSettings.json * Move new service collection extensions to SharedWeb * Upgrade Caching.StackExchangeRedis package to v6 * Cleanup and fix leftover merge conflicts * Remove use of Newtonsoft.Json in distributed cache extensions * Cleanup more formatting * Fix formatting * Fix startup issue caused by merge and fix integration test Signed-off-by: Shane Melton <smelton@bitwarden.com> * Linting fix Signed-off-by: Shane Melton <smelton@bitwarden.com>
This commit is contained in:
48
src/Core/Utilities/DistributedCacheExtensions.cs
Normal file
48
src/Core/Utilities/DistributedCacheExtensions.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using System.Text.Json;
|
||||
using Microsoft.Extensions.Caching.Distributed;
|
||||
|
||||
namespace Bit.Core.Utilities
|
||||
{
|
||||
public static class DistributedCacheExtensions
|
||||
{
|
||||
public static void Set<T>(this IDistributedCache cache, string key, T value)
|
||||
{
|
||||
Set(cache, key, value, new DistributedCacheEntryOptions());
|
||||
}
|
||||
|
||||
public static void Set<T>(this IDistributedCache cache, string key, T value,
|
||||
DistributedCacheEntryOptions options)
|
||||
{
|
||||
var bytes = JsonSerializer.SerializeToUtf8Bytes(value);
|
||||
cache.Set(key, bytes, options);
|
||||
}
|
||||
|
||||
public static Task SetAsync<T>(this IDistributedCache cache, string key, T value)
|
||||
{
|
||||
return SetAsync(cache, key, value, new DistributedCacheEntryOptions());
|
||||
}
|
||||
|
||||
public static Task SetAsync<T>(this IDistributedCache cache, string key, T value,
|
||||
DistributedCacheEntryOptions options)
|
||||
{
|
||||
var bytes = JsonSerializer.SerializeToUtf8Bytes(value);
|
||||
return cache.SetAsync(key, bytes, options);
|
||||
}
|
||||
|
||||
public static bool TryGetValue<T>(this IDistributedCache cache, string key, out T value)
|
||||
{
|
||||
var val = cache.Get(key);
|
||||
value = default;
|
||||
if (val == null) return false;
|
||||
try
|
||||
{
|
||||
value = JsonSerializer.Deserialize<T>(val);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user