Created
May 24, 2018 21:42
-
-
Save MorbosVermin/5a846f3cd8d96ac9fc9f1ae45097df73 to your computer and use it in GitHub Desktop.
.Net C# Class for getting configuration information from Vault (vaultproject.io)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using log4net; | |
using System; | |
using System.Collections.Generic; | |
using System.Security; | |
using VaultSharp; | |
using VaultSharp.Backends.Authentication.Models; | |
using VaultSharp.Backends.Authentication.Models.Token; | |
using VaultSharp.Backends.Authentication.Models.UsernamePassword; | |
namespace MyApp | |
{ | |
/// <summary> | |
/// Example configuration management class using VaultSharp. | |
/// </summary> | |
public class VaultConfigurationManager | |
{ | |
static readonly ILog Log = LogManager.GetLogger(typeof(VaultConfigurationManager)); | |
string BasePath { get; set; } | |
IVaultClient Client { get; set; } | |
/// <summary> | |
/// Connection strings from {BasePath}/connection_strings | |
/// </summary> | |
public List<string> ConnectionStrings { get; private set; } | |
/// <summary> | |
/// Application Settings located at {BasePath}/appsettings | |
/// </summary> | |
public Dictionary<string, dynamic> AppSettings { get; private set; } | |
/// <summary> | |
/// Constructor | |
/// </summary> | |
/// <param name="vaultWithPort">URL of Vault instance</param> | |
/// <param name="authInfo">Authentication to use with Vault.</param> | |
/// <param name="basePath">Base path for application settings and connection strings.</param> | |
public VaultConfigurationManager( | |
Uri vaultWithPort, | |
IAuthenticationInfo authInfo, | |
string basePath = "secret/myapp") | |
{ | |
Client = VaultClientFactory.CreateVaultClient(vaultWithPort, authInfo); | |
BasePath = basePath; | |
AppSettings = new Dictionary<string, dynamic>(); | |
ConnectionStrings = new List<string>(); | |
Initialize(); | |
} | |
/// <summary> | |
/// Username and Password Authentication | |
/// </summary> | |
/// <param name="vaultWithPort"></param> | |
/// <param name="username"></param> | |
/// <param name="password"></param> | |
/// <param name="basePath"></param> | |
public VaultConfigurationManager( | |
Uri vaultWithPort, | |
string username, | |
string password, | |
string basePath = "secret/myapp") | |
: this(vaultWithPort, new UsernamePasswordAuthenticationInfo(username, password), basePath) | |
{ | |
} | |
/// <summary> | |
/// Token Authentication | |
/// </summary> | |
/// <param name="vaultWithPort"></param> | |
/// <param name="token"></param> | |
/// <param name="basePath"></param> | |
public VaultConfigurationManager( | |
Uri vaultWithPort, | |
string token, | |
string basePath = "secret/myapp") | |
: this(vaultWithPort, new TokenAuthenticationInfo(token), basePath) | |
{ | |
} | |
/// <summary> | |
/// Initializes AppSettings and ConnectionStrings properties from Vault. | |
/// </summary> | |
private void Initialize() | |
{ | |
try | |
{ | |
Log.Debug("Initializing AppSettings from Vault..."); | |
var appSettings = | |
Client.ReadSecretAsync(string.Format("{0}/appsettings", BasePath)) | |
.GetAwaiter() | |
.GetResult(); | |
foreach(string name in appSettings.Data.Keys) | |
{ | |
AppSettings[name] = | |
name.Equals("password", StringComparison.CurrentCultureIgnoreCase) ? | |
GetSecureString(appSettings.Data[name].ToString().ToCharArray()) : | |
appSettings.Data[name]; | |
} | |
Log.Debug("Initializing ConnectionStrings from Vault..."); | |
var conStrings = | |
Client.ReadSecretAsync(string.Format("{0}/connection_strings", BasePath)) | |
.GetAwaiter() | |
.GetResult(); | |
foreach(string name in conStrings.Data.Keys) | |
{ | |
ConnectionStrings.Add(conStrings.Data[name].ToString()); | |
} | |
}catch(Exception e) | |
{ | |
Log.Error(string.Format("Unable to get application settings and/or connection strings: {0}", e.Message), e); | |
} | |
} | |
/// <summary> | |
/// Converts the given char[] to SecureString for more secure memory storage. Then | |
/// this method will "clear" the char[] given. | |
/// </summary> | |
/// <param name="value"></param> | |
/// <returns></returns> | |
private SecureString GetSecureString(char[] value) | |
{ | |
SecureString s = new SecureString(); | |
foreach (char v in value) | |
s.AppendChar(v); | |
s.MakeReadOnly(); | |
Array.Fill(value, '0'); | |
return s; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment