public Dictionary <WeightedRequest, int> SuccessHistogram() { var histogram = Logger.SuccessHistogram; var weightedHistogram = new Dictionary <WeightedRequest, int>(); foreach (var entry in histogram) { var weightedRequest = RequestUris.Find(uri => uri.Uri == entry.Key); weightedHistogram.Add(weightedRequest, entry.Value); } return(weightedHistogram); }
public ManagedServiceAccessToken(IAzureAccount account, IAzureEnvironment environment, string resourceId, string tenant = "Common") { if (account == null || string.IsNullOrEmpty(account.Id) || !account.IsPropertySet(AzureAccount.Property.MSILoginUri)) { throw new ArgumentNullException(nameof(account)); } if (string.IsNullOrWhiteSpace(tenant)) { throw new ArgumentNullException(nameof(tenant)); } if (environment == null) { throw new ArgumentNullException(nameof(environment)); } _account = account; _resourceId = GetResource(resourceId, environment); var idType = GetIdentityType(account); foreach (var uri in BuildTokenUri(_account.GetProperty(AzureAccount.Property.MSILoginUri), account, idType, _resourceId)) { RequestUris.Enqueue(uri); } if (account.IsPropertySet(AzureAccount.Property.MSILoginUriBackup)) { foreach (var uri in BuildTokenUri(_account.GetProperty(AzureAccount.Property.MSILoginUriBackup), account, idType, _resourceId)) { RequestUris.Enqueue(uri); } } _tenant = tenant; IHttpOperationsFactory factory; if (!AzureSession.Instance.TryGetComponent(HttpClientOperationsFactory.Name, out factory)) { factory = HttpClientOperationsFactory.Create(); } _tokenGetter = factory.GetHttpOperations <ManagedServiceTokenInfo>(true).WithHeader("Metadata", new[] { "true" }); if (account.IsPropertySet(AzureAccount.Property.MSILoginSecret)) { _tokenGetter = _tokenGetter.WithHeader("Secret", new[] { account.GetProperty(AzureAccount.Property.MSILoginSecret) }); } }
public async Task StartPinging() { Logger = new RequestLogger(); Logger.AddFailureWatch(this); Timer = new Stopwatch(); Timer.Start(); var pingTasks = RequestUris.Select(uri => new ServerPinger(uri.Uri, TimeSpan.Zero, Logger).BeginPinging(TokenSource.Token)); // Tasks are running... now we just wait... var completeTask = Task.WhenAll(pingTasks); await completeTask; Timer.Stop(); }
void GetOrRenewAuthentication() { if (_expiration - DateTime.UtcNow < ManagedServiceTokenInfo.TimeoutThreshold) { ManagedServiceTokenInfo info = null; while (info == null && RequestUris.Count > 0) { var currentRequestUri = RequestUris.Dequeue(); try { info = _tokenGetter.GetAsync(currentRequestUri, CancellationToken.None).ConfigureAwait(false).GetAwaiter().GetResult(); // if a request was succesful, we should not check any other Uris RequestUris.Clear(); RequestUris.Enqueue(currentRequestUri); } catch (Exception e) when((e is CloudException || e is HttpRequestException) && RequestUris.Count > 0) { // skip to the next uri } } SetToken(info); } }
public HcVaultService(IOptions <ConfigOptions> options, IConfiguration configuration, ILogger <HcVaultService> logger, HttpClient httpClient, IUserInputService userInputService) { _logger = logger; var config = options.Value; _requestUris = config.RequestUris; var environment = userInputService.PromptUserForInput("Choose the used HC Vault environment ({0}):", configuration[Environment], new [] { "Development", "Staging", "Production" }); configuration[Environment] = environment; var paths = config.Paths[environment]; _cosSecretPath = paths.CosSecretsPath; _httpClient = httpClient; _httpClient.BaseAddress = new Uri(paths.BaseUrl); _credentials = config.Credentials; }
// Don't serialize empty arrays public bool ShouldSerializeRequestUris() => RequestUris.Any();