feat(cluster): parse OTOPCUA_ROLES env var with validation
This commit is contained in:
29
src/Core/ZB.MOM.WW.OtOpcUa.Cluster/RoleParser.cs
Normal file
29
src/Core/ZB.MOM.WW.OtOpcUa.Cluster/RoleParser.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
namespace ZB.MOM.WW.OtOpcUa.Cluster;
|
||||
|
||||
public static class RoleParser
|
||||
{
|
||||
private static readonly HashSet<string> Allowed = new(StringComparer.Ordinal)
|
||||
{
|
||||
"admin", "driver", "dev",
|
||||
};
|
||||
|
||||
public static string[] Parse(string? raw)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(raw)) return Array.Empty<string>();
|
||||
|
||||
var roles = raw
|
||||
.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
|
||||
.Select(r => r.ToLowerInvariant())
|
||||
.Distinct()
|
||||
.ToArray();
|
||||
|
||||
foreach (var r in roles)
|
||||
{
|
||||
if (!Allowed.Contains(r))
|
||||
throw new ArgumentException(
|
||||
$"Unknown role '{r}'. Allowed: {string.Join(", ", Allowed)}.", nameof(raw));
|
||||
}
|
||||
|
||||
return roles;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user