Skip to content

Commit 837ae9a

Browse files
author
Bob Pokorny
committed
Made changes to bind certs to IIS and fix Object not found error.
1 parent b0f2d43 commit 837ae9a

File tree

6 files changed

+472
-27
lines changed

6 files changed

+472
-27
lines changed

IISU/ImplementedStoreTypes/WinIIS/IISBindingInfo.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,22 @@ public IISBindingInfo(Dictionary<string, object> bindingInfo)
4242

4343
private string MigrateSNIFlag(string input)
4444
{
45-
// Check if the input is numeric, if so, just return it as an integer
4645
if (int.TryParse(input, out int numericValue))
4746
{
4847
return numericValue.ToString();
4948
}
5049

51-
if (string.IsNullOrEmpty(input)) { throw new ArgumentNullException("SNI/SSL Flag", "The SNI or SSL Flag flag must not be empty or null."); }
50+
if (string.IsNullOrEmpty(input))
51+
throw new ArgumentNullException("SNI/SSL Flag", "The SNI or SSL Flag must not be empty or null.");
52+
53+
// Normalize input
54+
var trimmedInput = input.Trim().ToLowerInvariant();
55+
56+
// Handle boolean values
57+
if (trimmedInput == "true")
58+
return "1";
59+
if (trimmedInput == "false")
60+
return "0";
5261

5362
// Handle the string cases
5463
switch (input.ToLower())

IISU/ImplementedStoreTypes/WinIIS/Inventory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public JobResult ProcessJob(InventoryJobConfiguration jobConfiguration, SubmitIn
107107
{
108108
_logger.LogTrace(LogHandler.FlattenException(ex));
109109

110-
var failureMessage = $"Inventory job failed for Site '{jobConfiguration.CertificateStoreDetails.StorePath}' on server '{jobConfiguration.CertificateStoreDetails.ClientMachine}' with error: '{LogHandler.FlattenException(ex)}'";
110+
var failureMessage = $"Inventory job failed for Site '{jobConfiguration.CertificateStoreDetails.StorePath}' on server '{jobConfiguration.CertificateStoreDetails.ClientMachine}' with error: '{ex.Message}'";
111111
_logger.LogWarning(failureMessage);
112112

113113
return new JobResult

IISU/ImplementedStoreTypes/WinIIS/Management.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,16 @@ public JobResult ProcessJob(ManagementJobConfiguration config)
103103
{
104104
string newThumbprint = AddCertificate(certificateContents, privateKeyPassword, cryptoProvider);
105105
_logger.LogTrace($"Completed adding the certificate to the store");
106+
_logger.LogTrace($"New thumbprint: {newThumbprint}");
106107

107108
// Bind Certificate to IIS Site
108109
if (newThumbprint != null)
109110
{
110111
IISBindingInfo bindingInfo = new IISBindingInfo(config.JobProperties);
111112
WinIISBinding.BindCertificate(_psHelper, bindingInfo, newThumbprint, "", _storePath);
112113

114+
_logger.LogTrace("Returned after binding certificate to store");
115+
113116
complete = new JobResult
114117
{
115118
Result = OrchestratorJobStatusJobResult.Success,

IISU/ImplementedStoreTypes/WinIIS/WinIISBinding.cs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,39 @@ public static void BindCertificate(PSHelper psHelper, IISBindingInfo bindingInfo
4949
// Optional parameters
5050
if (!string.IsNullOrEmpty(bindingInfo.HostName)) { parameters.Add("HostName", bindingInfo.HostName); }
5151

52-
_results = psHelper.ExecutePowerShell("New-KFIISSiteBinding", parameters); // returns true if successful
53-
_logger.LogTrace("Returned from executing PS function (New-KFIISSiteBinding)");
54-
55-
// This should return the thumbprint of the certificate
56-
if (_results != null && _results.Count > 0)
52+
try
5753
{
58-
bool success = _results[0].BaseObject is bool value && value;
59-
if (success)
54+
_results = psHelper.ExecutePowerShell("New-KFIISSiteBinding", parameters); // returns true if successful
55+
_logger.LogTrace("Returned from executing PS function (New-KFIISSiteBinding)");
56+
57+
if (_results != null && _results.Count > 0)
6058
{
61-
_logger.LogTrace($"Bound certificate with the thumbprint: '{thumbprint}' to site: '{bindingInfo.SiteName}' successfully.");
62-
return;
59+
var baseObject = _results[0]?.BaseObject;
60+
if (baseObject is bool value)
61+
{
62+
if (value)
63+
{
64+
_logger.LogTrace($"Bound certificate with the thumbprint: '{thumbprint}' to site: '{bindingInfo.SiteName}' successfully.");
65+
}
66+
else
67+
{
68+
_logger.LogTrace("Something happened and the binding failed.");
69+
}
70+
}
71+
else
72+
{
73+
_logger.LogWarning("Unexpected result type returned from script: " + baseObject?.GetType().Name);
74+
}
6375
}
6476
else
6577
{
66-
_logger.LogTrace("Something happened and the binding failed.");
78+
_logger.LogWarning("PowerShell script returned no results.");
6779
}
6880
}
69-
70-
throw new Exception($"An unknown error occurred while attempting to bind thumbprint: {thumbprint} to site: '{bindingInfo.SiteName}'. \nCheck the UO Logs for more information.");
81+
catch (Exception ex)
82+
{
83+
throw new Exception($"An unknown error occurred while attempting to bind thumbprint: {thumbprint} to site: '{bindingInfo.SiteName}'. \n{ex.Message}");
84+
}
7185
}
7286

7387
public static bool UnBindCertificate(PSHelper psHelper, IISBindingInfo bindingInfo)

IISU/PSHelper.cs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -181,20 +181,28 @@ private void InitializeRemoteSession()
181181
else
182182
{
183183
_logger.LogTrace("Initializing WinRM connection");
184-
var pw = new NetworkCredential(serverUserName, serverPassword).SecurePassword;
185-
PSCredential myCreds = new PSCredential(serverUserName, pw);
184+
try
185+
{
186+
var pw = new NetworkCredential(serverUserName, serverPassword).SecurePassword;
187+
PSCredential myCreds = new PSCredential(serverUserName, pw);
186188

187-
// Create the PSSessionOption object
188-
var sessionOption = new PSSessionOption
189+
// Create the PSSessionOption object
190+
var sessionOption = new PSSessionOption
191+
{
192+
IncludePortInSPN = useSPN
193+
};
194+
195+
PS.AddCommand("New-PSSession")
196+
.AddParameter("ComputerName", ClientMachineName)
197+
.AddParameter("Port", port)
198+
.AddParameter("Credential", myCreds)
199+
.AddParameter("SessionOption", sessionOption);
200+
}
201+
catch (Exception)
189202
{
190-
IncludePortInSPN = useSPN
191-
};
203+
throw new Exception("Problems establishing network credentials. Please check the User name and Password for the Certificate Store");
204+
}
192205

193-
PS.AddCommand("New-PSSession")
194-
.AddParameter("ComputerName", ClientMachineName)
195-
.AddParameter("Port", port)
196-
.AddParameter("Credential", myCreds)
197-
.AddParameter("SessionOption", sessionOption);
198206
}
199207

200208
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));

0 commit comments

Comments
 (0)