Skip to content

Commit

Permalink
ab#52646
Browse files Browse the repository at this point in the history
  • Loading branch information
leefine02 authored and leefine02 committed Jun 5, 2024
1 parent 9eae5ea commit 0adb97a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
9 changes: 5 additions & 4 deletions F5WafOrchestrator/CA/Management.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ public JobResult ProcessJob(ManagementJobConfiguration config)
private void PerformCaCertAddition(ManagementJobConfiguration config)
{
_logger.MethodEntry(LogLevel.Debug);

if (F5Client.CertificateExistsInF5(config.CertificateStoreDetails.StorePath, config.JobCertificate.Alias) && !config.Overwrite)

bool certificateExists = F5Client.CertificateExistsInF5(config.CertificateStoreDetails.StorePath, config.JobCertificate.Alias, false);

if (certificateExists && !config.Overwrite)
{
string message =
$"Certificate with alias \"{config.JobCertificate.Alias}\" already exists in F5, and job was not configured to overwrite.";
Expand All @@ -80,8 +82,7 @@ private void PerformCaCertAddition(ManagementJobConfiguration config)
}

F5WafClient.CaPostRoot reqBody = F5Client.FormatCaCertificateRequest(config.JobCertificate);
if (F5Client.CertificateExistsInF5(config.CertificateStoreDetails.StorePath, config.JobCertificate.Alias) &&
config.Overwrite)
if (certificateExists && config.Overwrite)
{
_logger.LogDebug("Overwrite is enabled, replacing certificate in F5 called \"{0}\"",
config.JobCertificate.Alias);
Expand Down
8 changes: 4 additions & 4 deletions F5WafOrchestrator/F5WafClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ internal void AddCaOrTlsCertificate(string f5Namespace, PostRoot? tlsReqBody, Ca
jsonReqBody = JsonSerializer.Serialize(tlsReqBody);
}

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, $"{HostName}/api/config/namespaces/{f5Namespace}/{isTLSCertificate}");
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, $"{HostName}/api/config/namespaces/{f5Namespace}/{certType}");

var stringReqBody = new StringContent(jsonReqBody, Encoding.UTF8, "application/json");
request.Content = stringReqBody;
Expand Down Expand Up @@ -478,17 +478,17 @@ internal void RemoveCaOrTlsCertificate(string f5Namespace, string certName, bool
_logger.MethodEntry(LogLevel.Debug);

string certType = isTLSCertificate ? "certificates" : "trusted_ca_lists";
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Delete, $"{HostName}/api/config/namespaces/{f5Namespace}/{isTLSCertificate}");
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Delete, $"{HostName}/api/config/namespaces/{f5Namespace}/{certType}");
string result = SubmitRequest(request);

_logger.MethodExit(LogLevel.Debug);
}

internal bool CertificateExistsInF5(string f5Namespace, string alias)
internal bool CertificateExistsInF5(string f5Namespace, string alias, bool isTLSCertificate)
{
_logger.MethodEntry(LogLevel.Debug);

var certsJson = GetTlsCertificatesFromF5(f5Namespace);
var certsJson = isTLSCertificate ? GetTlsCertificatesFromF5(f5Namespace) : GetCaCertificatesFromF5(f5Namespace);
var certs = JsonDocument.Parse(certsJson);

_logger.MethodExit(LogLevel.Debug);
Expand Down
9 changes: 4 additions & 5 deletions F5WafOrchestrator/TLS/Management.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,17 @@ private void PerformTlsCertAddition(ManagementJobConfiguration config)
{
_logger.MethodEntry(LogLevel.Debug);

if (F5Client.CertificateExistsInF5(config.CertificateStoreDetails.StorePath, config.JobCertificate.Alias) &&
!config.Overwrite)
bool certificateExists = F5Client.CertificateExistsInF5(config.CertificateStoreDetails.StorePath, config.JobCertificate.Alias, true);

if (certificateExists && !config.Overwrite)
{
string message =
$"Certificate with alias \"{config.JobCertificate.Alias}\" already exists in F5, and job was not configured to overwrite.";
throw new Exception(message);
}

F5WafClient.PostRoot reqBody = F5Client.FormatTlsCertificateRequest(config.JobCertificate);
if (F5Client.CertificateExistsInF5(config.CertificateStoreDetails.StorePath,
config.JobCertificate.Alias) &&
config.Overwrite)
if (certificateExists && config.Overwrite)
{
_logger.LogDebug("Overwrite is enabled, replacing certificate in F5 called \"{0}\"",
config.JobCertificate.Alias);
Expand Down

0 comments on commit 0adb97a

Please sign in to comment.