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
5 changes: 3 additions & 2 deletions google/cloud/credentials.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,14 @@ std::shared_ptr<Credentials> MakeImpersonateServiceAccountCredentials(
std::shared_ptr<Credentials> MakeServiceAccountCredentials(
std::string json_object, Options opts) {
return std::make_shared<internal::ServiceAccountConfig>(
std::move(json_object), absl::nullopt, std::move(opts));
internal::ServiceAccountJsonObject{std::move(json_object)},
std::move(opts));
}

std::shared_ptr<Credentials> MakeServiceAccountCredentialsFromFile(
std::string const& file_path, Options opts) {
return std::make_shared<internal::ServiceAccountConfig>(
absl::nullopt, file_path, std::move(opts));
internal::ServiceAccountFilePath{file_path}, std::move(opts));
}

std::shared_ptr<Credentials> MakeExternalAccountCredentials(
Expand Down
13 changes: 8 additions & 5 deletions google/cloud/internal/credentials_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,14 @@ std::vector<std::string> const& ImpersonateServiceAccountConfig::delegates()
return options_.get<DelegatesOption>();
}

ServiceAccountConfig::ServiceAccountConfig(
absl::optional<std::string> json_object,
absl::optional<std::string> file_path, Options opts)
: json_object_(std::move(json_object)),
file_path_(std::move(file_path)),
ServiceAccountConfig::ServiceAccountConfig(ServiceAccountFilePath path,
Options opts)
: file_path_(std::move(path.value)),
options_(PopulateAuthOptions(std::move(opts))) {}

ServiceAccountConfig::ServiceAccountConfig(ServiceAccountJsonObject json,
Options opts)
: json_object_(std::move(json.value)),
options_(PopulateAuthOptions(std::move(opts))) {}

ExternalAccountConfig::ExternalAccountConfig(std::string json_object,
Expand Down
19 changes: 14 additions & 5 deletions google/cloud/internal/credentials_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,22 @@ class ImpersonateServiceAccountConfig : public Credentials {
Options options_;
};

struct ServiceAccountFilePath {
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 Google C++ Style Guide requires a comment for every class and struct describing its purpose. This struct is used to disambiguate the ServiceAccountConfig constructors.

/// A wrapper for a service account file path.
struct ServiceAccountFilePath {
References
  1. Every class should have a comment that describes what it is and what it should be used for. (link)

std::string value;
explicit ServiceAccountFilePath(std::string p) : value(std::move(p)) {}
};

struct ServiceAccountJsonObject {
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 Google C++ Style Guide requires a comment for every class and struct describing its purpose. This struct is used to disambiguate the ServiceAccountConfig constructors.

/// A wrapper for a service account JSON object string.
struct ServiceAccountJsonObject {
References
  1. Every class should have a comment that describes what it is and what it should be used for. (link)

std::string value;
explicit ServiceAccountJsonObject(std::string j) : value(std::move(j)) {}
};

class ServiceAccountConfig : public Credentials {
public:
// Only one of json_object or file_path should have a value.
// TODO(#15886): Use the C++ type system to make better constructors that
// enforces this comment.
ServiceAccountConfig(absl::optional<std::string> json_object,
absl::optional<std::string> file_path, Options opts);
// Just declarations ending with a semicolon
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

This comment is redundant and does not provide useful information about the class's contract or behavior. Consider removing it or replacing it with a meaningful description of the constructors.

  /// Constructs a configuration from a service account file path.

explicit ServiceAccountConfig(ServiceAccountFilePath path, Options opts);

explicit ServiceAccountConfig(ServiceAccountJsonObject json, Options opts);

absl::optional<std::string> const& json_object() const {
return json_object_;
Expand Down
3 changes: 3 additions & 0 deletions google/cloud/internal/credentials_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,14 @@ TEST(Credentials, ImpersonateServiceAccountCredentialsDefaultWithOptions) {
}

TEST(Credentials, ServiceAccount) {
// If MakeServiceAccountCredentials treats this string as a JSON object:
auto credentials = MakeServiceAccountCredentials("test-only-invalid");
TestCredentialsVisitor visitor;
CredentialsVisitor::dispatch(*credentials, visitor);
ASSERT_EQ("ServiceAccountConfig", visitor.name);
// Update: Ensure the correct field is populated and the other is empty
EXPECT_EQ("test-only-invalid", visitor.json_object);
EXPECT_EQ(absl::nullopt, visitor.file_path);
}

TEST(Credentials, ExternalAccount) {
Expand Down
5 changes: 3 additions & 2 deletions google/cloud/internal/unified_rest_credentials_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,9 @@ TEST(UnifiedRestCredentialsTest, ServiceAccount) {
MockClientFactory client_factory;
EXPECT_CALL(client_factory, Call).Times(0);

auto const config =
internal::ServiceAccountConfig(contents.dump(), absl::nullopt, Options{});
auto const config = internal::ServiceAccountConfig(
internal::ServiceAccountJsonObject{contents.dump()}, Options{});

auto credentials = MapCredentials(config, client_factory.AsStdFunction());
auto access_token = credentials->GetToken(now);
ASSERT_STATUS_OK(access_token);
Expand Down
Loading