Skip to content

Commit 73b33f7

Browse files
authored
48248 dev (#21)
* Version 2.0 - added sync from KF to DC. (#12) * Fixed issue with no input for either field type leading to a crash. Two other bugs fixed. * Fixed issue with additional_emails field not syncing. * Added independent logging via NLog. * Added a switch to control syncing of fields deactivated in DigiCert. * Added: automatic banned character recognition, paginated processing - as fixes for two bugs * Added information on the new replacechar.json file. * Fixes ab#47259 * Fixes ab#47725 * Fixes ab#46882
1 parent 1412c73 commit 73b33f7

File tree

9 files changed

+495
-279
lines changed

9 files changed

+495
-279
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
Version 2.1.0
2+
3+
Added a system that gathers all non-Keyfactor friendly characters and allows the user to configure an alternative.
4+
Added pagination based batch processing, memory consumption has been drastically reduced.
5+
16
Version 2.0.3
27

38
Added a setting to enable or disable syncing deactivated custom fields from DigiCert.

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,17 @@ This should include the Keyfactor API endpoint, of the format https://domain.com
5050
This should include the common prefix all DigiCert certs have in your Keyfactor instance. For example, "DigiCert"
5151
- <b>ImportAllCustomDigicertFields</b>
5252
This setting enables the tool to import all of the custom metadata fields included in DigiCert and sync all of their data.
53-
- <b>ReplaceDigicertWhiteSpaceCharacterInName</b>
54-
In case the ImportAllCustomDigicertFields setting is used, this is necessary to for metadata field label conversion. DigiCert supports spaces in labels and Keyfactor does not, so this replaces the spaces in the name with your character sequence of choice.
53+
54+
During the first run, the tool will scan the custom fields it will be importing for characters that are not supported in Keyfactor Metadata field names.
55+
Each unsupported character will be shown in a file named "replacechar.json" and its replacement can be selected. If the values in the file are not populated, the tool will not run a second time.
5556
- <b>ImportDataForDeactivatedDigiCertFields</b>
5657
If this is enabled, custom metadata fields that were deactivated in DigiCert will also be synced, and the data stored in these fields in certificates will be too.
5758

59+
### replacechar.json settings
60+
This file is populated during the first run of the tool if the ImportAllCustomDigicertFields setting is toggled.
61+
The only text that needs replacing is shown as "null", and can be filled with any alphanumeric string. The "_" and "-" characters are also supported.
62+
63+
5864
### manualfields.json settings
5965
This file is used to specify which metadata fields should be synced up.
6066

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2021 Keyfactor
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using System.Text.RegularExpressions;
16+
using Newtonsoft.Json.Linq;
17+
18+
namespace DigicertMetadataSync;
19+
20+
internal partial class DigicertSync
21+
{
22+
public static List<CharDBItem> BannedCharacterParse(string input)
23+
{
24+
string pattern = "[a-zA-Z0-9-_]";
25+
26+
List<CharDBItem> bannedChars = new List<CharDBItem>();
27+
28+
foreach (char c in input)
29+
{
30+
if (!Regex.IsMatch(c.ToString(), pattern))
31+
{
32+
CharDBItem localitem = new CharDBItem();
33+
localitem.character = c.ToString();
34+
localitem.replacementcharacter = "null";
35+
bannedChars.Add(localitem);
36+
}
37+
}
38+
39+
if (bannedChars.Count > 0)
40+
{
41+
Console.WriteLine("The field name " + input + " contains the following invalid characters: " +
42+
string.Join("", bannedChars.Select(item => item.character)));
43+
}
44+
else
45+
{
46+
Console.WriteLine("The field name " + input + " is valid.");
47+
}
48+
49+
return bannedChars;
50+
}
51+
52+
public static void CheckForChars(List<ReadInMetadataField> input, List<CharDBItem> allBannedChars, bool restartandconfigrequired)
53+
{
54+
foreach (var dgfield in input)
55+
{
56+
List<CharDBItem> newChars = BannedCharacterParse(dgfield.DigicertFieldName);
57+
foreach (var newchar in newChars)
58+
{
59+
bool exists = allBannedChars.Any(allcharchar => allcharchar.character == newchar.character);
60+
if (!exists)
61+
{
62+
allBannedChars.Add(newchar);
63+
restartandconfigrequired = true;
64+
}
65+
}
66+
}
67+
}
68+
}

digicert-metadata-sync/Helpers.cs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
16+
using System.Collections.Generic;
17+
1518
using System.Text.RegularExpressions;
1619
using Newtonsoft.Json.Linq;
1720

@@ -54,9 +57,13 @@ public static Dictionary<string, object> ClassConverter(object obj)
5457
return null;
5558
}
5659

57-
public static string ReplaceAllWhiteSpaces(string str, string replacement)
60+
public static string ReplaceAllBannedCharacters(string input, List<CharDBItem>allBannedChars)
5861
{
59-
return Regex.Replace(str, @"\s+", "_-_");
62+
foreach (CharDBItem item in allBannedChars)
63+
{
64+
input = input.Replace(item.character, item.replacementcharacter);
65+
}
66+
return input;
6067
}
6168

6269
public static bool CheckMode(string mode)
@@ -65,17 +72,18 @@ public static bool CheckMode(string mode)
6572
return false;
6673
}
6774

68-
private static List<KeyfactorMetadataInstanceSendoff> convertlisttokf(List<ReadInMetadataField> inputlist,
69-
string replacementcharacter)
75+
private static List<KeyfactorMetadataInstanceSendoff> convertlisttokf(List<ReadInMetadataField> inputlist, List<CharDBItem> allBannedChars, bool importallcustomfields)
7076
{
7177
var formattedlist = new List<KeyfactorMetadataInstanceSendoff>();
7278
if (inputlist.Count != 0)
7379
foreach (var input in inputlist)
7480
{
7581
var formatinstance = new KeyfactorMetadataInstanceSendoff();
76-
if (input.KeyfactorMetadataFieldName == null || input.KeyfactorMetadataFieldName == "")
77-
//If name is emtpy, use autocomplete.
78-
formatinstance.Name = ReplaceAllWhiteSpaces(input.DigicertFieldName, replacementcharacter);
82+
83+
if (input.KeyfactorMetadataFieldName == null || input.KeyfactorMetadataFieldName == "" || input.FieldType == "Custom")
84+
//If name is empty, clean up the characters.
85+
formatinstance.Name = ReplaceAllBannedCharacters(input.DigicertFieldName, allBannedChars);
86+
7987
else
8088
//Use user input preferred name.
8189
formatinstance.Name = input.KeyfactorMetadataFieldName;
@@ -86,6 +94,9 @@ private static List<KeyfactorMetadataInstanceSendoff> convertlisttokf(List<ReadI
8694
formatinstance.Description = input.KeyfactorDescription;
8795
formattedlist.Add(formatinstance);
8896
}
97+
98+
99+
89100
return formattedlist;
90101
}
91102

0 commit comments

Comments
 (0)