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