private async Task <string> GetAuthToken()
        {
            string secret = await _keyVaultHelper.GetSecretAsync(_configuration.KeyVaultName, _configuration.AADSecretName);

            var credential  = new ClientCredential(_configuration.AADClientId, secret);
            var authContext = new AuthenticationContext(string.Format(AAD_AUTHORITY_FORMAT, _configuration.TenantId), false);
            var token       = await authContext.AcquireTokenAsync(AAD_RESOURCE, credential);

            return(token.AccessToken);
        }
Beispiel #2
0
        public StorageUtility(IOptionsMonitor <StorageAccountConfigurationOption> options, ILogger <StorageUtility> logger,
                              IKeyVaultHelper keyVaultHelper)
        {
            string              key = keyVaultHelper.GetSecretAsync(options.CurrentValue.Config.VaultName, options.CurrentValue.Config.AccountKey).Result;
            StorageCredentials  storageCredentials  = new StorageCredentials(options.CurrentValue.Config.AccountName, key);
            CloudStorageAccount cloudStorageAccount = new CloudStorageAccount(storageCredentials, true);

            _cloudBlobClient  = cloudStorageAccount.CreateCloudBlobClient();
            _cloudTableClient = cloudStorageAccount.CreateCloudTableClient();
            _logger           = logger ?? throw new ArgumentNullException(nameof(logger));
        }
        public async Task <IQueueClient> GetQueueClientAsync(string queueName)
        {
            try
            {
                string SERVICE_BUS_QUEUE_CONNECTIONSTRING = await _keyVaultHelper.GetSecretAsync(PlatformConfigurationConstants.SERVICEBUS_CONNECTION_STRING);

                _queueClient = new QueueClient(SERVICE_BUS_QUEUE_CONNECTIONSTRING, queueName);
                return(_queueClient);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
Beispiel #4
0
        public async Task <IEnumerable <Usage> > GetMeterEventsByHour(DateTime startTime, string query)
        {
            List <Usage> usageEventList = new List <Usage>();

            try
            {
                _logger.LogInformation($"Query sql database to get telemetry data from {startTime} to {startTime.AddHours(1)} with query {query}.");
                _logger.LogInformation($"Key vault name {_configuration.KeyVaultName}.");
                _logger.LogInformation($"ConnectionStringSecretName {_configuration.ConnectionStringSecretName}");
                string connectionString = await _keyVaultHelper.GetSecretAsync(_configuration.KeyVaultName, _configuration.ConnectionStringSecretName);

                using (SqlConnection conn = new SqlConnection(connectionString))
                {
                    SqlCommand command = conn.CreateCommand();
                    command.CommandText = query;
                    command.Parameters.AddWithValue("@effectiveStartTime", startTime);
                    conn.Open();
                    SqlDataReader reader = await command.ExecuteReaderAsync();

                    while (reader.Read())
                    {
                        usageEventList.Add(new Usage()
                        {
                            EffectiveStartTime = startTime.ToString(),
                            ResourceId         = reader["resourceId"].ToString(),
                            Quantity           = (float)reader["quantity"]
                        });
                    }
                }

                _logger.LogInformation($"{usageEventList.Count} event(s) returned.");

                return(usageEventList);
            }
            catch (Exception e)
            {
                throw new LunaServerException($"SQL query failed with error {e.Message}", innerException: e);
            }
        }