From a3be06cedaadafbabe8dd42f5c8821b8b7a9242f Mon Sep 17 00:00:00 2001 From: Gauri Kalra Date: Thu, 26 Nov 2026 07:31:57 +0000 Subject: [PATCH 1/2] fix(storage): gate GCE DirectPath default on ALTS-capable credentials --- .../storage/internal/grpc/default_options.cc | 56 ++- .../internal/grpc/default_options_test.cc | 359 ++++++++++++++++++ 2 files changed, 412 insertions(+), 3 deletions(-) diff --git a/google/cloud/storage/internal/grpc/default_options.cc b/google/cloud/storage/internal/grpc/default_options.cc index 929ca523e63bd..4bd985a00d68c 100644 --- a/google/cloud/storage/internal/grpc/default_options.cc +++ b/google/cloud/storage/internal/grpc/default_options.cc @@ -17,7 +17,9 @@ #include "google/cloud/storage/internal/grpc/channel_telemetry.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/credentials_impl.h" #include "google/cloud/internal/detect_gcp.h" #include "google/cloud/internal/getenv.h" #include "google/cloud/internal/populate_common_options.h" @@ -29,6 +31,7 @@ #include "absl/strings/str_cat.h" #include #include +#include #include #include #include @@ -65,6 +68,53 @@ int DefaultGrpcNumChannels(std::string const& endpoint) { return (std::max)(kMinimumChannels, static_cast(count)); } +// Regular DirectPath (`google-c2p:///storage.googleapis.com`) relies on ALTS. +// With TLS-only channel credentials, handshakes to DirectPath backends fail and +// gRPC falls back to the front-end cluster over the single `google-c2p` +// channel. Only `GoogleDefaultCredentialsConfig` builds ALTS-capable channel +// credentials; a caller-supplied `GrpcCredentialOption` without +// `UnifiedCredentialsOption` is opaque and keeps existing behavior. +bool SupportsDirectPathAlts(Options const& options) { + if (!options.has()) return true; + std::shared_ptr const& credentials = + options.get(); + if (!credentials) return false; + + struct Visitor : public google::cloud::internal::CredentialsVisitor { + bool supported = false; + void visit( + google::cloud::internal::ErrorCredentialsConfig const&) override {} + void visit( + google::cloud::internal::InsecureCredentialsConfig const&) override {} + void visit(google::cloud::internal::GoogleDefaultCredentialsConfig const&) + override { + supported = true; + } + void visit(google::cloud::internal::AccessTokenConfig const&) override {} + void visit(google::cloud::internal::ImpersonateServiceAccountConfig const&) + override {} + void visit(google::cloud::internal::ServiceAccountConfig const&) override {} + void visit(google::cloud::internal::ExternalAccountConfig const&) override { + } + void visit(google::cloud::internal::ApiKeyConfig const&) override {} + void visit(google::cloud::internal::ComputeEngineCredentialsConfig const&) + override {} + void visit(google::cloud::internal::AuthorizedUserConfig const&) override {} + void visit( + google::cloud::internal::GDCHServiceAccountConfig const&) override {} + } visitor; + google::cloud::internal::CredentialsVisitor::dispatch(*credentials, visitor); + return visitor.supported; +} + +bool ShouldDefaultToDirectPath( + Options const& options, + std::shared_ptr const& gcp_detector) { + return SupportsDirectPathAlts(options) && + (gcp_detector->IsGoogleCloudBios() || + gcp_detector->IsGoogleCloudServerless()); +} + } // namespace Options DefaultOptionsGrpc( @@ -158,14 +208,14 @@ Options DefaultOptionsGrpc( // Unless the application configured an endpoint or universe domain, default // to direct connectivity: the Interconnect target when that feature is - // enabled and supported, otherwise the standard target when running in GCP. + // enabled and supported (which uses TLS), otherwise the standard target when + // running in GCP with ALTS-capable credentials. if (!options.has() && !options.has()) { if (direct_path_interconnect) { options.set(absl::StrCat( "google-c2p:///", kDirectPathInterconnectHost, "?force-xds")); - } else if (gcp_detector->IsGoogleCloudBios() || - gcp_detector->IsGoogleCloudServerless()) { + } else if (ShouldDefaultToDirectPath(options, gcp_detector)) { options.set("google-c2p:///storage.googleapis.com"); } } diff --git a/google/cloud/storage/internal/grpc/default_options_test.cc b/google/cloud/storage/internal/grpc/default_options_test.cc index fb2ce3797b68e..1a976e6a4ae9e 100644 --- a/google/cloud/storage/internal/grpc/default_options_test.cc +++ b/google/cloud/storage/internal/grpc/default_options_test.cc @@ -18,9 +18,11 @@ #include "google/cloud/common_options.h" #include "google/cloud/credentials.h" #include "google/cloud/grpc_options.h" +#include "google/cloud/internal/credentials_impl.h" #include "google/cloud/internal/detect_gcp.h" #include "google/cloud/internal/unified_grpc_credentials.h" #include "google/cloud/opentelemetry_options.h" +#include "google/cloud/status.h" #include "google/cloud/testing_util/chrono_output.h" #include "google/cloud/testing_util/credentials.h" #include "google/cloud/testing_util/scoped_environment.h" @@ -28,6 +30,8 @@ #include "absl/strings/str_cat.h" #include #include +#include +#include #include #include #include @@ -310,6 +314,361 @@ TEST(DefaultOptionsGrpc, DefaultEndpointsDirectPath) { EXPECT_FALSE(options.get()); } +struct DirectPathCredentialsTestCase { + std::string test_name; + std::function make_options; + bool expected_direct_path; +}; + +class DefaultEndpointsDirectPathCredentialsTest + : public ::testing::TestWithParam {}; + +/// @test Verify that on GCE, `DefaultOptionsGrpc()` defaults to the +/// `google-c2p` DirectPath endpoint only when the configured credentials can +/// use ALTS (Google Default credentials or an opaque `GrpcCredentialOption`). +/// Non-ALTS credential types fall back to the multi-channel CloudPath default. +TEST_P(DefaultEndpointsDirectPathCredentialsTest, GatesEndpointOnCredentials) { + DirectPathCredentialsTestCase const& test_case = GetParam(); + ScopedEnvironment testbench( + "CLOUD_STORAGE_EXPERIMENTAL_GRPC_TESTBENCH_ENDPOINT", {}); + ScopedEnvironment interconnect( + "GOOGLE_CLOUD_ENABLE_DIRECT_PATH_XDS_OVER_INTERCONNECT", {}); + ScopedEnvironment universe_domain("GOOGLE_CLOUD_UNIVERSE_DOMAIN", {}); + auto mock_detector = std::make_shared(); + EXPECT_CALL(*mock_detector, IsGoogleCloudBios()).WillRepeatedly(Return(true)); + EXPECT_CALL(*mock_detector, IsGoogleCloudServerless()) + .WillRepeatedly(Return(false)); + + Options const options = + DefaultOptionsGrpc(test_case.make_options(), mock_detector); + EXPECT_EQ(options.get(), "storage.googleapis.com"); + EXPECT_FALSE(options.get()); + if (test_case.expected_direct_path) { + EXPECT_EQ(options.get(), + "google-c2p:///storage.googleapis.com"); + EXPECT_EQ(options.get(), 1); + } else { + EXPECT_EQ(options.get(), "storage.googleapis.com"); + EXPECT_GE(options.get(), 4); + } +} + +INSTANTIATE_TEST_SUITE_P( + DefaultOptionsGrpc, DefaultEndpointsDirectPathCredentialsTest, + ::testing::Values( + DirectPathCredentialsTestCase{"DefaultCredentials", + [] { return Options{}; }, true}, + DirectPathCredentialsTestCase{ + "GoogleDefaultCredentials", + [] { + return Options{}.set( + MakeGoogleDefaultCredentials()); + }, + true}, + DirectPathCredentialsTestCase{ + "ComputeEngineCredentials", + [] { + return Options{}.set( + MakeComputeEngineCredentials()); + }, + false}, + DirectPathCredentialsTestCase{ + "ComputeEngineCredentialsWithCaRoots", + [] { + return Options{} + .set(MakeComputeEngineCredentials()) + .set("/path/to/roots.pem"); + }, + false}, + DirectPathCredentialsTestCase{ + "ComputeEngineCredentialsWithOptionCaRoots", + [] { + return Options{}.set( + MakeComputeEngineCredentials( + Options{}.set( + "/path/to/roots.pem"))); + }, + false}, + DirectPathCredentialsTestCase{ + "AccessTokenCredentials", + [] { + return Options{}.set( + MakeAccessTokenCredentials("test-token", + std::chrono::system_clock::now() + + std::chrono::hours(1))); + }, + false}, + DirectPathCredentialsTestCase{ + "ServiceAccountCredentials", + [] { + return Options{}.set( + MakeServiceAccountCredentials("{}")); + }, + false}, + DirectPathCredentialsTestCase{ + "ImpersonateServiceAccountCredentials", + [] { + return Options{}.set( + MakeImpersonateServiceAccountCredentials( + MakeGoogleDefaultCredentials(), + "target@example.iam.gserviceaccount.com")); + }, + false}, + DirectPathCredentialsTestCase{ + "ApiKeyCredentials", + [] { + return Options{}.set( + MakeApiKeyCredentials("test-api-key")); + }, + false}, + DirectPathCredentialsTestCase{ + "ExternalAccountCredentials", + [] { + return Options{}.set( + MakeExternalAccountCredentials("{}")); + }, + false}, + DirectPathCredentialsTestCase{ + "GDCHServiceAccountCredentials", + [] { + return Options{}.set( + MakeGDCHServiceAccountCredentials( + "{}", "https://storage.googleapis.com")); + }, + false}, + DirectPathCredentialsTestCase{ + "InsecureCredentials", + [] { + return Options{}.set( + MakeInsecureCredentials()); + }, + false}, + DirectPathCredentialsTestCase{ + "ErrorCredentials", + [] { + return Options{}.set( + google::cloud::internal::MakeErrorCredentials( + Status(StatusCode::kInternal, "test-error"))); + }, + false}, + DirectPathCredentialsTestCase{ + "UserAccountCredentials", + [] { + return Options{}.set( + google::cloud::internal::MakeUserAccountCredentials("{}")); + }, + false}, + // `GrpcCredentialOption` cannot be inspected for ALTS capability and + // preserves existing `google-c2p` behavior. + DirectPathCredentialsTestCase{ + "GrpcCredentialOptionOnly", + [] { + return Options{}.set( + grpc::InsecureChannelCredentials()); + }, + true}, + // `UnifiedCredentialsOption` takes precedence over + // `GrpcCredentialOption` when both are present. + DirectPathCredentialsTestCase{ + "UnifiedNonAltsOverridesGrpcCredentialOption", + [] { + return Options{} + .set(MakeComputeEngineCredentials()) + .set( + grpc::InsecureChannelCredentials()); + }, + false}, + DirectPathCredentialsTestCase{ + "UnifiedGoogleDefaultOverridesGrpcCredentialOption", + [] { + return Options{} + .set(MakeGoogleDefaultCredentials()) + .set( + grpc::InsecureChannelCredentials()); + }, + true}, + // A null `UnifiedCredentialsOption` pointer must not be dereferenced + // and does not qualify for ALTS, even when paired with + // `GrpcCredentialOption` (`UnifiedCredentialsOption` takes precedence). + DirectPathCredentialsTestCase{ + "NullUnifiedCredentialsOption", + [] { return Options{}.set(nullptr); }, + false}, + DirectPathCredentialsTestCase{ + "NullUnifiedOverridesGrpcCredentialOption", + [] { + return Options{} + .set(nullptr) + .set( + grpc::InsecureChannelCredentials()); + }, + false}), + [](::testing::TestParamInfo const& info) { + return info.param.test_name; + }); + +/// @test Verify serverless-only GCP detection applies the same ALTS credential +/// gate as GCE BIOS detection. +TEST(DefaultOptionsGrpc, DefaultEndpointsServerlessGatesOnCredentials) { + ScopedEnvironment testbench( + "CLOUD_STORAGE_EXPERIMENTAL_GRPC_TESTBENCH_ENDPOINT", {}); + ScopedEnvironment interconnect( + "GOOGLE_CLOUD_ENABLE_DIRECT_PATH_XDS_OVER_INTERCONNECT", {}); + ScopedEnvironment universe_domain("GOOGLE_CLOUD_UNIVERSE_DOMAIN", {}); + auto mock_detector = std::make_shared(); + EXPECT_CALL(*mock_detector, IsGoogleCloudBios()) + .WillRepeatedly(Return(false)); + EXPECT_CALL(*mock_detector, IsGoogleCloudServerless()) + .WillRepeatedly(Return(true)); + + Options const default_creds = DefaultOptionsGrpc( + Options{}.set(MakeGoogleDefaultCredentials()), + mock_detector); + EXPECT_EQ(default_creds.get(), + "google-c2p:///storage.googleapis.com"); + EXPECT_EQ(default_creds.get(), "storage.googleapis.com"); + EXPECT_EQ(default_creds.get(), 1); + EXPECT_FALSE(default_creds.get()); + + Options const compute_creds = DefaultOptionsGrpc( + Options{}.set(MakeComputeEngineCredentials()), + mock_detector); + EXPECT_EQ(compute_creds.get(), "storage.googleapis.com"); + EXPECT_EQ(compute_creds.get(), "storage.googleapis.com"); + EXPECT_GE(compute_creds.get(), 4); + EXPECT_FALSE(compute_creds.get()); +} + +struct ExplicitDirectPathEndpointTestCase { + std::string test_name; + std::string endpoint; +}; + +class ExplicitDirectPathEndpointWithNonAltsCredsTest + : public ::testing::TestWithParam {}; + +/// @test Verify explicit `google-c2p` endpoints are preserved even when paired +/// with non-ALTS credentials, because the library only gates the default +/// endpoint it selects. +TEST_P(ExplicitDirectPathEndpointWithNonAltsCredsTest, PreservesEndpoint) { + ExplicitDirectPathEndpointTestCase const& test_case = GetParam(); + ScopedEnvironment testbench( + "CLOUD_STORAGE_EXPERIMENTAL_GRPC_TESTBENCH_ENDPOINT", {}); + ScopedEnvironment interconnect( + "GOOGLE_CLOUD_ENABLE_DIRECT_PATH_XDS_OVER_INTERCONNECT", {}); + ScopedEnvironment universe_domain("GOOGLE_CLOUD_UNIVERSE_DOMAIN", {}); + auto mock_detector = std::make_shared(); + EXPECT_CALL(*mock_detector, IsGoogleCloudBios()).Times(0); + EXPECT_CALL(*mock_detector, IsGoogleCloudServerless()).Times(0); + + Options const options = DefaultOptionsGrpc( + Options{} + .set(test_case.endpoint) + .set(MakeComputeEngineCredentials()), + mock_detector); + EXPECT_EQ(options.get(), test_case.endpoint); + EXPECT_EQ(options.get(), 1); + EXPECT_FALSE(options.get()); +} + +INSTANTIATE_TEST_SUITE_P( + DefaultOptionsGrpc, ExplicitDirectPathEndpointWithNonAltsCredsTest, + ::testing::Values( + ExplicitDirectPathEndpointTestCase{ + "StandardScheme", "google-c2p:///storage.googleapis.com"}, + ExplicitDirectPathEndpointTestCase{ + "ExperimentalScheme", + "google-c2p-experimental:///storage.googleapis.com"}, + ExplicitDirectPathEndpointTestCase{ + "OtherHost", "google-c2p:///other-host.googleapis.com"}), + [](::testing::TestParamInfo const& + info) { return info.param.test_name; }); + +/// @test Verify non-ALTS credentials remain on CloudPath off GCP. +TEST(DefaultOptionsGrpc, DefaultEndpointsCloudPathWithNonAltsCredentials) { + ScopedEnvironment testbench( + "CLOUD_STORAGE_EXPERIMENTAL_GRPC_TESTBENCH_ENDPOINT", {}); + ScopedEnvironment interconnect( + "GOOGLE_CLOUD_ENABLE_DIRECT_PATH_XDS_OVER_INTERCONNECT", {}); + ScopedEnvironment universe_domain("GOOGLE_CLOUD_UNIVERSE_DOMAIN", {}); + + Options const options = DefaultOptionsGrpc( + Options{}.set(MakeComputeEngineCredentials()), + MakeNotOnGcpDetector()); + EXPECT_EQ(options.get(), "storage.googleapis.com"); + EXPECT_EQ(options.get(), "storage.googleapis.com"); + EXPECT_GE(options.get(), 4); + EXPECT_FALSE(options.get()); +} + +/// @test Verify DirectPath over Interconnect uses TLS rather than ALTS and +/// therefore still selects the Interconnect endpoint with non-ALTS credentials, +/// while falling back to CloudPath on GCE when gRPC does not support +/// `force-xds`. +TEST(DefaultOptionsGrpc, DirectPathOverInterconnectWithNonAltsCredentials) { + ScopedEnvironment testbench( + "CLOUD_STORAGE_EXPERIMENTAL_GRPC_TESTBENCH_ENDPOINT", {}); + ScopedEnvironment universe_domain("GOOGLE_CLOUD_UNIVERSE_DOMAIN", {}); + { + ScopedEnvironment interconnect( + "GOOGLE_CLOUD_ENABLE_DIRECT_PATH_XDS_OVER_INTERCONNECT", {}); + auto mock_detector = std::make_shared(); + EXPECT_CALL(*mock_detector, IsGoogleCloudBios()).Times(0); + EXPECT_CALL(*mock_detector, IsGoogleCloudServerless()).Times(0); + + Options const options = DefaultOptionsGrpc( + Options{} + .set( + true) + .set(MakeComputeEngineCredentials()), + mock_detector, /*grpc_supports_force_xds=*/true); + EXPECT_EQ(options.get(), + "google-c2p:///storage-direct.googleapis.com?force-xds"); + EXPECT_EQ(options.get(), "storage-direct.googleapis.com"); + EXPECT_EQ(options.get(), 1); + EXPECT_TRUE(options.get()); + } + { + ScopedEnvironment interconnect( + "GOOGLE_CLOUD_ENABLE_DIRECT_PATH_XDS_OVER_INTERCONNECT", "true"); + auto mock_detector = std::make_shared(); + EXPECT_CALL(*mock_detector, IsGoogleCloudBios()).Times(0); + EXPECT_CALL(*mock_detector, IsGoogleCloudServerless()).Times(0); + + Options const options = DefaultOptionsGrpc( + Options{}.set(MakeComputeEngineCredentials()), + mock_detector, /*grpc_supports_force_xds=*/true); + EXPECT_EQ(options.get(), + "google-c2p:///storage-direct.googleapis.com?force-xds"); + EXPECT_EQ(options.get(), "storage-direct.googleapis.com"); + EXPECT_EQ(options.get(), 1); + EXPECT_TRUE(options.get()); + } + { + ScopedEnvironment interconnect( + "GOOGLE_CLOUD_ENABLE_DIRECT_PATH_XDS_OVER_INTERCONNECT", {}); + auto mock_detector = std::make_shared(); + EXPECT_CALL(*mock_detector, IsGoogleCloudBios()) + .WillRepeatedly(Return(true)); + EXPECT_CALL(*mock_detector, IsGoogleCloudServerless()) + .WillRepeatedly(Return(false)); + + Options const options = DefaultOptionsGrpc( + Options{} + .set( + true) + .set(MakeComputeEngineCredentials()), + mock_detector, /*grpc_supports_force_xds=*/false); + EXPECT_TRUE( + options + .get()); + EXPECT_EQ(options.get(), "storage.googleapis.com"); + EXPECT_EQ(options.get(), "storage.googleapis.com"); + EXPECT_GE(options.get(), 4); + EXPECT_FALSE(options.get()); + } +} + /// @test Verify the option defaults to disabled, and that the resolved default /// is reported when neither the option nor the environment variable is set. TEST(DefaultOptionsGrpc, DirectPathOverInterconnectDefault) { From 110863ccb24b04b83d3251be91ab2181e7507f2f Mon Sep 17 00:00:00 2001 From: Gauri Kalra Date: Thu, 26 Nov 2026 09:17:52 +0000 Subject: [PATCH 2/2] cleanup(storage): clarify DirectPath credentials gate naming and comments --- .../storage/internal/grpc/default_options.cc | 15 +++++++++------ .../storage/internal/grpc/default_options_test.cc | 7 ++++--- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/google/cloud/storage/internal/grpc/default_options.cc b/google/cloud/storage/internal/grpc/default_options.cc index 4bd985a00d68c..c21ef89d368ec 100644 --- a/google/cloud/storage/internal/grpc/default_options.cc +++ b/google/cloud/storage/internal/grpc/default_options.cc @@ -71,10 +71,13 @@ int DefaultGrpcNumChannels(std::string const& endpoint) { // Regular DirectPath (`google-c2p:///storage.googleapis.com`) relies on ALTS. // With TLS-only channel credentials, handshakes to DirectPath backends fail and // gRPC falls back to the front-end cluster over the single `google-c2p` -// channel. Only `GoogleDefaultCredentialsConfig` builds ALTS-capable channel -// credentials; a caller-supplied `GrpcCredentialOption` without -// `UnifiedCredentialsOption` is opaque and keeps existing behavior. -bool SupportsDirectPathAlts(Options const& options) { +// channel. This visitor mirrors +// `google::cloud::internal::CreateAuthenticationStrategy()` in +// `google/cloud/internal/unified_grpc_credentials.cc`, where only +// `GoogleDefaultCredentialsConfig` builds ALTS-capable channel credentials. A +// caller-supplied `GrpcCredentialOption` without `UnifiedCredentialsOption` is +// opaque and keeps existing behavior. +bool MaySupportDirectPathAlts(Options const& options) { if (!options.has()) return true; std::shared_ptr const& credentials = options.get(); @@ -110,7 +113,7 @@ bool SupportsDirectPathAlts(Options const& options) { bool ShouldDefaultToDirectPath( Options const& options, std::shared_ptr const& gcp_detector) { - return SupportsDirectPathAlts(options) && + return MaySupportDirectPathAlts(options) && (gcp_detector->IsGoogleCloudBios() || gcp_detector->IsGoogleCloudServerless()); } @@ -209,7 +212,7 @@ Options DefaultOptionsGrpc( // Unless the application configured an endpoint or universe domain, default // to direct connectivity: the Interconnect target when that feature is // enabled and supported (which uses TLS), otherwise the standard target when - // running in GCP with ALTS-capable credentials. + // running in GCP and the configured credentials may support ALTS. if (!options.has() && !options.has()) { if (direct_path_interconnect) { diff --git a/google/cloud/storage/internal/grpc/default_options_test.cc b/google/cloud/storage/internal/grpc/default_options_test.cc index 1a976e6a4ae9e..967d5c3e11670 100644 --- a/google/cloud/storage/internal/grpc/default_options_test.cc +++ b/google/cloud/storage/internal/grpc/default_options_test.cc @@ -324,9 +324,10 @@ class DefaultEndpointsDirectPathCredentialsTest : public ::testing::TestWithParam {}; /// @test Verify that on GCE, `DefaultOptionsGrpc()` defaults to the -/// `google-c2p` DirectPath endpoint only when the configured credentials can -/// use ALTS (Google Default credentials or an opaque `GrpcCredentialOption`). -/// Non-ALTS credential types fall back to the multi-channel CloudPath default. +/// `google-c2p` DirectPath endpoint only when the configured credentials are +/// Google Default credentials or an opaque `GrpcCredentialOption`. Other +/// `UnifiedCredentialsOption` types fall back to the multi-channel CloudPath +/// default. TEST_P(DefaultEndpointsDirectPathCredentialsTest, GatesEndpointOnCredentials) { DirectPathCredentialsTestCase const& test_case = GetParam(); ScopedEnvironment testbench(