Skip to content

Conversation

@felipecrv
Copy link
Contributor

@felipecrv felipecrv commented Jan 4, 2024

Rationale for this change

  • Default credentials should be used by default.
  • There should be a way to connect to public containers without credentials (aka "anonymous credential").

What changes are included in this PR?

  • Sync ordering of declarations and definitions in the AzureOptions classs
  • Use default credentials even when ConfigureDefaultCredential() isn't explicitly called
  • Create clients when credential_kind_ is "anonymous" instead of returning an error

Are these changes tested?

By new and existing tests.

@github-actions
Copy link

github-actions bot commented Jan 4, 2024

⚠️ GitHub issue #39449 has been automatically assigned in GitHub to PR creator.

@felipecrv felipecrv marked this pull request as ready for review January 4, 2024 01:44
@felipecrv felipecrv requested a review from kou January 4, 2024 01:44
@felipecrv
Copy link
Contributor Author

@Tom-Newton

@felipecrv felipecrv added this to the 15.0.0 milestone Jan 4, 2024
Comment on lines +97 to +115
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we really need to change this. I would rather just keep kTokenCredential to cover all the credentials that are based on https://github.com/Azure/azure-sdk-for-cpp/blob/e5e675440b44ace7d7a9e7bc303f877c06b59ea5/sdk/core/azure-core/inc/azure/core/credentials/credentials.hpp#L68

Copy link
Contributor Author

@felipecrv felipecrv Jan 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need this to support Equals. Think of it as runtime type-information that describes which concrete implementation of TokenCredential is being used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides that, we would need to make a distinction between kDefault and all the others kToken at least (to support the implicit default behavior). It's clearer if we then make a distinction on all of them.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I would have done it differently but I don't feel strongly.

@github-actions github-actions bot added awaiting committer review Awaiting committer review and removed awaiting review Awaiting review labels Jan 4, 2024
@felipecrv felipecrv requested a review from Tom-Newton January 4, 2024 15:43
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about always creating Azure::Identity::DefaultAzureCredential instead of using mutable?

diff --git a/cpp/src/arrow/filesystem/azurefs.cc b/cpp/src/arrow/filesystem/azurefs.cc
index e98d56c73..5099cd25d 100644
--- a/cpp/src/arrow/filesystem/azurefs.cc
+++ b/cpp/src/arrow/filesystem/azurefs.cc
@@ -110,7 +110,6 @@ std::string AzureOptions::AccountDfsUrl(const std::string& account_name) const {
 
 Status AzureOptions::ConfigureDefaultCredential() {
   credential_kind_ = CredentialKind::kDefault;
-  token_credential_ = std::make_shared<Azure::Identity::DefaultAzureCredential>();
   return Status::OK();
 }
 
@@ -160,10 +159,9 @@ Result<std::unique_ptr<Blobs::BlobServiceClient>> AzureOptions::MakeBlobServiceC
     case CredentialKind::kAnonymous:
       return std::make_unique<Blobs::BlobServiceClient>(AccountBlobUrl(account_name));
     case CredentialKind::kDefault:
-      if (!token_credential_) {
-        token_credential_ = std::make_shared<Azure::Identity::DefaultAzureCredential>();
-      }
-      [[fallthrough]];
+      return std::make_unique<Blobs::BlobServiceClient>(
+          AccountBlobUrl(account_name),
+          std::make_shared<Azure::Identity::DefaultAzureCredential>());
     case CredentialKind::kClientSecret:
     case CredentialKind::kManagedIdentity:
     case CredentialKind::kWorkloadIdentity:
@@ -186,10 +184,9 @@ AzureOptions::MakeDataLakeServiceClient() const {
       return std::make_unique<DataLake::DataLakeServiceClient>(
           AccountDfsUrl(account_name));
     case CredentialKind::kDefault:
-      if (!token_credential_) {
-        token_credential_ = std::make_shared<Azure::Identity::DefaultAzureCredential>();
-      }
-      [[fallthrough]];
+      return std::make_unique<DataLake::DataLakeServiceClient>(
+          AccountDfsUrl(account_name),
+          std::make_shared<Azure::Identity::DefaultAzureCredential>());
     case CredentialKind::kClientSecret:
     case CredentialKind::kManagedIdentity:
     case CredentialKind::kWorkloadIdentity:
diff --git a/cpp/src/arrow/filesystem/azurefs.h b/cpp/src/arrow/filesystem/azurefs.h
index 55f89ba47..ba612f799 100644
--- a/cpp/src/arrow/filesystem/azurefs.h
+++ b/cpp/src/arrow/filesystem/azurefs.h
@@ -117,7 +117,7 @@ struct ARROW_EXPORT AzureOptions {
 
   std::shared_ptr<Azure::Storage::StorageSharedKeyCredential>
       storage_shared_key_credential_;
-  mutable std::shared_ptr<Azure::Core::Credentials::TokenCredential> token_credential_;
+  std::shared_ptr<Azure::Core::Credentials::TokenCredential> token_credential_;
 
  public:
   AzureOptions();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating DefaultAzureCredential is a lot of non-trivial work: env vars, multiple allocations and probes. We can't assume it to be a cheap operation. mutable is a common solution to lazily-initialized class members and this one has a very well-defined behavior: it's set once, then it doesn't change at all anymore.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And it's important that we do mutate it in ConfigureDefaultCredential because the user might want to explicitly initialize the credential immediately after doing something with the env variables used by the Azure SDK or right before unsetting the variables.

@github-actions github-actions bot added awaiting changes Awaiting changes awaiting change review Awaiting change review and removed awaiting committer review Awaiting committer review awaiting changes Awaiting changes labels Jan 4, 2024
@felipecrv felipecrv requested a review from kou January 5, 2024 01:36
@felipecrv
Copy link
Contributor Author

Rebased and force-pushed after the merge of my bigger PR related to directory semantics.

Copy link
Contributor

@Tom-Newton Tom-Newton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@felipecrv felipecrv merged commit 33c64ed into apache:main Jan 5, 2024
@felipecrv felipecrv removed the awaiting change review Awaiting change review label Jan 5, 2024
@felipecrv felipecrv deleted the azure_auth branch January 5, 2024 23:34
@conbench-apache-arrow
Copy link

After merging your PR, Conbench analyzed the 6 benchmarking runs that have been run so far on merge-commit 33c64ed.

There were no benchmark performance regressions. 🎉

The full Conbench report has more details. It also includes information about 4 possible false positives for unstable benchmarks that are known to sometimes produce them.

dgreiss pushed a commit to dgreiss/arrow that referenced this pull request Feb 19, 2024
…pport anonymous credentials explicitly (apache#39450)

### Rationale for this change

 - Default credentials should be used by default.
 - There should be a way to connect to public containers without credentials (aka "anonymous credential").

### What changes are included in this PR?

 - Sync ordering of declarations and definitions in the `AzureOptions` classs
 - Use default credentials even when `ConfigureDefaultCredential()` isn't explicitly called
 - Create clients when `credential_kind_` is "anonymous" instead of returning an error
 
### Are these changes tested?

By new and existing tests.
* Closes: apache#39449

Lead-authored-by: Felipe Oliveira Carvalho <[email protected]>
Co-authored-by: Sutou Kouhei <[email protected]>
Signed-off-by: Felipe Oliveira Carvalho <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[C++][FS] Use default Azure credentials implicitly and support anonymous credentials explictly

3 participants