| | 1 | | using System.Reflection; |
| | 2 | | using System.Security; |
| | 3 | |
|
| | 4 | | namespace Configuration.Helper; |
| | 5 | |
|
| | 6 | | /// <summary>Base helper class to access configuration file information.</summary> |
| | 7 | | public abstract class ConfigFileHelper |
| | 8 | | { |
| | 9 | | #region Properties and Constants |
| | 10 | |
|
| | 11 | | /// <summary>Gets or sets the configuration file name.</summary> |
| | 12 | | protected string ConfigFile |
| | 13 | | { |
| 9 | 14 | | get { return _configFile; } |
| 42 | 15 | | set { Initialize( ref value ); } |
| | 16 | | } |
| | 17 | |
|
| | 18 | | /// <summary>Gets the collection of program arguments.</summary> |
| 3 | 19 | | public IDictionary<string, string> Arguments { get; } |
| | 20 | |
|
| | 21 | | /// <summary>Gets the current settings.</summary> |
| 21 | 22 | | public ISettingsStore? Settings { get; private set; } |
| | 23 | |
|
| | 24 | | /// <summary>Configuration file extension (suffix) including the period.</summary> |
| | 25 | | public const string cExtension = ".config"; |
| | 26 | |
|
| | 27 | | /// <summary>JSON configuration file extension (suffix) including the period.</summary> |
| | 28 | | public const string cJsonExtension = ".json"; |
| | 29 | |
|
| | 30 | | #endregion |
| | 31 | |
|
| | 32 | | #region Instance Variables |
| | 33 | |
|
| | 34 | | /// <summary>Configuration file information.</summary> |
| 14 | 35 | | private string _configFile = string.Empty; |
| | 36 | |
|
| | 37 | | #endregion |
| | 38 | |
|
| | 39 | | #region Constructor and Initialization |
| | 40 | |
|
| | 41 | | /// <summary>Initializes a new instance of the class using a configuration file name.</summary> |
| | 42 | | /// <param name="configFile">Configuration filename.</param> |
| 14 | 43 | | protected ConfigFileHelper( string configFile ) |
| 14 | 44 | | { |
| 14 | 45 | | ConfigFile = configFile; // This triggers the initialization |
| 14 | 46 | | Arguments = new Dictionary<string, string>( StringComparer.OrdinalIgnoreCase ); |
| 14 | 47 | | } |
| | 48 | |
|
| | 49 | | /// <summary>Initializes the configuration file settings.</summary> |
| | 50 | | /// <param name="configFile">Configuration file name.</param> |
| | 51 | | protected virtual void Initialize( ref string configFile ) |
| 14 | 52 | | { |
| 14 | 53 | | _configFile = IOHelper.GetFullPath( configFile ); |
| 14 | 54 | | Settings = GetConfiguration( _configFile ); |
| 14 | 55 | | } |
| | 56 | |
|
| | 57 | | #endregion |
| | 58 | |
|
| | 59 | | #region Protected Methods |
| | 60 | |
|
| | 61 | | /// <summary>Returns the setting value in the configuration file for a given key.</summary> |
| | 62 | | /// <param name="settingKey">Key of the setting.</param> |
| | 63 | | /// <returns><see langword="null"/> is returned if the setting is not found.</returns> |
| | 64 | | protected string? GetSetting( string settingKey ) |
| 6 | 65 | | { |
| | 66 | | // Check the required parameter is supplied |
| 6 | 67 | | if( string.IsNullOrWhiteSpace( settingKey ) ) |
| 1 | 68 | | { |
| 1 | 69 | | return string.Empty; |
| | 70 | | } |
| | 71 | |
|
| | 72 | | // Return the setting value |
| 5 | 73 | | settingKey = settingKey.Trim(); |
| 5 | 74 | | return Settings?.AppSettings.GetSetting( settingKey ); |
| 6 | 75 | | } |
| | 76 | |
|
| | 77 | | /// <summary>Returns the setting value in the configuration file for a given key and prefix.</summary> |
| | 78 | | /// <param name="settingKey">Key of the setting.</param> |
| | 79 | | /// <param name="prefix">Prefix for the setting.</param> |
| | 80 | | /// <returns><see langword="null"/> is returned if the setting is not found.</returns> |
| | 81 | | protected string? GetSetting( string settingKey, string prefix ) |
| 4 | 82 | | { |
| | 83 | | // Check the required parameter is supplied |
| 4 | 84 | | if( string.IsNullOrEmpty( settingKey ) ) |
| 1 | 85 | | { |
| 1 | 86 | | return string.Empty; |
| | 87 | | } |
| | 88 | |
|
| | 89 | | // Return the setting value |
| 3 | 90 | | string? retValue = null; |
| 3 | 91 | | if( !string.IsNullOrWhiteSpace( prefix ) ) |
| 2 | 92 | | { |
| 2 | 93 | | retValue = GetSetting( prefix.Trim() + settingKey ); |
| 2 | 94 | | } |
| | 95 | |
|
| 3 | 96 | | return retValue ?? GetSetting( settingKey ); |
| 4 | 97 | | } |
| | 98 | |
|
| | 99 | | #endregion |
| | 100 | |
|
| | 101 | | #region Public Methods |
| | 102 | |
|
| | 103 | | /// <summary>Returns the argument value in the configuration file for a given key.</summary> |
| | 104 | | /// <param name="argumentKey">Key of the argument.</param> |
| | 105 | | /// <returns>An empty string is returned if the argument key is not found.</returns> |
| | 106 | | public string GetArgument( string argumentKey ) |
| 3 | 107 | | { |
| | 108 | | // Check the required parameter is supplied |
| 3 | 109 | | if( string.IsNullOrWhiteSpace( argumentKey ) ) |
| 1 | 110 | | { |
| 1 | 111 | | return string.Empty; |
| | 112 | | } |
| | 113 | |
|
| | 114 | | // Return the setting value |
| 2 | 115 | | argumentKey = argumentKey.Trim(); |
| 2 | 116 | | return Arguments.TryGetValue( argumentKey, out string? value ) ? value : string.Empty; |
| 3 | 117 | | } |
| | 118 | |
|
| | 119 | | #endregion |
| | 120 | |
|
| | 121 | | #region Public Static Functions |
| | 122 | |
|
| | 123 | | /// <summary>Gets a Setting Store object.</summary> |
| | 124 | | /// <param name="configFile">Configuration file name.</param> |
| | 125 | | /// <returns>ISettingsStore object.</returns> |
| | 126 | | /// <exception cref="ArgumentNullException">Thrown if the parameter is <see langword="null"/>.</exception> |
| | 127 | | public static ISettingsStore GetConfiguration( string configFile ) |
| 31 | 128 | | { |
| | 129 | | // Check the required parameter is supplied |
| 31 | 130 | | ArgumentNullException.ThrowIfNull( configFile ); |
| | 131 | |
|
| 31 | 132 | | bool exists = IOHelper.DoesFileExist( configFile ); |
| 31 | 133 | | if( !exists ) |
| 14 | 134 | | { |
| | 135 | | // No path so try using current location |
| 14 | 136 | | string path = IOHelper.Combine( Assembly.GetExecutingAssembly().Location, IOHelper.GetFileName( configFile ) ); |
| 14 | 137 | | if( configFile != path ) |
| 14 | 138 | | { |
| 14 | 139 | | configFile = IOHelper.GetFullPath( configFile ); |
| 14 | 140 | | } |
| 14 | 141 | | } |
| | 142 | |
|
| 31 | 143 | | return FileSettingsStore.Create( configFile ); |
| 31 | 144 | | } |
| | 145 | |
|
| | 146 | | /// <summary>Converts a string to a secure string.</summary> |
| | 147 | | /// <param name="strValue">Value to secure - this will be empty on return.</param> |
| | 148 | | /// <returns>If the string value is <see langword="null"/>, empty, or whitespace, then the |
| | 149 | | /// return value will have a length of zero.</returns> |
| | 150 | | public static SecureString ConvertToSecureString( ref string strValue ) |
| 3 | 151 | | { |
| | 152 | | // Set the return value |
| 3 | 153 | | SecureString retValue = new(); |
| | 154 | |
|
| | 155 | | // Check the required parameter is supplied |
| 3 | 156 | | if( string.IsNullOrWhiteSpace( strValue ) || strValue.Length == 0 ) |
| 1 | 157 | | { |
| 1 | 158 | | retValue.MakeReadOnly(); |
| 1 | 159 | | return retValue; |
| | 160 | | } |
| | 161 | |
|
| | 162 | | // Populate the secure string and clear the parameter value |
| 2 | 163 | | Array.ForEach( strValue.ToCharArray(), retValue.AppendChar ); |
| 2 | 164 | | strValue = string.Empty; |
| | 165 | |
|
| | 166 | | // Return the secure string as read-only |
| 2 | 167 | | retValue.MakeReadOnly(); |
| 2 | 168 | | return retValue; |
| 3 | 169 | | } |
| | 170 | |
|
| | 171 | | /// <summary>Gets a secure string value of a configuration file key.</summary> |
| | 172 | | /// <param name="configFile">Full path and name of the configuration file.</param> |
| | 173 | | /// <param name="sectionName">Name of the section containing the key.</param> |
| | 174 | | /// <param name="key">Key of the value to return.</param> |
| | 175 | | /// <returns>If the key cannot be retrieved then the return value will have a length of zero.</returns> |
| | 176 | | /// <exception cref="ArgumentException">Thrown when the <paramref name="sectionName"/> parameter is empty.</exception> |
| | 177 | | public static SecureString GetSecureSetting( string configFile, string sectionName, string key ) |
| 2 | 178 | | { |
| 2 | 179 | | string? str = null; |
| 2 | 180 | | if( IOHelper.DoesFileExist( configFile ) ) |
| 2 | 181 | | { |
| 2 | 182 | | ISettingsStore config = GetConfiguration( configFile ); |
| 2 | 183 | | ISettingsSection section = config.GetSection( sectionName ); |
| 2 | 184 | | str = section.Settings.Count > 0 ? section.GetSetting( key ) : string.Empty; |
| 2 | 185 | | } |
| | 186 | |
|
| 2 | 187 | | return !string.IsNullOrEmpty( str ) ? ConvertToSecureString( ref str ) : new SecureString(); |
| 2 | 188 | | } |
| | 189 | |
|
| | 190 | | #endregion |
| | 191 | | } |