/// <summary> /// Integrates Lockbox as a configuration source for the application settings. /// Specified configuration will be fetched via HTTP request and added to the builder pipeline. /// </summary> /// <param name="builder">Instance of IConfigurationBuilder</param> /// <param name="encryptionKey">Encryption key used for encrypting values. If omitted then "LOCKBOX_ENCRYPTION_KEY" environment variable will be used instead.</param> /// <param name="apiUrl">URL of the Lockbox API. If omitted then "LOCKBOX_API_URL" environment variable will be used instead.</param> /// <param name="apiKey">API key that will be used for authenticating HTTP requests to the Lockbox API. If omitted then "LOCKBOX_API_KEY" environment variable will be used instead.</param> /// <param name="boxName">Name of the box that contains specified entry defined below. If omitted then "LOCKBOX_BOX_NAME" environment variable will be used instead.</param> /// <param name="entryKey">Name of the entry within a box that will be used for getting configuration values. If omitted then "LOCKBOX_ENTRY_KEY" environment variable will be used instead.</param> /// <returns></returns> /// <exception cref="ArgumentException"></exception> public static IConfigurationBuilder AddLockbox(this IConfigurationBuilder builder, string encryptionKey = null, string apiUrl = null, string apiKey = null, string boxName = null, string entryKey = null) { encryptionKey = GetParameterOrFail(encryptionKey, EncryptionKeyEnvironmentVariable, "encryption key"); apiUrl = GetParameterOrFail(apiUrl, ApiUrlEnvironmentVariable, "API key"); apiKey = GetParameterOrFail(apiKey, ApiKeyEnvironmentVariable, "API url"); boxName = GetParameterOrFail(boxName, BoxNameEnvironmentVariable, "box name"); entryKey = GetParameterOrFail(entryKey, EntryKeyEnvironmentVariable, "entry key"); var lockboxClient = new LockboxEntryClient(encryptionKey, apiUrl, apiKey); var entryDictionary = lockboxClient.GetEntryAsync(boxName, entryKey).Result; if (entryDictionary == null) { throw new ArgumentException($"Lockbox entry has not been found for key: '{entryKey}'.", nameof(entryKey)); } var data = JsonParser.Parse((JObject)entryDictionary); var source = new MemoryConfigurationSource { InitialData = data }; builder.Add(source); return(builder); }
public string Load() { _encryptionKey = GetParameterOrFail(_encryptionKey, EncryptionKeyEnvironmentVariable, "encryption key"); _apiUrl = GetParameterOrFail(_apiUrl, ApiUrlEnvironmentVariable, "API key"); _apiKey = GetParameterOrFail(_apiKey, ApiKeyEnvironmentVariable, "API url"); _boxName = GetParameterOrFail(_boxName, BoxNameEnvironmentVariable, "box name"); _entryKey = GetParameterOrFail(_entryKey, EntryKeyEnvironmentVariable, "entry key"); var lockboxClient = new LockboxEntryClient(_encryptionKey, _apiUrl, _apiKey); var task = lockboxClient.GetEntryAsync <MediumSettings>(_boxName, _entryKey); task.Wait(); var entry = task.Result; if (entry == null) { throw new ArgumentException($"Lockbox entry has not been found for key: '{_entryKey}'.", nameof(_entryKey)); } return(JsonConvert.SerializeObject(entry)); }