Skip to content

Commit 00f9d40

Browse files
leefine02leefine02
authored andcommitted
1 parent 2ee36a9 commit 00f9d40

File tree

5 files changed

+8
-87
lines changed

5 files changed

+8
-87
lines changed

RemoteFile/ApplicationSettings.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ public class ApplicationSettings
3333
public static string DefaultLinuxPermissionsOnStoreCreation { get { return configuration.ContainsKey("DefaultLinuxPermissionsOnStoreCreation") ? configuration["DefaultLinuxPermissionsOnStoreCreation"] : DEFAULT_LINUX_PERMISSION_SETTING; } }
3434
public static string DefaultOwnerOnStoreCreation { get { return configuration.ContainsKey("DefaultOwnerOnStoreCreation") ? configuration["DefaultOwnerOnStoreCreation"] : DEFAULT_OWNER_SETTING; } }
3535
public static string DefaultSudoImpersonatedUser { get { return configuration.ContainsKey("DefaultSudoImpersonatedUser") ? configuration["DefaultSudoImpersonatedUser"] : DEFAULT_SUDO_IMPERSONATION_SETTING; } }
36-
public static bool CreateCSROnDevice { get { return configuration.ContainsKey("CreateCSROnDevice") ? configuration["CreateCSROnDevice"]?.ToUpper() == "Y" : false; } }
3736
public static string TempFilePathForODKG { get { return configuration.ContainsKey("TempFilePathForODKG") ? configuration["TempFilePathForODKG"] : string.Empty; } }
3837
public static bool UseShellCommands { get { return configuration.ContainsKey("UseShellCommands") ? configuration["UseShellCommands"]?.ToUpper() == "Y" : true; } }
3938
public static int SSHPort

RemoteFile/ReenrollmentBase.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,7 @@ public JobResult ProcessJob(ReenrollmentJobConfiguration config, SubmitReenrollm
7575
// generate CSR and call back to enroll certificate
7676
string csr = string.Empty;
7777
AsymmetricAlgorithm privateKey;
78-
if (CreateCSROnDevice)
79-
{
80-
csr = certificateStore.GenerateCSROnDevice(SubjectText, config.Overwrite, config.Alias, KeyTypeEnum, KeySize, config.SANs, out privateKey);
81-
}
82-
else
83-
{
84-
csr = certificateStore.GenerateCSR(SubjectText, config.Overwrite, config.Alias, KeyTypeEnum, KeySize, config.SANs, out privateKey);
85-
}
78+
csr = certificateStore.GenerateCSR(SubjectText, config.Overwrite, config.Alias, KeyTypeEnum, KeySize, config.SANs, out privateKey);
8679

8780
X509Certificate2 cert = submitReenrollment.Invoke(csr);
8881

RemoteFile/RemoteCertificateStore.cs

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -349,15 +349,6 @@ internal static PathFile SplitStorePathFile(string pathFileName)
349349
}
350350
}
351351

352-
internal string GenerateCSROnDevice(string subjectText, bool overwrite, string alias, SupportedKeyTypeEnum keyType, int keySize, Dictionary<string, string[]> sans, out AsymmetricAlgorithm privateKey)
353-
{
354-
string csr = string.Empty;
355-
privateKey = RSA.Create();
356-
357-
358-
return csr;
359-
}
360-
361352
internal string GenerateCSR(string subjectText, bool overwrite, string alias, SupportedKeyTypeEnum keyType, int keySize, Dictionary<string, string[]> sans, out AsymmetricAlgorithm privateKey)
362353
{
363354
if (CertificateStore.ContainsAlias(alias) && !overwrite)
@@ -379,69 +370,6 @@ internal string GenerateCSR(string subjectText, bool overwrite, string alias, Su
379370
return csr;
380371
}
381372

382-
internal string GenerateCSROnDevice(string subjectText, bool overwrite, string alias, SupportedKeyTypeEnum keyType, int keySize, Dictionary<string, string[]> sans, out AsymmetricAlgorithm privateKey)
383-
{
384-
string path = ApplicationSettings.TempFilePathForODKG;
385-
if (path.Substring(path.Length - 1, 1) != "/") path += "/";
386-
string fileName = Guid.NewGuid().ToString();
387-
388-
System.Security.Cryptography.X509Certificates.X500DistinguishedName dn = new System.Security.Cryptography.X509Certificates.X500DistinguishedName(subjectText);
389-
string opensslSubject = dn.Format(true).Replace("S=", "ST=");
390-
opensslSubject = opensslSubject.Replace(System.Environment.NewLine, "/");
391-
opensslSubject = "/" + opensslSubject.Substring(0, opensslSubject.Length - 1);
392-
393-
string cmd = $"openssl req -new -newkey REPLACE -nodes -keyout {path}{fileName}.key -out {path}{fileName}.csr -subj '{opensslSubject}'";
394-
switch (keyType)
395-
{
396-
case SupportedKeyTypeEnum.RSA:
397-
cmd = cmd.Replace("REPLACE", $"rsa:{keySize.ToString()}");
398-
break;
399-
case SupportedKeyTypeEnum.ECC:
400-
string algName = "prime256v1";
401-
switch (keySize)
402-
{
403-
case 384:
404-
algName = "secp384r1";
405-
break;
406-
case 521:
407-
algName = "secp521r1";
408-
break;
409-
}
410-
cmd = cmd.Replace("REPLACE", $"ec:<(openssl ecparam -name {algName})");
411-
break;
412-
}
413-
414-
string csr = string.Empty;
415-
416-
try
417-
{
418-
try
419-
{
420-
RemoteHandler.RunCommand(cmd, null, ApplicationSettings.UseSudo, null);
421-
}
422-
catch (Exception ex)
423-
{
424-
if (!ex.Message.Contains("----"))
425-
throw;
426-
}
427-
428-
string privateKeyString = Encoding.UTF8.GetString(RemoteHandler.DownloadCertificateFile(path + fileName + ".key"));
429-
privateKey = keyType == SupportedKeyTypeEnum.RSA ? RSA.Create() : ECDsa.Create();
430-
privateKey.ImportFromPem(privateKeyString);
431-
432-
csr = Encoding.UTF8.GetString(RemoteHandler.DownloadCertificateFile(path + fileName + ".csr"));
433-
}
434-
finally
435-
{
436-
if (RemoteHandler.DoesFileExist(path + fileName + ".key"))
437-
RemoteHandler.RemoveCertificateFile(path, fileName + ".key");
438-
if (RemoteHandler.DoesFileExist(path + fileName + ".csr"))
439-
RemoteHandler.RemoveCertificateFile(path, fileName + ".csr");
440-
}
441-
442-
return csr;
443-
}
444-
445373
internal void Initialize(string sudoImpersonatedUser, bool useShellCommands)
446374
{
447375
logger.MethodEntry(LogLevel.Debug);

RemoteFile/RemoteFileJobTypeBase.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,6 @@ internal void SetJobProperties(JobConfiguration config, CertificateStore certifi
6969
false :
7070
Convert.ToBoolean(properties.IncludePortInSPN.Value);
7171

72-
CreateCSROnDevice = properties.CreateCSROnDevice == null || string.IsNullOrEmpty(properties.CreateCSROnDevice.Value) ?
73-
ApplicationSettings.CreateCSROnDevice :
74-
Convert.ToBoolean(properties.CreateCSROnDevice.Value);
75-
7672
UseShellCommands = properties.UseShellCommands == null || string.IsNullOrEmpty(properties.UseShellCommands.Value) ?
7773
ApplicationSettings.UseShellCommands :
7874
properties.UseShellCommands;
@@ -93,7 +89,6 @@ internal void SetJobProperties(JobConfiguration config, CertificateStore certifi
9389
logger.LogDebug($"RemoveRootCertificate: {RemoveRootCertificate}");
9490
logger.LogDebug($"SSHPort: {SSHPort}");
9591
logger.LogDebug($"IncludePortInSPN: {IncludePortInSPN}");
96-
logger.LogDebug($"CreateCSROnDevice: {CreateCSROnDevice}");
9792
logger.LogDebug($"KeyType: {KeyType}");
9893
logger.LogDebug($"KeySize: {KeySize}");
9994
logger.LogDebug($"SubjectText: {SubjectText}");

RemoteFile/RemoteHandlers/SSHHandler.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ namespace Keyfactor.Extensions.Orchestrator.RemoteFile.RemoteHandlers
2727
{
2828
class SSHHandler : BaseRemoteHandler
2929
{
30+
private readonly string[] IgnoreErrors = { "Could not chdir to home directory" };
3031
private ConnectionInfo Connection { get; set; }
3132
private string SudoImpersonatedUser { get; set; }
3233
private bool IsStoreServerLinux { get; set; }
@@ -141,7 +142,7 @@ public override string RunCommand(string commandText, object[] arguments, bool w
141142
command.Execute();
142143
_logger.LogDebug($"SSH Results: {displayCommand}::: {command.Result}::: {command.Error}");
143144

144-
if (!String.IsNullOrEmpty(command.Error))
145+
if (!String.IsNullOrEmpty(command.Error) && !IgnoreError(command.Error))
145146
throw new ApplicationException(command.Error);
146147

147148
_logger.MethodExit(LogLevel.Debug);
@@ -514,5 +515,10 @@ private void CheckConnection()
514515
throw;
515516
}
516517
}
518+
519+
private bool IgnoreError(string err)
520+
{
521+
return IgnoreErrors.Any(p => err.Contains(p, StringComparison.OrdinalIgnoreCase));
522+
}
517523
}
518524
}

0 commit comments

Comments
 (0)