diff --git a/CHANGELOG.md b/CHANGELOG.md index dc1f4a2..6f3a28e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +v1.1.1 +- Fixed error handing to match Hydrant new API Structure + v1.1.0 - Added Support for Meta Data In Keyfactor - Put enroll on a timer to wait for request so Meta Data Could be Pulled down diff --git a/HydrantIdProxy/src/HydrantIdProxy/Client/HydrantIdClient.cs b/HydrantIdProxy/src/HydrantIdProxy/Client/HydrantIdClient.cs index 085e2c8..3fa72bb 100644 --- a/HydrantIdProxy/src/HydrantIdProxy/Client/HydrantIdClient.cs +++ b/HydrantIdProxy/src/HydrantIdProxy/Client/HydrantIdClient.cs @@ -192,8 +192,8 @@ public async Task GetSubmitGetCertificateAsync(string certificateId Logger.Error($"Error Occured in HydrantIdClient.GetSubmitGetCertificateAsync: {e.Message}"); throw; } - } - + } + public async Task GetSubmitGetCertificateByCsrAsync(string requestTrackingId) { try diff --git a/HydrantIdProxy/src/HydrantIdProxy/Client/Models/ErrorReturn.cs b/HydrantIdProxy/src/HydrantIdProxy/Client/Models/ErrorReturn.cs index 814e189..0a3ecf9 100644 --- a/HydrantIdProxy/src/HydrantIdProxy/Client/Models/ErrorReturn.cs +++ b/HydrantIdProxy/src/HydrantIdProxy/Client/Models/ErrorReturn.cs @@ -5,7 +5,7 @@ namespace Keyfactor.HydrantId.Client.Models { public class ErrorReturn : IErrorReturn { - [JsonProperty("status", NullValueHandling = NullValueHandling.Ignore)] public int Status { get; set; } - [JsonProperty("error", NullValueHandling = NullValueHandling.Ignore)] public string Error { get; set; } + [JsonProperty("status", NullValueHandling = NullValueHandling.Ignore)] public string Status { get; set; } + [JsonProperty("message", NullValueHandling = NullValueHandling.Ignore)] public string Error { get; set; } } } diff --git a/HydrantIdProxy/src/HydrantIdProxy/HydrantIdProxy.cs b/HydrantIdProxy/src/HydrantIdProxy/HydrantIdProxy.cs index 848c608..5c4cc18 100644 --- a/HydrantIdProxy/src/HydrantIdProxy/HydrantIdProxy.cs +++ b/HydrantIdProxy/src/HydrantIdProxy/HydrantIdProxy.cs @@ -1,7 +1,7 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; -using System.Diagnostics; +using System.Diagnostics; using System.Linq; using System.Security.Cryptography.X509Certificates; using System.Text; @@ -117,7 +117,7 @@ public override void Synchronize(ICertificateDataReader certificateDataReader, foreach (var cert in splitCerts) try { - var currentCert = new X509Certificate2(Encoding.ASCII.GetBytes(cert)); + var currentCert = new X509Certificate2(Encoding.ASCII.GetBytes(cert)); var caReqId = $"{currentResponseItem.Id}-{currentCert.SerialNumber}"; Logger.Trace($"Split Cert Value: {cert}"); blockingBuffer.Add(new CAConnectorCertificate @@ -195,10 +195,22 @@ public override EnrollmentResult Enroll(ICertificateDataReader certificateDataRe enrollmentResponse = Task.Run(async () => await HydrantIdClient.GetSubmitEnrollmentAsync(enrollmentRequest)) .Result; - Logger.Trace($"Enrollment Response JSON: {JsonConvert.SerializeObject(enrollmentResponse)}"); - - csrTrackingResponse = GetCertificateOnTimer(enrollmentResponse.RequestStatus.Id); - + Logger.Trace($"Enrollment Response JSON: {JsonConvert.SerializeObject(enrollmentResponse)}"); + + if (enrollmentResponse?.ErrorReturn?.Status != "Failure") + { + csrTrackingResponse = GetCertificateOnTimer(enrollmentResponse?.RequestStatus?.Id); + } + else + { + return new EnrollmentResult + { + Status = 30, //failure + StatusMessage = $"Enrollment Failed with error {enrollmentResponse?.ErrorReturn?.Error}" + }; + } + + Logger.MethodExit(ILogExtensions.MethodLogLevel.Debug); break; @@ -223,41 +235,52 @@ public override EnrollmentResult Enroll(ICertificateDataReader certificateDataRe Task.Run(async () => await HydrantIdClient.GetSubmitRenewalAsync(certificateId, renewalRequest)) .Result; - Logger.Trace($"Renew Response JSON: {JsonConvert.SerializeObject(enrollmentResponse)}"); - - - csrTrackingResponse = GetCertificateOnTimer(enrollmentResponse.RequestStatus.Id); - + Logger.Trace($"Renew Response JSON: {JsonConvert.SerializeObject(enrollmentResponse)}"); + + if (enrollmentResponse?.ErrorReturn?.Status != "Failure") + { + csrTrackingResponse = GetCertificateOnTimer(enrollmentResponse?.RequestStatus?.Id); + } + else + { + return new EnrollmentResult + { + Status = 30, //failure + StatusMessage = $"Enrollment Failed with error {enrollmentResponse?.ErrorReturn?.Error}" + }; + } break; - } - - return _requestManager.GetEnrollmentResult(csrTrackingResponse); + } + + + var cert = GetSingleRecord(csrTrackingResponse.Id.ToString()); + return _requestManager.GetEnrollmentResult(csrTrackingResponse,cert); } - private Certificate GetCertificateOnTimer(string Id) - { - //Get the csr tracking response from the tracking Id returned from Enrollment - var stopwatch = new Stopwatch(); - stopwatch.Start(); - - Certificate csrTrackingResponse = null; - - while (stopwatch.Elapsed < TimeSpan.FromSeconds(60) && csrTrackingResponse == null) - { - try - { - csrTrackingResponse = - Task.Run(async () => await HydrantIdClient.GetSubmitGetCertificateByCsrAsync(Id)) - .Result; - } - catch (System.AggregateException e) - { - Logger.Trace($"Enrollment Response Not Available Yet, try again {LogHandler.FlattenException(e)}."); - } - Thread.Sleep(1000); - } - - return csrTrackingResponse; + private Certificate GetCertificateOnTimer(string Id) + { + //Get the csr tracking response from the tracking Id returned from Enrollment + var stopwatch = new Stopwatch(); + stopwatch.Start(); + + Certificate csrTrackingResponse = null; + + while (stopwatch.Elapsed < TimeSpan.FromSeconds(30) && csrTrackingResponse == null) + { + try + { + csrTrackingResponse = + Task.Run(async () => await HydrantIdClient.GetSubmitGetCertificateByCsrAsync(Id)) + .Result; + } + catch (System.AggregateException e) + { + Logger.Trace($"Enrollment Response Not Available Yet, try again {LogHandler.FlattenException(e)}."); + } + Thread.Sleep(1000); + } + + return csrTrackingResponse; } public override CAConnectorCertificate GetSingleRecord(string caRequestId) diff --git a/HydrantIdProxy/src/HydrantIdProxy/Interfaces/IErrorReturn.cs b/HydrantIdProxy/src/HydrantIdProxy/Interfaces/IErrorReturn.cs index 2e60da5..0c6c062 100644 --- a/HydrantIdProxy/src/HydrantIdProxy/Interfaces/IErrorReturn.cs +++ b/HydrantIdProxy/src/HydrantIdProxy/Interfaces/IErrorReturn.cs @@ -2,7 +2,7 @@ { public interface IErrorReturn { - int Status { get; set; } + string Status { get; set; } string Error { get; set; } } } \ No newline at end of file diff --git a/HydrantIdProxy/src/HydrantIdProxy/RequestManager.cs b/HydrantIdProxy/src/HydrantIdProxy/RequestManager.cs index aadecc7..3938ebb 100644 --- a/HydrantIdProxy/src/HydrantIdProxy/RequestManager.cs +++ b/HydrantIdProxy/src/HydrantIdProxy/RequestManager.cs @@ -224,12 +224,12 @@ public CertRequestBodySubjectAltNames GetSansRequest(Dictionary