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

ClientCertificateCredential: better exception message for unsupported file extensions #5197

Merged
merged 8 commits into from
Nov 28, 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
1 change: 1 addition & 0 deletions .vscode/cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"ABFS",
"ABNF",
"adamdebreceni",
"adfs",
"Adls",
"ahojnnes",
"ahsonkhan",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <azure/core/base64.hpp>
#include <azure/core/datetime.hpp>
#include <azure/core/internal/cryptography/sha_hash.hpp>
#include <azure/core/internal/strings.hpp>
#include <azure/core/io/body_stream.hpp>
#include <azure/core/platform.hpp>
#include <azure/core/uuid.hpp>
Expand Down Expand Up @@ -399,16 +400,33 @@ ClientCertificateCredential::ClientCertificateCredential(
CertificateThumbprint mdVec;
try
{
if (clientCertificatePath.empty())
{
throw AuthenticationException("Certificate file path is empty.");
}
antkmsft marked this conversation as resolved.
Show resolved Hide resolved

using Azure::Core::_internal::StringExtensions;
std::string const PemExtension = ".pem";
auto const certFileExtensionStart = clientCertificatePath.find_last_of('.');
auto const certFileExtension = certFileExtensionStart != std::string::npos
? clientCertificatePath.substr(certFileExtensionStart)
: std::string{};
antkmsft marked this conversation as resolved.
Show resolved Hide resolved

if (!StringExtensions::LocaleInvariantCaseInsensitiveEqual(certFileExtension, PemExtension))
{
throw AuthenticationException(
"Certificate format"
+ (certFileExtension.empty() ? " " : " ('" + certFileExtension + "') ")
+ "is not supported. Please convert your certificate to '" + PemExtension + "'.");
}

std::tie(mdVec, m_pkey) = ReadPemCertificate(clientCertificatePath);
}
catch (AuthenticationException&)
{
throw;
}
catch (std::exception& e)
catch (std::exception const& e)
{
// WIL does not throw AuthenticationException.
antkmsft marked this conversation as resolved.
Show resolved Hide resolved
throw AuthenticationException(e.what());
throw AuthenticationException(
std::string("Identity: ClientCertificateCredential: ") + e.what());
}

// Get thumbprint as hex string:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,77 @@ TEST_P(GetCredentialName, )
EXPECT_EQ(cred.GetCredentialName(), "ClientCertificateCredential");
}

TEST(ClientCertificateCredential, UnsupportedExtension)
{
try
{
ClientCertificateCredential const cred(
"01234567-89ab-cdef-fedc-ba8976543210", "fedcba98-7654-3210-0123-456789abcdef", "file.pfx");

EXPECT_TRUE(
!"ClientCertificateCredential with unsupported extension (.pfx) is supposed to throw.");
antkmsft marked this conversation as resolved.
Show resolved Hide resolved
}
catch (Azure::Core::Credentials::AuthenticationException const& ex)
{
EXPECT_EQ(
ex.what(),
std::string("Identity: ClientCertificateCredential: "
"Certificate format ('.pfx') is not supported. "
"Please convert your certificate to '.pem'."));
}

try
{
ClientCertificateCredential const cred(
"01234567-89ab-cdef-fedc-ba8976543210",
"fedcba98-7654-3210-0123-456789abcdef",
"file.cert");

EXPECT_TRUE(
!"ClientCertificateCredential with unsupported extension (.cert) is supposed to throw.");
}
catch (Azure::Core::Credentials::AuthenticationException const& ex)
{
EXPECT_EQ(
ex.what(),
std::string("Identity: ClientCertificateCredential: "
"Certificate format ('.cert') is not supported. "
"Please convert your certificate to '.pem'."));
}

try
{
ClientCertificateCredential const cred(
"01234567-89ab-cdef-fedc-ba8976543210",
"fedcba98-7654-3210-0123-456789abcdef",
"noextension");

EXPECT_TRUE(!"ClientCertificateCredential without an extension is supposed to throw.");
}
catch (Azure::Core::Credentials::AuthenticationException const& ex)
{
EXPECT_EQ(
ex.what(),
std::string("Identity: ClientCertificateCredential: "
"Certificate format is not supported. "
"Please convert your certificate to '.pem'."));
}

try
{
ClientCertificateCredential const cred(
"01234567-89ab-cdef-fedc-ba8976543210", "fedcba98-7654-3210-0123-456789abcdef", "");

EXPECT_TRUE(!"ClientCertificateCredential with an empty path is supposed to throw.");
}
catch (Azure::Core::Credentials::AuthenticationException const& ex)
{
EXPECT_EQ(
ex.what(),
std::string("Identity: ClientCertificateCredential: Certificate file path is empty."));
}
}

TEST(ClientCertificateCredential, GetOptionsFromEnvironment)
{
{
Expand Down