Example #1
0
        public async Task SetPropertiesAsync_StaticWebsite()
        {
            // Arrange
            BlobServiceClient     service    = GetServiceClient_SharedKey();
            BlobServiceProperties properties = await service.GetPropertiesAsync();

            BlobStaticWebsite originalBlobStaticWebsite = properties.StaticWebsite;

            string errorDocument404Path     = "error/404.html";
            string defaultIndexDocumentPath = "index.html";

            properties.StaticWebsite = new BlobStaticWebsite
            {
                Enabled = true,
                ErrorDocument404Path     = errorDocument404Path,
                DefaultIndexDocumentPath = defaultIndexDocumentPath
            };

            // Act
            await service.SetPropertiesAsync(properties);

            // Assert
            properties = await service.GetPropertiesAsync();

            Assert.IsTrue(properties.StaticWebsite.Enabled);
            Assert.AreEqual(errorDocument404Path, properties.StaticWebsite.ErrorDocument404Path);
            Assert.AreEqual(defaultIndexDocumentPath, properties.StaticWebsite.DefaultIndexDocumentPath);

            // Cleanup
            properties.StaticWebsite = originalBlobStaticWebsite;
            await service.SetPropertiesAsync(properties);
        }
        public async Task <HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
        {
            try
            {
                var blobServiceClient = new BlobServiceClient(_connectionString);
                var serviceProperties = await blobServiceClient.GetPropertiesAsync(cancellationToken);

                if (!string.IsNullOrEmpty(_containerName))
                {
                    var containerClient = blobServiceClient.GetBlobContainerClient(_containerName);

                    if (!await containerClient.ExistsAsync(cancellationToken))
                    {
                        return(new HealthCheckResult(context.Registration.FailureStatus, description: $"Container '{_containerName}' not exists"));
                    }

                    await containerClient.GetPropertiesAsync(cancellationToken : cancellationToken);
                }

                return(HealthCheckResult.Healthy());
            }
            catch (Exception ex)
            {
                return(new HealthCheckResult(context.Registration.FailureStatus, exception: ex));
            }
        }
Example #3
0
        public async Task SharedKeyAuthAsync()
        {
            // Get a Storage account name, shared key, and endpoint Uri.
            //
            // You can obtain both from the Azure Portal by clicking Access
            // Keys under Settings in the Portal Storage account blade.
            //
            // You can also get access to your account keys from the Azure CLI
            // with:
            //
            //     az storage account keys list --account-name <account_name> --resource-group <resource_group>
            //
            string accountName = StorageAccountName;
            string accountKey  = StorageAccountKey;
            Uri    serviceUri  = StorageAccountBlobUri;

            // Create a SharedKeyCredential that we can use to authenticate
            StorageSharedKeyCredential credential = new StorageSharedKeyCredential(accountName, accountKey);

            // Create a client that can authenticate with a connection string
            BlobServiceClient service = new BlobServiceClient(serviceUri, credential);

            // Make a service request to verify we've successfully authenticated
            await service.GetPropertiesAsync();
        }
        public async Task DisableSoftDelete()
        {
            BlobServiceClient service = GetServiceClient_SharedKey();
            Response <BlobServiceProperties> properties = await service.GetPropertiesAsync();

            properties.Value.DeleteRetentionPolicy = new BlobRetentionPolicy
            {
                Enabled = false
            };
            await service.SetPropertiesAsync(properties);

            do
            {
                await Delay(250);

                properties = await service.GetPropertiesAsync();
            } while (properties.Value.DeleteRetentionPolicy.Enabled);
        }
Example #5
0
        public async Task EnableSoftDelete()
        {
            BlobServiceClient service = BlobsClientBuilder.GetServiceClient_SharedKey();
            Response <BlobServiceProperties> properties = await service.GetPropertiesAsync();

            properties.Value.DeleteRetentionPolicy = new BlobRetentionPolicy()
            {
                Enabled = true,
                Days    = 2
            };
            await service.SetPropertiesAsync(properties);

            do
            {
                await Delay(250);

                properties = await service.GetPropertiesAsync();
            } while (!properties.Value.DeleteRetentionPolicy.Enabled);
        }
Example #6
0
        public async Task GetPropertiesAsync()
        {
            // Arrange
            BlobServiceClient service = GetServiceClient_SharedKey();

            // Act
            Response <BlobServiceProperties> response = await service.GetPropertiesAsync();

            // Assert
            Assert.IsNotNull(response.Value.DeleteRetentionPolicy);
        }
        /// <summary>
        /// Determines whether Azure Blob is ready
        /// </summary>
        public async Task <Status> IsReadyAsync(CancellationToken cancellationToken)
        {
            BlobServiceClient blobServiceClient = new BlobServiceClient(_connectionString);

            Azure.Response <BlobServiceProperties> serviceProperties =
                await blobServiceClient.GetPropertiesAsync(cancellationToken);

            return(new Status
            {
                IsReady = serviceProperties != null,
                Message = $"Service version: {serviceProperties.Value.DefaultServiceVersion}."
            });
        }
Example #8
0
        public async Task GetPropertiesAsync_Error()
        {
            // Arrange
            BlobServiceClient service = InstrumentClient(
                new BlobServiceClient(
                    GetServiceClient_SharedKey().Uri,
                    GetOptions()));

            // Act
            await TestHelper.AssertExpectedExceptionAsync <RequestFailedException>(
                service.GetPropertiesAsync(),
                e => { });
        }
        private async Task VerifyServiceAvailable(BlobServiceClient client)
        {
            try
            {
                var propertiesResponse = await client.GetPropertiesAsync();

                Assert.True(true);
            }
            catch (Exception e)
            {
                Assert.False(true, $"Could not establish connection to BlobService. {e}");
            }
        }
Example #10
0
        public async Task SharedAccessSignatureAuthAsync()
        {
            // Create a service level SAS that only allows reading from service
            // level APIs
            AccountSasBuilder sas = new AccountSasBuilder
            {
                // Allow access to blobs
                Services = new AccountSasServices()
                {
                    Blobs = true
                }.ToString(),

                // Allow access to the service level APIs
                              ResourceTypes = new AccountSasResourceTypes()
                {
                    Service = true
                }.ToString(),

                // Allow read access
                              Permissions = new AccountSasPermissions()
                {
                    Read = true
                }.ToString(),

                // Access expires in 1 hour!
                              ExpiryTime = DateTimeOffset.UtcNow.AddHours(1)
            };

            // Create a SharedKeyCredential that we can use to sign the SAS token
            StorageSharedKeyCredential credential = new StorageSharedKeyCredential(StorageAccountName, StorageAccountKey);

            // Build a SAS URI
            UriBuilder sasUri = new UriBuilder(StorageAccountBlobUri);

            sasUri.Query = sas.ToSasQueryParameters(credential).ToString();

            // Create a client that can authenticate with the SAS URI
            BlobServiceClient service = new BlobServiceClient(sasUri.Uri);

            // Make a service request to verify we've succesfully authenticated
            await service.GetPropertiesAsync();

            // Try to create a new container (which is beyond our
            // delegated permission)
            StorageRequestFailedException ex =
                Assert.ThrowsAsync <StorageRequestFailedException>(
                    async() => await service.CreateBlobContainerAsync(Randomize("sample-container")));

            Assert.AreEqual(403, ex.Status);
        }
        private async Task <bool> DoesOAuthWorkAsync()
        {
            TestContext.Error.WriteLine($"Blob Probing OAuth {Process.GetCurrentProcess().Id}");

            try
            {
                for (int i = 0; i < 10; i++)
                {
                    BlobServiceClient serviceClient = new BlobServiceClient(
                        new Uri(TestConfigurations.DefaultTargetOAuthTenant.BlobServiceEndpoint),
                        GetOAuthCredential(TestConfigurations.DefaultTargetOAuthTenant));
                    await serviceClient.GetPropertiesAsync();

                    var containerName   = Guid.NewGuid().ToString();
                    var containerClient = serviceClient.GetBlobContainerClient(containerName);
                    await containerClient.CreateIfNotExistsAsync();

                    try
                    {
                        await containerClient.GetPropertiesAsync();

                        var blobName   = Guid.NewGuid().ToString();
                        var blobClient = containerClient.GetAppendBlobClient(blobName);
                        await blobClient.CreateIfNotExistsAsync();

                        await blobClient.GetPropertiesAsync();

                        var userDelegationKey = await serviceClient.GetUserDelegationKeyAsync(startsOn : null, expiresOn : DateTimeOffset.UtcNow.AddHours(1));

                        var sasBuilder = new BlobSasBuilder(BlobSasPermissions.All, DateTimeOffset.UtcNow.AddHours(1))
                        {
                            BlobContainerName = containerName,
                            BlobName          = blobName,
                        };
                        var sas = sasBuilder.ToSasQueryParameters(userDelegationKey.Value, serviceClient.AccountName).ToString();
                        await new BlobBaseClient(blobClient.Uri, new AzureSasCredential(sas)).GetPropertiesAsync();
                    }
                    finally
                    {
                        await containerClient.DeleteIfExistsAsync();
                    }
                }
            } catch (RequestFailedException e) when(e.Status == 403 && e.ErrorCode == "AuthorizationPermissionMismatch")
            {
                TestContext.Error.WriteLine($"Blob Probing OAuth - not ready {Process.GetCurrentProcess().Id}");
                return(false);
            }
            TestContext.Error.WriteLine($"Blob Probing OAuth - ready {Process.GetCurrentProcess().Id}");
            return(true);
        }
Example #12
0
        public void AddsTimeoutToQuery()
        {
            // Arrange
            var client = new BlobServiceClient(_endpoint, _credentials, _clientOptions);

            // Act
            using (StorageExtensions.CreateServiceTimeoutScope(ServerTimeout))
            {
                Assert.ThrowsAsync <RequestFailedException>(async() => await client.GetPropertiesAsync());
            }

            // Assert
            StringAssert.Contains($"timeout={ServerTimeoutSeconds}", _transport.SingleRequest.Uri.ToString());
        }
Example #13
0
        public async Task SetPropertiesAsync()
        {
            // Arrange
            BlobServiceClient     service    = GetServiceClient_SharedKey();
            BlobServiceProperties properties = await service.GetPropertiesAsync();

            BlobCorsRule[] originalCors = properties.Cors.ToArray();
            properties.Cors =
                new[]
            {
                new BlobCorsRule
                {
                    MaxAgeInSeconds = 1000,
                    AllowedHeaders  = "x-ms-meta-data*,x-ms-meta-target*,x-ms-meta-abc",
                    AllowedMethods  = "PUT,GET",
                    AllowedOrigins  = "*",
                    ExposedHeaders  = "x-ms-meta-*"
                }
            };

            // Act
            await service.SetPropertiesAsync(properties);

            // Assert
            properties = await service.GetPropertiesAsync();

            Assert.AreEqual(1, properties.Cors.Count());
            Assert.IsTrue(properties.Cors[0].MaxAgeInSeconds == 1000);

            // Cleanup
            properties.Cors = originalCors;
            await service.SetPropertiesAsync(properties);

            properties = await service.GetPropertiesAsync();

            Assert.AreEqual(originalCors.Count(), properties.Cors.Count());
        }
Example #14
0
        public async Task SetPropertiesAsync_Error()
        {
            // Arrange
            BlobServiceClient     service        = GetServiceClient_SharedKey();
            BlobServiceProperties properties     = (await service.GetPropertiesAsync()).Value;
            BlobServiceClient     invalidService = InstrumentClient(
                new BlobServiceClient(
                    GetServiceClient_SharedKey().Uri,
                    GetOptions()));

            // Act
            await TestHelper.AssertExpectedExceptionAsync <RequestFailedException>(
                invalidService.SetPropertiesAsync(properties),
                e => { });
        }
        public void Ctor_With_Sas_Does_Not_Reorder_Services()
        {
            // Arrange
            var uri           = new Uri("http://127.0.0.1/accountName?sv=2015-04-05&ss=bqtf&srt=sco&st=2021-03-29T02%3A25%3A53Z&se=2021-06-09T02%3A40%3A53Z&sp=crwdlaup&sig=XXXXX");
            var transport     = new MockTransport(r => new MockResponse(404));
            var clientOptions = new BlobClientOptions()
            {
                Transport = transport
            };

            // Act
            var client = new BlobServiceClient(uri, clientOptions);

            Assert.ThrowsAsync <RequestFailedException>(async() => await client.GetPropertiesAsync());

            // Act
            StringAssert.Contains("ss=bqtf", transport.SingleRequest.Uri.ToString());
        }
Example #16
0
        public async Task ConnectionStringAsync()
        {
            // Get a connection string to our Azure Storage account.  You can
            // obtain your connection string from the Azure Portal (click
            // Access Keys under Settings in the Portal Storage account blade)
            // or using the Azure CLI with:
            //
            //     az storage account show-connection-string --name <account_name> --resource-group <resource_group>
            //
            // And you can provide the connection string to your application
            // using an environment variable.
            string connectionString = ConnectionString;

            // Create a client that can authenticate with a connection string
            BlobServiceClient service = new BlobServiceClient(connectionString);

            // Make a service request to verify we've successfully authenticated
            await service.GetPropertiesAsync();
        }
Example #17
0
        public async Task ActiveDirectoryAuthAsync()
        {
            // Create a token credential that can use our Azure Active
            // Directory application to authenticate with Azure Storage
            TokenCredential credential =
                new ClientSecretCredential(
                    ActiveDirectoryTenantId,
                    ActiveDirectoryApplicationId,
                    ActiveDirectoryApplicationSecret,
                    new TokenCredentialOptions()
            {
                AuthorityHost = ActiveDirectoryAuthEndpoint
            });

            // Create a client that can authenticate using our token credential
            BlobServiceClient service = new BlobServiceClient(ActiveDirectoryBlobUri, credential);

            // Make a service request to verify we've successfully authenticated
            await service.GetPropertiesAsync();
        }
        public static async Task EnableLoggingAsync(BlobServiceClient blobClient, CancellationToken cancellationToken)
        {
            BlobServiceProperties serviceProperties = await blobClient.GetPropertiesAsync(cancellationToken).ConfigureAwait(false);

            // Merge write onto it.
            BlobAnalyticsLogging loggingProperties = serviceProperties.Logging;

            if (!loggingProperties.Write)
            {
                // First activating. Be sure to set a retention policy if there isn't one.
                loggingProperties.Write           = true;
                loggingProperties.RetentionPolicy = new BlobRetentionPolicy()
                {
                    Enabled = true,
                    Days    = 7,
                };

                // Leave metrics untouched
                await blobClient.SetPropertiesAsync(serviceProperties, cancellationToken).ConfigureAwait(false);
            }
        }
Example #19
0
        public async Task <string> UploadFile(string containerName, string fileName, IFormFile file, bool overwrite = true)
        {
            // Create the container if not exists and return a container client object
            BlobContainerClient containerClient = new BlobContainerClient(_connectionString, containerName);
            BlobServiceClient   serviceClient   = new BlobServiceClient(_connectionString);
            var props = (await serviceClient.GetPropertiesAsync()).Value;

            props.DefaultServiceVersion = "2019-07-07";
            await serviceClient.SetPropertiesAsync(props);

            await containerClient.CreateIfNotExistsAsync(PublicAccessType.Blob);

            // Get a reference to a blob
            BlobClient blobClient = containerClient.GetBlobClient(fileName);

            if (!overwrite)
            {
                string newFileName;
                int    count = 1;
                while (await blobClient.ExistsAsync())
                {
                    newFileName = $"{Path.GetFileNameWithoutExtension(fileName)}-{count++}{Path.GetExtension(fileName)}";
                    blobClient  = containerClient.GetBlobClient(newFileName);
                }
            }

            // Open the file and upload its data
            var headers = new BlobHttpHeaders
            {
                ContentType        = file.ContentType,
                ContentDisposition = file.ContentDisposition
            };

            await using var uploadFileStream = file.OpenReadStream();
            await blobClient.UploadAsync(uploadFileStream, headers);

            return(blobClient.Uri.AbsoluteUri);
        }
Example #20
0
        private async Task <bool> DoesOAuthWorkAsync()
        {
            TestContext.Error.WriteLine("Blob Probing OAuth");

            try
            {
                BlobServiceClient serviceClient = new BlobServiceClient(
                    new Uri(TestConfigurations.DefaultTargetOAuthTenant.BlobServiceEndpoint),
                    GetOAuthCredential(TestConfigurations.DefaultTargetOAuthTenant));
                await serviceClient.GetPropertiesAsync();

                var containerName   = Guid.NewGuid().ToString();
                var containerClient = serviceClient.GetBlobContainerClient(containerName);
                await containerClient.CreateIfNotExistsAsync();

                try
                {
                    await containerClient.GetPropertiesAsync();

                    var blobName   = Guid.NewGuid().ToString();
                    var blobClient = containerClient.GetAppendBlobClient(blobName);
                    await blobClient.CreateIfNotExistsAsync();

                    await blobClient.GetPropertiesAsync();
                }
                finally
                {
                    await containerClient.DeleteIfExistsAsync();
                }
            } catch (RequestFailedException e) when(e.Status == 403 && e.ErrorCode == "AuthorizationPermissionMismatch")
            {
                TestContext.Error.WriteLine("Blob Probing OAuth - not ready");
                return(false);
            }
            TestContext.Error.WriteLine("Blob Probing OAuth - ready");
            return(true);
        }
Example #21
0
        private async Task <bool> DoesOAuthWorkAsync()
        {
            TestContext.Error.WriteLine($"Datalake Probing OAuth {Process.GetCurrentProcess().Id}");

            try
            {
                for (int i = 0; i < 10; i++)
                {
                    // Check flat account. For some reason we observe failures if that one doesn't work before we start datalake run.
                    {
                        BlobServiceClient serviceClient = new BlobServiceClient(
                            new Uri(TestConfigurations.DefaultTargetOAuthTenant.BlobServiceEndpoint),
                            GetOAuthCredential(TestConfigurations.DefaultTargetOAuthTenant));
                        await serviceClient.GetPropertiesAsync();

                        var containerName   = Guid.NewGuid().ToString();
                        var containerClient = serviceClient.GetBlobContainerClient(containerName);
                        await containerClient.CreateIfNotExistsAsync();

                        try
                        {
                            await containerClient.GetPropertiesAsync();

                            var blobName   = Guid.NewGuid().ToString();
                            var blobClient = containerClient.GetAppendBlobClient(blobName);
                            await blobClient.CreateIfNotExistsAsync();

                            await blobClient.GetPropertiesAsync();

                            var userDelegationKey = await serviceClient.GetUserDelegationKeyAsync(startsOn : null, expiresOn : DateTimeOffset.UtcNow.AddHours(1));

                            var sasBuilder = new BlobSasBuilder(BlobSasPermissions.All, DateTimeOffset.UtcNow.AddHours(1))
                            {
                                BlobContainerName = containerName,
                                BlobName          = blobName,
                            };
                            var sas = sasBuilder.ToSasQueryParameters(userDelegationKey.Value, serviceClient.AccountName).ToString();
                            await new BlobBaseClient(blobClient.Uri, new AzureSasCredential(sas)).GetPropertiesAsync();
                        }
                        finally
                        {
                            await containerClient.DeleteIfExistsAsync();
                        }
                    }

                    // Check hierarchical account.
                    {
                        DataLakeServiceClient serviceClient = new DataLakeServiceClient(
                            new Uri(TestConfigurations.DefaultTargetHierarchicalNamespaceTenant.BlobServiceEndpoint),
                            GetOAuthCredential(TestConfigurations.DefaultTargetHierarchicalNamespaceTenant));
                        await serviceClient.GetPropertiesAsync();

                        var fileSystemName   = Guid.NewGuid().ToString();
                        var fileSystemClient = serviceClient.GetFileSystemClient(fileSystemName);
                        await fileSystemClient.CreateIfNotExistsAsync();

                        try
                        {
                            var directoryName   = Guid.NewGuid().ToString();
                            var directoryClient = fileSystemClient.GetDirectoryClient(directoryName);
                            await directoryClient.CreateIfNotExistsAsync();

                            await directoryClient.GetPropertiesAsync();

                            var fileName   = Guid.NewGuid().ToString();
                            var fileClient = directoryClient.GetFileClient(fileName);
                            await fileClient.CreateIfNotExistsAsync();

                            await fileClient.GetPropertiesAsync();

                            // call some APIs that talk to DFS endoint as well.
                            await fileClient.AppendAsync(new MemoryStream(new byte[] { 1 }), 0);

                            await fileClient.GetAccessControlAsync();

                            var userDelegationKey = await serviceClient.GetUserDelegationKeyAsync(startsOn : null, expiresOn : DateTimeOffset.UtcNow.AddHours(1));

                            var sasBuilder = new DataLakeSasBuilder(DataLakeSasPermissions.All, DateTimeOffset.UtcNow.AddHours(1))
                            {
                                FileSystemName = fileSystemName,
                                Path           = fileClient.Path,
                            };
                            var sas = sasBuilder.ToSasQueryParameters(userDelegationKey.Value, serviceClient.AccountName).ToString();
                            await new DataLakeFileClient(fileClient.Uri, new AzureSasCredential(sas)).GetPropertiesAsync();
                        }
                        finally
                        {
                            await fileSystemClient.DeleteIfExistsAsync();
                        }
                    }
                }
            }
            catch (RequestFailedException e) when(e.Status == 403 && e.ErrorCode == "AuthorizationPermissionMismatch")
            {
                TestContext.Error.WriteLine($"Datalake Probing OAuth - not ready {Process.GetCurrentProcess().Id}");
                return(false);
            }
            TestContext.Error.WriteLine($"Datalake Probing OAuth - ready {Process.GetCurrentProcess().Id}");
            return(true);
        }
        private async Task <bool> DoesOAuthWorkAsync()
        {
            TestContext.Error.WriteLine("Datalake Probing OAuth");

            try
            {
                // Check flat account. For some reason we observe failures if that one doesn't work before we start datalake run.
                {
                    BlobServiceClient serviceClient = new BlobServiceClient(
                        new Uri(TestConfigurations.DefaultTargetOAuthTenant.BlobServiceEndpoint),
                        GetOAuthCredential(TestConfigurations.DefaultTargetOAuthTenant));
                    await serviceClient.GetPropertiesAsync();

                    var containerName   = Guid.NewGuid().ToString();
                    var containerClient = serviceClient.GetBlobContainerClient(containerName);
                    await containerClient.CreateIfNotExistsAsync();

                    try
                    {
                        await containerClient.GetPropertiesAsync();

                        var blobName   = Guid.NewGuid().ToString();
                        var blobClient = containerClient.GetAppendBlobClient(blobName);
                        await blobClient.CreateIfNotExistsAsync();

                        await blobClient.GetPropertiesAsync();
                    }
                    finally
                    {
                        await containerClient.DeleteIfExistsAsync();
                    }
                }

                // Check hierarchical account.
                {
                    DataLakeServiceClient serviceClient = new DataLakeServiceClient(
                        new Uri(TestConfigurations.DefaultTargetHierarchicalNamespaceTenant.BlobServiceEndpoint),
                        GetOAuthCredential(TestConfigurations.DefaultTargetHierarchicalNamespaceTenant));
                    await serviceClient.GetPropertiesAsync();

                    var fileSystemName   = Guid.NewGuid().ToString();
                    var fileSystemClient = serviceClient.GetFileSystemClient(fileSystemName);
                    await fileSystemClient.CreateIfNotExistsAsync();

                    try
                    {
                        var directoryName   = Guid.NewGuid().ToString();
                        var directoryClient = fileSystemClient.GetDirectoryClient(directoryName);
                        await directoryClient.CreateIfNotExistsAsync();

                        await directoryClient.GetPropertiesAsync();

                        var fileName   = Guid.NewGuid().ToString();
                        var fileClient = directoryClient.GetFileClient(fileName);
                        await fileClient.CreateIfNotExistsAsync();

                        await fileClient.GetPropertiesAsync();

                        // call some APIs that talk to DFS endoint as well.
                        await fileClient.AppendAsync(new MemoryStream(new byte[] { 1 }), 0);

                        await fileClient.GetAccessControlAsync();
                    }
                    finally
                    {
                        await fileSystemClient.DeleteIfExistsAsync();
                    }
                }
            }
            catch (RequestFailedException e) when(e.Status == 403 && e.ErrorCode == "AuthorizationPermissionMismatch")
            {
                TestContext.Error.WriteLine("Datalake Probing OAuth - not ready");
                return(false);
            }
            TestContext.Error.WriteLine("Datalake Probing OAuth - ready");
            return(true);
        }