stabilizaiton work - projects rework for maintenanceability and ui livening
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace StellaOps.Configuration;
|
||||
|
||||
public sealed class AuthorityDelegationOptions
|
||||
{
|
||||
private readonly IList<AuthorityServiceAccountSeedOptions> _serviceAccounts = new List<AuthorityServiceAccountSeedOptions>();
|
||||
private readonly Dictionary<string, AuthorityTenantDelegationOptions> _tenantOverrides = new(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
public AuthorityDelegationQuotaOptions Quotas { get; } = new();
|
||||
|
||||
public IList<AuthorityServiceAccountSeedOptions> ServiceAccounts => (IList<AuthorityServiceAccountSeedOptions>)_serviceAccounts;
|
||||
|
||||
internal void NormalizeAndValidate(IList<AuthorityTenantOptions> tenants)
|
||||
{
|
||||
Quotas.Validate(nameof(Quotas));
|
||||
|
||||
var tenantIds = tenants is { Count: > 0 }
|
||||
? tenants
|
||||
.Where(static tenant => !string.IsNullOrWhiteSpace(tenant.Id))
|
||||
.Select(static tenant => tenant.Id.Trim())
|
||||
.ToHashSet(StringComparer.OrdinalIgnoreCase)
|
||||
: new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
var seenAccounts = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
||||
_tenantOverrides.Clear();
|
||||
foreach (var tenant in tenants)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(tenant.Id))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var normalizedTenant = tenant.Id.Trim().ToLowerInvariant();
|
||||
_tenantOverrides[normalizedTenant] = tenant.Delegation;
|
||||
}
|
||||
|
||||
foreach (var account in _serviceAccounts)
|
||||
{
|
||||
account.Normalize();
|
||||
account.Validate(tenantIds);
|
||||
|
||||
if (!seenAccounts.Add(account.AccountId))
|
||||
{
|
||||
throw new InvalidOperationException($"Delegation configuration contains duplicate service account id '{account.AccountId}'.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int ResolveMaxActiveTokens(string? tenantId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(tenantId))
|
||||
{
|
||||
return Quotas.MaxActiveTokens;
|
||||
}
|
||||
|
||||
var normalized = tenantId.Trim().ToLowerInvariant();
|
||||
if (_tenantOverrides.TryGetValue(normalized, out var options))
|
||||
{
|
||||
return options.ResolveMaxActiveTokens(this);
|
||||
}
|
||||
|
||||
return Quotas.MaxActiveTokens;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user