Repository navigation
fix(storage): restore emulator credentials #16687
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -83,13 +83,16 @@ Options DefaultOptionsGrpc( | |
| options = google::cloud::internal::MergeOptions( | ||
| std::move(options), Options{}.set<storage::UploadBufferSizeOption>( | ||
| kDefaultGrpcUploadBufferSize)); | ||
| options = | ||
| storage::internal::DefaultOptionsWithCredentials(std::move(options)); | ||
| if (!options.has<UnifiedCredentialsOption>() && | ||
| !options.has<GrpcCredentialOption>()) { | ||
| // `DefaultOptions()` sets no credentials. Use Application Default | ||
| // Credentials unless `UnifiedCredentialsOption` is set, so the REST emulator | ||
| // endpoint does not change the gRPC credentials. As in | ||
| // `DefaultOptionsWithCredentials()`, capture the auth options before | ||
| // `DefaultOptions()` adds the `CLOUD_STORAGE_ENABLE_TRACING` components. | ||
| auto auth_options = google::cloud::internal::MakeAuthOptions(options); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Per the repository style guide, |
||
| options = storage::internal::DefaultOptions(std::move(options)); | ||
| if (!options.has<UnifiedCredentialsOption>()) { | ||
| options.set<UnifiedCredentialsOption>( | ||
| google::cloud::MakeGoogleDefaultCredentials( | ||
| google::cloud::internal::MakeAuthOptions(options))); | ||
| google::cloud::MakeGoogleDefaultCredentials(std::move(auth_options))); | ||
| } | ||
|
|
||
| auto const preserve_creds = | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,18 +15,21 @@ | |
| #include "google/cloud/storage/internal/grpc/default_options.h" | ||
| #include "google/cloud/storage/grpc_plugin.h" | ||
| #include "google/cloud/storage/options.h" | ||
| #include "google/cloud/common_options.h" | ||
| #include "google/cloud/credentials.h" | ||
| #include "google/cloud/grpc_options.h" | ||
| #include "google/cloud/internal/detect_gcp.h" | ||
| #include "google/cloud/internal/unified_grpc_credentials.h" | ||
| #include "google/cloud/testing_util/chrono_output.h" | ||
| #include "google/cloud/testing_util/credentials.h" | ||
| #include "google/cloud/testing_util/scoped_environment.h" | ||
| #include "google/cloud/universe_domain_options.h" | ||
| #include "absl/strings/str_cat.h" | ||
| #include <gmock/gmock.h> | ||
| #include <grpcpp/grpcpp.h> | ||
| #include <memory> | ||
| #include <optional> | ||
| #include <set> | ||
| #include <string> | ||
| #include <tuple> | ||
| #include <utility> | ||
|
|
@@ -45,12 +48,43 @@ class MockGcpDetector : public google::cloud::internal::GcpDetector { | |
|
|
||
| using ::google::cloud::internal::GrpcGoogleDefaultTlsOnlyOption; | ||
| using ::google::cloud::testing_util::ScopedEnvironment; | ||
| using ::testing::ElementsAre; | ||
| using ::testing::Return; | ||
| using ::testing::UnorderedElementsAre; | ||
|
|
||
| google::cloud::Options TestOptions() { | ||
| return Options{}.set<UnifiedCredentialsOption>(MakeInsecureCredentials()); | ||
| } | ||
|
|
||
| std::shared_ptr<google::cloud::internal::GcpDetector> MakeNotOnGcpDetector() { | ||
| auto mock_detector = std::make_shared<MockGcpDetector>(); | ||
| EXPECT_CALL(*mock_detector, IsGoogleCloudBios()) | ||
| .WillRepeatedly(Return(false)); | ||
| EXPECT_CALL(*mock_detector, IsGoogleCloudServerless()) | ||
| .WillRepeatedly(Return(false)); | ||
| return mock_detector; | ||
| } | ||
|
|
||
| /// Returns the name of the `UnifiedCredentialsOption` in @p options, or an | ||
| /// empty string if the option is not set. | ||
| std::string CredentialsName(Options const& options) { | ||
| if (!options.has<UnifiedCredentialsOption>()) return {}; | ||
| ::google::cloud::testing_util::TestCredentialsVisitor visitor; | ||
| ::google::cloud::internal::CredentialsVisitor::dispatch( | ||
| *options.get<UnifiedCredentialsOption>(), visitor); | ||
| return visitor.name; | ||
| } | ||
|
|
||
| /// Returns the logging components of the `UnifiedCredentialsOption` in | ||
| /// @p options, or an empty set if the option is not set. | ||
| std::set<std::string> CredentialsLoggingComponents(Options const& options) { | ||
| if (!options.has<UnifiedCredentialsOption>()) return {}; | ||
| ::google::cloud::testing_util::TestCredentialsVisitor visitor; | ||
| ::google::cloud::internal::CredentialsVisitor::dispatch( | ||
| *options.get<UnifiedCredentialsOption>(), visitor); | ||
| return visitor.options.get<LoggingComponentsOption>(); | ||
| } | ||
|
|
||
| TEST(DefaultOptionsGrpc, DefaultOptionsGrpcChannelCount) { | ||
| using ::google::cloud::GrpcNumChannelsOption; | ||
| struct TestCase { | ||
|
|
@@ -79,6 +113,65 @@ TEST(DefaultOptionsGrpc, DefaultOptionsGrpcChannelCount) { | |
| } | ||
| } | ||
|
|
||
| /// @test Verify the REST emulator endpoints do not change gRPC credentials. | ||
| TEST(DefaultOptionsGrpc, RestEmulatorDoesNotChangeGrpcCredentials) { | ||
| for (char const* var : {"CLOUD_STORAGE_EMULATOR_ENDPOINT", | ||
| "CLOUD_STORAGE_TESTBENCH_ENDPOINT"}) { | ||
| SCOPED_TRACE(var); | ||
| ScopedEnvironment emulator("CLOUD_STORAGE_EMULATOR_ENDPOINT", {}); | ||
| ScopedEnvironment old_emulator("CLOUD_STORAGE_TESTBENCH_ENDPOINT", {}); | ||
| ScopedEnvironment testbench( | ||
| "CLOUD_STORAGE_EXPERIMENTAL_GRPC_TESTBENCH_ENDPOINT", {}); | ||
| ScopedEnvironment rest(var, "http://localhost:1234"); | ||
| Options const options = | ||
| DefaultOptionsGrpc(Options{}, MakeNotOnGcpDetector()); | ||
| EXPECT_EQ(CredentialsName(options), "GoogleDefaultCredentialsConfig"); | ||
| } | ||
| } | ||
|
|
||
| /// @test Verify the gRPC testbench still uses insecure credentials when the | ||
| /// REST emulator endpoint is also set. | ||
| TEST(DefaultOptionsGrpc, GrpcTestbenchStillInsecureWithRestEmulator) { | ||
| ScopedEnvironment emulator("CLOUD_STORAGE_EMULATOR_ENDPOINT", | ||
| "http://localhost:1234"); | ||
| ScopedEnvironment testbench( | ||
| "CLOUD_STORAGE_EXPERIMENTAL_GRPC_TESTBENCH_ENDPOINT", "localhost:9999"); | ||
| ScopedEnvironment preserve( | ||
| "GOOGLE_CLOUD_CPP_STORAGE_TESTING_PRESERVE_CREDENTIALS", {}); | ||
| Options const options = DefaultOptionsGrpc(Options{}, MakeNotOnGcpDetector()); | ||
| EXPECT_EQ(CredentialsName(options), "InsecureCredentialsConfig"); | ||
| } | ||
|
|
||
| /// @test Verify explicit credentials are kept when the REST emulator endpoint | ||
| /// is set. | ||
| TEST(DefaultOptionsGrpc, GrpcKeepsExplicitCredentialsWithRestEmulator) { | ||
| ScopedEnvironment emulator("CLOUD_STORAGE_EMULATOR_ENDPOINT", | ||
| "http://localhost:1234"); | ||
| ScopedEnvironment testbench( | ||
| "CLOUD_STORAGE_EXPERIMENTAL_GRPC_TESTBENCH_ENDPOINT", {}); | ||
| Options const options = DefaultOptionsGrpc( | ||
| Options{}.set<UnifiedCredentialsOption>(MakeAccessTokenCredentials( | ||
| "token1", std::chrono::system_clock::now() + std::chrono::hours(1))), | ||
| MakeNotOnGcpDetector()); | ||
| EXPECT_EQ(CredentialsName(options), "AccessTokenConfig"); | ||
| } | ||
|
|
||
| /// @test Verify `CLOUD_STORAGE_ENABLE_TRACING` configures the client logging | ||
| /// only. The default credentials use `GOOGLE_CLOUD_CPP_ENABLE_TRACING`. | ||
| TEST(DefaultOptionsGrpc, DefaultCredentialsIgnoreStorageTracingEnvironment) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| ScopedEnvironment emulator("CLOUD_STORAGE_EMULATOR_ENDPOINT", {}); | ||
| ScopedEnvironment testbench( | ||
| "CLOUD_STORAGE_EXPERIMENTAL_GRPC_TESTBENCH_ENDPOINT", {}); | ||
| ScopedEnvironment storage_tracing("CLOUD_STORAGE_ENABLE_TRACING", | ||
| "raw-client,http"); | ||
| ScopedEnvironment common_tracing("GOOGLE_CLOUD_CPP_ENABLE_TRACING", "auth"); | ||
| Options const options = DefaultOptionsGrpc(Options{}, MakeNotOnGcpDetector()); | ||
| EXPECT_THAT(options.get<LoggingComponentsOption>(), | ||
| UnorderedElementsAre("raw-client", "http")); | ||
| EXPECT_EQ(CredentialsName(options), "GoogleDefaultCredentialsConfig"); | ||
| EXPECT_THAT(CredentialsLoggingComponents(options), ElementsAre("auth")); | ||
| } | ||
|
|
||
| TEST(DefaultOptionsGrpc, DefaultEndpointsCloudPath) { | ||
| auto mock_detector = std::make_shared<MockGcpDetector>(); | ||
| EXPECT_CALL(*mock_detector, IsGoogleCloudBios()) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calling
MakeAuthOptions(opts)beforeDefaultOptions()preventsGOOGLE_CLOUD_CPP_OPENTELEMETRY_TRACINGfrom reaching the default credentials. UnlikeLoggingComponentsOption,PopulateAuthOptions()does not readGOOGLE_CLOUD_CPP_OPENTELEMETRY_TRACING, so OAuth token requests lose OpenTelemetry spans unlessOpenTelemetryTracingOptionis set in code.Consider copying
OpenTelemetryTracingOptionfromoptsintoauth_optionsafterDefaultOptions()runs in bothDefaultOptionsWithCredentials()andDefaultOptionsGrpc(), with a unit test covering that env var.