コード例 #1
0
        private static void VerifySecretToload(ParallelSecretLoader secretLoader, Dictionary <string, LoadedSecret> newLoadedSecrets, Dictionary <string, LoadedSecret> oldLoadedSecrets, SecretProperties secret)
        {
            var secretId = secret.Name;

            if (oldLoadedSecrets != null &&
                oldLoadedSecrets.TryGetValue(secretId, out var existingSecret) &&
                existingSecret.IsUpToDate(secret.UpdatedOn))
            {
                oldLoadedSecrets.Remove(secretId);
                newLoadedSecrets.Add(secretId, existingSecret);
            }
            else
            {
                secretLoader.AddSecretToLoad(secret.Name);
            }
        }
コード例 #2
0
        private async Task LoadAsync()
        {
            using var secretLoader = new ParallelSecretLoader(_client);
            var newLoadedSecrets = new Dictionary <string, LoadedSecret>();
            var oldLoadedSecrets = Interlocked.Exchange(ref _loadedSecrets, null);

            if (_fullLoad)
            {
                AsyncPageable <SecretProperties> secretPages = _client.GetPropertiesOfSecretsAsync();
                await foreach (var secret in secretPages.ConfigureAwait(false))
                {
                    if (secret.Enabled != true)
                    {
                        continue;
                    }
                    VerifySecretToload(secretLoader, newLoadedSecrets, oldLoadedSecrets, secret);
                }
            }
            else
            {
                foreach (var key in _uploadKeyList)
                {
                    AsyncPageable <SecretProperties> secretProperties = _client.GetPropertiesOfSecretVersionsAsync(key);
                    var secretList = await secretProperties.ToListAsync();

                    var secret = secretList.OrderByDescending(s => s.UpdatedOn).FirstOrDefault(w => w.Enabled.GetValueOrDefault());
                    if (secret != null)
                    {
                        VerifySecretToload(secretLoader, newLoadedSecrets, oldLoadedSecrets, secret);
                    }
                }

                foreach (var keyValue in _uploadAndMapKeys)
                {
                    AsyncPageable <SecretProperties> secretProperties = _client.GetPropertiesOfSecretVersionsAsync(keyValue.Key);
                    var secretList = await secretProperties.ToListAsync();

                    var secret = secretList.OrderByDescending(s => s.UpdatedOn).FirstOrDefault(w => w.Enabled.GetValueOrDefault());
                    if (secret != null)
                    {
                        VerifySecretToload(secretLoader, newLoadedSecrets, oldLoadedSecrets, secret);
                    }
                }
            }

            var loadedSecrets = await secretLoader.WaitForAllSecrets().ConfigureAwait(false);

            foreach (var secretBundle in loadedSecrets)
            {
                string configName = secretBundle.Value.Name;

                if (!_fullLoad)
                {
                    if (_uploadAndMapKeys.Keys.Contains(configName))
                    {
                        configName = _uploadAndMapKeys[configName];
                    }
                }

                if (!string.IsNullOrWhiteSpace(_configurationSectionPrefix))
                {
                    configName = _configurationSectionPrefix + ConfigurationPath.KeyDelimiter + configName;
                }

                newLoadedSecrets.Add(secretBundle.Value.Name, new LoadedSecret(_keyVaultSecretNameEncoder(configName),
                                                                               secretBundle.Value.Value, secretBundle.Value.Properties.UpdatedOn));
            }

            _loadedSecrets = newLoadedSecrets;

            // Reload is needed if we are loading secrets that were not loaded before or
            // secret that was loaded previously is not available anymore
            if (loadedSecrets.Any() || oldLoadedSecrets?.Any() == true)
            {
                SetData(_loadedSecrets, fireToken: oldLoadedSecrets != null);
            }

            // schedule a polling task only if none exists and a valid delay is specified
            if (_pollingTask == null && _reloadInterval != null)
            {
                _pollingTask = PollForSecretChangesAsync();
            }
        }