Azure / azure-sdk-for-cpp

This repository is for active development of the Azure SDK for C++. For consumers of the SDK we recommend visiting our versioned developer docs at https://azure.github.io/azure-sdk-for-cpp.
MIT License
170 stars 118 forks source link

Consider improving the error message and wrapping the exception if the account key provided is not base64 encoded. #5746

Open ahsonkhan opened 4 days ago

ahsonkhan commented 4 days ago

GitHub Copilot suggests the following code snippet when using Azure::Storage::Blobs.

#include <iostream>
#include <azure/storage/blobs.hpp>

int main()
{
  using namespace Azure::Storage::Blobs;

  auto blobServiceClient = BlobServiceClient::CreateFromConnectionString("DefaultEndpointsProtocol=https;AccountName=storageaccountname;AccountKey=storageaccountkey;EndpointSuffix=core.windows.net");

  auto containerClient = blobServiceClient.GetBlobContainerClient("containername");

  containerClient.CreateIfNotExists();
}

Take a note at the connection string passed in, particularly the AccountKey. The account key string is not a valid base64 encoded string.

Running the following sample results in an std::runtime_error with the exception message:

Unexpected end of Base64 encoded string.

https://github.com/Azure/azure-sdk-for-cpp/blob/55c241902d6897f9c8b9aee2668c76c5d0723ba4/sdk/core/azure-core/src/base64.cpp#L385-L389

After the connection string is parsed, the account key is passed directly to Base64Decode, assumed to be valid, and the exception bubbles up to the caller. It could be difficult for the end user to discover what caused this exception since the error message is not clear.

https://github.com/Azure/azure-sdk-for-cpp/blob/55c241902d6897f9c8b9aee2668c76c5d0723ba4/sdk/storage/azure-storage-common/src/shared_key_policy.cpp#L165-L167

Should the exception be wrapped in some other type (maybe RequestFailedException) with a more useful/context aware exception message? Or maybe the exception should occur at client construction time given the connection string is malformed.

Call Stack:

azure-storage-common.dll!Azure::Storage::_internal::SharedKeyPolicy::GetSignature(const Azure::Core::Http::Request & request) Line 83   C++
azure-storage-blobs.dll!Azure::Storage::_internal::SharedKeyPolicy::Send(Azure::Core::Http::Request & request, Azure::Core::Http::Policies::NextHttpPolicy nextPolicy, const Azure::Core::Context & context) Line 34    C++
[External Code] 
azure-storage-common.dll!Azure::Storage::_internal::StoragePerRetryPolicy::Send(Azure::Core::Http::Request & request, Azure::Core::Http::Policies::NextHttpPolicy nextPolicy, const Azure::Core::Context & context) Line 57 C++
[External Code] 
azure-storage-common.dll!Azure::Storage::_internal::StorageSwitchToSecondaryPolicy::Send(Azure::Core::Http::Request & request, Azure::Core::Http::Policies::NextHttpPolicy nextPolicy, const Azure::Core::Context & context) Line 36    C++
[External Code] 
azure-storage-blobs.dll!Azure::Core::Http::Policies::_internal::RequestIdPolicy::Send(Azure::Core::Http::Request & request, Azure::Core::Http::Policies::NextHttpPolicy nextPolicy, const Azure::Core::Context & context) Line 453  C++
[External Code] 
azure-storage-blobs.dll!Azure::Storage::_internal::StorageServiceVersionPolicy::Send(Azure::Core::Http::Request & request, Azure::Core::Http::Policies::NextHttpPolicy nextPolicy, const Azure::Core::Context & context) Line 34    C++
azure-storage-blobs.dll!Azure::Core::Http::_internal::HttpPipeline::Send(Azure::Core::Http::Request & request, const Azure::Core::Context & context) Line 230   C++
azure-storage-blobs.dll!Azure::Storage::Blobs::_detail::BlobContainerClient::Create(Azure::Core::Http::_internal::HttpPipeline & pipeline, const Azure::Core::Url & url, const Azure::Storage::Blobs::_detail::BlobContainerClient::CreateBlobContainerOptions & options, const Azure::Core::Context & context) Line 1415   C++
azure-storage-blobs.dll!Azure::Storage::Blobs::BlobContainerClient::Create(const Azure::Storage::Blobs::CreateBlobContainerOptions & options, const Azure::Core::Context & context) Line 256    C++
azure-storage-blobs.dll!Azure::Storage::Blobs::BlobContainerClient::CreateIfNotExists(const Azure::Storage::Blobs::CreateBlobContainerOptions & options, const Azure::Core::Context & context) Line 266 C++
SampleAzureSDK.exe!main() Line 18   C++