public A2ARetrievableAccount GetPluginVaultAccount(ISafeguardConnection sgConnection, string name)
        {
            var plugin = _configDb.GetPluginByName(name);

            if (plugin == null)
            {
                throw LogAndException($"Plugin {name} not found");
            }
            if (plugin.IsSystemOwned && !plugin.VaultAccountId.HasValue)
            {
                var account = new A2ARetrievableAccount()
                {
                    AccountName        = WellKnownData.DevOpsCredentialName(plugin.Name, _configDb.SvcId),
                    AccountDescription = "Internal account",
                    SystemName         = WellKnownData.DevOpsAssetName(_configDb.SvcId),
                    SystemDescription  = WellKnownData.DevOpsAssetName(_configDb.SvcId)
                };

                var addon = _configDb.GetAllAddons().FirstOrDefault(a => a.Manifest.PluginName.Equals(plugin.Name));
                if (addon != null)
                {
                    account.AccountName       = addon.VaultAccountName;
                    account.SystemName        = addon.Name;
                    account.SystemDescription = addon.Name;
                }

                return(account);
            }
            if (!plugin.VaultAccountId.HasValue)
            {
                return(null);
            }

            return(_safeguardLogic.GetA2ARetrievableAccount(sgConnection, plugin.VaultAccountId.Value, A2ARegistrationType.Vault));
        }
        private void RefreshPluginCredential(ISafeguardConnection sgConnection, Plugin plugin)
        {
            if (plugin.VaultAccountId.HasValue)
            {
                try
                {
                    var a2aAccount = _safeguardLogic.GetA2ARetrievableAccount(sgConnection, plugin.VaultAccountId.Value,
                                                                              A2ARegistrationType.Vault);
                    if (a2aAccount != null)
                    {
                        SendPluginVaultCredentials(plugin.Name, a2aAccount.ApiKey);
                        return;
                    }

                    _logger.Error(
                        $"Failed to refresh the credential for plugin {plugin.Name} account {plugin.VaultAccountId}.");
                }
                catch (Exception ex)
                {
                    var msg =
                        $"Failed to refresh the api key for {plugin.Name} account {plugin.VaultAccountId}: {ex.Message}";
                    _logger.Error(ex, msg);
                }
            }
            else if (plugin.IsSystemOwned)
            {
                var addon = _configDb.GetAllAddons().FirstOrDefault(a => a.Manifest.PluginName.Equals(plugin.Name));
                if (addon != null && addon.VaultCredentials.ContainsKey(WellKnownData.DevOpsCredentialName(addon.VaultAccountName, _configDb.SvcId)))
                {
                    SendPluginVaultCredentialOnly(plugin.Name, addon.VaultCredentials[WellKnownData.DevOpsCredentialName(addon.VaultAccountName, _configDb.SvcId)]);
                }
            }
        }
        public void AddonPropertyChangedHandler(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (sender is Addon addon)
            {
                if (addon.CredentialsUpdated)
                {
                    addon.CredentialsUpdated = false;

                    _logger.Information("Addon accounts have changed.  Saving changes.");

                    Dictionary <string, string> credentials = new Dictionary <string, string>();
                    foreach (var credential in addon.VaultCredentials)
                    {
                        credentials.Add(WellKnownData.DevOpsCredentialName(credential.Key, _configDb.SvcId),
                                        credential.Value);
                    }

                    addon.VaultCredentials = credentials;
                    _configDb.SaveAddon(addon);
                }
            }
        }