Skip to content

Commit 0d3f83b

Browse files
feat: Incremental Sync Support for DCOM Gateway
* Incremental Sync Support for DCOM Gateway --------- Co-authored-by: Morgan Gangwere <470584+indrora@users.noreply.github.com>
1 parent 1300102 commit 0d3f83b

File tree

6 files changed

+78
-35
lines changed

6 files changed

+78
-35
lines changed

.claude/settings.local.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"permissions": {
3+
"allow": [
4+
"Bash(dotnet build:*)"
5+
]
6+
}
7+
}

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
v1.2.0
2+
- Incremental Sync Support with effectiveDate within last 5 days
3+
14
v1.1.2
25
- Fix Renewal bug referencing the wrong REST Resource V1
36

CscGlobalCaProxy/Client/CscGlobalClient.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,25 @@ public async Task<CertificateListResponse> SubmitCertificateListRequestAsync()
163163
return certificateListResponse;
164164
}
165165

166+
public async Task<CertificateListResponse> SubmitIncrementalCertificateListRequestAsync(DateTime effectiveDate)
167+
{
168+
Logger.MethodEntry(ILogExtensions.MethodLogLevel.Debug);
169+
var dateFilter = effectiveDate.ToString("yyyy/MM/dd");
170+
Logger.Trace($"Incremental Sync with effectiveDate filter: {dateFilter}");
171+
var resp = RestClient.GetAsync($"/dbs/api/v2/tls/certificate?filter=effectiveDate=ge={dateFilter}").Result;
172+
173+
if (!resp.IsSuccessStatusCode)
174+
{
175+
var responseMessage = resp.Content.ReadAsStringAsync().Result;
176+
Logger.Error(
177+
$"Failed Request to Keyfactor. Retrying request. Status Code {resp.StatusCode} | Message: {responseMessage}");
178+
}
179+
180+
var certificateListResponse =
181+
JsonConvert.DeserializeObject<CertificateListResponse>(await resp.Content.ReadAsStringAsync());
182+
return certificateListResponse;
183+
}
184+
166185
private HttpClient ConfigureRestClient()
167186
{
168187
var clientHandler = new WebRequestHandler();

CscGlobalCaProxy/Constants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ public class Constants
66
public static string CscGlobalApiKey = "ApiKey";
77
public static string BearerToken = "BearerToken";
88
public static int DefaultPageSize = 100;
9+
public static int IncrementalSyncDays = 5;
910
}
1011
}

CscGlobalCaProxy/CscGlobalCaProxy.cs

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -81,54 +81,64 @@ public override void Synchronize(ICertificateDataReader certificateDataReader,
8181
Logger.MethodEntry(ILogExtensions.MethodLogLevel.Debug);
8282
try
8383
{
84+
CertificateListResponse certs;
85+
8486
if (certificateAuthoritySyncInfo.DoFullSync)
8587
{
86-
var certs = Task.Run(async () => await CscGlobalClient.SubmitCertificateListRequestAsync()).Result;
88+
Logger.Trace("Performing Full Sync");
89+
certs = Task.Run(async () => await CscGlobalClient.SubmitCertificateListRequestAsync()).Result;
90+
}
91+
else
92+
{
93+
// Incremental sync - get certificates from the last X days (default 5)
94+
var effectiveDate = DateTime.Now.AddDays(-Constants.IncrementalSyncDays);
95+
Logger.Trace($"Performing Incremental Sync with effectiveDate: {effectiveDate:yyyy/MM/dd}");
96+
certs = Task.Run(async () => await CscGlobalClient.SubmitIncrementalCertificateListRequestAsync(effectiveDate)).Result;
97+
}
98+
99+
foreach (var currentResponseItem in certs.Results)
100+
{
101+
102+
cancelToken.ThrowIfCancellationRequested();
103+
Logger.Trace($"Took Certificate ID {currentResponseItem?.Uuid} from Queue");
104+
var certStatus = _requestManager.MapReturnStatus(currentResponseItem?.Status);
87105

88-
foreach (var currentResponseItem in certs.Results)
106+
//Keyfactor sync only seems to work when there is a valid cert and I can only get Active valid certs from Csc Global
107+
if (certStatus == Convert.ToInt32(PKIConstants.Microsoft.RequestDisposition.ISSUED) ||
108+
certStatus == Convert.ToInt32(PKIConstants.Microsoft.RequestDisposition.REVOKED))
89109
{
110+
//One click renewal/reissue won't work for this implementation so there is an option to disable it by not syncing back template
111+
var productId = "CscGlobal";
112+
if (EnableTemplateSync) productId = currentResponseItem?.CertificateType;
90113

91-
cancelToken.ThrowIfCancellationRequested();
92-
Logger.Trace($"Took Certificate ID {currentResponseItem?.Uuid} from Queue");
93-
var certStatus = _requestManager.MapReturnStatus(currentResponseItem?.Status);
114+
var fileContent =
115+
Encoding.ASCII.GetString(
116+
Convert.FromBase64String(currentResponseItem?.Certificate ?? string.Empty));
94117

95-
//Keyfactor sync only seems to work when there is a valid cert and I can only get Active valid certs from Csc Global
96-
if (certStatus == Convert.ToInt32(PKIConstants.Microsoft.RequestDisposition.ISSUED) ||
97-
certStatus == Convert.ToInt32(PKIConstants.Microsoft.RequestDisposition.REVOKED))
118+
if (fileContent.Length > 0)
98119
{
99-
//One click renewal/reissue won't work for this implementation so there is an option to disable it by not syncing back template
100-
var productId = "CscGlobal";
101-
if (EnableTemplateSync) productId = currentResponseItem?.CertificateType;
102-
103-
var fileContent =
104-
Encoding.ASCII.GetString(
105-
Convert.FromBase64String(currentResponseItem?.Certificate ?? string.Empty));
120+
Logger.Trace($"File Content {fileContent}");
121+
var certData = fileContent.Replace("\r\n", string.Empty);
122+
var certString = GetEndEntityCertificate(certData);
123+
var currentCert = new X509Certificate2(Encoding.ASCII.GetBytes(certString));
106124

107-
if (fileContent.Length > 0)
125+
if (certString.Length > 0)
108126
{
109-
Logger.Trace($"File Content {fileContent}");
110-
var certData = fileContent.Replace("\r\n", string.Empty);
111-
var certString = GetEndEntityCertificate(certData);
112-
var currentCert = new X509Certificate2(Encoding.ASCII.GetBytes(certString));
113-
114-
if (certString.Length > 0)
127+
blockingBuffer.Add(new CAConnectorCertificate
115128
{
116-
blockingBuffer.Add(new CAConnectorCertificate
117-
{
118-
CARequestID = $"{currentResponseItem?.Uuid}",
119-
Certificate = certString,
120-
SubmissionDate = currentResponseItem?.OrderDate == null
121-
? Convert.ToDateTime(currentCert.NotBefore)
122-
: Convert.ToDateTime(currentResponseItem.OrderDate),
123-
Status = certStatus,
124-
ProductID = productId
125-
}, cancelToken);
126-
}
129+
CARequestID = $"{currentResponseItem?.Uuid}",
130+
Certificate = certString,
131+
SubmissionDate = currentResponseItem?.OrderDate == null
132+
? Convert.ToDateTime(currentCert.NotBefore)
133+
: Convert.ToDateTime(currentResponseItem.OrderDate),
134+
Status = certStatus,
135+
ProductID = productId
136+
}, cancelToken);
127137
}
128138
}
129139
}
130-
blockingBuffer.CompleteAdding();
131140
}
141+
blockingBuffer.CompleteAdding();
132142
}
133143
catch (Exception e)
134144
{

CscGlobalCaProxy/Interfaces/ICscGlobalClient.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Concurrent;
1+
using System;
2+
using System.Collections.Concurrent;
23
using System.Threading;
34
using System.Threading.Tasks;
45
using Keyfactor.AnyGateway.CscGlobal.Client.Models;
@@ -20,6 +21,8 @@ Task<ReissueResponse> SubmitReissueAsync(
2021

2122
Task<CertificateListResponse> SubmitCertificateListRequestAsync();
2223

24+
Task<CertificateListResponse> SubmitIncrementalCertificateListRequestAsync(DateTime effectiveDate);
25+
2326
Task<RevokeResponse> SubmitRevokeCertificateAsync(string uuId);
2427
}
2528
}

0 commit comments

Comments
 (0)