Initial commit: ESB Certificate Manager.
EOF Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,503 @@
|
||||
using System.Net;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using ZA.CoreService.ESBCertificateManager.Models;
|
||||
|
||||
namespace ZA.CoreService.ESBCertificateManager.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Verwaltet Sonic ESB Container über die Management Console.
|
||||
///
|
||||
/// Modus = WinRm (Standard, Sonic 10.x):
|
||||
/// PowerShell Remoting (Invoke-Command) auf dem Sonic-Server.
|
||||
/// Erfordert WinRM auf dem Zielrechner: Enable-PSRemoting -Force
|
||||
///
|
||||
/// Modus = HttpApi:
|
||||
/// HTTP REST API mit automatischer Pfad-Erkennung.
|
||||
/// Probiert: /mf/rest/v1, /api/v1, /sonic/management, /containers
|
||||
/// </summary>
|
||||
public sealed class SonicManagementClient : IDisposable
|
||||
{
|
||||
private readonly SonicConnection _connection;
|
||||
private readonly HttpClient? _http;
|
||||
private readonly WinRmExecutor? _winRm;
|
||||
|
||||
// Gecachter HTTP-Basis-Pfad (nur im HttpApi-Modus)
|
||||
private string? _resolvedContainerBasePath;
|
||||
|
||||
private static readonly string[] CandidateContainerPaths =
|
||||
[
|
||||
"/mf/rest/v1/domains/{domain}/containers",
|
||||
"/api/v1/domains/{domain}/containers",
|
||||
"/sonic/management/domains/{domain}/containers",
|
||||
"/containers"
|
||||
];
|
||||
|
||||
public SonicManagementClient(SonicConnection connection)
|
||||
{
|
||||
_connection = connection;
|
||||
|
||||
if (connection.ManagementMode == SonicManagementMode.WinRm)
|
||||
{
|
||||
_winRm = new WinRmExecutor(connection);
|
||||
}
|
||||
else
|
||||
{
|
||||
HttpClientHandler handler = new()
|
||||
{
|
||||
ServerCertificateCustomValidationCallback =
|
||||
HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
|
||||
};
|
||||
|
||||
_http = new HttpClient(handler)
|
||||
{
|
||||
BaseAddress = BuildHttpBaseUri(connection.ConnectionUrl, connection.ManagementHttpPort),
|
||||
Timeout = TimeSpan.FromSeconds(Math.Clamp(connection.TimeoutSeconds, 5, 300))
|
||||
};
|
||||
|
||||
string credentials = Convert.ToBase64String(
|
||||
Encoding.UTF8.GetBytes($"{connection.Username}:{connection.Password}"));
|
||||
_http.DefaultRequestHeaders.Authorization =
|
||||
new AuthenticationHeaderValue("Basic", credentials);
|
||||
_http.DefaultRequestHeaders.Accept.Add(
|
||||
new MediaTypeWithQualityHeaderValue("application/json"));
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------
|
||||
// Öffentliche API
|
||||
// ---------------------------------------------------------------
|
||||
|
||||
/// <summary>
|
||||
/// Prüft die Verbindung zur Management Console.
|
||||
/// WinRm: TCP-Ping auf WinRM-Port + Test-PSSession.
|
||||
/// Http: Probe gegen bekannte API-Pfade.
|
||||
/// </summary>
|
||||
public async Task<(bool Success, string? Error, string? ResolvedPath)> CheckConnectionAsync(
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (_connection.ManagementMode == SonicManagementMode.WinRm)
|
||||
{
|
||||
(bool ok, string? error) = await _winRm!.TestConnectionAsync(cancellationToken);
|
||||
return (ok, error, ok ? $"WinRM auf {ExtractHost(_connection.ConnectionUrl)}:{_connection.WinRmPort}" : null);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
string? path = await ResolveContainerBasePathAsync(cancellationToken);
|
||||
if (path is null)
|
||||
{
|
||||
return (false,
|
||||
$"Keine Sonic HTTP-API unter {_http!.BaseAddress} gefunden.\n" +
|
||||
$"Geprüfte Pfade: {string.Join(", ", GetCandidatePaths())}\n" +
|
||||
$"Tipp: ManagementMode auf 'WinRm' setzen falls kein HTTP-API vorhanden.",
|
||||
null);
|
||||
}
|
||||
|
||||
return (true, null, path);
|
||||
}
|
||||
catch (TaskCanceledException) when (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return (false,
|
||||
$"HTTP-Timeout nach {_connection.TimeoutSeconds}s – Port {_connection.ManagementHttpPort} nicht erreichbar.\n" +
|
||||
$"Tipp: ManagementMode auf 'WinRm' setzen.",
|
||||
null);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return (false, ex.Message, null);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Listet alle Container der Domain auf.
|
||||
/// </summary>
|
||||
public async Task<(bool Success, IReadOnlyList<string> ContainerNames, string? Error)> GetContainersAsync(
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (_connection.ManagementMode == SonicManagementMode.WinRm)
|
||||
{
|
||||
return await GetContainersViaWinRmAsync(cancellationToken);
|
||||
}
|
||||
|
||||
return await GetContainersViaHttpAsync(cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Startet den Container neu.
|
||||
/// </summary>
|
||||
public async Task<(bool Success, string Status, string? Detail)> RestartContainerAsync(
|
||||
string containerName,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (_connection.ManagementMode == SonicManagementMode.WinRm)
|
||||
{
|
||||
return await RestartViaWinRmAsync(containerName, cancellationToken);
|
||||
}
|
||||
|
||||
return await RestartViaHttpAsync(containerName, cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Importiert XApi-Ressourcen und startet den Container neu.
|
||||
/// </summary>
|
||||
public async Task<(bool Success, string Status, string? Detail)> ImportXapiAndRestartAsync(
|
||||
string containerName,
|
||||
string xapiSourcePath,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (_connection.ManagementMode == SonicManagementMode.WinRm)
|
||||
{
|
||||
return await ImportXapiViaWinRmAsync(containerName, xapiSourcePath, cancellationToken);
|
||||
}
|
||||
|
||||
return await ImportXapiViaHttpAsync(containerName, xapiSourcePath, cancellationToken);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------
|
||||
// WinRM-Implementierungen
|
||||
// ---------------------------------------------------------------
|
||||
|
||||
private async Task<(bool, IReadOnlyList<string>, string?)> GetContainersViaWinRmAsync(
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_connection.WinRmContainerListScript))
|
||||
{
|
||||
return (false, [],
|
||||
"WinRmContainerListScript ist nicht konfiguriert.\n" +
|
||||
"Beispiel: \"Get-Service -DisplayName 'Sonic*' | Select-Object -ExpandProperty DisplayName\"");
|
||||
}
|
||||
|
||||
string script = WinRmExecutor.ApplyScriptTemplate(
|
||||
_connection.WinRmContainerListScript,
|
||||
containerName: string.Empty,
|
||||
domainName: _connection.DomainName);
|
||||
|
||||
(bool ok, string? output, string? error) =
|
||||
await _winRm!.RunScriptAsync(script, cancellationToken);
|
||||
|
||||
if (!ok)
|
||||
{
|
||||
return (false, [], $"WinRM Container-Liste fehlgeschlagen: {error}");
|
||||
}
|
||||
|
||||
List<string> names = (output ?? string.Empty)
|
||||
.Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
|
||||
.Where(l => l.Length > 0)
|
||||
.ToList();
|
||||
|
||||
return (true, names, null);
|
||||
}
|
||||
|
||||
private async Task<(bool, string, string?)> RestartViaWinRmAsync(
|
||||
string containerName, CancellationToken cancellationToken)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_connection.WinRmRestartScript))
|
||||
{
|
||||
return (false, "Neustart fehlgeschlagen",
|
||||
"WinRmRestartScript ist nicht konfiguriert.\n" +
|
||||
"Beispiel für Windows-Service: \"Restart-Service -Name 'CT-ZADBService' -Force\"\n" +
|
||||
"Platzhalter {container} wird durch den Container-Namen ersetzt.");
|
||||
}
|
||||
|
||||
string script = WinRmExecutor.ApplyScriptTemplate(
|
||||
_connection.WinRmRestartScript,
|
||||
containerName,
|
||||
_connection.DomainName);
|
||||
|
||||
(bool ok, string? output, string? error) =
|
||||
await _winRm!.RunScriptAsync(script, cancellationToken);
|
||||
|
||||
if (!ok)
|
||||
{
|
||||
return (false, "Neustart fehlgeschlagen (WinRM)",
|
||||
$"Fehler: {error}\nAusgabe: {output}");
|
||||
}
|
||||
|
||||
await Task.Delay(
|
||||
TimeSpan.FromSeconds(Math.Clamp(_connection.PostRestartDelaySeconds, 0, 120)),
|
||||
cancellationToken);
|
||||
|
||||
return (true, "Container neugestartet (WinRM)",
|
||||
$"Ausgabe: {output ?? "(keine)"}");
|
||||
}
|
||||
|
||||
private async Task<(bool, string, string?)> ImportXapiViaWinRmAsync(
|
||||
string containerName, string xapiSourcePath, CancellationToken cancellationToken)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_connection.WinRmXapiImportScript))
|
||||
{
|
||||
return (false, "XApi-Import fehlgeschlagen",
|
||||
"WinRmXapiImportScript ist nicht konfiguriert.");
|
||||
}
|
||||
|
||||
string script = WinRmExecutor.ApplyScriptTemplate(
|
||||
_connection.WinRmXapiImportScript,
|
||||
containerName,
|
||||
_connection.DomainName,
|
||||
xapiSourcePath);
|
||||
|
||||
(bool importOk, string? importOut, string? importErr) =
|
||||
await _winRm!.RunScriptAsync(script, cancellationToken);
|
||||
|
||||
if (!importOk)
|
||||
{
|
||||
return (false, "XApi-Import fehlgeschlagen (WinRM)", importErr ?? importOut);
|
||||
}
|
||||
|
||||
(bool restartOk, string restartStatus, string? restartDetail) =
|
||||
await RestartViaWinRmAsync(containerName, cancellationToken);
|
||||
|
||||
return restartOk
|
||||
? (true, "XApi importiert + Container neugestartet (WinRM)",
|
||||
$"Import: {importOut} | Restart: {restartDetail}")
|
||||
: (false, restartStatus, restartDetail);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------
|
||||
// HTTP-Implementierungen
|
||||
// ---------------------------------------------------------------
|
||||
|
||||
private async Task<(bool, IReadOnlyList<string>, string?)> GetContainersViaHttpAsync(
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
string? basePath = await ResolveContainerBasePathAsync(cancellationToken);
|
||||
if (basePath is null)
|
||||
{
|
||||
return (false, [], "Kein HTTP-API-Pfad gefunden. ManagementMode auf 'WinRm' setzen?");
|
||||
}
|
||||
|
||||
using HttpResponseMessage response = await _http!.GetAsync(basePath, cancellationToken);
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
return (false, [], $"HTTP {(int)response.StatusCode}: {response.ReasonPhrase}");
|
||||
}
|
||||
|
||||
string json = await response.Content.ReadAsStringAsync(cancellationToken);
|
||||
return (true, ParseContainerNames(json), null);
|
||||
}
|
||||
catch (Exception ex) when (ex is not OperationCanceledException)
|
||||
{
|
||||
return (false, [], ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<(bool, string, string?)> RestartViaHttpAsync(
|
||||
string containerName, CancellationToken cancellationToken)
|
||||
{
|
||||
string encoded = Uri.EscapeDataString(containerName);
|
||||
|
||||
try
|
||||
{
|
||||
string? basePath = await ResolveContainerBasePathAsync(cancellationToken);
|
||||
if (basePath is null)
|
||||
{
|
||||
return (false, "Neustart fehlgeschlagen",
|
||||
"Kein HTTP-API-Pfad gefunden. ManagementMode auf 'WinRm' setzen.");
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(_connection.ContainerRestartPath))
|
||||
{
|
||||
string customPath = ApplyTemplate(_connection.ContainerRestartPath, encoded);
|
||||
(bool ok, _, string? d) = await PostAsync(customPath, cancellationToken);
|
||||
if (ok) { await DelayAsync(cancellationToken); return (true, "Container neugestartet", d); }
|
||||
}
|
||||
|
||||
(bool r1Ok, _, string? r1d) = await PostAsync($"{basePath}/{encoded}/restart", cancellationToken);
|
||||
if (r1Ok) { await DelayAsync(cancellationToken); return (true, "Container neugestartet", r1d); }
|
||||
|
||||
(bool r2Ok, _, string? r2d) = await PutStateAsync(basePath, encoded, "running", cancellationToken);
|
||||
if (r2Ok) { await DelayAsync(cancellationToken); return (true, "Container neugestartet (State-API)", r2d); }
|
||||
|
||||
return await StopThenStartAsync(basePath, encoded, cancellationToken);
|
||||
}
|
||||
catch (OperationCanceledException) { throw; }
|
||||
catch (Exception ex) { return (false, "Neustart fehlgeschlagen", ex.Message); }
|
||||
}
|
||||
|
||||
private async Task<(bool, string, string?)> ImportXapiViaHttpAsync(
|
||||
string containerName, string xapiSourcePath, CancellationToken cancellationToken)
|
||||
{
|
||||
if (!File.Exists(xapiSourcePath))
|
||||
{
|
||||
return (false, "XApi-Import fehlgeschlagen", $"Quelldatei nicht gefunden: {xapiSourcePath}");
|
||||
}
|
||||
|
||||
string encoded = Uri.EscapeDataString(containerName);
|
||||
string? basePath = await ResolveContainerBasePathAsync(cancellationToken);
|
||||
if (basePath is null) return (false, "XApi-Import fehlgeschlagen", "Kein HTTP-API-Pfad.");
|
||||
|
||||
string path = $"{basePath}/{encoded}/xapi/import";
|
||||
await using FileStream fs = File.OpenRead(xapiSourcePath);
|
||||
string mt = xapiSourcePath.EndsWith(".zip", StringComparison.OrdinalIgnoreCase) ? "application/zip" : "application/xml";
|
||||
using StreamContent content = new(fs);
|
||||
content.Headers.ContentType = new MediaTypeHeaderValue(mt);
|
||||
using HttpResponseMessage rsp = await _http!.PostAsync(path, content, cancellationToken);
|
||||
|
||||
if (!rsp.IsSuccessStatusCode)
|
||||
{
|
||||
string body = await rsp.Content.ReadAsStringAsync(cancellationToken);
|
||||
return (false, "XApi-Import fehlgeschlagen", $"HTTP {(int)rsp.StatusCode}: {Truncate(body)}");
|
||||
}
|
||||
|
||||
(bool restartOk, string rs, string? rd) = await RestartViaHttpAsync(containerName, cancellationToken);
|
||||
return restartOk
|
||||
? (true, "XApi importiert + Container neugestartet", rd)
|
||||
: (false, rs, rd);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------
|
||||
// HTTP-Pfad-Erkennung
|
||||
// ---------------------------------------------------------------
|
||||
|
||||
private async Task<string?> ResolveContainerBasePathAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (_resolvedContainerBasePath is not null) return _resolvedContainerBasePath;
|
||||
|
||||
foreach (string p in GetCandidatePaths())
|
||||
{
|
||||
try
|
||||
{
|
||||
using HttpResponseMessage r = await _http!.GetAsync(p, cancellationToken);
|
||||
if (r.IsSuccessStatusCode || r.StatusCode == HttpStatusCode.Unauthorized)
|
||||
{
|
||||
_resolvedContainerBasePath = p;
|
||||
return p;
|
||||
}
|
||||
}
|
||||
catch (HttpRequestException) { }
|
||||
catch (TaskCanceledException) when (!cancellationToken.IsCancellationRequested) { }
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private IEnumerable<string> GetCandidatePaths()
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(_connection.ContainerListPath))
|
||||
yield return ApplyTemplate(_connection.ContainerListPath, string.Empty).TrimEnd('/');
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(_connection.ApiBasePath))
|
||||
{
|
||||
string d = Uri.EscapeDataString(_connection.DomainName);
|
||||
yield return $"{_connection.ApiBasePath.TrimEnd('/')}/domains/{d}/containers";
|
||||
}
|
||||
|
||||
foreach (string pattern in CandidateContainerPaths)
|
||||
yield return pattern.Replace("{domain}", Uri.EscapeDataString(_connection.DomainName), StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------
|
||||
// HTTP-Aktions-Helfer
|
||||
// ---------------------------------------------------------------
|
||||
|
||||
private async Task<(bool, string, string?)> PostAsync(string path, CancellationToken ct)
|
||||
{
|
||||
using HttpResponseMessage r = await _http!.PostAsync(path, null, ct);
|
||||
if (r.IsSuccessStatusCode)
|
||||
return (true, $"OK ({(int)r.StatusCode})", $"POST {path} → {(int)r.StatusCode}");
|
||||
string body = await r.Content.ReadAsStringAsync(ct);
|
||||
return (false, $"Fehler ({(int)r.StatusCode})", $"POST {path} → {(int)r.StatusCode}: {Truncate(body)}");
|
||||
}
|
||||
|
||||
private async Task<(bool, string, string?)> PutStateAsync(
|
||||
string basePath, string encoded, string state, CancellationToken ct)
|
||||
{
|
||||
string path = $"{basePath}/{encoded}";
|
||||
using StringContent body = new($"{{\"state\":\"{state}\"}}", Encoding.UTF8, "application/json");
|
||||
using HttpResponseMessage r = await _http!.PutAsync(path, body, ct);
|
||||
if (r.IsSuccessStatusCode)
|
||||
return (true, $"State={state}", $"PUT {path} state={state} → {(int)r.StatusCode}");
|
||||
string b = await r.Content.ReadAsStringAsync(ct);
|
||||
return (false, "State fehlgeschlagen", $"PUT {path} → {(int)r.StatusCode}: {Truncate(b)}");
|
||||
}
|
||||
|
||||
private async Task<(bool, string, string?)> StopThenStartAsync(
|
||||
string basePath, string encoded, CancellationToken ct)
|
||||
{
|
||||
string stopPath = string.IsNullOrWhiteSpace(_connection.ContainerStopPath)
|
||||
? $"{basePath}/{encoded}/stop"
|
||||
: ApplyTemplate(_connection.ContainerStopPath, encoded);
|
||||
|
||||
string startPath = string.IsNullOrWhiteSpace(_connection.ContainerStartPath)
|
||||
? $"{basePath}/{encoded}/start"
|
||||
: ApplyTemplate(_connection.ContainerStartPath, encoded);
|
||||
|
||||
(bool sOk, _, string? sd) = await PostAsync(stopPath, ct);
|
||||
if (!sOk) return (false, "Container-Stop fehlgeschlagen", sd);
|
||||
|
||||
await Task.Delay(TimeSpan.FromSeconds(3), ct);
|
||||
|
||||
(bool stOk, _, string? std) = await PostAsync(startPath, ct);
|
||||
if (!stOk) return (false, "Container-Start fehlgeschlagen", std);
|
||||
|
||||
await DelayAsync(ct);
|
||||
return (true, "Container neugestartet (Stop+Start)", $"{sd} | {std}");
|
||||
}
|
||||
|
||||
private async Task DelayAsync(CancellationToken ct)
|
||||
{
|
||||
int d = Math.Clamp(_connection.PostRestartDelaySeconds, 0, 120);
|
||||
if (d > 0) await Task.Delay(TimeSpan.FromSeconds(d), ct);
|
||||
}
|
||||
|
||||
private string ApplyTemplate(string template, string encodedName)
|
||||
=> template
|
||||
.Replace("{domain}", Uri.EscapeDataString(_connection.DomainName), StringComparison.OrdinalIgnoreCase)
|
||||
.Replace("{container}", encodedName, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
private static Uri BuildHttpBaseUri(string connectionUrl, int httpPort)
|
||||
{
|
||||
try { return new Uri($"http://{new Uri(connectionUrl).Host}:{httpPort}"); }
|
||||
catch { return new Uri(connectionUrl); }
|
||||
}
|
||||
|
||||
private static string ExtractHost(string connectionUrl)
|
||||
{
|
||||
try { return new Uri(connectionUrl).Host; }
|
||||
catch { return connectionUrl; }
|
||||
}
|
||||
|
||||
private static List<string> ParseContainerNames(string json)
|
||||
{
|
||||
try
|
||||
{
|
||||
using JsonDocument doc = JsonDocument.Parse(json);
|
||||
JsonElement root = doc.RootElement;
|
||||
List<string> names = [];
|
||||
|
||||
IEnumerable<JsonElement> elements = root.ValueKind == JsonValueKind.Array
|
||||
? root.EnumerateArray()
|
||||
: root.ValueKind == JsonValueKind.Object
|
||||
? new[] { "containers", "data", "items", "result" }
|
||||
.Where(root.TryGetProperty)
|
||||
.SelectMany(k => { root.TryGetProperty(k, out JsonElement a); return a.EnumerateArray(); })
|
||||
: [];
|
||||
|
||||
foreach (JsonElement el in elements)
|
||||
{
|
||||
string? name = el.ValueKind == JsonValueKind.String
|
||||
? el.GetString()
|
||||
: new[] { "name", "containerName", "id", "configId" }
|
||||
.Where(k => el.TryGetProperty(k, out _))
|
||||
.Select(k => { el.TryGetProperty(k, out JsonElement p); return p.GetString(); })
|
||||
.FirstOrDefault();
|
||||
if (name is not null) names.Add(name);
|
||||
}
|
||||
|
||||
return names;
|
||||
}
|
||||
catch { return []; }
|
||||
}
|
||||
|
||||
private static string Truncate(string v, int max = 400)
|
||||
=> v.Length <= max ? v : v[..max] + "…";
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_http?.Dispose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user