Skip to content

Commit c451d99

Browse files
author
Lee Fine
committed
ab#80665
1 parent 92da1c0 commit c451d99

59 files changed

Lines changed: 36 additions & 2283 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CitrixAdcOrchestratorJobExtension.sln

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 16
4-
VisualStudioVersion = 16.0.31229.75
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.13.35931.197 d17.13
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Keyfactor.Extensions.Orchestrator.CitricAdc", "CitrixAdcOrchestratorJobExtension\Keyfactor.Extensions.Orchestrator.CitricAdc.csproj", "{2B3106BF-A1B4-4BCC-9650-597B576E14D0}"
77
EndProject
8-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CitrixAdcTestConsole", "CitrixAdcTestConsole\CitrixAdcTestConsole.csproj", "{28D3BFB3-1484-4A4A-9BC1-3D20255943FD}"
9-
EndProject
108
Global
119
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1210
Debug|Any CPU = Debug|Any CPU
@@ -17,10 +15,6 @@ Global
1715
{2B3106BF-A1B4-4BCC-9650-597B576E14D0}.Debug|Any CPU.Build.0 = Debug|Any CPU
1816
{2B3106BF-A1B4-4BCC-9650-597B576E14D0}.Release|Any CPU.ActiveCfg = Release|Any CPU
1917
{2B3106BF-A1B4-4BCC-9650-597B576E14D0}.Release|Any CPU.Build.0 = Release|Any CPU
20-
{28D3BFB3-1484-4A4A-9BC1-3D20255943FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21-
{28D3BFB3-1484-4A4A-9BC1-3D20255943FD}.Debug|Any CPU.Build.0 = Debug|Any CPU
22-
{28D3BFB3-1484-4A4A-9BC1-3D20255943FD}.Release|Any CPU.ActiveCfg = Release|Any CPU
23-
{28D3BFB3-1484-4A4A-9BC1-3D20255943FD}.Release|Any CPU.Build.0 = Release|Any CPU
2418
EndGlobalSection
2519
GlobalSection(SolutionProperties) = preSolution
2620
HideSolutionNode = FALSE

CitrixAdcOrchestratorJobExtension/CitrixAdcStore.cs

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@
1616
using System.IO;
1717
using System.Linq;
1818
using System.Collections.Generic;
19-
using System.Runtime.ConstrainedExecution;
2019
using System.Security.Cryptography.X509Certificates;
2120
using System.Text;
22-
using System.Xml.Linq;
2321
using com.citrix.netscaler.nitro.exception;
2422
using com.citrix.netscaler.nitro.resource.Base;
2523
using com.citrix.netscaler.nitro.resource.config.ssl;
@@ -28,6 +26,9 @@
2826
using com.citrix.netscaler.nitro.util;
2927
using Keyfactor.Logging;
3028
using Keyfactor.Orchestrators.Extensions;
29+
using Keyfactor.PKI.CryptographicObjects.Formatters;
30+
using Keyfactor.PKI.PEM;
31+
using Keyfactor.PKI.PrivateKeys;
3132
using Microsoft.Extensions.Logging;
3233
using Newtonsoft.Json;
3334
using Org.BouncyCastle.Crypto;
@@ -328,8 +329,9 @@ public void UpdateKeyPair(string keyPairName, string certFileName, string keyFil
328329
}
329330
catch (nitro_exception ne)
330331
{
331-
Logger.LogError($"Exception occured while trying to add or update {keyPairName}. {LogHandler.FlattenException(ne)}");
332-
throw;
332+
string error = $"Exception occured while trying to add or update {keyPairName}.";
333+
Logger.LogError(error + LogHandler.FlattenException(ne));
334+
throw new Exception(error, ne);
333335
}
334336

335337
Logger.MethodExit(LogLevel.Debug);
@@ -478,47 +480,33 @@ public void LinkToIssuer(string cert, string privateKeyPassword, string keyPairN
478480
Logger.MethodExit(LogLevel.Debug);
479481
}
480482

481-
private (string, string) GetPemFromPfx(byte[] pfxBytes, char[] pfxPassword)
483+
private (string, string) GetPemFromPfx(byte[] pfxBytes, char[] pfxPassword, string storePassword)
482484
{
483485
Logger.MethodEntry(LogLevel.Debug);
484486

485487
try
486488
{
487-
var p = new Pkcs12Store(new MemoryStream(pfxBytes), pfxPassword);
488-
489-
// Extract private key
490-
var memoryStream = new MemoryStream();
491-
TextWriter streamWriter = new StreamWriter(memoryStream);
492-
var pemWriter = new PemWriter(streamWriter);
489+
Pkcs12StoreBuilder storeBuilder = new Pkcs12StoreBuilder();
490+
Pkcs12Store store = storeBuilder.Build();
491+
store.Load(new MemoryStream(pfxBytes), pfxPassword);
493492

494-
var alias = p.Aliases.Cast<string>().SingleOrDefault(a => p.IsKeyEntry(a));
495-
Logger.LogTrace($"alias: {alias}");
493+
var alias = store.Aliases.Cast<string>().SingleOrDefault(p => store.IsKeyEntry(p));
496494

497-
var publicKey = p.GetCertificate(alias).Certificate.GetPublicKey();
498-
if (p.GetKey(alias) == null) throw new Exception($"Unable to get the key for alias: {alias}");
499-
var privateKey = p.GetKey(alias).Key;
500-
var keyPair = new AsymmetricCipherKeyPair(publicKey, privateKey);
495+
X509CertificateEntry[] chainEntries = store.GetCertificateChain(alias);
496+
Org.BouncyCastle.X509.X509Certificate endCertificate = chainEntries[0].Certificate;
501497

502-
pemWriter.WriteObject(keyPair.Private);
503-
streamWriter.Flush();
504-
var privateKeyString = Encoding.ASCII.GetString(memoryStream.GetBuffer()).Trim().Replace("\r", "")
505-
.Replace("\0", "");
506-
memoryStream.Close();
507-
streamWriter.Close();
498+
AsymmetricKeyParameter privateKey = store.GetKey(alias).Key;
499+
PrivateKeyConverter keyConverter = PrivateKeyConverterFactory.FromBCPrivateKeyAndCert(privateKey, endCertificate);
508500

509-
// Extract server certificate
510-
var certStart = "-----BEGIN CERTIFICATE-----\n";
511-
var certEnd = "\n-----END CERTIFICATE-----";
501+
string pemString = CryptographicObjectFormatter.PEM.Format(endCertificate, false);
502+
string keyString = string.Empty;
512503

513-
string Pemify(string ss)
514-
{
515-
return ss.Length <= 64 ? ss : ss.Substring(0, 64) + "\n" + Pemify(ss.Substring(64));
516-
}
504+
if (string.IsNullOrEmpty(storePassword))
505+
keyString = PemUtilities.DERToPEM(keyConverter.ToPkcs8BlobUnencrypted(), Keyfactor.PKI.PEM.PemUtilities.PemObjectType.PrivateKey);
506+
else
507+
keyString = CryptographicObjectFormatter.PEM.Format(keyConverter, storePassword);
517508

518-
var certPem =
519-
certStart + Pemify(Convert.ToBase64String(p.GetCertificate(alias).Certificate.GetEncoded())) +
520-
certEnd;
521-
return (certPem, privateKeyString);
509+
return (pemString, keyString);
522510
}
523511
catch (Exception e)
524512
{
@@ -611,14 +599,14 @@ private systemfile GetSystemFile(string fileName)
611599
}
612600
}
613601

614-
public (systemfile pemFile, systemfile privateKeyFile) UploadCertificate(string contents, string pwd,
602+
public (systemfile pemFile, systemfile privateKeyFile) UploadCertificate(string contents, string certTempPassword, string storePassword,
615603
string alias, bool overwrite)
616604
{
617605
Logger.MethodEntry(LogLevel.Debug);
618606

619607
try
620608
{
621-
var (certificate, privateKey) = GetPemFromPfx(Convert.FromBase64String(contents), pwd.ToCharArray());
609+
var (certificate, privateKey) = GetPemFromPfx(Convert.FromBase64String(contents), certTempPassword.ToCharArray(), storePassword);
622610

623611
//upload certificate and key
624612
systemfile certificateFile = UploadFile(alias, certificate, true, 0);

CitrixAdcOrchestratorJobExtension/Keyfactor.Extensions.Orchestrator.CitricAdc.csproj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22

33
<PropertyGroup>
44
<AppendTargetFrameworkToOutputPath>true</AppendTargetFrameworkToOutputPath>
5-
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
5+
<TargetFramework>net8.0</TargetFramework>
66
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
77
<ImplicitUsings>disable</ImplicitUsings>
88
</PropertyGroup>
99

1010
<ItemGroup>
1111
<PackageReference Include="Keyfactor.Common" Version="2.3.6" />
12-
<PackageReference Include="Keyfactor.Logging" Version="1.1.1" />
1312
<PackageReference Include="Keyfactor.Orchestrators.IOrchestratorJobExtensions" Version="0.7.0" />
14-
<PackageReference Include="Portable.BouncyCastle" Version="1.8.10" />
13+
<PackageReference Include="Keyfactor.PKI" Version="8.2.2" />
1514
<PackageReference Include="System.Text.Encodings.Web" Version="6.0.0" />
1615

1716
<None Update="manifest.json">

CitrixAdcOrchestratorJobExtension/Management.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public JobResult ProcessJob(ManagementJobConfiguration jobConfiguration)
126126
}
127127
}
128128

129-
PerformAdd(store, jobConfiguration.JobCertificate, virtualServerNames,
129+
PerformAdd(store, jobConfiguration.JobCertificate, jobConfiguration.CertificateStoreDetails.StorePassword, virtualServerNames,
130130
aliasExists, jobConfiguration.Overwrite, sniCerts, linkToIssuer);
131131

132132
if (ApplicationSettings.AutoSaveConfig)
@@ -162,7 +162,7 @@ public JobResult ProcessJob(ManagementJobConfiguration jobConfiguration)
162162
{
163163
Result = OrchestratorJobStatusJobResult.Warning,
164164
JobHistoryId = jobConfiguration.JobHistoryId,
165-
FailureMessage = ex.Message
165+
FailureMessage = LogHandler.FlattenException(ex, true)
166166
};
167167
}
168168
catch (Exception ex)
@@ -172,14 +172,15 @@ public JobResult ProcessJob(ManagementJobConfiguration jobConfiguration)
172172
{
173173
Result = OrchestratorJobStatusJobResult.Failure,
174174
JobHistoryId = jobConfiguration.JobHistoryId,
175-
FailureMessage = ex.Message
175+
FailureMessage = LogHandler.FlattenException(ex, true)
176176
};
177177
}
178178

179179
JobResult result = new JobResult
180180
{
181181
Result = OrchestratorJobStatusJobResult.Success,
182-
JobHistoryId = jobConfiguration.JobHistoryId
182+
JobHistoryId = jobConfiguration.JobHistoryId,
183+
FailureMessage = "testing 1-2-3, testing"
183184
};
184185

185186
_logger.LogDebug("Logging out of Citrix...");
@@ -191,14 +192,14 @@ public JobResult ProcessJob(ManagementJobConfiguration jobConfiguration)
191192
return result;
192193
}
193194

194-
private void PerformAdd(CitrixAdcStore store, ManagementJobCertificate cert,
195+
private void PerformAdd(CitrixAdcStore store, ManagementJobCertificate cert, string storePassword,
195196
List<string> virtualServerNames, bool aliasExists, bool overwrite, List<bool> sniCerts, bool linkToIssuer)
196197
{
197198
_logger.MethodEntry(LogLevel.Debug);
198199

199200
_logger.LogDebug("Updating keyPair");
200201

201-
var (pemFile, privateKeyFile) = store.UploadCertificate(cert.Contents, cert.PrivateKeyPassword, cert.Alias, overwrite);
202+
var (pemFile, privateKeyFile) = store.UploadCertificate(cert.Contents, cert.PrivateKeyPassword, storePassword, cert.Alias, overwrite);
202203
store.UpdateKeyPair(cert.Alias, pemFile.filename, privateKeyFile.filename);
203204

204205
_logger.LogDebug("Updating cert bindings");

CitrixAdcTestConsole/CitrixAdcTestConsole.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
109
<PackageReference Include="Moq.AutoMock" Version="3.4.0" />
1110
<PackageReference Include="RestSharp" Version="112.1.0" />
1211
</ItemGroup>
-2.8 MB
Binary file not shown.
-273 KB
Binary file not shown.

0 commit comments

Comments
 (0)