|
| 1 | + |
| 2 | +namespace TestPlotly |
| 3 | +{ |
| 4 | + |
| 5 | + |
| 6 | + public class SecretManager |
| 7 | + { |
| 8 | + |
| 9 | + // TestPlotly.SecretManager.GetSecret<string>("DefaultDbPassword") |
| 10 | + // TestPlotly.SecretManager.GetSecret<string>("GoogleGeoCodingApiKey") |
| 11 | + public static T GetSecret<T>(string secretName) |
| 12 | + { |
| 13 | + string asmName = typeof(SecretManager).Assembly.FullName; |
| 14 | + |
| 15 | + int ipos = asmName.IndexOf(','); |
| 16 | + if (ipos != -1) |
| 17 | + { |
| 18 | + asmName = asmName.Substring(0, ipos); |
| 19 | + } |
| 20 | + |
| 21 | + return GetSecret<T>(secretName, asmName); |
| 22 | + } // End Function GetSecret |
| 23 | + |
| 24 | + |
| 25 | + public static T GetSecret<T>(string secretName, string asmName) |
| 26 | + { |
| 27 | + T obj = default(T); |
| 28 | + |
| 29 | + if (System.Environment.OSVersion.Platform == System.PlatformID.Unix) |
| 30 | + { |
| 31 | + obj = SecretManagerHelper.GetEtcKey<T>("/etc/COR/" + asmName, secretName); |
| 32 | + if(obj == null) |
| 33 | + obj = SecretManagerHelper.GetEtcKey<T>(@"/etc/COR/All", secretName); |
| 34 | + } |
| 35 | + else |
| 36 | + { |
| 37 | + obj = SecretManagerHelper.GetRegistryKey<T>(@"Software\COR\" + asmName, secretName); |
| 38 | + if(obj == null) |
| 39 | + obj = SecretManagerHelper.GetRegistryKey<T>(@"Software\COR\All", secretName); |
| 40 | + } |
| 41 | + |
| 42 | + return obj; |
| 43 | + } // End Function GetSecret |
| 44 | + |
| 45 | + |
| 46 | + } // End Class SecretManager |
| 47 | + |
| 48 | + |
| 49 | + public class SecretManagerHelper |
| 50 | + { |
| 51 | + |
| 52 | + |
| 53 | + public static T GetRegistryKey<T>(string key, string value) |
| 54 | + { |
| 55 | + object obj = GetRegistryKey(key, value); |
| 56 | + return ObjectToGeneric<T>(obj); |
| 57 | + } // End Function GetRegistryKey |
| 58 | + |
| 59 | + |
| 60 | + public static T GetEtcKey<T>(string path, string value) |
| 61 | + { |
| 62 | + string obj = null; |
| 63 | + |
| 64 | + string p = System.IO.Path.Combine(path, value); |
| 65 | + if(System.IO.File.Exists(p)) |
| 66 | + obj = System.IO.File.ReadAllText(p, System.Text.Encoding.Default); |
| 67 | + |
| 68 | + if(obj == null) |
| 69 | + return ObjectToGeneric<T>((object)obj); |
| 70 | + |
| 71 | + // || obj.EndsWith(" ") || obj.EndsWith("\t") |
| 72 | + while ( obj.EndsWith("\r") || obj.EndsWith("\n") ) |
| 73 | + obj = obj.Substring(0, obj.Length - 1); |
| 74 | + |
| 75 | + return ObjectToGeneric<T>((object)obj); |
| 76 | + } // End Function GetRegistryKey |
| 77 | + |
| 78 | + |
| 79 | + private static object GetRegistryKey(string key, string value) |
| 80 | + { |
| 81 | + object objReturnValue = null; |
| 82 | + // HKEY_CURRENT_USER |
| 83 | + |
| 84 | + //using (Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine |
| 85 | + using (Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.CurrentUser |
| 86 | + .OpenSubKey(key)) |
| 87 | + { |
| 88 | + if (regKey != null) |
| 89 | + { |
| 90 | + objReturnValue = regKey.GetValue(value); |
| 91 | + } // End if (regKey != null) |
| 92 | + |
| 93 | + } // End Using regKey |
| 94 | + |
| 95 | + return objReturnValue; |
| 96 | + } // End Function GetRegistryKey |
| 97 | + |
| 98 | + |
| 99 | + private static T InlineTypeAssignHelper<T>(object UTO) |
| 100 | + { |
| 101 | + if (UTO == null) |
| 102 | + { |
| 103 | + T NullSubstitute = default(T); |
| 104 | + return NullSubstitute; |
| 105 | + } // End if (UTO == null) |
| 106 | + |
| 107 | + return (T)UTO; |
| 108 | + } // End Template InlineTypeAssignHelper |
| 109 | + |
| 110 | + |
| 111 | + private static T ObjectToGeneric<T>(object objReturnValue) |
| 112 | + { |
| 113 | + string strReturnValue = null; |
| 114 | + System.Type tReturnType = typeof(T); |
| 115 | + |
| 116 | + if (!object.ReferenceEquals(tReturnType, typeof(System.Byte[]))) |
| 117 | + { |
| 118 | + if(objReturnValue != null) |
| 119 | + strReturnValue = System.Convert.ToString(objReturnValue); |
| 120 | + } // End if (!object.ReferenceEquals(tReturnType, typeof(System.Byte[]))) |
| 121 | + |
| 122 | + try |
| 123 | + { |
| 124 | + |
| 125 | + if (object.ReferenceEquals(tReturnType, typeof(object))) |
| 126 | + { |
| 127 | + return InlineTypeAssignHelper<T>(objReturnValue); |
| 128 | + } |
| 129 | + else if (object.ReferenceEquals(tReturnType, typeof(string))) |
| 130 | + { |
| 131 | + return InlineTypeAssignHelper<T>(strReturnValue); |
| 132 | + } // End if string |
| 133 | + else if (object.ReferenceEquals(tReturnType, typeof(bool))) |
| 134 | + { |
| 135 | + bool bReturnValue = false; |
| 136 | + bool bSuccess = bool.TryParse(strReturnValue, out bReturnValue); |
| 137 | + |
| 138 | + if (bSuccess) |
| 139 | + return InlineTypeAssignHelper<T>(bReturnValue); |
| 140 | + |
| 141 | + if (strReturnValue == "0") |
| 142 | + return InlineTypeAssignHelper<T>(false); |
| 143 | + |
| 144 | + return InlineTypeAssignHelper<T>(true); |
| 145 | + } // End if bool |
| 146 | + else if (object.ReferenceEquals(tReturnType, typeof(int))) |
| 147 | + { |
| 148 | + int iReturnValue = int.Parse(strReturnValue); |
| 149 | + return InlineTypeAssignHelper<T>(iReturnValue); |
| 150 | + } // End if int |
| 151 | + else if (object.ReferenceEquals(tReturnType, typeof(uint))) |
| 152 | + { |
| 153 | + uint uiReturnValue = uint.Parse(strReturnValue); |
| 154 | + return InlineTypeAssignHelper<T>(uiReturnValue); |
| 155 | + } // End if uint |
| 156 | + else if (object.ReferenceEquals(tReturnType, typeof(long))) |
| 157 | + { |
| 158 | + long lngReturnValue = long.Parse(strReturnValue); |
| 159 | + return InlineTypeAssignHelper<T>(lngReturnValue); |
| 160 | + } // End if long |
| 161 | + else if (object.ReferenceEquals(tReturnType, typeof(ulong))) |
| 162 | + { |
| 163 | + ulong ulngReturnValue = ulong.Parse(strReturnValue); |
| 164 | + return InlineTypeAssignHelper<T>(ulngReturnValue); |
| 165 | + } // End if ulong |
| 166 | + else if (object.ReferenceEquals(tReturnType, typeof(float))) |
| 167 | + { |
| 168 | + float fltReturnValue = float.Parse(strReturnValue); |
| 169 | + return InlineTypeAssignHelper<T>(fltReturnValue); |
| 170 | + } |
| 171 | + else if (object.ReferenceEquals(tReturnType, typeof(double))) |
| 172 | + { |
| 173 | + double dblReturnValue = double.Parse(strReturnValue); |
| 174 | + return InlineTypeAssignHelper<T>(dblReturnValue); |
| 175 | + } |
| 176 | + else if (object.ReferenceEquals(tReturnType, typeof(System.Net.IPAddress))) |
| 177 | + { |
| 178 | + System.Net.IPAddress ipaAddress = null; |
| 179 | + |
| 180 | + if (string.IsNullOrEmpty(strReturnValue)) |
| 181 | + return InlineTypeAssignHelper<T>(ipaAddress); |
| 182 | + |
| 183 | + ipaAddress = System.Net.IPAddress.Parse(strReturnValue); |
| 184 | + return InlineTypeAssignHelper<T>(ipaAddress); |
| 185 | + } // End if IPAddress |
| 186 | + else if (object.ReferenceEquals(tReturnType, typeof(System.Byte[]))) |
| 187 | + { |
| 188 | + if (objReturnValue == System.DBNull.Value) |
| 189 | + return InlineTypeAssignHelper<T>(null); |
| 190 | + |
| 191 | + return InlineTypeAssignHelper<T>(objReturnValue); |
| 192 | + } |
| 193 | + else if (object.ReferenceEquals(tReturnType, typeof(System.Guid))) |
| 194 | + { |
| 195 | + if (string.IsNullOrEmpty(strReturnValue)) return InlineTypeAssignHelper<T>(null); |
| 196 | + |
| 197 | + return InlineTypeAssignHelper<T>(new System.Guid(strReturnValue)); |
| 198 | + } // End if GUID |
| 199 | + else if (object.ReferenceEquals(tReturnType, typeof(System.DateTime))) |
| 200 | + { |
| 201 | + System.DateTime bReturnValue = System.DateTime.Now; |
| 202 | + bool bSuccess = System.DateTime.TryParse(strReturnValue, out bReturnValue); |
| 203 | + |
| 204 | + if (bSuccess) |
| 205 | + return InlineTypeAssignHelper<T>(bReturnValue); |
| 206 | + |
| 207 | + if (strReturnValue == "0") |
| 208 | + return InlineTypeAssignHelper<T>(false); |
| 209 | + |
| 210 | + return InlineTypeAssignHelper<T>(true); |
| 211 | + } // End if datetime |
| 212 | + else // No datatype matches |
| 213 | + { |
| 214 | + throw new System.NotImplementedException("ExecuteScalar<T>: This type is not yet defined."); |
| 215 | + } // End else of if tReturnType = datatype |
| 216 | + |
| 217 | + } // End Try |
| 218 | + catch (System.Exception ex) |
| 219 | + { |
| 220 | + throw; |
| 221 | + } // End Catch |
| 222 | + |
| 223 | + return InlineTypeAssignHelper<T>(null); |
| 224 | + } // End Function ObjectToGeneric |
| 225 | + |
| 226 | + |
| 227 | + } // End Class SecretManager |
| 228 | + |
| 229 | + |
| 230 | +} // End Namespace |
0 commit comments