|
11 | 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | 12 | // See the License for the specific language governing permissions and |
13 | 13 | // limitations under the License. |
| 14 | +using Keyfactor.Extensions.Orchestrator.WindowsCertStore.IISU; |
14 | 15 | using Keyfactor.Logging; |
15 | 16 | using Microsoft.Extensions.Logging; |
16 | 17 | using System; |
17 | 18 | using System.Collections.Generic; |
| 19 | +using System.Collections.ObjectModel; |
18 | 20 | using System.Management.Automation; |
19 | 21 | using System.Management.Automation.Runspaces; |
| 22 | +using System.Runtime.ConstrainedExecution; |
20 | 23 | using System.Text; |
21 | 24 |
|
22 | 25 | namespace Keyfactor.Extensions.Orchestrator.WindowsCertStore |
23 | 26 | { |
24 | | - abstract class ClientPSCertStoreInventory |
| 27 | + public abstract class ClientPSCertStoreInventory |
25 | 28 | { |
26 | 29 | private ILogger _logger; |
| 30 | + |
| 31 | + protected ClientPSCertStoreInventory() |
| 32 | + { |
| 33 | + _logger = LogHandler.GetClassLogger<ClientPSCertStoreInventory>(); |
| 34 | + } |
| 35 | + |
27 | 36 | public ClientPSCertStoreInventory(ILogger logger) |
28 | 37 | { |
29 | 38 | _logger = logger; |
30 | 39 | } |
31 | 40 |
|
| 41 | + public List<Certificate> GetCertificatesFromStore(RemoteSettings settings, string storePath) |
| 42 | + { |
| 43 | + try |
| 44 | + { |
| 45 | + ILogger _logger = LogHandler.GetClassLogger(this.GetType()); |
| 46 | + |
| 47 | + List<Certificate> myCertificates = new(); |
| 48 | + |
| 49 | + _logger.LogTrace("Attempting to establish PowerShell connection."); |
| 50 | + using (PSHelper ps = new(settings.Protocol, settings.Port, settings.IncludePortInSPN, settings.ClientMachineName, settings.ServerUserName, settings.ServerPassword)) |
| 51 | + { |
| 52 | + _logger.LogTrace("Initializing connection"); |
| 53 | + ps.Initialize(); |
| 54 | + |
| 55 | + var scriptParameters = new Dictionary<string, object> |
| 56 | + { |
| 57 | + { "StoreName", storePath } |
| 58 | + }; |
| 59 | + |
| 60 | + var results = ps.ExecuteCommand(PSHelper.LoadScript("WinCertInventory.ps1"), scriptParameters); |
| 61 | + |
| 62 | + foreach (var c in results) |
| 63 | + { |
| 64 | + myCertificates.Add(new Certificate |
| 65 | + { |
| 66 | + Thumbprint = $"{c.Properties["Thumbprint"]?.Value}", |
| 67 | + HasPrivateKey = bool.Parse($"{c.Properties["HasPrivateKey"]?.Value}"), |
| 68 | + RawData = (byte[])c.Properties["RawData"]?.Value, |
| 69 | + CryptoServiceProvider = $"{c.Properties["CSP"]?.Value}", |
| 70 | + SAN = Certificate.Utilities.FormatSAN($"{c.Properties["san"]?.Value}") |
| 71 | + }); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + _logger.LogTrace($"found: {myCertificates.Count} certificate(s), exiting GetCertificatesFromStore()"); |
| 76 | + return myCertificates; |
| 77 | + |
| 78 | + } |
| 79 | + catch (Exception ex) |
| 80 | + { |
| 81 | + throw new Exception ("An error occurred while attempting to read the certificates from the store.\n" + ex.Message.ToString()); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + // ORIG |
32 | 86 | public List<Certificate> GetCertificatesFromStore(Runspace runSpace, string storePath) |
33 | 87 | { |
| 88 | + ILogger _logger = LogHandler.GetClassLogger(this.GetType()); |
| 89 | + |
34 | 90 | List<Certificate> myCertificates = new List<Certificate>(); |
35 | 91 | try |
36 | 92 | { |
|
0 commit comments