Skip to content

Commit 3cddd20

Browse files
author
Lee Fine
committed
initial-integration-tess
1 parent 59a4788 commit 3cddd20

File tree

4 files changed

+162
-7
lines changed

4 files changed

+162
-7
lines changed

RemoteFileIntegrationTests/BaseRFPEMTest.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Org.BouncyCastle.Asn1.X509;
1+
using Keyfactor.PKI.Extensions;
2+
using Org.BouncyCastle.Asn1.X509;
23
using Org.BouncyCastle.Crypto;
34
using Org.BouncyCastle.Crypto.Generators;
45
using Org.BouncyCastle.Crypto.Operators;
@@ -33,17 +34,17 @@ public static void RemoveStore(string fileName, bool withExtKeyFile, STORE_ENVIR
3334
RemoveFile($"{fileName}.key", storeEnvironment);
3435
}
3536

36-
public static void CreateCertificateAndKey()
37+
public static string CreateCertificateAndKey(string certNameString)
3738
{
3839
if (!string.IsNullOrEmpty(pemCertificate))
39-
return;
40+
return string.Empty;
4041

4142
var keyGen = new RsaKeyPairGenerator();
4243
keyGen.Init(new KeyGenerationParameters(new SecureRandom(), 2048));
4344
AsymmetricCipherKeyPair keyPair = keyGen.GenerateKeyPair();
4445

4546
// Define certificate attributes
46-
var certName = new X509Name(EnvironmentVariables.CertificateSubjectDN);
47+
var certName = new X509Name(certNameString);
4748
BigInteger serialNumber = BigInteger.ProbablePrime(120, new Random());
4849

4950
// Validity period
@@ -79,6 +80,8 @@ public static void CreateCertificateAndKey()
7980
pw.Writer.Flush();
8081
pemKey = sw.ToString();
8182
}
83+
84+
return certificate.Thumbprint();
8285
}
8386
}
8487
}

RemoteFileIntegrationTests/EnvironmentVariables.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ internal class EnvironmentVariables
1515
public static string? WindowsServer { get { return Environment.GetEnvironmentVariable("WindowsServer"); } }
1616
public static string? WindowsStorePath { get { return Environment.GetEnvironmentVariable("WindowsStorePath"); } }
1717
public static string? StorePassword { get { return Environment.GetEnvironmentVariable("StorePassword"); } }
18-
public static string? CertificateSubjectDN { get { return Environment.GetEnvironmentVariable("CertificateSubjectDN"); } }
18+
public static string? ExistingCertificateSubjectDN { get { return Environment.GetEnvironmentVariable("ExistingCertificateSubjectDN"); } }
19+
public static string? NewCertificaetSubjectDN { get { return Environment.GetEnvironmentVariable("NewCertificaetSubjectDN"); } }
1920
}
2021
}
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
using Keyfactor.Extensions.Orchestrator.RemoteFile.PEM;
2+
using Keyfactor.Orchestrators.Common.Enums;
3+
using Keyfactor.Orchestrators.Extensions;
4+
using Keyfactor.Orchestrators.Extensions.Interfaces;
5+
6+
using Moq;
7+
8+
using Newtonsoft.Json;
9+
10+
using Org.BouncyCastle.X509;
11+
using Org.BouncyCastle.Utilities.IO.Pem;
12+
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;
13+
using Microsoft.PowerShell.Commands;
14+
15+
namespace RemoteFileIntegrationTests
16+
{
17+
public class RFPEMManagementAddTests : BaseRFPEMTest, IClassFixture<RFPEMManagementAddTestsFixture>
18+
{
19+
public static TestConfig[] TestConfigs = {
20+
new TestConfig() { FileName = "Test0001", HasSeparatePrivateKey = false, WithCertificate = false, StoreEnvironment = STORE_ENVIRONMENT_ENUM.LINUX},
21+
new TestConfig() { FileName = "Test0002", HasSeparatePrivateKey = false, WithCertificate = true, StoreEnvironment = STORE_ENVIRONMENT_ENUM.LINUX},
22+
new TestConfig() { FileName = "Test0003", HasSeparatePrivateKey = true, WithCertificate = false, StoreEnvironment = STORE_ENVIRONMENT_ENUM.LINUX},
23+
new TestConfig() { FileName = "Test0004", HasSeparatePrivateKey = true, WithCertificate = true, StoreEnvironment = STORE_ENVIRONMENT_ENUM.LINUX},
24+
};
25+
26+
public static string ExistingAlias { get; set; }
27+
28+
[Fact]
29+
public void RFPEM_Inventory_InternalPrivateKey_EmptyStore_Linux_Test0001()
30+
{
31+
RunTest(TestConfigs[0]);
32+
}
33+
34+
[Fact]
35+
public void RFPEM_Inventory_InternalPrivateKey_WithCert_Linux_Test0002()
36+
{
37+
RunTest(TestConfigs[1]);
38+
}
39+
40+
[Fact]
41+
public void RFPEM_Inventory_InternalPrivateKey_EmptyStore_Linux_Test0003()
42+
{
43+
RunTest(TestConfigs[2]);
44+
}
45+
46+
[Fact]
47+
public void RFPEM_Inventory_InternalPrivateKey_WithCert_Linux_Test0004()
48+
{
49+
RunTest(TestConfigs[3]);
50+
}
51+
52+
private void RunTest(TestConfig testConfig)
53+
{
54+
InventoryJobConfiguration config = BuildBaseInventoryConfig(testConfig.WithCertificate ? ExistingAlias : string.Empty);
55+
config.CertificateStoreDetails.ClientMachine = EnvironmentVariables.LinuxServer;
56+
config.CertificateStoreDetails.StorePath = EnvironmentVariables.LinuxStorePath + $"{testConfig.FileName}.pem";
57+
config.CertificateStoreDetails.Properties = "{}";
58+
if (testConfig.HasSeparatePrivateKey)
59+
config.CertificateStoreDetails.Properties = JsonConvert.SerializeObject(new Dictionary<string, string?>() { { "SeparatePrivateKeyFilePath", Environment.GetEnvironmentVariable("LinuxStorePath") + $"{testConfig.FileName}.key" } });
60+
else
61+
config.CertificateStoreDetails.ClientMachine = EnvironmentVariables.LinuxServer;
62+
63+
Mock<IPAMSecretResolver> secretResolver = GetMockSecretResolver(config);
64+
65+
Mock<SubmitInventoryUpdate> submitInventoryUpdate = new Mock<SubmitInventoryUpdate>();
66+
67+
Inventory inventory = new Inventory(secretResolver.Object);
68+
JobResult result = inventory.ProcessJob(config, submitInventoryUpdate.Object);
69+
70+
Assert.Equal(OrchestratorJobStatusJobResult.Success, result.Result);
71+
72+
if (testConfig.WithCertificate)
73+
{
74+
IInvocation invocation = submitInventoryUpdate.Invocations[0];
75+
List<CurrentInventoryItem> inventoryItems = (List<CurrentInventoryItem>)invocation.Arguments[0];
76+
Assert.Single(inventoryItems);
77+
78+
using (StringReader rdr = new StringReader(inventoryItems[0].Certificates.First()))
79+
{
80+
PemReader pemReader = new PemReader(rdr);
81+
PemObject pemObject = pemReader.ReadPemObject();
82+
X509CertificateParser parser = new X509CertificateParser();
83+
X509Certificate certificate = parser.ReadCertificate(pemObject.Content);
84+
85+
Assert.Equal(EnvironmentVariables.ExistingCertificateSubjectDN, certificate.SubjectDN.ToString());
86+
}
87+
}
88+
}
89+
90+
private ManagementJobConfiguration BuildBaseInventoryConfig()
91+
{
92+
ManagementJobConfiguration config = new ManagementJobConfiguration();
93+
config.JobCertificate = new ManagementJobCertificate();
94+
config.JobCertificate.Contents = ;
95+
config.Capability = "Management";
96+
config.CertificateStoreDetails = new CertificateStore();
97+
config.JobId = new Guid();
98+
config.JobProperties = new Dictionary<string, object>();
99+
config.ServerUsername = EnvironmentVariables.LinuxUserId;
100+
config.ServerPassword = EnvironmentVariables.LinuxUserPassword;
101+
102+
return config;
103+
}
104+
105+
public class TestConfig
106+
{
107+
internal string FileName { get; set; }
108+
internal bool HasSeparatePrivateKey { get; set; }
109+
internal bool WithCertificate { get; set; }
110+
internal bool Overwrite { get; set; }
111+
internal BaseTest.STORE_ENVIRONMENT_ENUM StoreEnvironment { get; set; }
112+
}
113+
}
114+
115+
public class RFPEMManagementAddTestsFixture : IDisposable
116+
{
117+
public RFPEMManagementAddTestsFixture()
118+
{
119+
RFPEMManagementAddTests.ExistingAlias = SetUp(EnvironmentVariables.ExistingCertificateSubjectDN ?? string.Empty);
120+
}
121+
122+
public void Dispose()
123+
{
124+
TearDown();
125+
}
126+
127+
private string SetUp(string certName)
128+
{
129+
string existingAlias = BaseRFPEMTest.CreateCertificateAndKey(certName);
130+
131+
BaseRFPEMTest.CreateStore(RFPEMManagementAddTests.TestConfigs[0].FileName, RFPEMManagementAddTests.TestConfigs[0].HasSeparatePrivateKey, RFPEMManagementAddTests.TestConfigs[0].WithCertificate, RFPEMManagementAddTests.TestConfigs[0].StoreEnvironment);
132+
BaseRFPEMTest.CreateStore(RFPEMManagementAddTests.TestConfigs[1].FileName, RFPEMManagementAddTests.TestConfigs[1].HasSeparatePrivateKey, RFPEMManagementAddTests.TestConfigs[1].WithCertificate, RFPEMManagementAddTests.TestConfigs[1].StoreEnvironment);
133+
BaseRFPEMTest.CreateStore(RFPEMManagementAddTests.TestConfigs[2].FileName, RFPEMManagementAddTests.TestConfigs[2].HasSeparatePrivateKey, RFPEMManagementAddTests.TestConfigs[2].WithCertificate, RFPEMManagementAddTests.TestConfigs[2].StoreEnvironment);
134+
BaseRFPEMTest.CreateStore(RFPEMManagementAddTests.TestConfigs[3].FileName, RFPEMManagementAddTests.TestConfigs[3].HasSeparatePrivateKey, RFPEMManagementAddTests.TestConfigs[3].WithCertificate, RFPEMManagementAddTests.TestConfigs[3].StoreEnvironment);
135+
136+
return existingAlias;
137+
}
138+
139+
private void TearDown()
140+
{
141+
BaseRFPEMTest.RemoveStore(RFPEMManagementAddTests.TestConfigs[0].FileName, RFPEMManagementAddTests.TestConfigs[0].HasSeparatePrivateKey, RFPEMManagementAddTests.TestConfigs[0].StoreEnvironment);
142+
BaseRFPEMTest.RemoveStore(RFPEMManagementAddTests.TestConfigs[1].FileName, RFPEMManagementAddTests.TestConfigs[1].HasSeparatePrivateKey, RFPEMManagementAddTests.TestConfigs[1].StoreEnvironment);
143+
BaseRFPEMTest.RemoveStore(RFPEMManagementAddTests.TestConfigs[2].FileName, RFPEMManagementAddTests.TestConfigs[2].HasSeparatePrivateKey, RFPEMManagementAddTests.TestConfigs[2].StoreEnvironment);
144+
BaseRFPEMTest.RemoveStore(RFPEMManagementAddTests.TestConfigs[3].FileName, RFPEMManagementAddTests.TestConfigs[3].HasSeparatePrivateKey, RFPEMManagementAddTests.TestConfigs[3].StoreEnvironment);
145+
}
146+
}
147+
148+
149+
150+
151+
}

RemoteFileIntegrationTests/RFPEMInventoryTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ private void RunTest(TestConfig testConfig)
7979
X509CertificateParser parser = new X509CertificateParser();
8080
X509Certificate certificate = parser.ReadCertificate(pemObject.Content);
8181

82-
Assert.Equal(EnvironmentVariables.CertificateSubjectDN, certificate.SubjectDN.ToString());
82+
Assert.Equal(EnvironmentVariables.ExistingCertificateSubjectDN, certificate.SubjectDN.ToString());
8383
}
8484
}
8585
}
@@ -120,7 +120,7 @@ public void Dispose()
120120

121121
private void SetUp()
122122
{
123-
BaseRFPEMTest.CreateCertificateAndKey();
123+
BaseRFPEMTest.CreateCertificateAndKey(EnvironmentVariables.ExistingCertificateSubjectDN ?? string.Empty);
124124

125125
BaseRFPEMTest.CreateStore(RFPEMInventoryTests.TestConfigs[0].FileName, RFPEMInventoryTests.TestConfigs[0].HasSeparatePrivateKey, RFPEMInventoryTests.TestConfigs[0].WithCertificate, RFPEMInventoryTests.TestConfigs[0].StoreEnvironment);
126126
BaseRFPEMTest.CreateStore(RFPEMInventoryTests.TestConfigs[1].FileName, RFPEMInventoryTests.TestConfigs[1].HasSeparatePrivateKey, RFPEMInventoryTests.TestConfigs[1].WithCertificate, RFPEMInventoryTests.TestConfigs[1].StoreEnvironment);

0 commit comments

Comments
 (0)