Skip to content

Commit eb060e0

Browse files
authored
Merge pull request #25 from Keyfactor/syncfilter
Option to filter sync by division ID
2 parents de0a9ba + 540dccd commit eb060e0

6 files changed

Lines changed: 71 additions & 36 deletions

File tree

digicert-certcentral-caplugin/API/ListCertificateOrders.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public ListCertificateOrdersRequest(bool ignoreExpired = false)
2929

3030
public bool ignoreExpired { get; set; }
3131
public int expiredWindow { get; set; } = 0;
32+
public string divID { get; set; } = string.Empty;
3233

3334
public new string BuildParameters()
3435
{
@@ -37,6 +38,10 @@ public ListCertificateOrdersRequest(bool ignoreExpired = false)
3738
sbParamters.Append("limit=").Append(this.limit.ToString());
3839
sbParamters.Append("&offset=").Append(HttpUtility.UrlEncode(this.offset.ToString()));
3940

41+
if (!string.IsNullOrEmpty(divID))
42+
{
43+
sbParamters.Append("&filters[container_id]=").Append(this.divID);
44+
}
4045
if (ignoreExpired)
4146
{
4247
DateTime cutoffDate = DateTime.Today.AddDays(-1 - expiredWindow);

digicert-certcentral-caplugin/API/ViewCertificateOrder.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ public ViewCertificateOrderResponse()
7878
[JsonProperty("product")]
7979
public Product product { get; set; }
8080

81+
[JsonProperty("container")]
82+
public Container container { get; set; }
83+
8184
[JsonProperty("organization_contact")]
8285
public Contact organization_contact { get; set; }
8386

digicert-certcentral-caplugin/CertCentralCAPlugin.cs

Lines changed: 58 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,13 @@ public Dictionary<string, PropertyConfigInfo> GetCAConnectorAnnotations()
411411
DefaultValue = "",
412412
Type = "String"
413413
},
414+
[CertCentralConstants.Config.SYNC_DIV_FILTER] = new PropertyConfigInfo()
415+
{
416+
Comments = "If you list one or more Divison IDs (also known as Container IDs) here (comma-separated), the sync process will filter records to only return orders from those divisions. If you want to sync all divisions, leave this field empty. Note that this has no relationship to the value of the DivisionId config field.",
417+
Hidden = false,
418+
DefaultValue = "",
419+
Type = "String"
420+
},
414421
[CertCentralConstants.Config.FILTER_EXPIRED] = new PropertyConfigInfo()
415422
{
416423
Comments = "If set to 'true', syncing will apply a filter to not return orders that are expired for longer than specified in SyncExpirationDays.",
@@ -700,10 +707,16 @@ public async Task Synchronize(BlockingCollection<AnyCAPluginCertificate> blockin
700707

701708
caList.ForEach(c => c.ToUpper());
702709

710+
List<string> divFilters = new List<string>() { "" };
711+
if (!string.IsNullOrEmpty(_config.SyncDivisionFilter))
712+
{
713+
divFilters = new List<string>();
714+
divFilters.AddRange(_config.SyncDivisionFilter.Split(','));
715+
}
703716

704717
if (fullSync)
705718
{
706-
bool ignoreExpired = false; int expiredWindow = 0;
719+
bool ignoreExpired = false; int expiredWindow = 0;
707720
if (_config.FilterExpiredOrders.HasValue && _config.FilterExpiredOrders.Value)
708721
{
709722
ignoreExpired = true;
@@ -712,50 +725,56 @@ public async Task Synchronize(BlockingCollection<AnyCAPluginCertificate> blockin
712725
expiredWindow = _config.SyncExpirationDays.Value;
713726
}
714727
}
728+
715729
long time = DateTime.Now.Ticks;
716730
long starttime = time;
717731
_logger.LogDebug($"SYNC: Starting sync at time {time}");
718-
ListCertificateOrdersResponse ordersResponse = client.ListAllCertificateOrders(ignoreExpired, expiredWindow);
719-
if (ordersResponse.Status == CertCentralBaseResponse.StatusType.ERROR)
732+
List<Order> allOrders = new List<Order>();
733+
foreach (string div in divFilters)
720734
{
721-
Error error = ordersResponse.Errors[0];
722-
_logger.LogError("Error in listing all certificate orders");
723-
throw new Exception($"DigiCert CertCentral web service returned {error.code} - {error.message} when retrieving all rows");
735+
ListCertificateOrdersResponse ordersResponse = client.ListAllCertificateOrders(ignoreExpired, expiredWindow, div);
736+
if (ordersResponse.Status == CertCentralBaseResponse.StatusType.ERROR)
737+
{
738+
Error error = ordersResponse.Errors[0];
739+
_logger.LogError("Error in listing all certificate orders");
740+
throw new Exception($"DigiCert CertCentral web service returned {error.code} - {error.message} when retrieving all rows");
741+
}
742+
else
743+
{
744+
allOrders.AddRange(ordersResponse.orders);
745+
}
724746
}
725-
else
747+
_logger.LogDebug($"SYNC: Found {allOrders.Count} records");
748+
foreach (var orderDetails in allOrders)
726749
{
727-
_logger.LogDebug($"SYNC: Found {ordersResponse.orders.Count} records");
728-
foreach (var orderDetails in ordersResponse.orders)
750+
List<AnyCAPluginCertificate> orderCerts = new List<AnyCAPluginCertificate>();
751+
try
729752
{
730-
List<AnyCAPluginCertificate> orderCerts = new List<AnyCAPluginCertificate>();
731-
try
753+
cancelToken.ThrowIfCancellationRequested();
754+
string caReqId = orderDetails.id + "-" + orderDetails.certificate.id;
755+
_logger.LogDebug($"SYNC: Retrieving certs for order id {orderDetails.id}");
756+
orderCerts = GetAllConnectorCertsForOrder(caReqId, caList, divFilters);
757+
if (orderCerts == null || orderCerts.Count == 0)
732758
{
733-
cancelToken.ThrowIfCancellationRequested();
734-
string caReqId = orderDetails.id + "-" + orderDetails.certificate.id;
735-
_logger.LogDebug($"SYNC: Retrieving certs for order id {orderDetails.id}");
736-
orderCerts = GetAllConnectorCertsForOrder(caReqId, caList);
737-
if (orderCerts == null || orderCerts.Count == 0)
738-
{
739-
continue;
740-
}
741-
_logger.LogDebug($"SYNC: Retrieved {orderCerts.Count} certs at time {DateTime.Now.Ticks}");
742-
}
743-
catch
744-
{
745-
skippedOrders.Add(orderDetails.id.ToString());
746-
_logger.LogWarning($"An error occurred attempting to sync order '{orderDetails.id}'. This order will be skipped.");
747759
continue;
748760
}
761+
_logger.LogDebug($"SYNC: Retrieved {orderCerts.Count} certs at time {DateTime.Now.Ticks}");
762+
}
763+
catch
764+
{
765+
skippedOrders.Add(orderDetails.id.ToString());
766+
_logger.LogWarning($"An error occurred attempting to sync order '{orderDetails.id}'. This order will be skipped.");
767+
continue;
768+
}
749769

750-
foreach (var cert in orderCerts)
751-
{
752-
certCount++;
753-
blockingBuffer.Add(cert);
754-
}
755-
770+
foreach (var cert in orderCerts)
771+
{
772+
certCount++;
773+
blockingBuffer.Add(cert);
756774
}
757-
_logger.LogDebug($"SYNC: Complete after {DateTime.Now.Ticks - starttime} ticks");
775+
758776
}
777+
_logger.LogDebug($"SYNC: Complete after {DateTime.Now.Ticks - starttime} ticks");
759778
}
760779
else
761780
{
@@ -776,7 +795,7 @@ public async Task Synchronize(BlockingCollection<AnyCAPluginCertificate> blockin
776795
{
777796
cancelToken.ThrowIfCancellationRequested();
778797
string caReqId = order.order_id + "-" + order.certificate_id;
779-
orderCerts = GetAllConnectorCertsForOrder(caReqId, caList);
798+
orderCerts = GetAllConnectorCertsForOrder(caReqId, caList, divFilters);
780799
if (orderCerts == null || orderCerts.Count > 0)
781800
{
782801
continue;
@@ -1330,7 +1349,7 @@ string FormatSyncDate(DateTime? syncTime)
13301349
/// </summary>
13311350
/// <param name="caRequestID"></param>
13321351
/// <returns></returns>
1333-
private List<AnyCAPluginCertificate> GetAllConnectorCertsForOrder(string caRequestID, List<string> caFilterIds)
1352+
private List<AnyCAPluginCertificate> GetAllConnectorCertsForOrder(string caRequestID, List<string> caFilterIds, List<string> divIds)
13341353
{
13351354
_logger.MethodEntry(LogLevel.Trace);
13361355
// Split ca request id into order and cert id
@@ -1348,6 +1367,11 @@ private List<AnyCAPluginCertificate> GetAllConnectorCertsForOrder(string caReque
13481367
_logger.LogTrace($"Found order ID {orderId} that does not match SyncCAFilter. CA ID: {orderResponse.certificate.ca_cert.Id} Skipping...");
13491368
return null;
13501369
}
1370+
if (divIds != null && divIds.Count > 0 && !divIds.Contains(orderResponse.container.Id.ToString()))
1371+
{
1372+
_logger.LogTrace($"Found order ID {orderId} that does not match Division filter. Division ID: {orderResponse.container.Id.ToString()} Skipping...");
1373+
return null;
1374+
}
13511375

13521376
var orderCerts = GetAllCertsForOrder(orderId);
13531377

digicert-certcentral-caplugin/CertCentralConfig.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,6 @@ public List<string> SyncCAs
3636

3737
public bool? FilterExpiredOrders { get; set; }
3838
public int? SyncExpirationDays { get; set; }
39+
public string SyncDivisionFilter { get; set; }
3940
}
4041
}

digicert-certcentral-caplugin/Client/CertCentralClient.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ public DownloadCertificateByFormatResponse DownloadCertificateByFormat(DownloadC
480480
return dlCertificateRequestResponse;
481481
}
482482

483-
public ListCertificateOrdersResponse ListAllCertificateOrders(bool ignoreExpired = false, int expiredWindow = 0)
483+
public ListCertificateOrdersResponse ListAllCertificateOrders(bool ignoreExpired = false, int expiredWindow = 0, string divId = "")
484484
{
485485
int batch = 1000;
486486
ListCertificateOrdersResponse totalResponse = new ListCertificateOrdersResponse();
@@ -492,7 +492,8 @@ public ListCertificateOrdersResponse ListAllCertificateOrders(bool ignoreExpired
492492
limit = batch,
493493
offset = totalResponse.orders.Count,
494494
ignoreExpired = ignoreExpired,
495-
expiredWindow = expiredWindow
495+
expiredWindow = expiredWindow,
496+
divID = divId
496497
};
497498

498499
CertCentralResponse response = Request(request, request.BuildParameters());

digicert-certcentral-caplugin/Constants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public class Config
2828
public const string REVOKE_CERT = "RevokeCertificateOnly";
2929
public const string ENABLED = "Enabled";
3030
public const string SYNC_CA_FILTER = "SyncCAFilter";
31+
public const string SYNC_DIV_FILTER = "SyncDivisionFilter";
3132
public const string FILTER_EXPIRED = "FilterExpiredOrders";
3233
public const string SYNC_EXPIRATION_DAYS = "SyncExpirationDays";
3334
public const string CERT_TYPE = "CertType";

0 commit comments

Comments
 (0)