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

feat: support logging for unified Rest credentials #10412

Merged
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: 16 additions & 0 deletions google/cloud/internal/oauth2_decorate_credentials.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,28 @@
#include "google/cloud/internal/oauth2_decorate_credentials.h"
#include "google/cloud/common_options.h"
#include "google/cloud/internal/oauth2_cached_credentials.h"
#include "google/cloud/internal/oauth2_logging_credentials.h"

namespace google {
namespace cloud {
namespace oauth2_internal {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN

std::shared_ptr<oauth2_internal::Credentials> Decorate(
std::shared_ptr<oauth2_internal::Credentials> impl, Options const& opts) {
impl = WithLogging(std::move(impl), opts, "refresh");
impl = WithCaching(std::move(impl));
return WithLogging(std::move(impl), opts, "cached");
}

std::shared_ptr<oauth2_internal::Credentials> WithLogging(
std::shared_ptr<oauth2_internal::Credentials> impl, Options const& opts,
std::string stage) {
if (opts.get<TracingComponentsOption>().count("auth") == 0) return impl;
return std::make_shared<oauth2_internal::LoggingCredentials>(
std::move(stage), TracingOptions(), std::move(impl));
}

std::shared_ptr<oauth2_internal::Credentials> WithCaching(
std::shared_ptr<oauth2_internal::Credentials> impl) {
return std::make_shared<oauth2_internal::CachedCredentials>(std::move(impl));
Expand Down
11 changes: 11 additions & 0 deletions google/cloud/internal/oauth2_decorate_credentials.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_INTERNAL_OAUTH2_DECORATE_CREDENTIALS_H

#include "google/cloud/internal/oauth2_credentials.h"
#include "google/cloud/options.h"
#include "google/cloud/version.h"
#include <memory>
#include <string>
Expand All @@ -25,6 +26,16 @@ namespace cloud {
namespace oauth2_internal {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN

/// Add a full stack of logging (if requested in @p opts) and caching decorators
/// to the credentials.
std::shared_ptr<oauth2_internal::Credentials> Decorate(
std::shared_ptr<oauth2_internal::Credentials> impl, Options const& opts);

/// Add only a logging decorator to the credentials if requested in @p opts
std::shared_ptr<oauth2_internal::Credentials> WithLogging(
std::shared_ptr<oauth2_internal::Credentials> impl, Options const& opts,
std::string stage);

/// Add only a caching decorator to the credentials.
std::shared_ptr<oauth2_internal::Credentials> WithCaching(
std::shared_ptr<oauth2_internal::Credentials> impl);
Expand Down
18 changes: 11 additions & 7 deletions google/cloud/internal/unified_rest_credentials.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

#include "google/cloud/internal/unified_rest_credentials.h"
#include "google/cloud/common_options.h"
#include "google/cloud/internal/make_jwt_assertion.h"
#include "google/cloud/internal/oauth2_access_token_credentials.h"
#include "google/cloud/internal/oauth2_anonymous_credentials.h"
Expand All @@ -21,6 +22,7 @@
#include "google/cloud/internal/oauth2_external_account_credentials.h"
#include "google/cloud/internal/oauth2_google_credentials.h"
#include "google/cloud/internal/oauth2_impersonate_service_account_credentials.h"
#include "google/cloud/internal/oauth2_logging_credentials.h"
#include "google/cloud/internal/oauth2_service_account_credentials.h"

namespace google {
Expand All @@ -36,7 +38,7 @@ using ::google::cloud::internal::GoogleDefaultCredentialsConfig;
using ::google::cloud::internal::ImpersonateServiceAccountConfig;
using ::google::cloud::internal::InsecureCredentialsConfig;
using ::google::cloud::internal::ServiceAccountConfig;
using ::google::cloud::oauth2_internal::WithCaching;
using ::google::cloud::oauth2_internal::Decorate;

std::shared_ptr<oauth2_internal::Credentials> MakeErrorCredentials(
Status status) {
Expand Down Expand Up @@ -77,7 +79,7 @@ std::shared_ptr<oauth2_internal::Credentials> MapCredentials(
google::cloud::oauth2_internal::GoogleDefaultCredentials(
cfg.options());
if (credentials) {
result = WithCaching(*std::move(credentials));
result = Decorate(*std::move(credentials), cfg.options());
return;
}
result = MakeErrorCredentials(std::move(credentials).status());
Expand All @@ -91,12 +93,13 @@ std::shared_ptr<oauth2_internal::Credentials> MapCredentials(
void visit(ImpersonateServiceAccountConfig& cfg) override {
result = std::make_shared<
oauth2_internal::ImpersonateServiceAccountCredentials>(cfg);
result = WithCaching(std::move(result));
result = Decorate(std::move(result), cfg.options());
}

void visit(ServiceAccountConfig& cfg) override {
result = WithCaching(CreateServiceAccountCredentialsFromJsonContents(
cfg.json_object(), cfg.options()));
result = Decorate(CreateServiceAccountCredentialsFromJsonContents(
cfg.json_object(), cfg.options()),
cfg.options());
}

void visit(ExternalAccountConfig& cfg) override {
Expand All @@ -111,9 +114,10 @@ std::shared_ptr<oauth2_internal::Credentials> MapCredentials(
return rest_internal::MakeDefaultRestClient(std::string{},
std::move(options));
};
result = WithCaching(
result = Decorate(
std::make_shared<oauth2_internal::ExternalAccountCredentials>(
*std::move(info), client_factory, cfg.options()));
*std::move(info), client_factory, cfg.options()),
cfg.options());
}
} visitor;

Expand Down
8 changes: 4 additions & 4 deletions google/cloud/storage/internal/unified_rest_credentials.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ using ::google::cloud::internal::GoogleDefaultCredentialsConfig;
using ::google::cloud::internal::ImpersonateServiceAccountConfig;
using ::google::cloud::internal::InsecureCredentialsConfig;
using ::google::cloud::internal::ServiceAccountConfig;
using ::google::cloud::oauth2_internal::WithCaching;
using ::google::cloud::oauth2_internal::Decorate;

std::shared_ptr<oauth2::Credentials> MakeErrorCredentials(Status status) {
return std::make_shared<ErrorCredentials>(std::move(status));
Expand Down Expand Up @@ -79,7 +79,7 @@ struct RestVisitor : public CredentialsVisitor {
auto credentials = oauth2_internal::GoogleDefaultCredentials(cfg.options());
if (credentials) {
result = std::make_shared<WrapRestCredentials>(
WithCaching(std::move(*credentials)));
Decorate(*std::move(credentials), cfg.options()));
return;
}
result = MakeErrorCredentials(std::move(credentials).status());
Expand Down Expand Up @@ -116,8 +116,8 @@ struct RestVisitor : public CredentialsVisitor {
};
auto impl = std::make_shared<oauth2_internal::ExternalAccountCredentials>(
*info, client_factory, cfg.options());
result =
std::make_shared<WrapRestCredentials>(WithCaching(std::move(impl)));
result = std::make_shared<WrapRestCredentials>(
Decorate(std::move(impl), cfg.options()));
}
};

Expand Down