From 8f8d27f162263f89ef1817ae39db2aa18e878a6b Mon Sep 17 00:00:00 2001 From: GyroGearl00se Date: Thu, 20 Jun 2024 17:25:33 +0200 Subject: [PATCH] unified sslVerify handling in both methods (bugfix) --- Services/SEMPService.cs | 130 ++++++++++++++++++++-------------------- 1 file changed, 64 insertions(+), 66 deletions(-) diff --git a/Services/SEMPService.cs b/Services/SEMPService.cs index 96fb49e..f093b5e 100644 --- a/Services/SEMPService.cs +++ b/Services/SEMPService.cs @@ -10,18 +10,16 @@ namespace SolaceWebClient.Services { public class SEMPService { - private readonly HttpClient _httpClient; private readonly ILogger _logger; public class GetQueuesDetails { - public string queueName { get; set; } - public string queueOwner { get; set; } + public string QueueName { get; set; } + public string QueueOwner { get; set; } } - public SEMPService(HttpClient httpClient, ILogger logger) + public SEMPService(ILogger logger) { - _httpClient = httpClient; _logger = logger; } @@ -30,53 +28,56 @@ public async Task> GetQueuesAsync(string url, string mess var getQueuesList = new List(); string requestUrl = $"{url}/SEMP/v2/config/msgVpns/{messageVpn}/queues?count=100"; - while (!string.IsNullOrEmpty(requestUrl)) + var handler = new HttpClientHandler(); + if (!sslVerify) { - var request = new HttpRequestMessage(HttpMethod.Get, requestUrl); - var authToken = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes($"{username}:{password}")); - request.Headers.Authorization = new AuthenticationHeaderValue("Basic", authToken); + handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true; + } - if (!sslVerify) - { - // Bypass SSL certificate validation - _httpClient.DefaultRequestHeaders.Add("IgnoreSslErrors", "true"); - } - _httpClient.DefaultRequestHeaders.Add("count", "100"); - HttpResponseMessage response; - try - { - response = await _httpClient.SendAsync(request); - } - catch (HttpRequestException e) + using (var client = new HttpClient(handler)) + { + while (!string.IsNullOrEmpty(requestUrl)) { - _logger.LogError($"Request error: {e.Message}"); - throw; - } + var request = new HttpRequestMessage(HttpMethod.Get, requestUrl); + var authToken = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes($"{username}:{password}")); + request.Headers.Authorization = new AuthenticationHeaderValue("Basic", authToken); - response.EnsureSuccessStatusCode(); - - var responseBody = await response.Content.ReadAsStringAsync(); - - using (JsonDocument doc = JsonDocument.Parse(responseBody)) - { - foreach (var element in doc.RootElement.GetProperty("data").EnumerateArray()) + HttpResponseMessage response; + try { - getQueuesList.Add(new GetQueuesDetails - { - queueName = element.GetProperty("queueName").GetString(), - queueOwner = element.GetProperty("owner").GetString() - }); + response = await client.SendAsync(request); } - - if (doc.RootElement.TryGetProperty("meta", out JsonElement metaElement) && - metaElement.TryGetProperty("paging", out JsonElement pagingElement) && - pagingElement.TryGetProperty("nextPageUri", out JsonElement nextPageUriElement)) + catch (HttpRequestException e) { - requestUrl = nextPageUriElement.GetString(); + _logger.LogError($"Request error: {e.Message}"); + throw; } - else + + response.EnsureSuccessStatusCode(); + + var responseBody = await response.Content.ReadAsStringAsync(); + + using (JsonDocument doc = JsonDocument.Parse(responseBody)) { - requestUrl = null; + foreach (var element in doc.RootElement.GetProperty("data").EnumerateArray()) + { + getQueuesList.Add(new GetQueuesDetails + { + QueueName = element.GetProperty("queueName").GetString(), + QueueOwner = element.GetProperty("owner").GetString() + }); + } + + if (doc.RootElement.TryGetProperty("meta", out JsonElement metaElement) && + metaElement.TryGetProperty("paging", out JsonElement pagingElement) && + pagingElement.TryGetProperty("nextPageUri", out JsonElement nextPageUriElement)) + { + requestUrl = nextPageUriElement.GetString(); + } + else + { + requestUrl = null; + } } } } @@ -84,24 +85,16 @@ public async Task> GetQueuesAsync(string url, string mess return getQueuesList; } - - public class ListenerPorts { public int SmfPort { get; set; } public int SmfsPort { get; set; } - public bool SmfsEnabled { get; set; } + public bool SmfsEnabled { get; set; } } - public async Task GetListenerAsync(string url, string messageVpn, string username, string password, bool sslVerify) { string requestUrl = $"{url}/SEMP/v2/config"; - var request = new HttpRequestMessage(HttpMethod.Get, requestUrl); - var authToken = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes($"{username}:{password}")); - request.Headers.Authorization = new AuthenticationHeaderValue("Basic", authToken); - - HttpResponseMessage response; var handler = new HttpClientHandler(); if (!sslVerify) @@ -111,6 +104,11 @@ public async Task GetListenerAsync(string url, string messageVpn, using (var client = new HttpClient(handler)) { + var request = new HttpRequestMessage(HttpMethod.Get, requestUrl); + var authToken = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes($"{username}:{password}")); + request.Headers.Authorization = new AuthenticationHeaderValue("Basic", authToken); + + HttpResponseMessage response; try { response = await client.SendAsync(request); @@ -120,24 +118,24 @@ public async Task GetListenerAsync(string url, string messageVpn, _logger.LogError($"Request error: {e.Message}"); throw; } - } - response.EnsureSuccessStatusCode(); + response.EnsureSuccessStatusCode(); - var responseBody = await response.Content.ReadAsStringAsync(); - ListenerPorts config = new ListenerPorts(); + var responseBody = await response.Content.ReadAsStringAsync(); + ListenerPorts config = new ListenerPorts(); - using (JsonDocument doc = JsonDocument.Parse(responseBody)) - { - var msgVpnElement = doc.RootElement.GetProperty("data"); - config.SmfPort = msgVpnElement.GetProperty("serviceSmfPlainTextListenPort").GetInt32(); - config.SmfsPort = msgVpnElement.GetProperty("serviceSmfTlsListenPort").GetInt32(); - config.SmfsEnabled = msgVpnElement.GetProperty("serviceSempTlsEnabled").GetBoolean(); + using (JsonDocument doc = JsonDocument.Parse(responseBody)) + { + var msgVpnElement = doc.RootElement.GetProperty("data"); + config.SmfPort = msgVpnElement.GetProperty("serviceSmfPlainTextListenPort").GetInt32(); + config.SmfsPort = msgVpnElement.GetProperty("serviceSmfTlsListenPort").GetInt32(); + config.SmfsEnabled = msgVpnElement.GetProperty("serviceSempTlsEnabled").GetBoolean(); + } + _logger.LogInformation($"SmfsEnabled: {config.SmfsEnabled}"); + _logger.LogInformation($"SmfPort: {config.SmfPort}"); + _logger.LogInformation($"SmfsPort: {config.SmfsPort}"); + return config; } - _logger.LogInformation($"SmfsEnabled: {config.SmfsEnabled}"); - _logger.LogInformation($"SmfPort: {config.SmfPort}"); - _logger.LogInformation($"SmfsPort: {config.SmfsPort}"); - return config; } } }