Skip to content

Commit e137012

Browse files
committed
Merge branch 'dev-2.1' of https://github.com/Keyfactor/digicert-certcentral-caplugin into dev-2.1
2 parents 7126e4c + eb060e0 commit e137012

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
@@ -418,6 +418,13 @@ public Dictionary<string, PropertyConfigInfo> GetCAConnectorAnnotations()
418418
DefaultValue = "",
419419
Type = "String"
420420
},
421+
[CertCentralConstants.Config.SYNC_DIV_FILTER] = new PropertyConfigInfo()
422+
{
423+
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.",
424+
Hidden = false,
425+
DefaultValue = "",
426+
Type = "String"
427+
},
421428
[CertCentralConstants.Config.FILTER_EXPIRED] = new PropertyConfigInfo()
422429
{
423430
Comments = "If set to 'true', syncing will apply a filter to not return orders that are expired for longer than specified in SyncExpirationDays.",
@@ -707,10 +714,16 @@ public async Task Synchronize(BlockingCollection<AnyCAPluginCertificate> blockin
707714

708715
caList.ForEach(c => c.ToUpper());
709716

717+
List<string> divFilters = new List<string>() { "" };
718+
if (!string.IsNullOrEmpty(_config.SyncDivisionFilter))
719+
{
720+
divFilters = new List<string>();
721+
divFilters.AddRange(_config.SyncDivisionFilter.Split(','));
722+
}
710723

711724
if (fullSync)
712725
{
713-
bool ignoreExpired = false; int expiredWindow = 0;
726+
bool ignoreExpired = false; int expiredWindow = 0;
714727
if (_config.FilterExpiredOrders.HasValue && _config.FilterExpiredOrders.Value)
715728
{
716729
ignoreExpired = true;
@@ -719,50 +732,56 @@ public async Task Synchronize(BlockingCollection<AnyCAPluginCertificate> blockin
719732
expiredWindow = _config.SyncExpirationDays.Value;
720733
}
721734
}
735+
722736
long time = DateTime.Now.Ticks;
723737
long starttime = time;
724738
_logger.LogDebug($"SYNC: Starting sync at time {time}");
725-
ListCertificateOrdersResponse ordersResponse = client.ListAllCertificateOrders(ignoreExpired, expiredWindow);
726-
if (ordersResponse.Status == CertCentralBaseResponse.StatusType.ERROR)
739+
List<Order> allOrders = new List<Order>();
740+
foreach (string div in divFilters)
727741
{
728-
Error error = ordersResponse.Errors[0];
729-
_logger.LogError("Error in listing all certificate orders");
730-
throw new Exception($"DigiCert CertCentral web service returned {error.code} - {error.message} when retrieving all rows");
742+
ListCertificateOrdersResponse ordersResponse = client.ListAllCertificateOrders(ignoreExpired, expiredWindow, div);
743+
if (ordersResponse.Status == CertCentralBaseResponse.StatusType.ERROR)
744+
{
745+
Error error = ordersResponse.Errors[0];
746+
_logger.LogError("Error in listing all certificate orders");
747+
throw new Exception($"DigiCert CertCentral web service returned {error.code} - {error.message} when retrieving all rows");
748+
}
749+
else
750+
{
751+
allOrders.AddRange(ordersResponse.orders);
752+
}
731753
}
732-
else
754+
_logger.LogDebug($"SYNC: Found {allOrders.Count} records");
755+
foreach (var orderDetails in allOrders)
733756
{
734-
_logger.LogDebug($"SYNC: Found {ordersResponse.orders.Count} records");
735-
foreach (var orderDetails in ordersResponse.orders)
757+
List<AnyCAPluginCertificate> orderCerts = new List<AnyCAPluginCertificate>();
758+
try
736759
{
737-
List<AnyCAPluginCertificate> orderCerts = new List<AnyCAPluginCertificate>();
738-
try
760+
cancelToken.ThrowIfCancellationRequested();
761+
string caReqId = orderDetails.id + "-" + orderDetails.certificate.id;
762+
_logger.LogDebug($"SYNC: Retrieving certs for order id {orderDetails.id}");
763+
orderCerts = GetAllConnectorCertsForOrder(caReqId, caList, divFilters);
764+
if (orderCerts == null || orderCerts.Count == 0)
739765
{
740-
cancelToken.ThrowIfCancellationRequested();
741-
string caReqId = orderDetails.id + "-" + orderDetails.certificate.id;
742-
_logger.LogDebug($"SYNC: Retrieving certs for order id {orderDetails.id}");
743-
orderCerts = GetAllConnectorCertsForOrder(caReqId, caList);
744-
if (orderCerts == null || orderCerts.Count == 0)
745-
{
746-
continue;
747-
}
748-
_logger.LogDebug($"SYNC: Retrieved {orderCerts.Count} certs at time {DateTime.Now.Ticks}");
749-
}
750-
catch
751-
{
752-
skippedOrders.Add(orderDetails.id.ToString());
753-
_logger.LogWarning($"An error occurred attempting to sync order '{orderDetails.id}'. This order will be skipped.");
754766
continue;
755767
}
768+
_logger.LogDebug($"SYNC: Retrieved {orderCerts.Count} certs at time {DateTime.Now.Ticks}");
769+
}
770+
catch
771+
{
772+
skippedOrders.Add(orderDetails.id.ToString());
773+
_logger.LogWarning($"An error occurred attempting to sync order '{orderDetails.id}'. This order will be skipped.");
774+
continue;
775+
}
756776

757-
foreach (var cert in orderCerts)
758-
{
759-
certCount++;
760-
blockingBuffer.Add(cert);
761-
}
762-
777+
foreach (var cert in orderCerts)
778+
{
779+
certCount++;
780+
blockingBuffer.Add(cert);
763781
}
764-
_logger.LogDebug($"SYNC: Complete after {DateTime.Now.Ticks - starttime} ticks");
782+
765783
}
784+
_logger.LogDebug($"SYNC: Complete after {DateTime.Now.Ticks - starttime} ticks");
766785
}
767786
else
768787
{
@@ -783,7 +802,7 @@ public async Task Synchronize(BlockingCollection<AnyCAPluginCertificate> blockin
783802
{
784803
cancelToken.ThrowIfCancellationRequested();
785804
string caReqId = order.order_id + "-" + order.certificate_id;
786-
orderCerts = GetAllConnectorCertsForOrder(caReqId, caList);
805+
orderCerts = GetAllConnectorCertsForOrder(caReqId, caList, divFilters);
787806
if (orderCerts == null || orderCerts.Count > 0)
788807
{
789808
continue;
@@ -1337,7 +1356,7 @@ string FormatSyncDate(DateTime? syncTime)
13371356
/// </summary>
13381357
/// <param name="caRequestID"></param>
13391358
/// <returns></returns>
1340-
private List<AnyCAPluginCertificate> GetAllConnectorCertsForOrder(string caRequestID, List<string> caFilterIds)
1359+
private List<AnyCAPluginCertificate> GetAllConnectorCertsForOrder(string caRequestID, List<string> caFilterIds, List<string> divIds)
13411360
{
13421361
_logger.MethodEntry(LogLevel.Trace);
13431362
// Split ca request id into order and cert id
@@ -1355,6 +1374,11 @@ private List<AnyCAPluginCertificate> GetAllConnectorCertsForOrder(string caReque
13551374
_logger.LogTrace($"Found order ID {orderId} that does not match SyncCAFilter. CA ID: {orderResponse.certificate.ca_cert.Id} Skipping...");
13561375
return null;
13571376
}
1377+
if (divIds != null && divIds.Count > 0 && !divIds.Contains(orderResponse.container.Id.ToString()))
1378+
{
1379+
_logger.LogTrace($"Found order ID {orderId} that does not match Division filter. Division ID: {orderResponse.container.Id.ToString()} Skipping...");
1380+
return null;
1381+
}
13581382

13591383
var orderCerts = GetAllCertsForOrder(orderId);
13601384

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)