Skip to content

Commit dc5fdfb

Browse files
author
Lee Fine
committed
initial-integration-tests
1 parent 95b44a0 commit dc5fdfb

File tree

6 files changed

+174
-8
lines changed

6 files changed

+174
-8
lines changed

RemoteFileIntegrationTests/BaseTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using static Org.BouncyCastle.Math.EC.ECCurve;
66
using Keyfactor.Orchestrators.Extensions;
77

8-
namespace RemoteFileIntegrationTests
8+
namespace Keyfactor.Extensions.Orchestrator.RemoteFileIntegrationTests
99
{
1010
public abstract class BaseTest
1111
{

RemoteFileIntegrationTests/EnvironmentVariables.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System.Text;
55
using System.Threading.Tasks;
66

7-
namespace RemoteFileIntegrationTests
7+
namespace Keyfactor.Extensions.Orchestrator.RemoteFileIntegrationTests
88
{
99
internal class EnvironmentVariables
1010
{

RemoteFileIntegrationTests/BaseRFPEMTest.cs renamed to RemoteFileIntegrationTests/RFPEMTests/BaseRFPEMTest.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88
using Org.BouncyCastle.Pkcs;
99
using Org.BouncyCastle.Security;
1010
using Org.BouncyCastle.X509;
11-
1211
using System.Text;
1312

14-
namespace RemoteFileIntegrationTests
13+
namespace Keyfactor.Extensions.Orchestrator.RemoteFileIntegrationTests.RFPEMTests
1514
{
1615
public class BaseRFPEMTest : BaseTest
1716
{
@@ -29,7 +28,7 @@ public enum CERT_TYPE_ENUM
2928

3029
public static void CreateStore(string fileName, bool withExtKeyFile, bool withCertificate, STORE_ENVIRONMENT_ENUM storeEnvironment)
3130
{
32-
string storeContents = withCertificate ? (withExtKeyFile ? pemCertificate : pemCertificate + System.Environment.NewLine + pemKey) : string.Empty;
31+
string storeContents = withCertificate ? withExtKeyFile ? pemCertificate : pemCertificate + Environment.NewLine + pemKey : string.Empty;
3332
CreateFile($"{fileName}.pem", Encoding.ASCII.GetBytes(storeContents), storeEnvironment);
3433
if (withExtKeyFile)
3534
CreateFile($"{fileName}.key", Encoding.ASCII.GetBytes(withCertificate ? pemKey : string.Empty), storeEnvironment);
@@ -92,7 +91,7 @@ public static string CreateCertificateAndKey(string certNameString, CERT_TYPE_EN
9291
using (var sw = new StringWriter())
9392
{
9493
var pw = new PemWriter(sw);
95-
pw.WriteObject((new Pkcs8Generator(keyPair.Private)).Generate());
94+
pw.WriteObject(new Pkcs8Generator(keyPair.Private).Generate());
9695
pw.Writer.Flush();
9796
pemKey = sw.ToString();
9897
}

RemoteFileIntegrationTests/RFPEMIManagementAddTests.cs renamed to RemoteFileIntegrationTests/RFPEMTests/RFPEMIManagementAddTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;
1616
using System.Text;
1717

18-
namespace RemoteFileIntegrationTests
18+
namespace Keyfactor.Extensions.Orchestrator.RemoteFileIntegrationTests.RFPEMTests
1919
{
2020
public class RFPEMManagementAddTests : BaseRFPEMTest, IClassFixture<RFPEMManagementAddTestsFixture>
2121
{
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
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+
using Keyfactor.PKI.X509;
6+
7+
using Moq;
8+
9+
using Newtonsoft.Json;
10+
11+
using Org.BouncyCastle.X509;
12+
using Org.BouncyCastle.Utilities.IO.Pem;
13+
using Org.BouncyCastle.Pkcs;
14+
using Keyfactor.PKI.Extensions;
15+
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;
16+
using System.Text;
17+
18+
namespace Keyfactor.Extensions.Orchestrator.RemoteFileIntegrationTests.RFPEMTests
19+
{
20+
public class RFPEMManagementRemoveTests : BaseRFPEMTest, IClassFixture<RFPEMManagementRemoveTestsFixture>
21+
{
22+
public static ManagementRemoveTestConfig[] TestConfigs = {
23+
new ManagementRemoveTestConfig() { FileName = "Test0012", UseExistingAlias = true, HasSeparatePrivateKey = false, WithCertificate = true, StoreEnvironment = STORE_ENVIRONMENT_ENUM.LINUX},
24+
new ManagementRemoveTestConfig() { FileName = "Test0013", UseExistingAlias = true, HasSeparatePrivateKey = true, WithCertificate = true, StoreEnvironment = STORE_ENVIRONMENT_ENUM.LINUX},
25+
new ManagementRemoveTestConfig() { FileName = "Test0014", UseExistingAlias = false, HasSeparatePrivateKey = false, WithCertificate = true, StoreEnvironment = STORE_ENVIRONMENT_ENUM.LINUX},
26+
new ManagementRemoveTestConfig() { FileName = "Test0015", UseExistingAlias = false, HasSeparatePrivateKey = true, WithCertificate = true, StoreEnvironment = STORE_ENVIRONMENT_ENUM.LINUX},
27+
new ManagementRemoveTestConfig() { FileName = "Test0016", UseExistingAlias = true, HasSeparatePrivateKey = false, WithCertificate = false, StoreEnvironment = STORE_ENVIRONMENT_ENUM.LINUX},
28+
new ManagementRemoveTestConfig() { FileName = "Test0017", UseExistingAlias = true, HasSeparatePrivateKey = true, WithCertificate = false, StoreEnvironment = STORE_ENVIRONMENT_ENUM.LINUX},
29+
};
30+
31+
private string NewDummyAlias = "abc";
32+
33+
public static string ExistingAlias { get; set; }
34+
35+
[Fact]
36+
public void RFPEM_ManagementRemove_ExistingAlias_InternalKey_NonEmptyStore()
37+
{
38+
RunTest(TestConfigs[0], OrchestratorJobStatusJobResult.Success, string.Empty);
39+
}
40+
41+
[Fact]
42+
public void RFPEM_ManagementRemove_ExistingAlias_ExternalKey_NonEmptyStore()
43+
{
44+
RunTest(TestConfigs[1], OrchestratorJobStatusJobResult.Success, string.Empty);
45+
}
46+
47+
[Fact]
48+
public void RFPEM_ManagementRemove_NonExistingAlias_InternalKey_NonEmptyStore()
49+
{
50+
RunTest(TestConfigs[2], OrchestratorJobStatusJobResult.Failure,
51+
$"Alias {NewDummyAlias} does not exist in certificate store");
52+
}
53+
54+
[Fact]
55+
public void RFPEM_ManagementRemove_NonExistingAlias_ExternalKey_NonEmptyStore()
56+
{
57+
RunTest(TestConfigs[3], OrchestratorJobStatusJobResult.Failure,
58+
$"Alias {NewDummyAlias} does not exist in certificate store");
59+
}
60+
61+
[Fact]
62+
public void RFPEM_ManagementRemove_ExistingAlias_InternalKey_EmptyStore()
63+
{
64+
RunTest(TestConfigs[4], OrchestratorJobStatusJobResult.Failure,
65+
$"Alias {ExistingAlias} does not exist in certificate store");
66+
}
67+
68+
[Fact]
69+
public void RFPEM_ManagementRemove_ExistingAlias_ExternalKey_EmptyStore()
70+
{
71+
RunTest(TestConfigs[5], OrchestratorJobStatusJobResult.Failure,
72+
$"Alias {ExistingAlias} does not exist in certificate store");
73+
}
74+
75+
private void RunTest(ManagementRemoveTestConfig testConfig, OrchestratorJobStatusJobResult expectedResult, string expectedMessage)
76+
{
77+
ManagementJobConfiguration config = new ManagementJobConfiguration();
78+
config.Capability = "Management";
79+
config.OperationType = CertStoreOperationType.Remove;
80+
config.JobId = new Guid();
81+
config.ServerUsername = EnvironmentVariables.LinuxUserId;
82+
config.ServerPassword = EnvironmentVariables.LinuxUserPassword;
83+
84+
config.JobProperties = new Dictionary<string, object>();
85+
86+
config.JobCertificate = new ManagementJobCertificate();
87+
config.JobCertificate.Alias = testConfig.UseExistingAlias ? ExistingAlias : NewDummyAlias;
88+
config.JobCertificate.PrivateKeyPassword = EnvironmentVariables.PrivateKeyPassword;
89+
(config.JobCertificate.Contents, _) = GetNewCert();
90+
91+
config.CertificateStoreDetails = new CertificateStore();
92+
config.CertificateStoreDetails.ClientMachine = EnvironmentVariables.LinuxServer;
93+
config.CertificateStoreDetails.StorePath = EnvironmentVariables.LinuxStorePath + $"{testConfig.FileName}.pem";
94+
config.CertificateStoreDetails.StorePassword = string.Empty;
95+
config.CertificateStoreDetails.Properties = "{}";
96+
if (testConfig.HasSeparatePrivateKey)
97+
config.CertificateStoreDetails.Properties = JsonConvert.SerializeObject(new Dictionary<string, string?>() { { "SeparatePrivateKeyFilePath", Environment.GetEnvironmentVariable("LinuxStorePath") + $"{testConfig.FileName}.key" } });
98+
else
99+
config.CertificateStoreDetails.ClientMachine = EnvironmentVariables.LinuxServer;
100+
101+
Mock<IPAMSecretResolver> secretResolver = GetMockSecretResolver(config);
102+
103+
Management management = new Management(secretResolver.Object);
104+
JobResult result = management.ProcessJob(config);
105+
106+
Assert.Equal(expectedResult, result.Result);
107+
if (!string.IsNullOrEmpty(expectedMessage))
108+
Assert.Contains(expectedMessage, result.FailureMessage);
109+
110+
if (expectedResult == OrchestratorJobStatusJobResult.Success)
111+
{
112+
byte[] certificateBytes = ReadFile(testConfig.FileName + ".pem", testConfig.StoreEnvironment);
113+
byte[] keyBytes = testConfig.HasSeparatePrivateKey ? ReadFile(testConfig.FileName + ".key", testConfig.StoreEnvironment) : [];
114+
string certificatePEM = Encoding.ASCII.GetString(certificateBytes) + (keyBytes.Length > 0 ? Encoding.ASCII.GetString(keyBytes) : string.Empty);
115+
Assert.Equal(0, certificatePEM.Split(new string[] { "BEGIN CERTIFICATE" }, StringSplitOptions.None).Length - 1);
116+
Assert.Equal(0, certificatePEM.Split(new string[] { "BEGIN PRIVATE KEY" }, StringSplitOptions.None).Length - 1);
117+
}
118+
}
119+
}
120+
121+
public class ManagementRemoveTestConfig
122+
{
123+
internal string FileName { get; set; }
124+
internal bool UseExistingAlias { get; set; }
125+
internal bool HasSeparatePrivateKey { get; set; }
126+
internal bool WithCertificate { get; set; }
127+
internal BaseTest.STORE_ENVIRONMENT_ENUM StoreEnvironment { get; set; }
128+
}
129+
130+
public class RFPEMManagementRemoveTestsFixture : IDisposable
131+
{
132+
public RFPEMManagementRemoveTestsFixture()
133+
{
134+
RFPEMManagementRemoveTests.ExistingAlias = SetUp(EnvironmentVariables.ExistingCertificateSubjectDN ?? string.Empty, EnvironmentVariables.NewCertificaetSubjectDN ?? string.Empty);
135+
}
136+
137+
public void Dispose()
138+
{
139+
TearDown();
140+
}
141+
142+
private string SetUp(string certName, string newCertName)
143+
{
144+
string existingAlias = BaseRFPEMTest.CreateCertificateAndKey(certName, BaseRFPEMTest.CERT_TYPE_ENUM.PEM);
145+
string newAlias = BaseRFPEMTest.CreateCertificateAndKey(newCertName, BaseRFPEMTest.CERT_TYPE_ENUM.PFX);
146+
147+
foreach(ManagementRemoveTestConfig config in RFPEMManagementRemoveTests.TestConfigs)
148+
{
149+
BaseRFPEMTest.CreateStore(config.FileName, config.HasSeparatePrivateKey, config.WithCertificate, config.StoreEnvironment);
150+
}
151+
152+
return existingAlias;
153+
}
154+
155+
private void TearDown()
156+
{
157+
foreach (ManagementRemoveTestConfig config in RFPEMManagementRemoveTests.TestConfigs)
158+
{
159+
BaseRFPEMTest.RemoveStore(config.FileName, config.HasSeparatePrivateKey, config.StoreEnvironment);
160+
}
161+
}
162+
}
163+
164+
165+
166+
167+
}

RemoteFileIntegrationTests/RFPEMInventoryTests.cs renamed to RemoteFileIntegrationTests/RFPEMTests/RFPEMInventoryTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
using Org.BouncyCastle.Utilities.IO.Pem;
1212
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;
1313

14-
namespace RemoteFileIntegrationTests
14+
namespace Keyfactor.Extensions.Orchestrator.RemoteFileIntegrationTests.RFPEMTests
1515
{
1616
public class RFPEMInventoryTests : BaseRFPEMTest, IClassFixture<RFPEMInventoryTestsFixture>
1717
{

0 commit comments

Comments
 (0)