1313using Microsoft . Extensions . Logging ;
1414using Keyfactor . Logging ;
1515using System . Reflection ;
16+ using Microsoft . PowerShell ;
1617
1718
1819namespace Keyfactor . Extensions . Orchestrator . RemoteFile
@@ -24,25 +25,25 @@ public class ApplicationSettings
2425 private const string DEFAULT_SUDO_IMPERSONATION_SETTING = "" ;
2526 private const int DEFAULT_SSH_PORT = 22 ;
2627
27- private static Dictionary < string , string > configuration ;
28-
29- public static bool UseSudo { get { return configuration . ContainsKey ( "UseSudo" ) ? configuration [ "UseSudo" ] ? . ToUpper ( ) == "Y" : false ; } }
30- public static bool CreateStoreIfMissing { get { return configuration . ContainsKey ( "CreateStoreIfMissing" ) ? configuration [ "CreateStoreIfMissing" ] ? . ToUpper ( ) == "Y" : false ; } }
31- public static bool UseNegotiate { get { return configuration . ContainsKey ( "UseNegotiate" ) ? configuration [ "UseNegotiate" ] ? . ToUpper ( ) == "Y" : false ; } }
32- public static string SeparateUploadFilePath { get { return configuration . ContainsKey ( "SeparateUploadFilePath" ) ? AddTrailingSlash ( configuration [ "SeparateUploadFilePath" ] ) : string . Empty ; } }
33- public static string DefaultLinuxPermissionsOnStoreCreation { get { return configuration . ContainsKey ( "DefaultLinuxPermissionsOnStoreCreation" ) ? configuration [ "DefaultLinuxPermissionsOnStoreCreation" ] : DEFAULT_LINUX_PERMISSION_SETTING ; } }
34- public static string DefaultOwnerOnStoreCreation { get { return configuration . ContainsKey ( "DefaultOwnerOnStoreCreation" ) ? configuration [ "DefaultOwnerOnStoreCreation" ] : DEFAULT_OWNER_SETTING ; } }
35- public static string DefaultSudoImpersonatedUser { get { return configuration . ContainsKey ( "DefaultSudoImpersonatedUser" ) ? configuration [ "DefaultSudoImpersonatedUser" ] : DEFAULT_SUDO_IMPERSONATION_SETTING ; } }
36- public static string TempFilePathForODKG { get { return configuration . ContainsKey ( "TempFilePathForODKG" ) ? configuration [ "TempFilePathForODKG" ] : string . Empty ; } }
37- public static bool UseShellCommands { get { return configuration . ContainsKey ( "UseShellCommands" ) ? configuration [ "UseShellCommands" ] ? . ToUpper ( ) == "Y" : true ; } }
28+ private static Dictionary < string , object > configuration ;
29+
30+ public static bool UseSudo { get { return configuration . ContainsKey ( "UseSudo" ) ? configuration [ "UseSudo" ] ? . ToString ( ) . ToUpper ( ) == "Y" : false ; } }
31+ public static bool CreateStoreIfMissing { get { return configuration . ContainsKey ( "CreateStoreIfMissing" ) ? configuration [ "CreateStoreIfMissing" ] ? . ToString ( ) . ToUpper ( ) == "Y" : false ; } }
32+ public static bool UseNegotiate { get { return configuration . ContainsKey ( "UseNegotiate" ) ? configuration [ "UseNegotiate" ] ? . ToString ( ) . ToUpper ( ) == "Y" : false ; } }
33+ public static string SeparateUploadFilePath { get { return configuration . ContainsKey ( "SeparateUploadFilePath" ) ? AddTrailingSlash ( configuration [ "SeparateUploadFilePath" ] . ToString ( ) ) : string . Empty ; } }
34+ public static string DefaultLinuxPermissionsOnStoreCreation { get { return configuration . ContainsKey ( "DefaultLinuxPermissionsOnStoreCreation" ) ? configuration [ "DefaultLinuxPermissionsOnStoreCreation" ] . ToString ( ) : DEFAULT_LINUX_PERMISSION_SETTING ; } }
35+ public static string DefaultOwnerOnStoreCreation { get { return configuration . ContainsKey ( "DefaultOwnerOnStoreCreation" ) ? configuration [ "DefaultOwnerOnStoreCreation" ] . ToString ( ) : DEFAULT_OWNER_SETTING ; } }
36+ public static string DefaultSudoImpersonatedUser { get { return configuration . ContainsKey ( "DefaultSudoImpersonatedUser" ) ? configuration [ "DefaultSudoImpersonatedUser" ] . ToString ( ) : DEFAULT_SUDO_IMPERSONATION_SETTING ; } }
37+ public static string TempFilePathForODKG { get { return configuration . ContainsKey ( "TempFilePathForODKG" ) ? configuration [ "TempFilePathForODKG" ] . ToString ( ) : string . Empty ; } }
38+ public static bool UseShellCommands { get { return configuration . ContainsKey ( "UseShellCommands" ) ? configuration [ "UseShellCommands" ] ? . ToString ( ) . ToUpper ( ) == "Y" : true ; } }
3839 public static int SSHPort
3940 {
4041 get
4142 {
42- if ( configuration . ContainsKey ( "SSHPort" ) && ! string . IsNullOrEmpty ( configuration [ "SSHPort" ] ) )
43+ if ( configuration . ContainsKey ( "SSHPort" ) && ! string . IsNullOrEmpty ( configuration [ "SSHPort" ] ? . ToString ( ) ) )
4344 {
4445 int sshPort ;
45- if ( int . TryParse ( configuration [ "SSHPort" ] , out sshPort ) )
46+ if ( int . TryParse ( configuration [ "SSHPort" ] ? . ToString ( ) , out sshPort ) )
4647 return sshPort ;
4748 else
4849 throw new RemoteFileException ( $ "Invalid optional config.json SSHPort value of { configuration [ "SSHPort" ] } . If present, this must be an integer value.") ;
@@ -53,13 +54,27 @@ public static int SSHPort
5354 }
5455 }
5556 }
57+ public static List < PostJobCommand > PostJobCommands
58+ {
59+ get
60+ {
61+ if ( configuration . ContainsKey ( "PostJobCommands" ) && configuration [ "PostJobCommands" ] != null )
62+ {
63+ return JsonConvert . DeserializeObject < List < PostJobCommand > > ( configuration [ "PostJobCommands" ] ? . ToString ( ) ) ;
64+ }
65+ else
66+ {
67+ return new List < PostJobCommand > ( ) ;
68+ }
69+ }
70+ }
5671
5772 static ApplicationSettings ( )
5873 {
5974 ILogger logger = LogHandler . GetClassLogger < ApplicationSettings > ( ) ;
6075 logger . MethodEntry ( LogLevel . Debug ) ;
6176
62- configuration = new Dictionary < string , string > ( ) ;
77+ configuration = new Dictionary < string , object > ( ) ;
6378 string configLocation = $ "{ Path . GetDirectoryName ( Assembly . GetExecutingAssembly ( ) . Location ) } { Path . DirectorySeparatorChar } config.json";
6479 string configContents = string . Empty ;
6580
@@ -81,11 +96,18 @@ static ApplicationSettings()
8196 return ;
8297 }
8398
84- configuration = JsonConvert . DeserializeObject < Dictionary < string , string > > ( configContents ) ;
99+ try
100+ {
101+ configuration = JsonConvert . DeserializeObject < Dictionary < string , object > > ( configContents ) ;
102+ }
103+ catch ( Exception ex )
104+ {
105+ throw new RemoteFileException ( RemoteFileException . FlattenExceptionMessages ( ex , "Error attempting to serialize config.json file. Please review your config.json file for proper formatting." ) ) ;
106+ }
85107 ValidateConfiguration ( logger ) ;
86108
87109 logger . LogDebug ( "Configuration Settings:" ) ;
88- foreach ( KeyValuePair < string , string > keyValue in configuration )
110+ foreach ( KeyValuePair < string , object > keyValue in configuration )
89111 {
90112 logger . LogDebug ( $ " { keyValue . Key } : { keyValue . Value } ") ;
91113 }
@@ -95,11 +117,11 @@ static ApplicationSettings()
95117
96118 private static void ValidateConfiguration ( ILogger logger )
97119 {
98- if ( ! configuration . ContainsKey ( "UseSudo" ) || ( configuration [ "UseSudo" ] . ToUpper ( ) != "Y" && configuration [ "UseSudo" ] . ToUpper ( ) != "N" ) )
120+ if ( ! configuration . ContainsKey ( "UseSudo" ) || ( configuration [ "UseSudo" ] ? . ToString ( ) . ToUpper ( ) != "Y" && configuration [ "UseSudo" ] ? . ToString ( ) . ToUpper ( ) != "N" ) )
99121 logger . LogDebug ( $ "Missing or invalid configuration parameter - UseSudo. Will set to default value of 'False'") ;
100- if ( ! configuration . ContainsKey ( "CreateStoreIfMissing" ) || ( configuration [ "CreateStoreIfMissing" ] . ToUpper ( ) != "Y" && configuration [ "CreateStoreIfMissing" ] . ToUpper ( ) != "N" ) )
122+ if ( ! configuration . ContainsKey ( "CreateStoreIfMissing" ) || ( configuration [ "CreateStoreIfMissing" ] ? . ToString ( ) . ToUpper ( ) != "Y" && configuration [ "CreateStoreIfMissing" ] ? . ToString ( ) . ToUpper ( ) != "N" ) )
101123 logger . LogDebug ( $ "Missing or invalid configuration parameter - CreateStoreIfMissing. Will set to default value of 'False'") ;
102- if ( ! configuration . ContainsKey ( "UseNegotiate" ) || ( configuration [ "UseNegotiate" ] . ToUpper ( ) != "Y" && configuration [ "UseNegotiate" ] . ToUpper ( ) != "N" ) )
124+ if ( ! configuration . ContainsKey ( "UseNegotiate" ) || ( configuration [ "UseNegotiate" ] ? . ToString ( ) . ToUpper ( ) != "Y" && configuration [ "UseNegotiate" ] ? . ToString ( ) . ToUpper ( ) != "N" ) )
103125 logger . LogDebug ( $ "Missing or invalid configuration parameter - UseNegotiate. Will set to default value of 'False'") ;
104126 if ( ! configuration . ContainsKey ( "SeparateUploadFilePath" ) )
105127 logger . LogDebug ( $ "Missing configuration parameter - SeparateUploadFilePath. Will set to default value of ''") ;
@@ -113,5 +135,12 @@ private static string AddTrailingSlash(string path)
113135 {
114136 return string . IsNullOrEmpty ( path ) ? path : path . Substring ( path . Length - 1 , 1 ) == @"/" ? path : path += @"/" ;
115137 }
138+
139+ public class PostJobCommand
140+ {
141+ public string Name { get ; set ; }
142+ public string Environment { get ; set ; }
143+ public string Command { get ; set ; }
144+ }
116145 }
117146}
0 commit comments