feat(server): add observability and operator controls (#16)
quality-gate / quality (push) Failing after 1m1s
quality-gate / quality (push) Failing after 1m1s
This commit is contained in:
@@ -19,16 +19,71 @@ internal sealed class HttpAbuseProtectionMiddleware(
|
||||
string operation = context.GetEndpoint()?.Metadata.GetMetadata<IEndpointNameMetadata>()
|
||||
?.EndpointName ?? "Unmatched";
|
||||
bool healthEndpoint = operation is "GetLiveness" or "GetReadiness";
|
||||
bool acquired = healthEndpoint
|
||||
? protection.TryAcquireHealthIngress(
|
||||
bool operatorEndpoint = operation is
|
||||
"GetOperatorStatus"
|
||||
or "RevokeOperatorListing"
|
||||
or "RevokeOperatorPrincipal"
|
||||
or "RevokeOperatorSigningKey"
|
||||
or "BeginOperatorDrain";
|
||||
if (operatorEndpoint
|
||||
&& !protection.IsOperatorSourceAllowed(context.Connection.RemoteIpAddress))
|
||||
{
|
||||
bool deniedSourceAdmitted = protection.TryAcquireHttpIngress(
|
||||
context.Connection.RemoteIpAddress,
|
||||
out AbuseProtectionService.AbuseLease? lease,
|
||||
out int retryAfterSeconds)
|
||||
: protection.TryAcquireHttpIngress(
|
||||
"Unmatched",
|
||||
out AbuseProtectionService.AbuseLease? deniedSourceLease,
|
||||
out int deniedRetryAfterSeconds);
|
||||
using (deniedSourceLease)
|
||||
{
|
||||
if (!deniedSourceAdmitted)
|
||||
{
|
||||
context.Response.Headers.RetryAfter = deniedRetryAfterSeconds.ToString(
|
||||
System.Globalization.CultureInfo.InvariantCulture);
|
||||
await WriteErrorAsync(
|
||||
context,
|
||||
StatusCodes.Status429TooManyRequests,
|
||||
RendezvousErrorCode.RateLimited,
|
||||
"The request rate limit was exceeded.",
|
||||
deniedRetryAfterSeconds).ConfigureAwait(false);
|
||||
return;
|
||||
}
|
||||
|
||||
await WriteErrorAsync(
|
||||
context,
|
||||
StatusCodes.Status404NotFound,
|
||||
RendezvousErrorCode.NotFound,
|
||||
"The requested resource was not found.").ConfigureAwait(false);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
AbuseProtectionService.AbuseLease? lease;
|
||||
int retryAfterSeconds;
|
||||
bool acquired;
|
||||
if (healthEndpoint)
|
||||
{
|
||||
acquired = protection.TryAcquireHealthIngress(
|
||||
context.Connection.RemoteIpAddress,
|
||||
out lease,
|
||||
out retryAfterSeconds);
|
||||
}
|
||||
else if (operatorEndpoint)
|
||||
{
|
||||
acquired = protection.TryAcquireOperatorIngress(
|
||||
context.Connection.RemoteIpAddress,
|
||||
out lease,
|
||||
out retryAfterSeconds);
|
||||
}
|
||||
else
|
||||
{
|
||||
acquired = protection.TryAcquireHttpIngress(
|
||||
context.Connection.RemoteIpAddress,
|
||||
operation,
|
||||
out lease,
|
||||
out retryAfterSeconds);
|
||||
}
|
||||
|
||||
if (!acquired)
|
||||
{
|
||||
context.Response.Headers.RetryAfter = retryAfterSeconds.ToString(
|
||||
|
||||
Reference in New Issue
Block a user