Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Identity: Improve diagnosability #4744

Merged
merged 10 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions sdk/identity/azure-identity/src/azure_cli_credential.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "private/token_credential_impl.hpp"

#include <azure/core/internal/environment.hpp>
#include <azure/core/internal/json/json.hpp>
#include <azure/core/internal/strings.hpp>
#include <azure/core/internal/unique_handle.hpp>
#include <azure/core/platform.hpp>
Expand Down Expand Up @@ -47,6 +48,7 @@ using Azure::Core::Credentials::AccessToken;
using Azure::Core::Credentials::AuthenticationException;
using Azure::Core::Credentials::TokenCredentialOptions;
using Azure::Core::Credentials::TokenRequestContext;
using Azure::Core::Json::_internal::json;
using Azure::Identity::AzureCliCredentialOptions;
using Azure::Identity::_detail::IdentityLog;
using Azure::Identity::_detail::TenantIdResolver;
Expand Down Expand Up @@ -160,10 +162,18 @@ AccessToken AzureCliCredential::GetToken(
return TokenCredentialImpl::ParseToken(
azCliResult, "accessToken", "expiresIn", "expiresOn");
}
catch (std::exception const&)
catch (json::exception const&)
{
// Throw the az command output (error message)
// limited to 250 characters (250 has no special meaning).
// json::exception gets thrown when a string we provided for parsing is not a json object.
// It should not get thrown if the string is a valid JSON, but there are specific problems
// with the token JSON object - missing property, failure to parse a specific property etc.
// I.e. this means that the az commnd has rather printed some error message
// (such as "ERROR: Please run az login to setup account.") instead of producing a JSON
// object output. In this case, we want the exception to be thrown with the output from the
// command (which is likely the error message) and not with the details of the exception
// that was thrown from ParseToken() (which most likely will be "Unexpected token ...").
// So, we limit the az command output (error message) limited to 250 characters so it is not
// too long, and throw that.
throw std::runtime_error(azCliResult.substr(0, 250));
}
}
Expand Down
Loading