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+ }
0 commit comments