Skip to content

Commit

Permalink
unified sslVerify handling in both methods (bugfix)
Browse files Browse the repository at this point in the history
  • Loading branch information
GyroGearl00se committed Jun 20, 2024
1 parent 67b0154 commit 8f8d27f
Showing 1 changed file with 64 additions and 66 deletions.
130 changes: 64 additions & 66 deletions Services/SEMPService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,16 @@ namespace SolaceWebClient.Services
{
public class SEMPService
{
private readonly HttpClient _httpClient;
private readonly ILogger<SEMPService> _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<SEMPService> logger)
public SEMPService(ILogger<SEMPService> logger)
{
_httpClient = httpClient;
_logger = logger;
}

Expand All @@ -30,78 +28,73 @@ public async Task<List<GetQueuesDetails>> GetQueuesAsync(string url, string mess
var getQueuesList = new List<GetQueuesDetails>();
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;
}
}
}
}

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<ListenerPorts> 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)
Expand All @@ -111,6 +104,11 @@ public async Task<ListenerPorts> 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);
Expand All @@ -120,24 +118,24 @@ public async Task<ListenerPorts> 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;
}
}
}

0 comments on commit 8f8d27f

Please sign in to comment.