private void PasswordChangeHandler(string eventName, string eventBody) { var eventInfo = JsonConvert.DeserializeObject <MonitoredPassword>(eventBody); Log.Information("Password changed for {MonitoredPassword}", eventInfo); // NOTE: eventInfo won't have the API key field filled out because that isn't in the eventBody Json // You can look up in the list of _monitoredPasswords to find the API key try { var apiKey = _monitoredPasswords.Single(mp => mp.AssetName == eventInfo.AssetName && mp.AccountName == eventInfo.AccountName).ApiKey; using (var password = _a2AContext.RetrievePassword(apiKey)) { // TODO: Add useful code here to do something with the fetched password // Also, note that the password you get back is a SecureString. In order to turn it back into a regular string // you can use the provided convenience function: // password.ToInsecureString() } } catch (Exception) { Log.Information("Password not in monitored list for handled event {MonitoredPassword}", eventInfo); } }
private void PasswordChangeHandler(string eventName, string eventBody) { var eventInfo = JsonHelper.DeserializeObject <EventInfo>(eventBody); try { var apiKeys = _retrievableAccounts.Where(mp => mp.AssetName == eventInfo.AssetName && mp.AccountName == eventInfo.AccountName).ToArray(); // Make sure that we have at least one plugin mapped to the account if (!apiKeys.Any()) { _logger.Error("No API keys were found by the password change handler."); } // Make sure that if there are more than one mapped plugin, all of the API key match for the same account var apiKey = apiKeys.FirstOrDefault()?.ApiKey; if (!apiKeys.All(x => x.ApiKey.Equals(apiKey))) { _logger.Error("Mismatched API keys for the same account were found by the password change handler."); } // At this point we should have one API key to retrieve. using (var password = _a2AContext.RetrievePassword(apiKey.ToSecureString())) { var accounts = _configDb.GetAccountMappings().ToList(); var selectedAccounts = accounts.Where(a => a.ApiKey.Equals(apiKey)); foreach (var account in selectedAccounts) { try { _logger.Information($"Sending password for account {account} to {account.VaultName}."); if (!_pluginManager.SendPassword(account.VaultName, account.AssetName, account.AccountName, password)) { _logger.Error( $"Unable to set the password for {account.AccountName} to {account.VaultName}."); } } catch (Exception ex) { _logger.Error( $"Unable to set the password for {account.AccountName} to {account.VaultName}: {ex.Message}."); } } } } catch (Exception ex) { _logger.Error($"Password change handler failed: {ex.Message}."); } }
private void PasswordChangeHandler(string eventName, string eventBody) { var configuration = _configurationRepository.GetConfiguration(); if (configuration == null || _retrievableAccounts == null) { _logger.Error("No configuration was found. DevOps service must be configured first or no retrievable accounts found."); return; } var eventInfo = JsonHelper.DeserializeObject <EventInfo>(eventBody); try { var apiKey = _retrievableAccounts.Single(mp => mp.SystemName == eventInfo.AssetName && mp.AccountName == eventInfo.AccountName).ApiKey; using (var password = _a2aContext.RetrievePassword(apiKey.ToSecureString())) { var accounts = configuration.AccountMapping.ToList(); var selectedAccounts = accounts.Where(a => a.ApiKey.Equals(apiKey)); foreach (var account in selectedAccounts) { try { _logger.Information($"Sending password for account {account.AccountName} to {account.VaultName}."); if (!_pluginManager.SendPassword(account.VaultName, account.AccountName, password)) { _logger.Error( $"Unable to set the password for {account.AccountName} to {account.VaultName}."); } } catch (Exception ex) { _logger.Error( $"Unable to set the password for {account.AccountName} to {account.VaultName}: {ex.Message}."); } } } } catch (Exception ex) { _logger.Error($"Password change handler failed: {ex.Message}."); } }
private void PullAndPushPasswordByApiKey(string apiKey, IEnumerable <AccountMapping> selectedAccounts) { using var password = _a2AContext.RetrievePassword(apiKey.ToSecureString()); foreach (var account in selectedAccounts) { var monitorEvent = new MonitorEvent() { Event = $"Sending password for account {account.AccountName} to {account.VaultName}.", Result = WellKnownData.SentPasswordSuccess, Date = DateTime.UtcNow }; if (_pluginManager.IsDisabledPlugin(account.VaultName)) { monitorEvent.Event = $"{account.VaultName} is disabled or not loaded. No password sent for account {account.AccountName}."; monitorEvent.Event = WellKnownData.SentPasswordFailure; } else { try { _logger.Information(monitorEvent.Event); if (!_pluginManager.SendPassword(account.VaultName, account.AssetName, account.AccountName, password, string.IsNullOrEmpty(account.AltAccountName) ? null : account.AltAccountName)) { _logger.Error( $"Unable to set the password for {account.AccountName} to {account.VaultName}."); monitorEvent.Result = WellKnownData.SentPasswordFailure; } } catch (Exception ex) { _logger.Error(ex, $"Unable to set the password for {account.AccountName} to {account.VaultName}: {ex.Message}."); monitorEvent.Result = WellKnownData.SentPasswordFailure; } } _lastEventsQueue.Enqueue(monitorEvent); } }
private void PasswordChangeHandler(string eventName, string eventBody) { var eventInfo = JsonHelper.DeserializeObject <EventInfo>(eventBody); try { var apiKeys = _retrievableAccounts.Where(mp => mp.AssetName == eventInfo.AssetName && mp.AccountName == eventInfo.AccountName).ToArray(); // Make sure that we have at least one plugin mapped to the account if (!apiKeys.Any()) { _logger.Error("No API keys were found by the password change handler."); } // Make sure that if there are more than one mapped plugin, all of the API key match for the same account var apiKey = apiKeys.FirstOrDefault()?.ApiKey; if (!apiKeys.All(x => x.ApiKey.Equals(apiKey))) { _logger.Error("Mismatched API keys for the same account were found by the password change handler."); } // At this point we should have one API key to retrieve. using (var password = _a2AContext.RetrievePassword(apiKey.ToSecureString())) { var accounts = _configDb.GetAccountMappings().ToList(); var selectedAccounts = accounts.Where(a => a.ApiKey.Equals(apiKey)); foreach (var account in selectedAccounts) { var monitorEvent = new MonitorEvent() { Event = $"Sending password for account {account.AccountName} to {account.VaultName}.", Result = WellKnownData.SentPasswordSuccess, Date = DateTime.UtcNow }; if (_pluginManager.IsDisabledPlugin(account.VaultName)) { monitorEvent.Event = $"{account.VaultName} is disabled or not loaded. No password sent for account {account.AccountName}."; monitorEvent.Event = WellKnownData.SentPasswordFailure; } else { try { _logger.Information(monitorEvent.Event); if (!_pluginManager.SendPassword(account.VaultName, account.AssetName, account.AccountName, password)) { _logger.Error( $"Unable to set the password for {account.AccountName} to {account.VaultName}."); monitorEvent.Result = WellKnownData.SentPasswordFailure; } } catch (Exception ex) { _logger.Error( $"Unable to set the password for {account.AccountName} to {account.VaultName}: {ex.Message}."); monitorEvent.Result = WellKnownData.SentPasswordFailure; } } _lastEventsQueue.Enqueue(monitorEvent); } } } catch (Exception ex) { _logger.Error($"Password change handler failed: {ex.Message}."); } }