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

Add ADFS support for ClientSecretCredential #1947

Merged
merged 24 commits into from
Mar 30, 2021
Merged

Add ADFS support for ClientSecretCredential #1947

merged 24 commits into from
Mar 30, 2021

Conversation

antkmsft
Copy link
Member

@antkmsft antkmsft commented Mar 19, 2021

Closes #345.

@antkmsft antkmsft added this to the [2021] April milestone Mar 19, 2021
@antkmsft antkmsft self-assigned this Mar 19, 2021
Copy link
Member

@RickWinter RickWinter left a comment

Choose a reason for hiding this comment

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

This needs test cases added.

@antkmsft antkmsft requested a review from RickWinter March 24, 2021 00:18
@antkmsft antkmsft requested a review from RickWinter March 26, 2021 20:41
@ghost
Copy link

ghost commented Mar 26, 2021

Hello @antkmsft!

Because this pull request has the auto-merge label, I will be glad to assist with helping to merge this pull request once all check-in policies pass.

p.s. you can customize the way I help with merging this pull request, such as holding this pull request until a specific person approves. Simply @mention me (@msftbot) and give me an instruction to get started! Learn more here.

@antkmsft antkmsft dismissed RickWinter’s stale review March 30, 2021 00:26

I implemented all of the @RickWinter's requests, and I haven't heard from him since then. If there's anything more, let me know.

@antkmsft antkmsft merged commit b606ff6 into Azure:master Mar 30, 2021
sdk/core/azure-core/src/datetime.cpp Show resolved Hide resolved
/**
* @brief Defines options for #Azure::Core::Credentials::TokenCredential.
*/
struct TokenCredentialOptions : public Azure::Core::_internal::ClientOptions
Copy link
Member

Choose a reason for hiding this comment

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

Needs CL entry.

Copy link
Member Author

Choose a reason for hiding this comment

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

It is not a breaking change, and has the default value. All the code you could compile before can still compile now.

Copy link
Member

@ahsonkhan ahsonkhan Mar 30, 2021

Choose a reason for hiding this comment

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

I understand. It's a new type/new feature (which are also added to CL) :)

: m_tenantId(std::move(tenantId)), m_clientId(std::move(clientId)),
m_clientSecret(std::move(clientSecret)), m_options(std::move(options))
{
}

/**
* @brief Construct a Client Secret credential.
Copy link
Member

Choose a reason for hiding this comment

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

This is an odd casing choice. Was it intentional?

: m_tenantId(std::move(tenantId)), m_clientId(std::move(clientId)),
m_clientSecret(std::move(clientSecret))
{
static_cast<Azure::Core::Credentials::TokenCredentialOptions&>(m_options) = options;
Copy link
Member

Choose a reason for hiding this comment

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

nit: Add a comment explaining why this cast is OK.

@@ -9,6 +9,40 @@
#include <chrono>
#include <sstream>

namespace {
// Assumes !scopes.empty()
std::string FormatScopes(std::vector<std::string> const& scopes, bool asResource)
Copy link
Member

Choose a reason for hiding this comment

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

What does asResource mean?

Copy link
Member Author

Choose a reason for hiding this comment

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

it is internal function. You can only have single scope as resource, and when you want to build resource, you don't add scope suffix.

{
auto resource = scopes[0];
constexpr char suffix[] = "/.default";
constexpr int suffixLen = sizeof(suffix) - 1;
Copy link
Member

Choose a reason for hiding this comment

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

Why are we doing -1. Add a comment.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think this necessarily requires a comment, this is the part of the language - when an array is initialized from a literal, \0 is added to the array - https://en.cppreference.com/w/c/language/array_initialization.

Copy link
Member

Choose a reason for hiding this comment

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

I don't think this necessarily requires a comment

Source comments are rarely necessary. It's just meant for helpful hints for future maintainability when code changes, moves around. What happens if I change line 19 tomorrow to std::string, now line 20 is broken, if I don't update -1, correct?

It's up to your discretion, as the PR author, what's clear/obvious by just reading the code.

Copy link
Member Author

Choose a reason for hiding this comment

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

I can add it, but if we change it to std::string, suffixLen should be removed entirely, it is no longer necessary. So, the only context in which it makes sense, is when suffix is a C-string, so it is 0-terminated, so that "- 1" is needed.

But even without the "-1", sizeof(std::string()) is broken. It will be the size of the std::string class, not the length of the string. length() should be used.

}

auto scopesIter = scopes.begin();
auto scopesStr = Azure::Core::Url::Encode(*scopesIter);
Copy link
Member

Choose a reason for hiding this comment

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

nit: re-add the comment here, that we assume scopes.size() > 0

ClientSecretCredentialOptions options;
options.AuthorityHost = authority;
ClientSecretCredentialOptions clientSecretCredentialOptions;
static_cast<Core::_internal::ClientOptions&>(clientSecretCredentialOptions) = options;
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be TokenCredentialOptions?

Suggested change
static_cast<Core::_internal::ClientOptions&>(clientSecretCredentialOptions) = options;
static_cast<Core::Credentials::TokenCredentialOptions&>(clientSecretCredentialOptions) = options;

What's this line doing? Is it only copying over the fields into the local clientSecretCredentialOptions that come from the base type options? Does it leave the other fields alone?

Also, add comment why static cast is OK here.

Copy link
Member

@ahsonkhan ahsonkhan Mar 30, 2021

Choose a reason for hiding this comment

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

@antkmsft does this need to be fixed for correctness?

Copy link
Member Author

Choose a reason for hiding this comment

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

For now we are fine, but yes, it needs fixing - I'll grab this plus a few other comments and create a follow-up PR.

}
else
{
m_credentialImpl.reset(new ClientSecretCredential(tenantId, clientId, clientSecret));
m_credentialImpl.reset(
new ClientSecretCredential(tenantId, clientId, clientSecret, options));
}
}
// TODO: These credential types are not implemented. Uncomment when implemented.
Copy link
Member

@ahsonkhan ahsonkhan Mar 30, 2021

Choose a reason for hiding this comment

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

Since you are hereand modifying this code already, remove this TODO comment from source and file an issue.

}
} // namespace

TEST(ClientSecretCredential, Regular)
Copy link
Member

Choose a reason for hiding this comment

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

How can I see the test coverage for this change and azure-identity overall?

It's not visible here: https://dev.azure.com/azure-sdk/public/_build/results?buildId=808213&view=results

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't know how the code coverage integration works, but if you don't see it, it most likely was not enable for identity. We need a work item to enable it.

@@ -46,6 +46,7 @@ set(
${CURL_TRANSPORT_ADAPTER_INC}
${WIN_TRANSPORT_ADAPTER_INC}
inc/azure/core/credentials/credentials.hpp
inc/azure/core/credentials/token_credential_options.hpp
Copy link
Member

Choose a reason for hiding this comment

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

We need to include these headers to core.hpp and add tests to the simplified header.
Fixed in #2070

@antkmsft antkmsft deleted the adfs branch June 9, 2021 21:32
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.

Add ADFS / Azure Stack Support
6 participants