-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathAzureAppConfigurationProvider.cs
1253 lines (1056 loc) · 52.4 KB
/
AzureAppConfigurationProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
//
using Azure;
using Azure.Data.AppConfiguration;
using Microsoft.Extensions.Configuration.AzureAppConfiguration.Extensions;
using Microsoft.Extensions.Configuration.AzureAppConfiguration.FeatureManagement;
using Microsoft.Extensions.Configuration.AzureAppConfiguration.Models;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Sockets;
using System.Security;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.Extensions.Configuration.AzureAppConfiguration
{
internal class AzureAppConfigurationProvider : ConfigurationProvider, IConfigurationRefresher, IDisposable
{
private bool _optional;
private bool _isInitialLoadComplete = false;
private bool _isFeatureManagementVersionInspected;
private readonly bool _requestTracingEnabled;
private readonly IConfigurationClientManager _configClientManager;
private Uri _lastSuccessfulEndpoint;
private AzureAppConfigurationOptions _options;
private Dictionary<string, ConfigurationSetting> _mappedData;
private Dictionary<KeyValueIdentifier, ConfigurationSetting> _watchedSettings = new Dictionary<KeyValueIdentifier, ConfigurationSetting>();
private RequestTracingOptions _requestTracingOptions;
private Dictionary<Uri, ConfigurationClientBackoffStatus> _configClientBackoffs = new Dictionary<Uri, ConfigurationClientBackoffStatus>();
private readonly TimeSpan MinRefreshInterval;
// The most-recent time when the refresh operation attempted to load the initial configuration
private DateTimeOffset InitializationCacheExpires = default;
private static readonly TimeSpan MinDelayForUnhandledFailure = TimeSpan.FromSeconds(5);
private static readonly TimeSpan DefaultMaxSetDirtyDelay = TimeSpan.FromSeconds(30);
// To avoid concurrent network operations, this flag is used to achieve synchronization between multiple threads.
private int _networkOperationsInProgress = 0;
private Logger _logger = new Logger();
private ILoggerFactory _loggerFactory;
private class ConfigurationClientBackoffStatus
{
public int FailedAttempts { get; set; }
public DateTimeOffset BackoffEndTime { get; set; }
}
public Uri AppConfigurationEndpoint
{
get
{
if (_options.Endpoints != null)
{
return _options.Endpoints.First();
}
if (_options.ConnectionStrings != null && _options.ConnectionStrings.Any() && _options.ConnectionStrings.First() != null)
{
// Use try-catch block to avoid throwing exceptions from property getter.
// https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/property
try
{
return new Uri(ConnectionStringUtils.Parse(_options.ConnectionStrings.First(), ConnectionStringUtils.EndpointSection));
}
catch (FormatException) { }
}
return null;
}
}
public ILoggerFactory LoggerFactory
{
get
{
return _loggerFactory;
}
set
{
_loggerFactory = value;
if (_loggerFactory != null)
{
_logger = new Logger(_loggerFactory.CreateLogger(LoggingConstants.AppConfigRefreshLogCategory));
if (_configClientManager is ConfigurationClientManager clientManager)
{
clientManager.SetLogger(_logger);
}
}
}
}
public AzureAppConfigurationProvider(IConfigurationClientManager configClientManager, AzureAppConfigurationOptions options, bool optional)
{
_configClientManager = configClientManager ?? throw new ArgumentNullException(nameof(configClientManager));
_options = options ?? throw new ArgumentNullException(nameof(options));
_optional = optional;
IEnumerable<KeyValueWatcher> watchers = options.ChangeWatchers.Union(options.MultiKeyWatchers);
if (watchers.Any())
{
MinRefreshInterval = watchers.Min(w => w.RefreshInterval);
}
else
{
MinRefreshInterval = RefreshConstants.DefaultRefreshInterval;
}
// Enable request tracing if not opt-out
string requestTracingDisabled = null;
try
{
requestTracingDisabled = Environment.GetEnvironmentVariable(RequestTracingConstants.RequestTracingDisabledEnvironmentVariable);
}
catch (SecurityException) { }
_requestTracingEnabled = bool.TryParse(requestTracingDisabled, out bool tracingDisabled) ? !tracingDisabled : true;
if (_requestTracingEnabled)
{
SetRequestTracingOptions();
}
}
/// <summary>
/// Loads (or reloads) the data for this provider.
/// </summary>
public override void Load()
{
var watch = Stopwatch.StartNew();
try
{
using var startupCancellationTokenSource = new CancellationTokenSource(_options.Startup.Timeout);
// Load() is invoked only once during application startup. We don't need to check for concurrent network
// operations here because there can't be any other startup or refresh operation in progress at this time.
LoadAsync(_optional, startupCancellationTokenSource.Token).ConfigureAwait(false).GetAwaiter().GetResult();
}
catch (ArgumentException)
{
// Instantly re-throw the exception
throw;
}
catch
{
// AzureAppConfigurationProvider.Load() method is called in the application's startup code path.
// Unhandled exceptions cause application crash which can result in crash loops as orchestrators attempt to restart the application.
// Knowing the intended usage of the provider in startup code path, we mitigate back-to-back crash loops from overloading the server with requests by waiting a minimum time to propogate fatal errors.
var waitInterval = MinDelayForUnhandledFailure.Subtract(watch.Elapsed);
if (waitInterval.Ticks > 0)
{
Task.Delay(waitInterval).ConfigureAwait(false).GetAwaiter().GetResult();
}
// Re-throw the exception after the additional delay (if required)
throw;
}
finally
{
// Set the provider for AzureAppConfigurationRefresher instance after LoadAll has completed.
// This stops applications from calling RefreshAsync until config has been initialized during startup.
var refresher = (AzureAppConfigurationRefresher)_options.GetRefresher();
refresher.SetProvider(this);
}
// Mark all settings have loaded at startup.
_isInitialLoadComplete = true;
}
public async Task RefreshAsync(CancellationToken cancellationToken)
{
// Ensure that concurrent threads do not simultaneously execute refresh operation.
if (Interlocked.Exchange(ref _networkOperationsInProgress, 1) == 0)
{
try
{
// FeatureManagement assemblies may not be loaded on provider startup, so version information is gathered upon first refresh for tracing
EnsureFeatureManagementVersionInspected();
var utcNow = DateTimeOffset.UtcNow;
IEnumerable<KeyValueWatcher> refreshableWatchers = _options.ChangeWatchers.Where(changeWatcher => utcNow >= changeWatcher.NextRefreshTime);
IEnumerable<KeyValueWatcher> refreshableMultiKeyWatchers = _options.MultiKeyWatchers.Where(changeWatcher => utcNow >= changeWatcher.NextRefreshTime);
// Skip refresh if mappedData is loaded, but none of the watchers or adapters are refreshable.
if (_mappedData != null &&
!refreshableWatchers.Any() &&
!refreshableMultiKeyWatchers.Any() &&
!_options.Adapters.Any(adapter => adapter.NeedsRefresh()))
{
return;
}
IEnumerable<ConfigurationClient> clients = _configClientManager.GetClients();
//
// Filter clients based on their backoff status
clients = clients.Where(client =>
{
Uri endpoint = _configClientManager.GetEndpointForClient(client);
if (!_configClientBackoffs.TryGetValue(endpoint, out ConfigurationClientBackoffStatus clientBackoffStatus))
{
clientBackoffStatus = new ConfigurationClientBackoffStatus();
_configClientBackoffs[endpoint] = clientBackoffStatus;
}
return clientBackoffStatus.BackoffEndTime <= utcNow;
}
);
if (!clients.Any())
{
_configClientManager.RefreshClients();
_logger.LogDebug(LogHelper.BuildRefreshSkippedNoClientAvailableMessage());
return;
}
// Check if initial configuration load had failed
if (_mappedData == null)
{
if (InitializationCacheExpires < utcNow)
{
InitializationCacheExpires = utcNow.Add(MinRefreshInterval);
await InitializeAsync(clients, cancellationToken).ConfigureAwait(false);
}
return;
}
//
// Avoid instance state modification
Dictionary<KeyValueIdentifier, ConfigurationSetting> watchedSettings = null;
List<KeyValueChange> keyValueChanges = null;
List<KeyValueChange> changedKeyValuesCollection = null;
Dictionary<string, ConfigurationSetting> data = null;
bool refreshAll = false;
StringBuilder logInfoBuilder = new StringBuilder();
StringBuilder logDebugBuilder = new StringBuilder();
await ExecuteWithFailOverPolicyAsync(clients, async (client) =>
{
data = null;
watchedSettings = null;
keyValueChanges = new List<KeyValueChange>();
changedKeyValuesCollection = null;
refreshAll = false;
Uri endpoint = _configClientManager.GetEndpointForClient(client);
logDebugBuilder.Clear();
logInfoBuilder.Clear();
foreach (KeyValueWatcher changeWatcher in refreshableWatchers)
{
string watchedKey = changeWatcher.Key;
string watchedLabel = changeWatcher.Label;
KeyValueIdentifier watchedKeyLabel = new KeyValueIdentifier(watchedKey, watchedLabel);
KeyValueChange change = default;
//
// Find if there is a change associated with watcher
if (_watchedSettings.TryGetValue(watchedKeyLabel, out ConfigurationSetting watchedKv))
{
await TracingUtils.CallWithRequestTracing(_requestTracingEnabled, RequestType.Watch, _requestTracingOptions,
async () => change = await client.GetKeyValueChange(watchedKv, cancellationToken).ConfigureAwait(false)).ConfigureAwait(false);
}
else
{
// Load the key-value in case the previous load attempts had failed
try
{
await CallWithRequestTracing(
async () => watchedKv = await client.GetConfigurationSettingAsync(watchedKey, watchedLabel, cancellationToken).ConfigureAwait(false)).ConfigureAwait(false);
}
catch (RequestFailedException e) when (e.Status == (int)HttpStatusCode.NotFound)
{
watchedKv = null;
}
if (watchedKv != null)
{
change = new KeyValueChange()
{
Key = watchedKv.Key,
Label = watchedKv.Label.NormalizeNull(),
Current = watchedKv,
ChangeType = KeyValueChangeType.Modified
};
}
}
// Check if a change has been detected in the key-value registered for refresh
if (change.ChangeType != KeyValueChangeType.None)
{
logDebugBuilder.AppendLine(LogHelper.BuildKeyValueReadMessage(change.ChangeType, change.Key, change.Label, endpoint.ToString()));
logInfoBuilder.AppendLine(LogHelper.BuildKeyValueSettingUpdatedMessage(change.Key));
keyValueChanges.Add(change);
if (changeWatcher.RefreshAll)
{
refreshAll = true;
break;
}
} else
{
logDebugBuilder.AppendLine(LogHelper.BuildKeyValueReadMessage(change.ChangeType, change.Key, change.Label, endpoint.ToString()));
}
}
if (refreshAll)
{
// Trigger a single load-all operation if a change was detected in one or more key-values with refreshAll: true
data = await LoadSelectedKeyValues(client, cancellationToken).ConfigureAwait(false);
watchedSettings = await LoadKeyValuesRegisteredForRefresh(client, data, cancellationToken).ConfigureAwait(false);
watchedSettings = UpdateWatchedKeyValueCollections(watchedSettings, data);
logInfoBuilder.AppendLine(LogHelper.BuildConfigurationUpdatedMessage());
return;
}
changedKeyValuesCollection = await GetRefreshedKeyValueCollections(refreshableMultiKeyWatchers, client, logDebugBuilder, logInfoBuilder, endpoint, cancellationToken).ConfigureAwait(false);
if (!changedKeyValuesCollection.Any())
{
logDebugBuilder.AppendLine(LogHelper.BuildFeatureFlagsUnchangedMessage(endpoint.ToString()));
}
},
cancellationToken)
.ConfigureAwait(false);
if (!refreshAll)
{
watchedSettings = new Dictionary<KeyValueIdentifier, ConfigurationSetting>(_watchedSettings);
foreach (KeyValueWatcher changeWatcher in refreshableWatchers.Concat(refreshableMultiKeyWatchers))
{
UpdateNextRefreshTime(changeWatcher);
}
foreach (KeyValueChange change in keyValueChanges.Concat(changedKeyValuesCollection))
{
KeyValueIdentifier changeIdentifier = new KeyValueIdentifier(change.Key, change.Label);
if (change.ChangeType == KeyValueChangeType.Modified)
{
ConfigurationSetting setting = change.Current;
ConfigurationSetting settingCopy = new ConfigurationSetting(setting.Key, setting.Value, setting.Label, setting.ETag);
watchedSettings[changeIdentifier] = settingCopy;
foreach (Func<ConfigurationSetting, ValueTask<ConfigurationSetting>> func in _options.Mappers)
{
setting = await func(setting).ConfigureAwait(false);
}
if (setting == null)
{
_mappedData.Remove(change.Key);
}
else
{
_mappedData[change.Key] = setting;
}
}
else if (change.ChangeType == KeyValueChangeType.Deleted)
{
_mappedData.Remove(change.Key);
watchedSettings.Remove(changeIdentifier);
}
// Invalidate the cached Key Vault secret (if any) for this ConfigurationSetting
foreach (IKeyValueAdapter adapter in _options.Adapters)
{
adapter.InvalidateCache(change.Current);
}
}
}
else
{
_mappedData = await MapConfigurationSettings(data).ConfigureAwait(false);
// Invalidate all the cached KeyVault secrets
foreach (IKeyValueAdapter adapter in _options.Adapters)
{
adapter.InvalidateCache();
}
// Update the next refresh time for all refresh registered settings and feature flags
foreach (KeyValueWatcher changeWatcher in _options.ChangeWatchers.Concat(_options.MultiKeyWatchers))
{
UpdateNextRefreshTime(changeWatcher);
}
}
if (_options.Adapters.Any(adapter => adapter.NeedsRefresh()) || changedKeyValuesCollection?.Any() == true || keyValueChanges.Any())
{
_watchedSettings = watchedSettings;
if (logDebugBuilder.Length > 0)
{
_logger.LogDebug(logDebugBuilder.ToString().Trim());
}
if (logInfoBuilder.Length > 0)
{
_logger.LogInformation(logInfoBuilder.ToString().Trim());
}
// PrepareData makes calls to KeyVault and may throw exceptions. But, we still update watchers before
// SetData because repeating appconfig calls (by not updating watchers) won't help anything for keyvault calls.
// As long as adapter.NeedsRefresh is true, we will attempt to update keyvault again the next time RefreshAsync is called.
SetData(await PrepareData(_mappedData, cancellationToken).ConfigureAwait(false));
}
}
finally
{
Interlocked.Exchange(ref _networkOperationsInProgress, 0);
}
}
}
public async Task<bool> TryRefreshAsync(CancellationToken cancellationToken)
{
try
{
await RefreshAsync(cancellationToken).ConfigureAwait(false);
}
catch (RequestFailedException rfe)
{
if (IsAuthenticationError(rfe))
{
_logger.LogWarning(LogHelper.BuildRefreshFailedDueToAuthenticationErrorMessage(rfe.Message));
}
else
{
_logger.LogWarning(LogHelper.BuildRefreshFailedErrorMessage(rfe.Message));
}
return false;
}
catch (KeyVaultReferenceException kvre)
{
_logger.LogWarning(LogHelper.BuildRefreshFailedDueToKeyVaultErrorMessage(kvre.Message));
return false;
}
catch (OperationCanceledException)
{
_logger.LogWarning(LogHelper.BuildRefreshCanceledErrorMessage());
return false;
}
catch (InvalidOperationException e)
{
_logger.LogWarning(LogHelper.BuildRefreshFailedErrorMessage(e.Message));
return false;
}
catch (AggregateException ae)
{
if (ae.InnerExceptions?.Any(e => e is RequestFailedException) ?? false)
{
if (IsAuthenticationError(ae))
{
_logger.LogWarning(LogHelper.BuildRefreshFailedDueToAuthenticationErrorMessage(ae.Message));
}
else
{
_logger.LogWarning(LogHelper.BuildRefreshFailedErrorMessage(ae.Message));
}
}
else if (ae.InnerExceptions?.Any(e => e is OperationCanceledException) ?? false)
{
_logger.LogWarning(LogHelper.BuildRefreshCanceledErrorMessage());
}
else
{
throw;
}
return false;
}
return true;
}
public void ProcessPushNotification(PushNotification pushNotification, TimeSpan? maxDelay)
{
if (pushNotification == null)
{
throw new ArgumentNullException(nameof(pushNotification));
}
if (string.IsNullOrEmpty(pushNotification.SyncToken))
{
throw new ArgumentException(
"Sync token is required.",
$"{nameof(pushNotification)}.{nameof(pushNotification.SyncToken)}");
}
if (string.IsNullOrEmpty(pushNotification.EventType))
{
throw new ArgumentException(
"Event type is required.",
$"{nameof(pushNotification)}.{nameof(pushNotification.EventType)}");
}
if (pushNotification.ResourceUri == null)
{
throw new ArgumentException(
"Resource URI is required.",
$"{nameof(pushNotification)}.{nameof(pushNotification.ResourceUri)}");
}
if (_configClientManager.UpdateSyncToken(pushNotification.ResourceUri, pushNotification.SyncToken))
{
SetDirty(maxDelay);
}
else
{
_logger.LogWarning(LogHelper.BuildPushNotificationUnregisteredEndpointMessage(pushNotification.ResourceUri.ToString()));
}
}
private void SetDirty(TimeSpan? maxDelay)
{
DateTimeOffset nextRefreshTime = AddRandomDelay(DateTimeOffset.UtcNow, maxDelay ?? DefaultMaxSetDirtyDelay);
foreach (KeyValueWatcher changeWatcher in _options.ChangeWatchers)
{
changeWatcher.NextRefreshTime = nextRefreshTime;
}
foreach (KeyValueWatcher changeWatcher in _options.MultiKeyWatchers)
{
changeWatcher.NextRefreshTime = nextRefreshTime;
}
}
private async Task<Dictionary<string, string>> PrepareData(Dictionary<string, ConfigurationSetting> data, CancellationToken cancellationToken = default)
{
var applicationData = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
// Reset old filter tracing in order to track the filter types present in the current response from server.
_options.FeatureFilterTracing.ResetFeatureFilterTracing();
foreach (KeyValuePair<string, ConfigurationSetting> kvp in data)
{
IEnumerable<KeyValuePair<string, string>> keyValuePairs = null;
keyValuePairs = await ProcessAdapters(kvp.Value, cancellationToken).ConfigureAwait(false);
foreach (KeyValuePair<string, string> kv in keyValuePairs)
{
string key = kv.Key;
foreach (string prefix in _options.KeyPrefixes)
{
if (key.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
{
key = key.Substring(prefix.Length);
break;
}
}
applicationData[key] = kv.Value;
}
}
return applicationData;
}
private async Task LoadAsync(bool ignoreFailures, CancellationToken cancellationToken)
{
var startupStopwatch = Stopwatch.StartNew();
int postFixedWindowAttempts = 0;
var startupExceptions = new List<Exception>();
try
{
while (true)
{
IEnumerable<ConfigurationClient> clients = _configClientManager.GetClients();
if (await TryInitializeAsync(clients, startupExceptions, cancellationToken).ConfigureAwait(false))
{
break;
}
TimeSpan delay;
if (startupStopwatch.Elapsed.TryGetFixedBackoff(out TimeSpan backoff))
{
delay = backoff;
}
else
{
postFixedWindowAttempts++;
delay = FailOverConstants.MinStartupBackoffDuration.CalculateBackoffDuration(
FailOverConstants.MaxBackoffDuration,
postFixedWindowAttempts);
}
try
{
await Task.Delay(delay, cancellationToken).ConfigureAwait(false);
}
catch (OperationCanceledException)
{
throw new TimeoutException(
$"The provider timed out while attempting to load.",
new AggregateException(startupExceptions));
}
}
}
catch (Exception exception) when (
ignoreFailures &&
(exception is RequestFailedException ||
exception is KeyVaultReferenceException ||
exception is TimeoutException ||
exception is OperationCanceledException ||
exception is InvalidOperationException ||
((exception as AggregateException)?.InnerExceptions?.Any(e =>
e is RequestFailedException ||
e is OperationCanceledException) ?? false)))
{ }
}
private async Task<bool> TryInitializeAsync(IEnumerable<ConfigurationClient> clients, List<Exception> startupExceptions, CancellationToken cancellationToken = default)
{
try
{
await InitializeAsync(clients, cancellationToken).ConfigureAwait(false);
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
return false;
}
catch (RequestFailedException exception)
{
if (IsFailOverable(exception))
{
startupExceptions.Add(exception);
return false;
}
throw;
}
catch (KeyVaultReferenceException exception)
{
if (IsFailOverable(exception))
{
startupExceptions.Add(exception);
return false;
}
throw;
}
catch (AggregateException exception)
{
if (exception.InnerExceptions?.Any(e => e is OperationCanceledException) ?? false)
{
if (!cancellationToken.IsCancellationRequested)
{
startupExceptions.Add(exception);
}
return false;
}
if (IsFailOverable(exception))
{
startupExceptions.Add(exception);
return false;
}
throw;
}
return true;
}
private async Task InitializeAsync(IEnumerable<ConfigurationClient> clients, CancellationToken cancellationToken = default)
{
Dictionary<string, ConfigurationSetting> data = null;
Dictionary<KeyValueIdentifier, ConfigurationSetting> watchedSettings = null;
await ExecuteWithFailOverPolicyAsync(
clients,
async (client) =>
{
data = await LoadSelectedKeyValues(
client,
cancellationToken)
.ConfigureAwait(false);
watchedSettings = await LoadKeyValuesRegisteredForRefresh(
client,
data,
cancellationToken)
.ConfigureAwait(false);
watchedSettings = UpdateWatchedKeyValueCollections(watchedSettings, data);
},
cancellationToken)
.ConfigureAwait(false);
// Update the next refresh time for all refresh registered settings and feature flags
foreach (KeyValueWatcher changeWatcher in _options.ChangeWatchers.Concat(_options.MultiKeyWatchers))
{
UpdateNextRefreshTime(changeWatcher);
}
if (data != null)
{
// Invalidate all the cached KeyVault secrets
foreach (IKeyValueAdapter adapter in _options.Adapters)
{
adapter.InvalidateCache();
}
Dictionary<string, ConfigurationSetting> mappedData = await MapConfigurationSettings(data).ConfigureAwait(false);
SetData(await PrepareData(mappedData, cancellationToken).ConfigureAwait(false));
_watchedSettings = watchedSettings;
_mappedData = mappedData;
}
}
private async Task<Dictionary<string, ConfigurationSetting>> LoadSelectedKeyValues(ConfigurationClient client, CancellationToken cancellationToken)
{
var serverData = new Dictionary<string, ConfigurationSetting>(StringComparer.OrdinalIgnoreCase);
// Use default query if there are no key-values specified for use other than the feature flags
bool useDefaultQuery = !_options.KeyValueSelectors.Any(selector => selector.KeyFilter == null ||
!selector.KeyFilter.StartsWith(FeatureManagementConstants.FeatureFlagMarker));
if (useDefaultQuery)
{
// Load all key-values with the null label.
var selector = new SettingSelector
{
KeyFilter = KeyFilter.Any,
LabelFilter = LabelFilter.Null
};
await CallWithRequestTracing(async () =>
{
await foreach (ConfigurationSetting setting in client.GetConfigurationSettingsAsync(selector, cancellationToken).ConfigureAwait(false))
{
serverData[setting.Key] = setting;
}
}).ConfigureAwait(false);
}
foreach (KeyValueSelector loadOption in _options.KeyValueSelectors)
{
IAsyncEnumerable<ConfigurationSetting> settingsEnumerable;
if (string.IsNullOrEmpty(loadOption.SnapshotName))
{
settingsEnumerable = client.GetConfigurationSettingsAsync(
new SettingSelector
{
KeyFilter = loadOption.KeyFilter,
LabelFilter = loadOption.LabelFilter
},
cancellationToken);
}
else
{
ConfigurationSnapshot snapshot;
try
{
snapshot = await client.GetSnapshotAsync(loadOption.SnapshotName).ConfigureAwait(false);
}
catch (RequestFailedException rfe) when (rfe.Status == (int)HttpStatusCode.NotFound)
{
throw new InvalidOperationException($"Could not find snapshot with name '{loadOption.SnapshotName}'.", rfe);
}
if (snapshot.SnapshotComposition != SnapshotComposition.Key)
{
throw new InvalidOperationException($"{nameof(snapshot.SnapshotComposition)} for the selected snapshot with name '{snapshot.Name}' must be 'key', found '{snapshot.SnapshotComposition}'.");
}
settingsEnumerable = client.GetConfigurationSettingsForSnapshotAsync(
loadOption.SnapshotName,
cancellationToken);
}
await CallWithRequestTracing(async () =>
{
await foreach (ConfigurationSetting setting in settingsEnumerable.ConfigureAwait(false))
{
serverData[setting.Key] = setting;
}
}).ConfigureAwait(false);
}
return serverData;
}
private async Task<Dictionary<KeyValueIdentifier, ConfigurationSetting>> LoadKeyValuesRegisteredForRefresh(ConfigurationClient client, IDictionary<string, ConfigurationSetting> existingSettings, CancellationToken cancellationToken)
{
Dictionary<KeyValueIdentifier, ConfigurationSetting> watchedSettings = new Dictionary<KeyValueIdentifier, ConfigurationSetting>();
foreach (KeyValueWatcher changeWatcher in _options.ChangeWatchers)
{
string watchedKey = changeWatcher.Key;
string watchedLabel = changeWatcher.Label;
KeyValueIdentifier watchedKeyLabel = new KeyValueIdentifier(watchedKey, watchedLabel);
// Skip the loading for the key-value in case it has already been loaded
if (existingSettings.TryGetValue(watchedKey, out ConfigurationSetting loadedKv)
&& watchedKeyLabel.Equals(new KeyValueIdentifier(loadedKv.Key, loadedKv.Label)))
{
watchedSettings[watchedKeyLabel] = new ConfigurationSetting(loadedKv.Key, loadedKv.Value, loadedKv.Label, loadedKv.ETag);
continue;
}
// Send a request to retrieve key-value since it may be either not loaded or loaded with a different label or different casing
ConfigurationSetting watchedKv = null;
try
{
await CallWithRequestTracing(async () => watchedKv = await client.GetConfigurationSettingAsync(watchedKey, watchedLabel, cancellationToken).ConfigureAwait(false)).ConfigureAwait(false);
}
catch (RequestFailedException e) when (e.Status == (int)HttpStatusCode.NotFound)
{
watchedKv = null;
}
// If the key-value was found, store it for updating the settings
if (watchedKv != null)
{
watchedSettings[watchedKeyLabel] = new ConfigurationSetting(watchedKv.Key, watchedKv.Value, watchedKv.Label, watchedKv.ETag);
existingSettings[watchedKey] = watchedKv;
}
}
return watchedSettings;
}
private Dictionary<KeyValueIdentifier, ConfigurationSetting> UpdateWatchedKeyValueCollections(Dictionary<KeyValueIdentifier, ConfigurationSetting> watchedSettings, IDictionary<string, ConfigurationSetting> existingSettings)
{
foreach (KeyValueWatcher changeWatcher in _options.MultiKeyWatchers)
{
IEnumerable<ConfigurationSetting> currentKeyValues = GetCurrentKeyValueCollection(changeWatcher.Key, changeWatcher.Label, existingSettings.Values);
foreach (ConfigurationSetting setting in currentKeyValues)
{
watchedSettings[new KeyValueIdentifier(setting.Key, setting.Label)] = new ConfigurationSetting(setting.Key, setting.Value, setting.Label, setting.ETag);
}
}
return watchedSettings;
}
private async Task<List<KeyValueChange>> GetRefreshedKeyValueCollections(
IEnumerable<KeyValueWatcher> multiKeyWatchers,
ConfigurationClient client,
StringBuilder logDebugBuilder,
StringBuilder logInfoBuilder,
Uri endpoint,
CancellationToken cancellationToken)
{
var keyValueChanges = new List<KeyValueChange>();
foreach (KeyValueWatcher changeWatcher in multiKeyWatchers)
{
IEnumerable<ConfigurationSetting> currentKeyValues = GetCurrentKeyValueCollection(changeWatcher.Key, changeWatcher.Label, _watchedSettings.Values);
keyValueChanges.AddRange(
await client.GetKeyValueChangeCollection(
currentKeyValues,
new GetKeyValueChangeCollectionOptions
{
KeyFilter = changeWatcher.Key,
Label = changeWatcher.Label.NormalizeNull(),
RequestTracingEnabled = _requestTracingEnabled,
RequestTracingOptions = _requestTracingOptions
},
logDebugBuilder,
logInfoBuilder,
endpoint,
cancellationToken)
.ConfigureAwait(false));
}
return keyValueChanges;
}
private void SetData(IDictionary<string, string> data)
{
// Set the application data for the configuration provider
Data = data;
// Notify that the configuration has been updated
OnReload();
}
private async Task<IEnumerable<KeyValuePair<string, string>>> ProcessAdapters(ConfigurationSetting setting, CancellationToken cancellationToken)
{
List<KeyValuePair<string, string>> keyValues = null;
foreach (IKeyValueAdapter adapter in _options.Adapters)
{
if (!adapter.CanProcess(setting))
{
continue;
}
IEnumerable<KeyValuePair<string, string>> kvs = await adapter.ProcessKeyValue(setting, AppConfigurationEndpoint, _logger, cancellationToken).ConfigureAwait(false);
if (kvs != null)
{
keyValues = keyValues ?? new List<KeyValuePair<string, string>>();
keyValues.AddRange(kvs);
}
}
return keyValues ?? Enumerable.Repeat(new KeyValuePair<string, string>(setting.Key, setting.Value), 1);
}
private Task CallWithRequestTracing(Func<Task> clientCall)
{
var requestType = _isInitialLoadComplete ? RequestType.Watch : RequestType.Startup;
return TracingUtils.CallWithRequestTracing(_requestTracingEnabled, requestType, _requestTracingOptions, clientCall);
}
private void SetRequestTracingOptions()
{
_requestTracingOptions = new RequestTracingOptions
{
HostType = TracingUtils.GetHostType(),
IsDevEnvironment = TracingUtils.IsDevEnvironment(),
IsKeyVaultConfigured = _options.IsKeyVaultConfigured,
IsKeyVaultRefreshConfigured = _options.IsKeyVaultRefreshConfigured,
ReplicaCount = _options.Endpoints?.Count() - 1 ?? _options.ConnectionStrings?.Count() - 1 ?? 0,
FilterTracing = _options.FeatureFilterTracing
};
}
private DateTimeOffset AddRandomDelay(DateTimeOffset dt, TimeSpan maxDelay)
{
long randomTicks = (long)(maxDelay.Ticks * RandomGenerator.NextDouble());
return dt.AddTicks(randomTicks);
}
private bool IsAuthenticationError(Exception ex)
{
if (ex is RequestFailedException rfe)
{
return rfe.Status == (int)HttpStatusCode.Unauthorized || rfe.Status == (int)HttpStatusCode.Forbidden;
}
if (ex is AggregateException ae)
{
return ae.InnerExceptions?.Any(inner => IsAuthenticationError(inner)) ?? false;
}
return false;
}
private void UpdateNextRefreshTime(KeyValueWatcher changeWatcher)
{
changeWatcher.NextRefreshTime = DateTimeOffset.UtcNow.Add(changeWatcher.RefreshInterval);
}
private async Task<T> ExecuteWithFailOverPolicyAsync<T>(
IEnumerable<ConfigurationClient> clients,
Func<ConfigurationClient, Task<T>> funcToExecute,
CancellationToken cancellationToken = default)
{
if (_options.LoadBalancingEnabled && _lastSuccessfulEndpoint != null && clients.Count() > 1)
{
// Ensure consistent elements in clients list
clients = new List<ConfigurationClient>(clients);
int nextClientIndex = 0;