Skip to content
Open
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
56 changes: 31 additions & 25 deletions google/cloud/internal/oauth2_minimal_iam_credentials_rest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,18 @@
#include "absl/strings/str_cat.h"
#include "absl/strings/str_join.h"
#include <nlohmann/json.hpp>
#include <string_view>

namespace google {
namespace cloud {
namespace oauth2_internal {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
namespace {

std::string IamCredentialsEndpoint(
StatusOr<std::string> const& universe_domain) {
return absl::StrCat("https://iamcredentials.",
universe_domain ? *universe_domain : "googleapis.com");
StatusOr<std::string> IamCredentialsEndpoint(
StatusOr<std::string> universe_domain) {
if (!universe_domain) return std::move(universe_domain).status();
return absl::StrCat("https://iamcredentials.", *universe_domain);
}
Comment on lines +39 to 43

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The universe_domain parameter is not a sink parameter because its value is only read to construct the endpoint URL and is not stored or moved into another object on the happy path. Passing it by const& avoids unnecessary moves or copies of StatusOr<std::string> when the function is called.

StatusOr<std::string> IamCredentialsEndpoint(
    StatusOr<std::string> const& universe_domain) {
  if (!universe_domain) return universe_domain.status();
  return absl::StrCat("https://iamcredentials.", *universe_domain);
}
References
  1. Scrutinize copies of non-fundamental C++ types. Since universe_domain is not a sink parameter, passing it by const reference avoids unnecessary moves/copies on the happy path. (link)


} // namespace
Expand All @@ -59,13 +60,15 @@ MinimalIamCredentialsRestStub::MinimalIamCredentialsRestStub(
StatusOr<google::cloud::AccessToken>
MinimalIamCredentialsRestStub::GenerateAccessToken(
GenerateAccessTokenRequest const& request) {
auto authorization_header =
StatusOr<std::string> path = MakeRequestPath(request);
if (!path) return std::move(path).status();
StatusOr<rest_internal::HttpHeader> authorization_header =
credentials_->Authorization(std::chrono::system_clock::now());
if (!authorization_header) return std::move(authorization_header).status();
rest_internal::RestRequest rest_request;
rest_request.AddHeader(*std::move(authorization_header));
rest_request.AddHeader("Content-Type", "application/json");
rest_request.SetPath(MakeRequestPath(request));
rest_request.SetPath(*std::move(path));
nlohmann::json payload{
{"delegates", request.delegates},
{"scope", request.scopes},
Expand All @@ -84,21 +87,27 @@ MinimalIamCredentialsRestStub::GenerateAccessToken(
{"serviceAccount", request.service_account}}));
}

std::string MinimalIamCredentialsRestStub::MakeRequestPath(
StatusOr<std::string> MinimalIamCredentialsRestStub::MakeRequestPath(
GenerateAccessTokenRequest const& request) const {
return absl::StrCat(IamCredentialsEndpoint(universe_domain(Options{})),
"/v1/projects/-/serviceAccounts/",
StatusOr<std::string> endpoint =
IamCredentialsEndpoint(universe_domain(options_));
if (!endpoint) return std::move(endpoint).status();
return absl::StrCat(*endpoint, "/v1/projects/-/serviceAccounts/",
request.service_account, ":generateAccessToken");
}

StatusOr<AllowedLocationsResponse>
MinimalIamCredentialsRestStub::AllowedLocationsHelper(std::string path) {
auto authorization_header =
MinimalIamCredentialsRestStub::AllowedLocationsHelper(
std::string_view relative_path) {
StatusOr<std::string> endpoint =
IamCredentialsEndpoint(universe_domain(options_));
if (!endpoint) return std::move(endpoint).status();
StatusOr<rest_internal::HttpHeader> authorization_header =
credentials_->Authorization(std::chrono::system_clock::now());
if (!authorization_header) return std::move(authorization_header).status();
rest_internal::RestRequest rest_request;
rest_request.AddHeader(*std::move(authorization_header));
rest_request.SetPath(std::move(path));
rest_request.SetPath(absl::StrCat(*endpoint, relative_path));

auto client = client_factory_(options_);
rest_internal::RestContext context;
Expand All @@ -115,29 +124,26 @@ MinimalIamCredentialsRestStub::AllowedLocationsHelper(std::string path) {
StatusOr<AllowedLocationsResponse>
MinimalIamCredentialsRestStub::AllowedLocations(
ServiceAccountAllowedLocationsRequest const& request) {
auto path = absl::StrCat(IamCredentialsEndpoint(universe_domain(Options{})),
"/v1/projects/-/serviceAccounts/",
request.service_account_email, "/allowedLocations");
return AllowedLocationsHelper(std::move(path));
return AllowedLocationsHelper(absl::StrCat("/v1/projects/-/serviceAccounts/",
request.service_account_email,
"/allowedLocations"));
}

StatusOr<AllowedLocationsResponse>
MinimalIamCredentialsRestStub::AllowedLocations(
WorkloadIdentityAllowedLocationsRequest const& request) {
auto path = absl::StrCat(IamCredentialsEndpoint(universe_domain(Options{})),
"/v1/projects/", request.project_id,
"/locations/global/workloadIdentityPools/",
request.pool_id, "/allowedLocations");
return AllowedLocationsHelper(std::move(path));
return AllowedLocationsHelper(
absl::StrCat("/v1/projects/", request.project_id,
"/locations/global/workloadIdentityPools/", request.pool_id,
"/allowedLocations"));
}

StatusOr<AllowedLocationsResponse>
MinimalIamCredentialsRestStub::AllowedLocations(
WorkforceIdentityAllowedLocationsRequest const& request) {
auto path = absl::StrCat(IamCredentialsEndpoint(universe_domain(Options{})),
"/v1/locations/global/workforcePools/",
request.pool_id, "/allowedLocations");
return AllowedLocationsHelper(std::move(path));
return AllowedLocationsHelper(
absl::StrCat("/v1/locations/global/workforcePools/", request.pool_id,
"/allowedLocations"));
}

MinimalIamCredentialsRestLogging::MinimalIamCredentialsRestLogging(
Expand Down
7 changes: 5 additions & 2 deletions google/cloud/internal/oauth2_minimal_iam_credentials_rest.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "google/cloud/status_or.h"
#include <chrono>
#include <string>
#include <string_view>
#include <vector>

namespace google {
Expand Down Expand Up @@ -107,8 +108,10 @@ class MinimalIamCredentialsRestStub : public MinimalIamCredentialsRest {
}

private:
std::string MakeRequestPath(GenerateAccessTokenRequest const& request) const;
StatusOr<AllowedLocationsResponse> AllowedLocationsHelper(std::string path);
StatusOr<std::string> MakeRequestPath(
GenerateAccessTokenRequest const& request) const;
StatusOr<AllowedLocationsResponse> AllowedLocationsHelper(
std::string_view relative_path);

std::shared_ptr<oauth2_internal::Credentials> credentials_;
Options options_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ TEST(MinimalIamCredentialsRestTest, GenerateAccessTokenWithUniverseDomain) {

TEST(MinimalIamCredentialsRestTest, GenerateAccessTokenCredentialFailure) {
auto mock_credentials = std::make_shared<MockCredentials>();
EXPECT_CALL(*mock_credentials, universe_domain)
.WillOnce(Return(StatusOr<std::string>{"googleapis.com"}));
EXPECT_CALL(*mock_credentials, GetToken).WillOnce([] {
return Status(StatusCode::kPermissionDenied, "Permission Denied");
});
Expand All @@ -329,10 +331,33 @@ TEST(MinimalIamCredentialsRestTest, GenerateAccessTokenCredentialFailure) {
auto stub = MinimalIamCredentialsRestStub(
std::move(mock_credentials), {}, mock_client_factory.AsStdFunction());
GenerateAccessTokenRequest request;
auto access_token = stub.GenerateAccessToken(request);
StatusOr<AccessToken> access_token = stub.GenerateAccessToken(request);
EXPECT_THAT(access_token, StatusIs(StatusCode::kPermissionDenied));
}

TEST(MinimalIamCredentialsRestTest, GenerateAccessTokenUniverseDomainFailure) {
struct TestOption {
using Type = std::string;
};
auto mock_credentials = std::make_shared<MockCredentials>();
EXPECT_CALL(*mock_credentials, universe_domain)
.WillOnce([](Options const& options) -> StatusOr<std::string> {
EXPECT_THAT(options.get<TestOption>(), Eq("forwarded"));
return Status(StatusCode::kUnavailable, "MDS unavailable");
});
EXPECT_CALL(*mock_credentials, GetToken).Times(0);
MockHttpClientFactory mock_client_factory;
EXPECT_CALL(mock_client_factory, Call).Times(0);
auto stub = MinimalIamCredentialsRestStub(
std::move(mock_credentials), Options{}.set<TestOption>("forwarded"),
mock_client_factory.AsStdFunction());
GenerateAccessTokenRequest request;
request.service_account = "foo@somewhere.com";
StatusOr<AccessToken> access_token = stub.GenerateAccessToken(request);
EXPECT_THAT(access_token,
StatusIs(StatusCode::kUnavailable, HasSubstr("MDS unavailable")));
}

TEST(MinimalIamCredentialsRestTest, GetUniverseDomainFromCredentials) {
auto constexpr kExpectedUniverseDomain = "my-ud.net";
auto mock_credentials = std::make_shared<MockCredentials>();
Expand Down Expand Up @@ -364,6 +389,70 @@ TEST(MinimalIamCredentialsRestTest, AllowedLocationsAuthorizationFailure) {
EXPECT_THAT(access_token, StatusIs(StatusCode::kPermissionDenied));
}

TEST(MinimalIamCredentialsRestTest,
ServiceAccountAllowedLocationsUniverseDomainFailure) {
struct TestOption {
using Type = std::string;
};
auto mock_credentials = std::make_shared<MockCredentials>();
EXPECT_CALL(*mock_credentials, universe_domain)
.WillOnce([](Options const& options) -> StatusOr<std::string> {
EXPECT_THAT(options.get<TestOption>(), Eq("forwarded"));
return Status(StatusCode::kUnavailable, "MDS unavailable");
});
EXPECT_CALL(*mock_credentials, GetToken).Times(0);
MockHttpClientFactory mock_client_factory;
EXPECT_CALL(mock_client_factory, Call).Times(0);
auto stub = MinimalIamCredentialsRestStub(
std::move(mock_credentials), Options{}.set<TestOption>("forwarded"),
mock_client_factory.AsStdFunction());
ServiceAccountAllowedLocationsRequest request;
request.service_account_email = "foo@somewhere.com";
StatusOr<AllowedLocationsResponse> allowed_locations =
stub.AllowedLocations(request);
EXPECT_THAT(allowed_locations,
StatusIs(StatusCode::kUnavailable, HasSubstr("MDS unavailable")));
}

TEST(MinimalIamCredentialsRestTest,
WorkloadIdentityAllowedLocationsUniverseDomainFailure) {
auto mock_credentials = std::make_shared<MockCredentials>();
EXPECT_CALL(*mock_credentials, universe_domain)
.WillOnce(Return(Status(StatusCode::kUnavailable, "MDS unavailable")));
EXPECT_CALL(*mock_credentials, GetToken).Times(0);
MockHttpClientFactory mock_client_factory;
EXPECT_CALL(mock_client_factory, Call).Times(0);
auto stub =
MinimalIamCredentialsRestStub(std::move(mock_credentials), Options{},
mock_client_factory.AsStdFunction());
WorkloadIdentityAllowedLocationsRequest request;
request.project_id = "my-project";
request.pool_id = "my-pool";
StatusOr<AllowedLocationsResponse> allowed_locations =
stub.AllowedLocations(request);
EXPECT_THAT(allowed_locations,
StatusIs(StatusCode::kUnavailable, HasSubstr("MDS unavailable")));
}

TEST(MinimalIamCredentialsRestTest,
WorkforceIdentityAllowedLocationsUniverseDomainFailure) {
auto mock_credentials = std::make_shared<MockCredentials>();
EXPECT_CALL(*mock_credentials, universe_domain)
.WillOnce(Return(Status(StatusCode::kUnavailable, "MDS unavailable")));
EXPECT_CALL(*mock_credentials, GetToken).Times(0);
MockHttpClientFactory mock_client_factory;
EXPECT_CALL(mock_client_factory, Call).Times(0);
auto stub =
MinimalIamCredentialsRestStub(std::move(mock_credentials), Options{},
mock_client_factory.AsStdFunction());
WorkforceIdentityAllowedLocationsRequest request;
request.pool_id = "my-pool";
StatusOr<AllowedLocationsResponse> allowed_locations =
stub.AllowedLocations(request);
EXPECT_THAT(allowed_locations,
StatusIs(StatusCode::kUnavailable, HasSubstr("MDS unavailable")));
}

// TODO(#16177): Update these tests to compile with MSVC.
#ifndef _WIN32

Expand Down
Loading