22using Keyfactor . Orchestrators . Common . Enums ;
33using Keyfactor . Orchestrators . Extensions ;
44using Keyfactor . Orchestrators . Extensions . Interfaces ;
5+ using Keyfactor . PKI . X509 ;
56
67using Moq ;
78
89using Newtonsoft . Json ;
910
1011using Org . BouncyCastle . X509 ;
1112using Org . BouncyCastle . Utilities . IO . Pem ;
13+ using Org . BouncyCastle . Pkcs ;
14+ using Keyfactor . PKI . Extensions ;
1215using Microsoft . VisualStudio . TestPlatform . ObjectModel . Client ;
13- using Microsoft . PowerShell . Commands ;
1416
1517namespace RemoteFileIntegrationTests
1618{
1719 public class RFPEMManagementAddTests : BaseRFPEMTest , IClassFixture < RFPEMManagementAddTestsFixture >
1820 {
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 } ,
21+ public static ManagementAddTestConfig [ ] TestConfigs = {
22+ new ManagementAddTestConfig ( ) { FileName = "Test0005" , UseExistingAlias = false , HasSeparatePrivateKey = false , WithCertificate = false , Overwrite = false , StoreEnvironment = STORE_ENVIRONMENT_ENUM . LINUX } ,
23+ new ManagementAddTestConfig ( ) { FileName = "Test0006" , UseExistingAlias = false , HasSeparatePrivateKey = true , WithCertificate = false , Overwrite = false , StoreEnvironment = STORE_ENVIRONMENT_ENUM . LINUX } ,
24+ new ManagementAddTestConfig ( ) { FileName = "Test0007" , UseExistingAlias = true , HasSeparatePrivateKey = false , WithCertificate = true , Overwrite = false , StoreEnvironment = STORE_ENVIRONMENT_ENUM . LINUX } ,
25+ new ManagementAddTestConfig ( ) { FileName = "Test0008" , UseExistingAlias = true , HasSeparatePrivateKey = false , WithCertificate = true , Overwrite = true , StoreEnvironment = STORE_ENVIRONMENT_ENUM . LINUX } ,
26+ new ManagementAddTestConfig ( ) { FileName = "Test0009" , UseExistingAlias = true , HasSeparatePrivateKey = true , WithCertificate = true , Overwrite = true , StoreEnvironment = STORE_ENVIRONMENT_ENUM . LINUX } ,
27+ new ManagementAddTestConfig ( ) { FileName = "Test0010" , UseExistingAlias = false , HasSeparatePrivateKey = false , WithCertificate = true , Overwrite = false , StoreEnvironment = STORE_ENVIRONMENT_ENUM . LINUX } ,
28+ new ManagementAddTestConfig ( ) { FileName = "Test0011" , UseExistingAlias = false , HasSeparatePrivateKey = true , WithCertificate = true , Overwrite = true , StoreEnvironment = STORE_ENVIRONMENT_ENUM . LINUX } ,
2429 } ;
2530
2631 public static string ExistingAlias { get ; set ; }
2732
2833 [ Fact ]
29- public void RFPEM_Inventory_InternalPrivateKey_EmptyStore_Linux_Test0001 ( )
34+ public void RFPEM_ManagementAdd_NewAlias_InternalKey_EmptyStore_NoOverwrite ( )
3035 {
31- RunTest ( TestConfigs [ 0 ] ) ;
36+ RunTest ( TestConfigs [ 0 ] , OrchestratorJobStatusJobResult . Success , string . Empty ) ;
3237 }
3338
3439 [ Fact ]
35- public void RFPEM_Inventory_InternalPrivateKey_WithCert_Linux_Test0002 ( )
40+ public void RFPEM_ManagementAdd_NewAlias_ExternalKey_EmptyStore_NoOverwrite ( )
3641 {
37- RunTest ( TestConfigs [ 1 ] ) ;
42+ RunTest ( TestConfigs [ 1 ] , OrchestratorJobStatusJobResult . Success , string . Empty ) ;
3843 }
3944
4045 [ Fact ]
41- public void RFPEM_Inventory_InternalPrivateKey_EmptyStore_Linux_Test0003 ( )
46+ public void RFPEM_ManagementAdd_ExistingAlias_InternalKey_NonEmptyStore_NoOverwrite ( )
4247 {
43- RunTest ( TestConfigs [ 2 ] ) ;
48+ RunTest ( TestConfigs [ 2 ] , OrchestratorJobStatusJobResult . Warning , "" ) ;
4449 }
4550
4651 [ Fact ]
47- public void RFPEM_Inventory_InternalPrivateKey_WithCert_Linux_Test0004 ( )
52+ public void RFPEM_ManagementAdd_ExistingAlias_InternalKey_NonEmptyStore_YesOverwrite ( )
4853 {
49- RunTest ( TestConfigs [ 3 ] ) ;
54+ RunTest ( TestConfigs [ 3 ] , OrchestratorJobStatusJobResult . Success , string . Empty ) ;
5055 }
5156
52- private void RunTest ( TestConfig testConfig )
57+ [ Fact ]
58+ public void RFPEM_ManagementAdd_ExistingAlias_ExternalKey_NonEmptyStore_YesOverwrite ( )
59+ {
60+ RunTest ( TestConfigs [ 4 ] , OrchestratorJobStatusJobResult . Success , string . Empty ) ;
61+ }
62+
63+ [ Fact ]
64+ public void RFPEM_ManagementAdd_NewAlias_InternalKey_NonEmptyStore_NoOverwrite ( )
65+ {
66+ RunTest ( TestConfigs [ 5 ] , OrchestratorJobStatusJobResult . Success , string . Empty ) ;
67+ }
68+
69+ [ Fact ]
70+ public void RFPEM_ManagementAdd_NewAlias_ExternalKey_NonEmptyStore_YesOverwrite ( )
71+ {
72+ RunTest ( TestConfigs [ 6 ] , OrchestratorJobStatusJobResult . Success , string . Empty ) ;
73+ }
74+
75+ private void RunTest ( ManagementAddTestConfig testConfig , OrchestratorJobStatusJobResult expectedResult , string expectedMessage )
5376 {
54- ManagementJobConfiguration config = BuildBaseInventoryConfig ( ) ;
55- config . JobCertificate . Alias = testConfig . WithCertificate ? ExistingAlias : string . Empty ;
77+ ManagementJobConfiguration config = new ManagementJobConfiguration ( ) ;
78+ config . Capability = "Management" ;
79+ config . OperationType = CertStoreOperationType . Add ;
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 : string . Empty ;
88+ config . JobCertificate . PrivateKeyPassword = EnvironmentVariables . PrivateKeyPassword ;
89+ ( config . JobCertificate . Contents , _ ) = GetNewCert ( ) ;
90+
91+ config . CertificateStoreDetails = new CertificateStore ( ) ;
5692 config . CertificateStoreDetails . ClientMachine = EnvironmentVariables . LinuxServer ;
5793 config . CertificateStoreDetails . StorePath = EnvironmentVariables . LinuxStorePath + $ "{ testConfig . FileName } .pem";
94+ config . CertificateStoreDetails . StorePassword = string . Empty ;
5895 config . CertificateStoreDetails . Properties = "{}" ;
5996 if ( testConfig . HasSeparatePrivateKey )
6097 config . CertificateStoreDetails . Properties = JsonConvert . SerializeObject ( new Dictionary < string , string ? > ( ) { { "SeparatePrivateKeyFilePath" , Environment . GetEnvironmentVariable ( "LinuxStorePath" ) + $ "{ testConfig . FileName } .key" } } ) ;
@@ -63,54 +100,37 @@ private void RunTest(TestConfig testConfig)
63100
64101 Mock < IPAMSecretResolver > secretResolver = GetMockSecretResolver ( config ) ;
65102
66- Mock < SubmitInventoryUpdate > submitInventoryUpdate = new Mock < SubmitInventoryUpdate > ( ) ;
67-
68103 Management management = new Management ( secretResolver . Object ) ;
69- management . ProcessJob ( config ) ;
104+ JobResult result = management . ProcessJob ( config ) ;
70105
71- Assert . Equal ( OrchestratorJobStatusJobResult . Success , result . Result ) ;
106+ Assert . Equal ( expectedResult , result . Result ) ;
107+ if ( ! string . IsNullOrEmpty ( expectedMessage ) )
108+ Assert . Equal ( expectedMessage , result . FailureMessage ) ;
72109
73- if ( testConfig . WithCertificate )
110+ if ( expectedResult == OrchestratorJobStatusJobResult . Success )
74111 {
75- IInvocation invocation = submitInventoryUpdate . Invocations [ 0 ] ;
76- List < CurrentInventoryItem > inventoryItems = ( List < CurrentInventoryItem > ) invocation . Arguments [ 0 ] ;
77- Assert . Single ( inventoryItems ) ;
78-
79- using ( StringReader rdr = new StringReader ( inventoryItems [ 0 ] . Certificates . First ( ) ) )
80- {
81- PemReader pemReader = new PemReader ( rdr ) ;
82- PemObject pemObject = pemReader . ReadPemObject ( ) ;
83- X509CertificateParser parser = new X509CertificateParser ( ) ;
84- X509Certificate certificate = parser . ReadCertificate ( pemObject . Content ) ;
85-
86- Assert . Equal ( EnvironmentVariables . ExistingCertificateSubjectDN , certificate . SubjectDN . ToString ( ) ) ;
87- }
112+ byte [ ] certificateBytes = ReadFile ( testConfig . FileName + ".pem" , testConfig . StoreEnvironment ) ;
113+ byte [ ] keyBytes = testConfig . HasSeparatePrivateKey ? ReadFile ( testConfig . FileName + ".key" , testConfig . StoreEnvironment ) : [ ] ;
114+ string certificatePEM = Convert . ToBase64String ( certificateBytes ) + ( keyBytes . Length > 0 ? Convert . ToBase64String ( keyBytes ) : string . Empty ) ;
115+ Assert . Equal ( 1 , certificatePEM . Split ( new string [ ] { "BEGIN CERTIFICATE" } , StringSplitOptions . None ) . Length - 1 ) ;
116+ Assert . Equal ( 1 , certificatePEM . Split ( new string [ ] { "BEGIN PRIVATE KEY" } , StringSplitOptions . None ) . Length - 1 ) ;
117+
118+ CertificateConverter converter = CertificateConverterFactory . FromPEM ( certificatePEM ) ;
119+ X509Certificate certificate = converter . ToBouncyCastleCertificate ( ) ;
120+ ( _ , string thumbprint ) = GetNewCert ( ) ;
121+ Assert . Equal ( thumbprint , certificate . Thumbprint ( ) ) ;
88122 }
89123 }
124+ }
90125
91- private ManagementJobConfiguration BuildBaseInventoryConfig ( )
92- {
93- ManagementJobConfiguration config = new ManagementJobConfiguration ( ) ;
94- config . JobCertificate = new ManagementJobCertificate ( ) ;
95- config . JobCertificate . Contents = GetNewCert ( ) ;
96- config . Capability = "Management" ;
97- config . CertificateStoreDetails = new CertificateStore ( ) ;
98- config . JobId = new Guid ( ) ;
99- config . JobProperties = new Dictionary < string , object > ( ) ;
100- config . ServerUsername = EnvironmentVariables . LinuxUserId ;
101- config . ServerPassword = EnvironmentVariables . LinuxUserPassword ;
102-
103- return config ;
104- }
105-
106- public class TestConfig
107- {
108- internal string FileName { get ; set ; }
109- internal bool HasSeparatePrivateKey { get ; set ; }
110- internal bool WithCertificate { get ; set ; }
111- internal bool Overwrite { get ; set ; }
112- internal BaseTest . STORE_ENVIRONMENT_ENUM StoreEnvironment { get ; set ; }
113- }
126+ public class ManagementAddTestConfig
127+ {
128+ internal string FileName { get ; set ; }
129+ internal bool UseExistingAlias { get ; set ; }
130+ internal bool HasSeparatePrivateKey { get ; set ; }
131+ internal bool WithCertificate { get ; set ; }
132+ internal bool Overwrite { get ; set ; }
133+ internal BaseTest . STORE_ENVIRONMENT_ENUM StoreEnvironment { get ; set ; }
114134 }
115135
116136 public class RFPEMManagementAddTestsFixture : IDisposable
@@ -130,20 +150,20 @@ private string SetUp(string certName, string newCertName)
130150 string existingAlias = BaseRFPEMTest . CreateCertificateAndKey ( certName , BaseRFPEMTest . CERT_TYPE_ENUM . PEM ) ;
131151 string newAlias = BaseRFPEMTest . CreateCertificateAndKey ( newCertName , BaseRFPEMTest . CERT_TYPE_ENUM . PFX ) ;
132152
133- BaseRFPEMTest . CreateStore ( RFPEMManagementAddTests . TestConfigs [ 0 ] . FileName , RFPEMManagementAddTests . TestConfigs [ 0 ] . HasSeparatePrivateKey , RFPEMManagementAddTests . TestConfigs [ 0 ] . WithCertificate , RFPEMManagementAddTests . TestConfigs [ 0 ] . StoreEnvironment ) ;
134- BaseRFPEMTest . CreateStore ( RFPEMManagementAddTests . TestConfigs [ 1 ] . FileName , RFPEMManagementAddTests . TestConfigs [ 1 ] . HasSeparatePrivateKey , RFPEMManagementAddTests . TestConfigs [ 1 ] . WithCertificate , RFPEMManagementAddTests . TestConfigs [ 1 ] . StoreEnvironment ) ;
135- BaseRFPEMTest . CreateStore ( RFPEMManagementAddTests . TestConfigs [ 2 ] . FileName , RFPEMManagementAddTests . TestConfigs [ 2 ] . HasSeparatePrivateKey , RFPEMManagementAddTests . TestConfigs [ 2 ] . WithCertificate , RFPEMManagementAddTests . TestConfigs [ 2 ] . StoreEnvironment ) ;
136- BaseRFPEMTest . CreateStore ( RFPEMManagementAddTests . TestConfigs [ 3 ] . FileName , RFPEMManagementAddTests . TestConfigs [ 3 ] . HasSeparatePrivateKey , RFPEMManagementAddTests . TestConfigs [ 3 ] . WithCertificate , RFPEMManagementAddTests . TestConfigs [ 3 ] . StoreEnvironment ) ;
153+ foreach ( ManagementAddTestConfig config in RFPEMManagementAddTests . TestConfigs )
154+ {
155+ BaseRFPEMTest . CreateStore ( config . FileName , config . HasSeparatePrivateKey , config . WithCertificate , config . StoreEnvironment ) ;
156+ }
137157
138158 return existingAlias ;
139159 }
140160
141161 private void TearDown ( )
142162 {
143- BaseRFPEMTest . RemoveStore ( RFPEMManagementAddTests . TestConfigs [ 0 ] . FileName , RFPEMManagementAddTests . TestConfigs [ 0 ] . HasSeparatePrivateKey , RFPEMManagementAddTests . TestConfigs [ 0 ] . StoreEnvironment ) ;
144- BaseRFPEMTest . RemoveStore ( RFPEMManagementAddTests . TestConfigs [ 1 ] . FileName , RFPEMManagementAddTests . TestConfigs [ 1 ] . HasSeparatePrivateKey , RFPEMManagementAddTests . TestConfigs [ 1 ] . StoreEnvironment ) ;
145- BaseRFPEMTest . RemoveStore ( RFPEMManagementAddTests . TestConfigs [ 2 ] . FileName , RFPEMManagementAddTests . TestConfigs [ 2 ] . HasSeparatePrivateKey , RFPEMManagementAddTests . TestConfigs [ 2 ] . StoreEnvironment ) ;
146- BaseRFPEMTest . RemoveStore ( RFPEMManagementAddTests . TestConfigs [ 3 ] . FileName , RFPEMManagementAddTests . TestConfigs [ 3 ] . HasSeparatePrivateKey , RFPEMManagementAddTests . TestConfigs [ 3 ] . StoreEnvironment ) ;
163+ foreach ( ManagementAddTestConfig config in RFPEMManagementAddTests . TestConfigs )
164+ {
165+ BaseRFPEMTest . RemoveStore ( config . FileName , config . HasSeparatePrivateKey , config . StoreEnvironment ) ;
166+ }
147167 }
148168 }
149169
0 commit comments