From 7d8a2865074a17aebca009d54d281438d6efdfc7 Mon Sep 17 00:00:00 2001 From: Gauri Kalra Date: Wed, 25 Nov 2026 05:03:35 +0000 Subject: [PATCH 1/2] fix(storage): make EncryptionKey stream output consistent with CreateHmacKeyResponse --- .../connection_impl_file_upload_test.cc | 69 +++++++++++ .../internal/connection_impl_object_test.cc | 29 +++++ .../storage/internal/logging_stub_test.cc | 96 +++++++++++++++ .../storage/internal/object_requests_test.cc | 20 +-- .../internal/tracing_connection_test.cc | 114 ++++++++++++++++++ google/cloud/storage/well_known_headers.cc | 4 +- .../cloud/storage/well_known_headers_test.cc | 53 +++++++- 7 files changed, 371 insertions(+), 14 deletions(-) diff --git a/google/cloud/storage/internal/connection_impl_file_upload_test.cc b/google/cloud/storage/internal/connection_impl_file_upload_test.cc index 208f394a4c4b9..f2f5eec97c8ca 100644 --- a/google/cloud/storage/internal/connection_impl_file_upload_test.cc +++ b/google/cloud/storage/internal/connection_impl_file_upload_test.cc @@ -18,6 +18,7 @@ #include "google/cloud/testing_util/status_matchers.h" #include #include +#include namespace google { namespace cloud { @@ -29,6 +30,9 @@ namespace { using ::google::cloud::storage::testing::MockGenericStub; using ::google::cloud::storage::testing::TempFile; using ::google::cloud::testing_util::StatusIs; +using ::testing::AllOf; +using ::testing::HasSubstr; +using ::testing::Not; using ::testing::Return; class ConnectionImplFileUploadTest : public ::testing::Test { @@ -144,6 +148,71 @@ TEST_F(ConnectionImplFileUploadTest, UploadFileResumableWithLimit) { EXPECT_EQ("01234", actual); } +/// @test Verify `EncryptionKey` option formatting in simple and resumable +/// file-upload error statuses. +TEST_F(ConnectionImplFileUploadTest, UploadFileErrorsFormatEncryptionKey) { + // /bin/echo -n "0123456789-ABCDEFGHIJ-0123456789" | openssl base64 + constexpr std::string_view kKeyBinary = "0123456789-ABCDEFGHIJ-0123456789"; + constexpr std::string_view kKeyBase64 = + "MDEyMzQ1Njc4OS1BQkNERUZHSElKLTAxMjM0NTY3ODk="; + EncryptionKey const key = + EncryptionKey::FromBinaryKey(std::string{kKeyBinary}); + + std::shared_ptr connection = MakeConnection(); + std::string const contents = "0123456789"; + TempFile temp(contents); + + InsertObjectMediaRequest simple_missing("test-bucket", "test-object", ""); + simple_missing.set_option(key); + StatusOr> const missing_result = + connection->UploadFileSimple("/no/such/file/exists.txt", 123, + simple_missing); + EXPECT_THAT(missing_result, + StatusIs(StatusCode::kNotFound, + AllOf(HasSubstr("x-goog-encryption-key: [censored]"), + Not(HasSubstr(kKeyBase64))))); + + InsertObjectMediaRequest simple_offset("test-bucket", "test-object", ""); + simple_offset.set_multiple_options(key, + UploadFromOffset(contents.size() + 1)); + StatusOr> const offset_result = + connection->UploadFileSimple(temp.name(), contents.size(), simple_offset); + EXPECT_THAT(offset_result, + StatusIs(StatusCode::kInvalidArgument, + AllOf(HasSubstr("x-goog-encryption-key: [censored]"), + Not(HasSubstr(kKeyBase64))))); + + InsertObjectMediaRequest simple_short_read("test-bucket", "test-object", ""); + simple_short_read.set_option(key); + StatusOr> const short_read_result = + connection->UploadFileSimple(temp.name(), contents.size() + 1, + simple_short_read); + EXPECT_THAT(short_read_result, + StatusIs(StatusCode::kInternal, + AllOf(HasSubstr("x-goog-encryption-key: [censored]"), + Not(HasSubstr(kKeyBase64))))); + + ResumableUploadRequest resumable_missing("test-bucket", "test-object"); + resumable_missing.set_option(key); + StatusOr> const resumable_missing_result = + connection->UploadFileResumable("/no/such/file/exists.txt", + resumable_missing); + EXPECT_THAT(resumable_missing_result, + StatusIs(StatusCode::kNotFound, + AllOf(HasSubstr("x-goog-encryption-key: [censored]"), + Not(HasSubstr(kKeyBase64))))); + + ResumableUploadRequest resumable_offset("test-bucket", "test-object"); + resumable_offset.set_multiple_options(key, + UploadFromOffset(contents.size() + 1)); + StatusOr> const resumable_offset_result = + connection->UploadFileResumable(temp.name(), resumable_offset); + EXPECT_THAT(resumable_offset_result, + StatusIs(StatusCode::kInvalidArgument, + AllOf(HasSubstr("x-goog-encryption-key: [censored]"), + Not(HasSubstr(kKeyBase64))))); +} + } // namespace } // namespace internal GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END diff --git a/google/cloud/storage/internal/connection_impl_object_test.cc b/google/cloud/storage/internal/connection_impl_object_test.cc index d900bb3191d15..453d1c1b3c6a0 100644 --- a/google/cloud/storage/internal/connection_impl_object_test.cc +++ b/google/cloud/storage/internal/connection_impl_object_test.cc @@ -14,6 +14,7 @@ #include "google/cloud/storage/internal/connection_impl.h" #include "google/cloud/storage/internal/object_requests.h" +#include "google/cloud/storage/object_read_stream.h" #include "google/cloud/storage/options.h" #include "google/cloud/storage/testing/canonical_errors.h" #include "google/cloud/storage/testing/mock_generic_stub.h" @@ -24,6 +25,7 @@ #include #include #include +#include #include #include @@ -45,12 +47,14 @@ using ::google::cloud::storage::testing::canonical_errors::PermanentError; using ::google::cloud::storage::testing::canonical_errors::TransientError; using ::google::cloud::testing_util::IsOk; using ::google::cloud::testing_util::ScopedLog; +using ::google::cloud::testing_util::StatusIs; using ::testing::AllOf; using ::testing::Contains; using ::testing::Each; using ::testing::HasSubstr; using ::testing::IsEmpty; using ::testing::Not; +using ::testing::Return; using ::testing::SizeIs; // Shaped like a real REST resumable session URL: the `upload_id` is a bearer @@ -560,6 +564,31 @@ TEST(StorageConnectionImpl, RestoreObjectPermanentFailure) { EXPECT_THAT(permanent.captured_authority_options(), RetryLoopUsesOptions()); } +/// @test Verify `EncryptionKey` option formatting in `DownloadStreamToFile` +/// error statuses. +TEST(StorageConnectionImpl, DownloadStreamToFileFormatsEncryptionKey) { + // /bin/echo -n "0123456789-ABCDEFGHIJ-0123456789" | openssl base64 + constexpr std::string_view kKeyBinary = "0123456789-ABCDEFGHIJ-0123456789"; + constexpr std::string_view kKeyBase64 = + "MDEyMzQ1Njc4OS1BQkNERUZHSElKLTAxMjM0NTY3ODk="; + + auto mock = std::make_unique(); + EXPECT_CALL(*mock, options).WillRepeatedly(Return(Options{})); + std::shared_ptr client = + StorageConnectionImpl::Create(std::move(mock), RetryTestOptions()); + google::cloud::internal::OptionsSpan const span(client->options()); + + ReadObjectRangeRequest request("test-bucket", "test-object"); + request.set_option(EncryptionKey::FromBinaryKey(std::string{kKeyBinary})); + ObjectReadStream stream; + Status const status = client->DownloadStreamToFile( + std::move(stream), "/no/such/directory/exists.bin", request); + EXPECT_THAT(status, + StatusIs(StatusCode::kInvalidArgument, + AllOf(HasSubstr("x-goog-encryption-key: [censored]"), + Not(HasSubstr(kKeyBase64))))); +} + } // namespace } // namespace internal GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END diff --git a/google/cloud/storage/internal/logging_stub_test.cc b/google/cloud/storage/internal/logging_stub_test.cc index 2012ae949cc21..e184a24925221 100644 --- a/google/cloud/storage/internal/logging_stub_test.cc +++ b/google/cloud/storage/internal/logging_stub_test.cc @@ -13,14 +13,19 @@ // limitations under the License. #include "google/cloud/storage/internal/logging_stub.h" +#include "google/cloud/storage/client.h" #include "google/cloud/storage/internal/bucket_metadata_parser.h" +#include "google/cloud/storage/internal/generic_stub_factory.h" #include "google/cloud/storage/internal/object_metadata_parser.h" #include "google/cloud/storage/testing/canonical_errors.h" #include "google/cloud/storage/testing/mock_generic_stub.h" +#include "google/cloud/common_options.h" #include "google/cloud/log.h" +#include "google/cloud/testing_util/scoped_environment.h" #include #include #include +#include #include #include @@ -33,7 +38,9 @@ namespace { using ::google::cloud::storage::testing::MockGenericStub; using ::google::cloud::storage::testing::canonical_errors::TransientError; +using ::google::cloud::testing_util::ScopedEnvironment; using ::testing::HasSubstr; +using ::testing::Not; using ::testing::Return; class MockLogBackend : public google::cloud::LogBackend { @@ -285,6 +292,95 @@ TEST_F(LoggingStubTest, ReadObjectSuccessLogsOnlyTheRequest) { ReadObjectRangeRequest("my-bucket", "my-object")); } +class LoggingStubComponentTest + : public LoggingStubTest, + public ::testing::WithParamInterface {}; + +/// @test Verify `EncryptionKey` and `SourceEncryptionKey` option formatting +/// across `LoggingStub` components. +TEST_P(LoggingStubComponentTest, FormatsEncryptionKeyOptions) { + // /bin/echo -n "0123456789-ABCDEFGHIJ-0123456789" | openssl base64 + constexpr std::string_view kDestKeyBinary = + "0123456789-ABCDEFGHIJ-0123456789"; + constexpr std::string_view kDestKeyBase64 = + "MDEyMzQ1Njc4OS1BQkNERUZHSElKLTAxMjM0NTY3ODk="; + // /bin/echo -n "9876543210-JIHGFEDCBA-9876543210" | openssl base64 + constexpr std::string_view kSourceKeyBinary = + "9876543210-JIHGFEDCBA-9876543210"; + constexpr std::string_view kSourceKeyBase64 = + "OTg3NjU0MzIxMC1KSUhHRkVEQ0JBLTk4NzY1NDMyMTA="; + + auto mock = std::make_unique(); + EXPECT_CALL(*mock, RewriteObject) + .WillOnce(Return(StatusOr(TransientError()))); + + EXPECT_CALL(*log_backend_, ProcessWithOwnership) + .WillOnce([kDestKeyBase64, kSourceKeyBase64](LogRecord const& lr) { + EXPECT_THAT(lr.message, HasSubstr("RewriteObjectRequest={")); + EXPECT_THAT(lr.message, HasSubstr("x-goog-encryption-key: [censored]")); + EXPECT_THAT(lr.message, + HasSubstr("x-goog-copy-source-encryption-key: [censored]")); + EXPECT_THAT(lr.message, Not(HasSubstr(kDestKeyBase64))); + EXPECT_THAT(lr.message, Not(HasSubstr(kSourceKeyBase64))); + }) + .WillOnce([](LogRecord const& lr) { + EXPECT_THAT(lr.message, HasSubstr("status={")); + }); + + std::unique_ptr client = + storage_internal::DecorateStub( + Options{}.set({std::string{GetParam()}}), + std::move(mock)); + rest_internal::RestContext context; + RewriteObjectRequest request("src-bucket", "src-object", "dst-bucket", + "dst-object", ""); + request.set_multiple_options( + EncryptionKey::FromBinaryKey(std::string{kDestKeyBinary}), + SourceEncryptionKey::FromBinaryKey(std::string{kSourceKeyBinary})); + (void)client->RewriteObject(context, Options{}, request); +} + +INSTANTIATE_TEST_SUITE_P(LoggingComponents, LoggingStubComponentTest, + ::testing::Values("raw-client", "rpc")); + +/// @test Verify `EncryptionKey` option formatting when logging is enabled via +/// `CLOUD_STORAGE_ENABLE_TRACING`. +TEST_F(LoggingStubTest, EnvTracingFormatsEncryptionKey) { + // /bin/echo -n "0123456789-ABCDEFGHIJ-0123456789" | openssl base64 + constexpr std::string_view kKeyBinary = "0123456789-ABCDEFGHIJ-0123456789"; + constexpr std::string_view kKeyBase64 = + "MDEyMzQ1Njc4OS1BQkNERUZHSElKLTAxMjM0NTY3ODk="; + + ScopedEnvironment env("CLOUD_STORAGE_ENABLE_TRACING", "raw-client"); + // `DefaultOptions` logs an INFO message when applying the env var. + EXPECT_CALL(*log_backend_, ProcessWithOwnership) + .WillOnce([](LogRecord const& lr) { + EXPECT_THAT(lr.message, HasSubstr("Enabling logging for raw-client")); + }); + Options const opts = DefaultOptions(Options{}); + + auto mock = std::make_unique(); + EXPECT_CALL(*mock, ReadObject) + .WillOnce(Return( + StatusOr>(TransientError()))); + EXPECT_CALL(*log_backend_, ProcessWithOwnership) + .WillOnce([kKeyBase64](LogRecord const& lr) { + EXPECT_THAT(lr.message, HasSubstr("ReadObjectRangeRequest={")); + EXPECT_THAT(lr.message, HasSubstr("x-goog-encryption-key: [censored]")); + EXPECT_THAT(lr.message, Not(HasSubstr(kKeyBase64))); + }) + .WillOnce([](LogRecord const& lr) { + EXPECT_THAT(lr.message, HasSubstr("status={")); + }); + + std::unique_ptr client = + storage_internal::DecorateStub(opts, std::move(mock)); + rest_internal::RestContext context; + ReadObjectRangeRequest request("my-bucket", "my-object"); + request.set_option(EncryptionKey::FromBinaryKey(std::string{kKeyBinary})); + (void)client->ReadObject(context, opts, request); +} + } // namespace } // namespace internal GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END diff --git a/google/cloud/storage/internal/object_requests_test.cc b/google/cloud/storage/internal/object_requests_test.cc index 1d45967ba28a3..f20c815bcfa75 100644 --- a/google/cloud/storage/internal/object_requests_test.cc +++ b/google/cloud/storage/internal/object_requests_test.cc @@ -257,8 +257,9 @@ TEST(ObjectRequestsTest, CopyAllOptions) { EXPECT_THAT(actual, HasSubstr("destinationKmsKeyName=test-only-kms-key")); EXPECT_THAT(actual, HasSubstr("destinationPredefinedAcl=private")); EXPECT_THAT(actual, HasSubstr("x-goog-encryption-algorithm: AES256")); - // /bin/echo -n ABCD1234 | openssl base64 -e - EXPECT_THAT(actual, HasSubstr("x-goog-encryption-key: MTIzNEFCQ0Q=")); + EXPECT_THAT(actual, HasSubstr("x-goog-encryption-key: [censored]")); + // /bin/echo -n 1234ABCD | openssl base64 + EXPECT_THAT(actual, Not(HasSubstr("MTIzNEFCQ0Q="))); // /bin/echo -n 1234ABCD | sha256sum | awk '{printf("%s", $1);}' | // xxd -r -p | openssl base64 EXPECT_THAT(actual, @@ -276,9 +277,10 @@ TEST(ObjectRequestsTest, CopyAllOptions) { EXPECT_THAT(actual, HasSubstr("sourceGeneration=7")); EXPECT_THAT(actual, HasSubstr("x-goog-copy-source-encryption-algorithm: AES256")); - // /bin/echo -n ABCD1234 | openssl base64 -e EXPECT_THAT(actual, - HasSubstr("x-goog-copy-source-encryption-key: QUJDRDEyMzQ=")); + HasSubstr("x-goog-copy-source-encryption-key: [censored]")); + // /bin/echo -n ABCD1234 | openssl base64 + EXPECT_THAT(actual, Not(HasSubstr("QUJDRDEyMzQ="))); // /bin/echo -n ABCD1234 | sha256sum | awk '{printf("%s", $1);}' | // xxd -r -p | openssl base64 EXPECT_THAT(actual, @@ -414,8 +416,9 @@ TEST(ObjectRequestsTest, Update) { EXPECT_THAT(actual, HasSubstr("content_type=application/json")); EXPECT_THAT(actual, HasSubstr("generation=7")); EXPECT_THAT(actual, HasSubstr("x-goog-encryption-algorithm: AES256")); - // /bin/echo -n 1234ABCD | openssl base64 -e - EXPECT_THAT(actual, HasSubstr("x-goog-encryption-key: MTIzNEFCQ0Q=")); + EXPECT_THAT(actual, HasSubstr("x-goog-encryption-key: [censored]")); + // /bin/echo -n 1234ABCD | openssl base64 + EXPECT_THAT(actual, Not(HasSubstr("MTIzNEFCQ0Q="))); // /bin/echo -n 1234ABCD | sha256sum | awk '{printf("%s", $1);}' | // xxd -r -p | openssl base64 EXPECT_THAT(actual, @@ -1076,8 +1079,9 @@ TEST(PatchObjectRequestTest, Builder) { EXPECT_THAT(actual, HasSubstr("contentType")); EXPECT_THAT(actual, HasSubstr("application/json")); EXPECT_THAT(actual, HasSubstr("x-goog-encryption-algorithm: AES256")); - // /bin/echo -n ABCD1234 | sha256sum #openssl base64 -e - EXPECT_THAT(actual, HasSubstr("x-goog-encryption-key: QUJDRDEyMzQ=")); + EXPECT_THAT(actual, HasSubstr("x-goog-encryption-key: [censored]")); + // /bin/echo -n ABCD1234 | openssl base64 + EXPECT_THAT(actual, Not(HasSubstr("QUJDRDEyMzQ="))); // /bin/echo -n ABCD1234 | sha256sum | awk '{printf("%s", $1);}' | // xxd -r -p | openssl base64 EXPECT_THAT(actual, diff --git a/google/cloud/storage/internal/tracing_connection_test.cc b/google/cloud/storage/internal/tracing_connection_test.cc index a8f8eb7355848..9b8867757a19d 100644 --- a/google/cloud/storage/internal/tracing_connection_test.cc +++ b/google/cloud/storage/internal/tracing_connection_test.cc @@ -13,15 +13,21 @@ // limitations under the License. #include "google/cloud/storage/internal/tracing_connection.h" +#include "google/cloud/storage/client.h" +#include "google/cloud/storage/internal/connection_factory.h" #include "google/cloud/storage/object_read_stream.h" #include "google/cloud/storage/testing/canonical_errors.h" #include "google/cloud/storage/testing/mock_client.h" +#include "google/cloud/storage/testing/mock_generic_stub.h" #include "google/cloud/internal/opentelemetry.h" +#include "google/cloud/opentelemetry_options.h" #include "google/cloud/testing_util/opentelemetry_matchers.h" +#include "google/cloud/testing_util/scoped_environment.h" #include "google/cloud/testing_util/status_matchers.h" #include #include #include +#include #include #include @@ -32,12 +38,14 @@ GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN namespace { using ::google::cloud::storage::testing::MockClient; +using ::google::cloud::storage::testing::MockGenericStub; using ::google::cloud::storage::testing::MockObjectReadSource; using ::google::cloud::storage::testing::canonical_errors::PermanentError; using ::google::cloud::storage::testing::canonical_errors::TransientError; using ::google::cloud::testing_util::InstallSpanCatcher; using ::google::cloud::testing_util::IsOk; using ::google::cloud::testing_util::OTelAttribute; +using ::google::cloud::testing_util::ScopedEnvironment; using ::google::cloud::testing_util::SpanHasAttributes; using ::google::cloud::testing_util::SpanHasInstrumentationScope; using ::google::cloud::testing_util::SpanIsRoot; @@ -48,7 +56,10 @@ using ::google::cloud::testing_util::StatusIs; using ::google::cloud::testing_util::ThereIsAnActiveSpan; using ::testing::AllOf; using ::testing::ElementsAre; +using ::testing::HasSubstr; using ::testing::Not; +using ::testing::Pointee; +using ::testing::Property; using ::testing::Return; TEST(TracingClientTest, Options) { @@ -1807,6 +1818,109 @@ TEST(TracingClientTest, BucketMetadataMaybeInvalidateObjectLevelNoEvict) { "test-object")); } +/// @test Verify `EncryptionKey` option formatting in file-transfer error spans. +TEST(TracingClientTest, FileTransferErrorFormatsEncryptionKey) { + // /bin/echo -n "0123456789-ABCDEFGHIJ-0123456789" | openssl base64 + constexpr std::string_view kKeyBinary = "0123456789-ABCDEFGHIJ-0123456789"; + constexpr std::string_view kKeyBase64 = + "MDEyMzQ1Njc4OS1BQkNERUZHSElKLTAxMjM0NTY3ODk="; + storage::EncryptionKey const key = + storage::EncryptionKey::FromBinaryKey(std::string{kKeyBinary}); + + std::shared_ptr span_catcher = + InstallSpanCatcher(); + auto mock = std::make_unique(); + EXPECT_CALL(*mock, options).WillRepeatedly(Return(Options{})); + Options const opts = + Options{} + .set(true) + .set(false); + std::shared_ptr connection = + MakeStorageConnection(opts, std::move(mock)); + google::cloud::internal::OptionsSpan const options_span( + connection->options()); + + storage::internal::InsertObjectMediaRequest upload_request("test-bucket", + "test-object", ""); + upload_request.set_option(key); + StatusOr> const upload_result = + connection->UploadFileSimple("/no/such/file/exists.txt", 123, + upload_request); + EXPECT_THAT(upload_result, StatusIs(StatusCode::kNotFound)); + + storage::internal::ReadObjectRangeRequest download_request("test-bucket", + "test-object"); + download_request.set_option(key); + storage::ObjectReadStream stream; + Status const download_status = connection->DownloadStreamToFile( + std::move(stream), "/no/such/directory/exists.bin", download_request); + EXPECT_THAT(download_status, StatusIs(StatusCode::kInvalidArgument)); + + EXPECT_THAT( + span_catcher->GetSpans(), + ElementsAre( + AllOf(SpanNamed("storage::Client::UploadFile/UploadFileSimple"), + Pointee(Property( + &opentelemetry::sdk::trace::SpanData::GetDescription, + AllOf(HasSubstr("x-goog-encryption-key: [censored]"), + Not(HasSubstr(kKeyBase64))))), + SpanHasAttributes(OTelAttribute( + "gl-cpp.error.message", + AllOf(HasSubstr("x-goog-encryption-key: [censored]"), + Not(HasSubstr(kKeyBase64)))))), + AllOf( + SpanNamed("storage::Client::DownloadToFile/DownloadStreamToFile"), + Pointee( + Property(&opentelemetry::sdk::trace::SpanData::GetDescription, + AllOf(HasSubstr("x-goog-encryption-key: [censored]"), + Not(HasSubstr(kKeyBase64))))), + SpanHasAttributes(OTelAttribute( + "gl-cpp.error.message", + AllOf(HasSubstr("x-goog-encryption-key: [censored]"), + Not(HasSubstr(kKeyBase64)))))))); +} + +/// @test Verify `EncryptionKey` option formatting in error spans enabled via +/// `GOOGLE_CLOUD_CPP_OPENTELEMETRY_TRACING`. +TEST(TracingClientTest, EnvTracingFormatsEncryptionKey) { + // /bin/echo -n "0123456789-ABCDEFGHIJ-0123456789" | openssl base64 + constexpr std::string_view kKeyBinary = "0123456789-ABCDEFGHIJ-0123456789"; + constexpr std::string_view kKeyBase64 = + "MDEyMzQ1Njc4OS1BQkNERUZHSElKLTAxMjM0NTY3ODk="; + + ScopedEnvironment env("GOOGLE_CLOUD_CPP_OPENTELEMETRY_TRACING", "1"); + std::shared_ptr span_catcher = + InstallSpanCatcher(); + auto mock = std::make_unique(); + EXPECT_CALL(*mock, options).WillRepeatedly(Return(Options{})); + Options const opts = storage::internal::DefaultOptions( + Options{}.set(false)); + std::shared_ptr connection = + MakeStorageConnection(opts, std::move(mock)); + google::cloud::internal::OptionsSpan const options_span( + connection->options()); + + storage::internal::ResumableUploadRequest request("test-bucket", + "test-object"); + request.set_option( + storage::EncryptionKey::FromBinaryKey(std::string{kKeyBinary})); + StatusOr> const result = + connection->UploadFileResumable("/no/such/file/exists.txt", request); + EXPECT_THAT(result, StatusIs(StatusCode::kNotFound)); + + EXPECT_THAT( + span_catcher->GetSpans(), + ElementsAre(AllOf( + SpanNamed("storage::Client::UploadFile/UploadFileResumable"), + Pointee(Property(&opentelemetry::sdk::trace::SpanData::GetDescription, + AllOf(HasSubstr("x-goog-encryption-key: [censored]"), + Not(HasSubstr(kKeyBase64))))), + SpanHasAttributes(OTelAttribute( + "gl-cpp.error.message", + AllOf(HasSubstr("x-goog-encryption-key: [censored]"), + Not(HasSubstr(kKeyBase64)))))))); +} + } // namespace GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END } // namespace storage_internal diff --git a/google/cloud/storage/well_known_headers.cc b/google/cloud/storage/well_known_headers.cc index 12797e92f563c..08e8947a16136 100644 --- a/google/cloud/storage/well_known_headers.cc +++ b/google/cloud/storage/well_known_headers.cc @@ -56,7 +56,7 @@ std::ostream& operator<<(std::ostream& os, EncryptionKey const& rhs) { char const* prefix = EncryptionKey::prefix(); if (rhs.has_value()) { return os << prefix << "algorithm: " << rhs.value().algorithm << "\n" - << prefix << "key: " << rhs.value().key << "\n" + << prefix << "key: [censored]\n" << prefix << "key-sha256: " << rhs.value().sha256; } return os << prefix << "*: "; @@ -74,7 +74,7 @@ std::ostream& operator<<(std::ostream& os, SourceEncryptionKey const& rhs) { char const* prefix = SourceEncryptionKey::prefix(); if (rhs.has_value()) { return os << prefix << "algorithm: " << rhs.value().algorithm << "\n" - << prefix << "key: " << rhs.value().key << "\n" + << prefix << "key: [censored]\n" << prefix << "key-sha256: " << rhs.value().sha256; } return os << prefix << "*: "; diff --git a/google/cloud/storage/well_known_headers_test.cc b/google/cloud/storage/well_known_headers_test.cc index 76358a9277227..413f816178ab0 100644 --- a/google/cloud/storage/well_known_headers_test.cc +++ b/google/cloud/storage/well_known_headers_test.cc @@ -14,6 +14,7 @@ #include "google/cloud/storage/well_known_headers.h" #include +#include #include namespace google { @@ -23,6 +24,7 @@ GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN namespace { using ::testing::HasSubstr; +using ::testing::Not; /// @test Verify that CustomHeader works as expected. TEST(WellKnownHeader, CustomHeader) { @@ -41,8 +43,9 @@ TEST(WellKnownHeader, EncryptionKey) { os << header; auto actual = os.str(); std::string prefix = "x-goog-encryption"; - EXPECT_THAT(actual, HasSubstr(prefix + "-algorithm: test-algo")); - EXPECT_THAT(actual, HasSubstr(prefix + "-key: test-fake-key")); + EXPECT_THAT(actual, HasSubstr(prefix + "-algorithm: test-algo\n")); + EXPECT_THAT(actual, HasSubstr(prefix + "-key: [censored]\n")); + EXPECT_THAT(actual, Not(HasSubstr("test-fake-key"))); EXPECT_THAT(actual, HasSubstr(prefix + "-key-sha256: test-sha")); } @@ -78,6 +81,10 @@ TEST(WellKnownHeader, EncryptionKeyFromBase64) { EXPECT_EQ(expected.value().algorithm, actual.value().algorithm); EXPECT_EQ(expected.value().key, actual.value().key); EXPECT_EQ(expected.value().sha256, actual.value().sha256); + std::ostringstream os; + os << actual; + EXPECT_THAT(os.str(), HasSubstr("x-goog-encryption-key: [censored]\n")); + EXPECT_THAT(os.str(), Not(HasSubstr(expected.value().key))); } /// @test Verify that SourceEncryptionKey streaming works as expected. @@ -88,11 +95,44 @@ TEST(WellKnownHeader, SourceEncryptionKey) { os << header; auto actual = os.str(); std::string prefix = "x-goog-copy-source-encryption"; - EXPECT_THAT(actual, HasSubstr(prefix + "-algorithm: test-algo")); - EXPECT_THAT(actual, HasSubstr(prefix + "-key: test-fake-key")); + EXPECT_THAT(actual, HasSubstr(prefix + "-algorithm: test-algo\n")); + EXPECT_THAT(actual, HasSubstr(prefix + "-key: [censored]\n")); + EXPECT_THAT(actual, Not(HasSubstr("test-fake-key"))); EXPECT_THAT(actual, HasSubstr(prefix + "-key-sha256: test-sha")); } +/// @test Verify that EncryptionKey and SourceEncryptionKey with an empty key +/// value format as expected. +TEST(WellKnownHeader, EncryptionKeyEmptyString) { + EncryptionKey const key(EncryptionKeyData{"AES256", "", "test-sha"}); + std::ostringstream key_os; + key_os << key; + EXPECT_THAT(key_os.str(), HasSubstr("x-goog-encryption-key: [censored]\n")); + + SourceEncryptionKey const source_key( + EncryptionKeyData{"AES256", "", "test-sha"}); + std::ostringstream source_os; + source_os << source_key; + EXPECT_THAT(source_os.str(), + HasSubstr("x-goog-copy-source-encryption-key: [censored]\n")); +} + +/// @test Verify that an unset EncryptionKey streams ``. +TEST(WellKnownHeader, EncryptionKeyEmpty) { + EncryptionKey const header; + std::ostringstream os; + os << header; + EXPECT_EQ(os.str(), "x-goog-encryption-*: "); +} + +/// @test Verify that an unset SourceEncryptionKey streams ``. +TEST(WellKnownHeader, SourceEncryptionKeyEmpty) { + SourceEncryptionKey const header; + std::ostringstream os; + os << header; + EXPECT_EQ(os.str(), "x-goog-copy-source-encryption-*: "); +} + /// @test Verify that SourceEncryptionKey::FromBinaryKey works as expected. TEST(WellKnownHeader, SourceEncryptionKeyFromBinary) { std::string key("01234567"); @@ -121,6 +161,11 @@ TEST(WellKnownHeader, SourceEncryptionKeyFromBase64) { EXPECT_EQ(expected.value().algorithm, actual.value().algorithm); EXPECT_EQ(expected.value().key, actual.value().key); EXPECT_EQ(expected.value().sha256, actual.value().sha256); + std::ostringstream os; + os << actual; + EXPECT_THAT(os.str(), + HasSubstr("x-goog-copy-source-encryption-key: [censored]\n")); + EXPECT_THAT(os.str(), Not(HasSubstr(expected.value().key))); } /// @test Verify that CreateKeyFromGenerator works as expected. From 252626de8ec23258d12b1ed9936c7c87743163cd Mon Sep 17 00:00:00 2001 From: Gauri Kalra Date: Wed, 25 Nov 2026 06:03:49 +0000 Subject: [PATCH 2/2] rename unset-key tests, separate log expectations --- google/cloud/storage/internal/logging_stub_test.cc | 1 + google/cloud/storage/well_known_headers_test.cc | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/google/cloud/storage/internal/logging_stub_test.cc b/google/cloud/storage/internal/logging_stub_test.cc index e184a24925221..6c8c09f23d386 100644 --- a/google/cloud/storage/internal/logging_stub_test.cc +++ b/google/cloud/storage/internal/logging_stub_test.cc @@ -358,6 +358,7 @@ TEST_F(LoggingStubTest, EnvTracingFormatsEncryptionKey) { EXPECT_THAT(lr.message, HasSubstr("Enabling logging for raw-client")); }); Options const opts = DefaultOptions(Options{}); + ::testing::Mock::VerifyAndClearExpectations(log_backend_.get()); auto mock = std::make_unique(); EXPECT_CALL(*mock, ReadObject) diff --git a/google/cloud/storage/well_known_headers_test.cc b/google/cloud/storage/well_known_headers_test.cc index 413f816178ab0..c2fc9ebdac79d 100644 --- a/google/cloud/storage/well_known_headers_test.cc +++ b/google/cloud/storage/well_known_headers_test.cc @@ -118,7 +118,7 @@ TEST(WellKnownHeader, EncryptionKeyEmptyString) { } /// @test Verify that an unset EncryptionKey streams ``. -TEST(WellKnownHeader, EncryptionKeyEmpty) { +TEST(WellKnownHeader, EncryptionKeyUnset) { EncryptionKey const header; std::ostringstream os; os << header; @@ -126,7 +126,7 @@ TEST(WellKnownHeader, EncryptionKeyEmpty) { } /// @test Verify that an unset SourceEncryptionKey streams ``. -TEST(WellKnownHeader, SourceEncryptionKeyEmpty) { +TEST(WellKnownHeader, SourceEncryptionKeyUnset) { SourceEncryptionKey const header; std::ostringstream os; os << header;