static void ThrowIfConnectionStringAndTokenProviderSpecified(DataBusSettings dataBusSettings)
 {
     if (dataBusSettings.StorageAccountName != null && dataBusSettings.UserProvidedConnectionString)
     {
         throw new Exception("More than one authentication method to Azure Service was supplied (using connection string and Managed Identity). Use one method only.");
     }
 }
Example #2
0
        public BlobStorageDataBus(CloudBlobContainer container, DataBusSettings settings, IAsyncTimer timer)
        {
            this.container = container;
            this.settings  = settings;
            this.timer     = timer;

            retryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(settings.BackOffInterval), settings.MaxRetries);
        }
        static CloudBlobContainer CreateCloudBlobContainer(DataBusSettings dataBusSettings)
        {
            CloudBlobContainer container;

            // Attempt managed identity identity first
            if (!string.IsNullOrWhiteSpace(dataBusSettings.StorageAccountName))
            {
                var azureServiceTokenProvider = new AzureServiceTokenProvider();
                var state              = (azureServiceTokenProvider, dataBusSettings);
                var tokenAndFrequency  = TokenRenewerAsync(state, CancellationToken.None).GetAwaiter().GetResult();
                var tokenCredential    = new TokenCredential(tokenAndFrequency.Token, TokenRenewerAsync, state, tokenAndFrequency.Frequency.Value);
                var storageCredentials = new StorageCredentials(tokenCredential);
                var containerPath      = $"https://{dataBusSettings.StorageAccountName}.blob.{dataBusSettings.EndpointSuffix}/{dataBusSettings.Container}";

                container = new CloudBlobContainer(new StorageUri(new Uri(containerPath)), storageCredentials);
            }
            else // fallback to connection string
            {
                var cloudBlobClient = CloudStorageAccount.Parse(dataBusSettings.ConnectionString).CreateCloudBlobClient();
                container = cloudBlobClient.GetContainerReference(dataBusSettings.Container);
            }

            return(container);
        }