diff --git a/google/cloud/storage/internal/grpc/bucket_metadata_parser.cc b/google/cloud/storage/internal/grpc/bucket_metadata_parser.cc index 36f159ace5da2..5cd2fe25f61ed 100644 --- a/google/cloud/storage/internal/grpc/bucket_metadata_parser.cc +++ b/google/cloud/storage/internal/grpc/bucket_metadata_parser.cc @@ -113,6 +113,10 @@ google::storage::v2::Bucket ToProto(storage::BucketMetadata const& rhs) { if (rhs.has_soft_delete_policy()) { *result.mutable_soft_delete_policy() = ToProto(rhs.soft_delete_policy()); } + if (rhs.has_object_retention()) { + result.mutable_object_retention()->set_enabled( + rhs.object_retention().enabled); + } return result; } @@ -161,6 +165,10 @@ storage::BucketMetadata FromProto(google::storage::v2::Bucket const& rhs, if (rhs.has_logging()) metadata.set_logging(FromProto(rhs.logging())); metadata.set_metageneration(rhs.metageneration()); metadata.set_name(GrpcBucketNameToId(rhs.name())); + if (rhs.has_object_retention()) { + metadata.set_object_retention( + storage::BucketObjectRetention{rhs.object_retention().enabled()}); + } if (rhs.has_owner()) { metadata.set_owner(storage_internal::FromProto(rhs.owner())); } diff --git a/google/cloud/storage/internal/grpc/bucket_metadata_parser_test.cc b/google/cloud/storage/internal/grpc/bucket_metadata_parser_test.cc index cb785d50a9d54..464360449304b 100644 --- a/google/cloud/storage/internal/grpc/bucket_metadata_parser_test.cc +++ b/google/cloud/storage/internal/grpc/bucket_metadata_parser_test.cc @@ -133,6 +133,7 @@ TEST(GrpcBucketMetadataParser, BucketAllFieldsRoundtrip) { effective_time { seconds: 1708000496 nanos: 700000000 } retention_duration { seconds: 864000 } } + object_retention { enabled: true } )pb"; EXPECT_TRUE(google::protobuf::TextFormat::ParseFromString(kText, &input)); @@ -254,6 +255,9 @@ TEST(GrpcBucketMetadataParser, BucketAllFieldsRoundtrip) { "softDeletePolicy": { "effectiveTime": "2024-02-15T12:34:56.700Z", "retentionDurationSeconds": "864000" + }, + "objectRetention": { + "mode": "Enabled" } })"""); ASSERT_THAT(expected, IsOk()); @@ -577,6 +581,23 @@ TEST(GrpcBucketMetadataParser, BucketCustomPlacementConfigRoundtrip) { EXPECT_THAT(end, IsProtoEqual(start)); } +TEST(GrpcBucketMetadataParser, BucketObjectRetentionRoundtrip) { + google::storage::v2::Bucket input; + storage::BucketMetadata const empty_metadata = FromProto(input, Options{}); + EXPECT_FALSE(empty_metadata.has_object_retention()); + google::storage::v2::Bucket const empty_roundtrip = ToProto(empty_metadata); + EXPECT_FALSE(empty_roundtrip.has_object_retention()); + + input.mutable_object_retention()->set_enabled(false); + storage::BucketMetadata const metadata = FromProto(input, Options{}); + ASSERT_TRUE(metadata.has_object_retention()); + EXPECT_EQ(metadata.object_retention(), storage::BucketObjectRetention{false}); + + google::storage::v2::Bucket const roundtrip = ToProto(metadata); + ASSERT_TRUE(roundtrip.has_object_retention()); + EXPECT_FALSE(roundtrip.object_retention().enabled()); +} + } // namespace GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END } // namespace storage_internal diff --git a/google/cloud/storage/internal/grpc/bucket_request_parser.cc b/google/cloud/storage/internal/grpc/bucket_request_parser.cc index 67e334e49c0ea..e77b9a58eb8db 100644 --- a/google/cloud/storage/internal/grpc/bucket_request_parser.cc +++ b/google/cloud/storage/internal/grpc/bucket_request_parser.cc @@ -418,6 +418,10 @@ google::storage::v2::CreateBucketRequest ToProto( result.set_predefined_default_object_acl( request.GetOption().value()); } + if (request.HasOption()) { + result.set_enable_object_retention( + request.GetOption().value()); + } *result.mutable_bucket() = storage_internal::ToProto(request.metadata()); result.mutable_bucket()->set_project("projects/" + request.project_id()); // Ignore fields commonly set by ToProto(). @@ -425,6 +429,8 @@ google::storage::v2::CreateBucketRequest ToProto( result.mutable_bucket()->set_bucket_id(""); result.mutable_bucket()->clear_create_time(); result.mutable_bucket()->clear_update_time(); + // REST uses only the enableObjectRetention query parameter. + result.mutable_bucket()->clear_object_retention(); return result; } diff --git a/google/cloud/storage/internal/grpc/bucket_request_parser_test.cc b/google/cloud/storage/internal/grpc/bucket_request_parser_test.cc index 95d9a026736c9..e6fe2a4b2315b 100644 --- a/google/cloud/storage/internal/grpc/bucket_request_parser_test.cc +++ b/google/cloud/storage/internal/grpc/bucket_request_parser_test.cc @@ -129,6 +129,7 @@ TEST(GrpcBucketRequestParser, CreateBucketMetadataAllOptions) { } predefined_acl: "projectPrivate" predefined_default_object_acl: "private" + enable_object_retention: true )pb", &expected)); @@ -167,8 +168,10 @@ TEST(GrpcBucketRequestParser, CreateBucketMetadataAllOptions) { storage::LifecycleRule::Delete())}}) .set_logging(storage::BucketLogging{ /*.log_bucket=*/"test-log-bucket", - /*.log_object_prefix=*/"test-log-object-prefix"})); + /*.log_object_prefix=*/"test-log-object-prefix"}) + .set_object_retention(storage::BucketObjectRetention{true})); req.set_multiple_options( + storage::EnableObjectRetention(true), storage::PredefinedAcl::ProjectPrivate(), storage::PredefinedDefaultObjectAcl::Private(), storage::Projection("full"), storage::UserProject("test-user-project"), diff --git a/google/cloud/storage/internal/grpc/object_metadata_parser.cc b/google/cloud/storage/internal/grpc/object_metadata_parser.cc index 2e321f070c8f2..932bf5d1014a7 100644 --- a/google/cloud/storage/internal/grpc/object_metadata_parser.cc +++ b/google/cloud/storage/internal/grpc/object_metadata_parser.cc @@ -20,7 +20,10 @@ #include "google/cloud/storage/internal/md5hash.h" #include "google/cloud/storage/version.h" #include "google/cloud/internal/big_endian.h" +#include "google/cloud/internal/make_status.h" #include "google/cloud/internal/time_utils.h" +#include "absl/strings/match.h" +#include "absl/strings/str_cat.h" namespace google { namespace cloud { @@ -46,6 +49,51 @@ StatusOr ToProto( return result; } +StatusOr ToProto( + storage::ObjectRetention const& rhs) { + google::storage::v2::Object::Retention result; + // Accept canonical TitleCase ("Unlocked"/"Locked") and uppercase proto enum + // names. + if (rhs.mode.empty()) { + result.set_mode(google::storage::v2::Object::Retention::MODE_UNSPECIFIED); + } else if (absl::EqualsIgnoreCase(rhs.mode, + storage::ObjectRetentionUnlocked())) { + result.set_mode(google::storage::v2::Object::Retention::UNLOCKED); + } else if (absl::EqualsIgnoreCase(rhs.mode, + storage::ObjectRetentionLocked())) { + result.set_mode(google::storage::v2::Object::Retention::LOCKED); + } else { + return google::cloud::internal::InvalidArgumentError( + absl::StrCat("unrecognized object retention mode: ", rhs.mode), + GCP_ERROR_INFO()); + } + if (rhs.retain_until_time != std::chrono::system_clock::time_point{}) { + *result.mutable_retain_until_time() = + google::cloud::internal::ToProtoTimestamp(rhs.retain_until_time); + } + return result; +} + +storage::ObjectRetention FromProto( + google::storage::v2::Object::Retention const& rhs) { + storage::ObjectRetention result; + switch (rhs.mode()) { + case google::storage::v2::Object::Retention::UNLOCKED: + result.mode = storage::ObjectRetentionUnlocked(); + break; + case google::storage::v2::Object::Retention::LOCKED: + result.mode = storage::ObjectRetentionLocked(); + break; + default: + break; + } + if (rhs.has_retain_until_time()) { + result.retain_until_time = + google::cloud::internal::ToChronoTimePoint(rhs.retain_until_time()); + } + return result; +} + std::string Crc32cFromProto(std::uint32_t v) { auto endian_encoded = google::cloud::internal::EncodeBigEndian(v); return storage::internal::Base64Encode(endian_encoded); @@ -148,6 +196,9 @@ storage::ObjectMetadata FromProto(google::storage::v2::Object object, google::cloud::internal::ToChronoTimePoint( object.retention_expire_time())); } + if (object.has_retention()) { + metadata.set_retention(FromProto(object.retention())); + } metadata.set_size(static_cast(object.size())); metadata.set_temporary_hold(object.temporary_hold()); if (object.has_delete_time()) { diff --git a/google/cloud/storage/internal/grpc/object_metadata_parser.h b/google/cloud/storage/internal/grpc/object_metadata_parser.h index 64a7feffac0ae..ecbc1b61fa466 100644 --- a/google/cloud/storage/internal/grpc/object_metadata_parser.h +++ b/google/cloud/storage/internal/grpc/object_metadata_parser.h @@ -31,6 +31,11 @@ StatusOr ToProto( storage::CustomerEncryption FromProto( google::storage::v2::CustomerEncryption rhs); +StatusOr ToProto( + storage::ObjectRetention const& rhs); +storage::ObjectRetention FromProto( + google::storage::v2::Object::Retention const& rhs); + std::string Crc32cFromProto(std::uint32_t); StatusOr Crc32cToProto(std::string const&); std::string MD5FromProto(std::string const&); diff --git a/google/cloud/storage/internal/grpc/object_metadata_parser_test.cc b/google/cloud/storage/internal/grpc/object_metadata_parser_test.cc index a3aca20736095..8d227f0965a86 100644 --- a/google/cloud/storage/internal/grpc/object_metadata_parser_test.cc +++ b/google/cloud/storage/internal/grpc/object_metadata_parser_test.cc @@ -19,6 +19,7 @@ #include "google/cloud/internal/sha256_hash.h" #include "google/cloud/testing_util/is_proto_equal.h" #include "google/cloud/testing_util/status_matchers.h" +#include "absl/strings/str_cat.h" #include #include @@ -31,6 +32,7 @@ namespace { namespace storage_proto = ::google::storage::v2; using ::google::cloud::internal::HexDecode; using ::google::cloud::testing_util::IsProtoEqual; +using ::google::cloud::testing_util::StatusIs; TEST(GrpcClientFromProto, ObjectSimple) { storage_proto::Object input; @@ -130,6 +132,13 @@ TEST(GrpcClientFromProto, ObjectSimple) { seconds: 1714979289 nanos: 123456789 } + retention { + mode: UNLOCKED + retain_until_time { + seconds: 1714979289 + nanos: 123456789 + } + } )""", &input)); @@ -204,7 +213,11 @@ TEST(GrpcClientFromProto, ObjectSimple) { "etag": "test-etag", "softDeleteTime": "2024-03-04T12:34:56.987654321Z", "hardDeleteTime": "2024-03-11T12:34:56.987654321Z", - "timeFinalized": "2024-05-06T07:08:09.123456789Z" + "timeFinalized": "2024-05-06T07:08:09.123456789Z", + "retention": { + "mode": "Unlocked", + "retainUntilTime": "2024-05-06T07:08:09.123456789Z" + } })"""); ASSERT_STATUS_OK(expected); @@ -222,6 +235,7 @@ TEST(GrpcClientFromProto, ObjectWithoutFinalizeTime) { ASSERT_TRUE(google::protobuf::TextFormat::ParseFromString(kText, &input)); storage::ObjectMetadata const actual = FromProto(input, Options{}); EXPECT_FALSE(actual.has_finalize_time()); + EXPECT_FALSE(actual.has_retention()); } TEST(GrpcClientFromProto, ObjectCustomerEncryptionRoundtrip) { @@ -275,6 +289,59 @@ TEST(GrpcClientFromProto, MD5Roundtrip) { } } +TEST(GrpcClientFromProto, ObjectRetentionRoundtrip) { + std::chrono::system_clock::time_point const tp = + std::chrono::system_clock::time_point{} + + std::chrono::seconds(1714979289) + std::chrono::microseconds(123456); + struct TestCase { + std::string text_proto; + storage::ObjectRetention expected; + } cases[] = { + {R"pb( + mode: UNLOCKED + retain_until_time { seconds: 1714979289 nanos: 123456000 } + )pb", + storage::ObjectRetention{storage::ObjectRetentionUnlocked(), tp}}, + {R"pb( + mode: LOCKED + retain_until_time { seconds: 1714979289 nanos: 123456000 } + )pb", + storage::ObjectRetention{storage::ObjectRetentionLocked(), tp}}, + {R"pb( + mode: MODE_UNSPECIFIED + )pb", + storage::ObjectRetention{}}, + }; + for (auto const& test : cases) { + SCOPED_TRACE(absl::StrCat("Testing with text_proto=", test.text_proto)); + google::storage::v2::Object::Retention start; + ASSERT_TRUE( + google::protobuf::TextFormat::ParseFromString(test.text_proto, &start)); + storage::ObjectRetention const middle = FromProto(start); + EXPECT_EQ(middle, test.expected); + StatusOr const end = + ToProto(middle); + ASSERT_STATUS_OK(end); + EXPECT_THAT(*end, IsProtoEqual(start)); + } + + // Verify uppercase and lowercase mode strings match. + StatusOr const unlocked_upper = + ToProto(storage::ObjectRetention{"UNLOCKED", tp}); + ASSERT_STATUS_OK(unlocked_upper); + EXPECT_EQ(unlocked_upper->mode(), + google::storage::v2::Object::Retention::UNLOCKED); + + StatusOr const locked_lower = + ToProto(storage::ObjectRetention{"locked", tp}); + ASSERT_STATUS_OK(locked_lower); + EXPECT_EQ(locked_lower->mode(), + google::storage::v2::Object::Retention::LOCKED); + + EXPECT_THAT(ToProto(storage::ObjectRetention{"test-only-invalid", tp}), + StatusIs(StatusCode::kInvalidArgument)); +} + } // namespace GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END } // namespace storage_internal diff --git a/google/cloud/storage/internal/grpc/object_request_parser.cc b/google/cloud/storage/internal/grpc/object_request_parser.cc index 83b2ed6b780f5..cc1792a8a67a1 100644 --- a/google/cloud/storage/internal/grpc/object_request_parser.cc +++ b/google/cloud/storage/internal/grpc/object_request_parser.cc @@ -17,6 +17,7 @@ #include "google/cloud/storage/internal/grpc/bucket_name.h" #include "google/cloud/storage/internal/grpc/object_access_control_parser.h" #include "google/cloud/storage/internal/grpc/object_metadata_parser.h" +#include "google/cloud/storage/internal/metadata_parser.h" #include "google/cloud/storage/internal/object_access_control_parser.h" #include "google/cloud/storage/internal/patch_builder_details.h" #include "google/cloud/internal/invoke_result.h" @@ -116,6 +117,13 @@ void SetResourceOptions(google::storage::v2::Object& resource, } } +Status SetRetention(Object& object, storage::ObjectRetention const& retention) { + StatusOr proto = storage_internal::ToProto(retention); + if (!proto) return std::move(proto).status(); + *object.mutable_retention() = *std::move(proto); + return Status{}; +} + template Status SetObjectMetadata(google::storage::v2::Object& resource, StorageRequest const& req) { @@ -164,6 +172,9 @@ Status SetObjectMetadata(google::storage::v2::Object& resource, custom_map[kv.first].set_value(kv.second.value); } } + if (metadata.has_retention()) { + return SetRetention(resource, metadata.retention()); + } return Status{}; } @@ -221,6 +232,18 @@ Status PatchTemporaryHold(Object& o, nlohmann::json const& p) { return Status{}; } +Status PatchRetention(Object& o, nlohmann::json const& p) { + if (p.is_null()) { + o.clear_retention(); + return Status{}; + } + StatusOr ts = + storage::internal::ParseTimestampField(p, "retainUntilTime"); + if (!ts) return std::move(ts).status(); + return SetRetention( + o, storage::ObjectRetention{p.value("mode", std::string{}), *ts}); +} + template StatusOr ToProtoImpl( Request const& request) { @@ -347,6 +370,10 @@ StatusOr ToProto( *destination.mutable_custom_time() = google::cloud::internal::ToProtoTimestamp(metadata.custom_time()); } + if (metadata.has_retention()) { + status = SetRetention(destination, metadata.retention()); + if (!status.ok()) return status; + } } for (auto const& s : request.source_objects()) { google::storage::v2::ComposeObjectRequest::SourceObject source; @@ -480,6 +507,10 @@ StatusOr ToProto( SetGenerationConditions(result, request); SetMetagenerationConditions(result, request); SetPredefinedAcl(result, request); + if (request.HasOption()) { + result.set_override_unlocked_retention( + request.GetOption().value()); + } auto& object = *result.mutable_object(); object.set_bucket(GrpcBucketIdToName(request.bucket_name())); @@ -497,6 +528,7 @@ StatusOr ToProto( {"customTime", "custom_time", PatchCustomTime}, {"eventBasedHold", "event_based_hold", PatchEventBasedHold}, {"temporaryHold", "temporary_hold", PatchTemporaryHold}, + {"retention", "retention", PatchRetention}, }; for (auto const& field : fields) { @@ -546,6 +578,10 @@ StatusOr ToProto( SetGenerationConditions(result, request); SetMetagenerationConditions(result, request); SetPredefinedAcl(result, request); + if (request.HasOption()) { + result.set_override_unlocked_retention( + request.GetOption().value()); + } auto& object = *result.mutable_object(); object.set_bucket(GrpcBucketIdToName(request.bucket_name())); @@ -581,6 +617,16 @@ StatusOr ToProto( *object.mutable_custom_time() = google::cloud::internal::ToProtoTimestamp( request.metadata().custom_time()); } + if (request.metadata().has_retention()) { + status = SetRetention(object, request.metadata().retention()); + if (!status.ok()) return status; + result.mutable_update_mask()->add_paths("retention"); + } else if (request.GetOption().value_or( + false)) { + // Like a REST PUT without "retention", clear the field only when the + // caller explicitly allows unlocked retention changes. + result.mutable_update_mask()->add_paths("retention"); + } // We need to check each modifiable field. result.mutable_update_mask()->add_paths("cache_control"); diff --git a/google/cloud/storage/internal/grpc/object_request_parser_test.cc b/google/cloud/storage/internal/grpc/object_request_parser_test.cc index 1abb3ae4a575a..012ab7f9b5e93 100644 --- a/google/cloud/storage/internal/grpc/object_request_parser_test.cc +++ b/google/cloud/storage/internal/grpc/object_request_parser_test.cc @@ -97,6 +97,10 @@ google::storage::v2::Object ExpectedFullObjectMetadata() { } event_based_hold: true custom_time { seconds: 1643126687 nanos: 123000000 } + retention { + mode: UNLOCKED + retain_until_time { seconds: 1643126687 nanos: 123000000 } + } )pb"; google::storage::v2::Object proto; if (TextFormat::ParseFromString(kProto, &proto)) return proto; @@ -125,7 +129,12 @@ storage::ObjectMetadata FullObjectMetadata() { .set_event_based_hold(true) .set_custom_time(std::chrono::system_clock::time_point{} + std::chrono::seconds(1643126687) + - std::chrono::milliseconds(123)); + std::chrono::milliseconds(123)) + .set_retention( + storage::ObjectRetention{storage::ObjectRetentionUnlocked(), + std::chrono::system_clock::time_point{} + + std::chrono::seconds(1643126687) + + std::chrono::milliseconds(123)}); } google::storage::v2::CommonObjectRequestParams @@ -466,6 +475,7 @@ TEST(GrpcObjectRequestParser, PatchObjectRequestAllOptions) { if_generation_not_match: 2 if_metageneration_match: 3 if_metageneration_not_match: 4 + override_unlocked_retention: true update_mask {} )pb"; google::storage::v2::UpdateObjectRequest expected; @@ -501,11 +511,17 @@ TEST(GrpcObjectRequestParser, PatchObjectRequestAllOptions) { .SetEventBasedHold(true) .SetCustomTime(std::chrono::system_clock::time_point{} + std::chrono::seconds(1643126687) + - std::chrono::milliseconds(123))); + std::chrono::milliseconds(123)) + .SetRetention( + storage::ObjectRetention{storage::ObjectRetentionUnlocked(), + std::chrono::system_clock::time_point{} + + std::chrono::seconds(1643126687) + + std::chrono::milliseconds(123)})); req.set_multiple_options( storage::Generation(7), storage::IfGenerationMatch(1), storage::IfGenerationNotMatch(2), storage::IfMetagenerationMatch(3), storage::IfMetagenerationNotMatch(4), + storage::OverrideUnlockedRetention(true), storage::PredefinedAcl("projectPrivate"), storage::EncryptionKey::FromBinaryKey("01234567"), storage::Projection("full"), storage::UserProject("test-user-project"), @@ -521,7 +537,7 @@ TEST(GrpcObjectRequestParser, PatchObjectRequestAllOptions) { "acl", "content_encoding", "content_disposition", "cache_control", "content_language", "content_type", "metadata.test-metadata-key1", "metadata.test-metadata-key2", "temporary_hold", "event_based_hold", - "custom_time", "contexts.custom.custom-key-1")); + "custom_time", "contexts.custom.custom-key-1", "retention")); // Clear the paths, which we already compared, and compare the proto. actual->mutable_update_mask()->clear_paths(); EXPECT_THAT(*actual, IsProtoEqual(expected)); @@ -548,7 +564,8 @@ TEST(GrpcObjectRequestParser, PatchObjectRequestAllResets) { .ResetMetadata() .ResetContexts() .ResetTemporaryHold() - .ResetCustomTime()); + .ResetCustomTime() + .ResetRetention()); auto actual = ToProto(req); ASSERT_STATUS_OK(actual); @@ -559,7 +576,7 @@ TEST(GrpcObjectRequestParser, PatchObjectRequestAllResets) { UnorderedElementsAre("acl", "content_encoding", "content_disposition", "cache_control", "content_language", "content_type", "metadata", "temporary_hold", "event_based_hold", - "custom_time", "contexts.custom")); + "custom_time", "contexts.custom", "retention")); // Clear the paths, which we already compared, and compare the proto. actual->mutable_update_mask()->clear_paths(); EXPECT_THAT(*actual, IsProtoEqual(expected)); @@ -681,6 +698,7 @@ TEST(GrpcObjectRequestParser, UpdateObjectRequestAllOptions) { if_generation_not_match: 2 if_metageneration_match: 3 if_metageneration_not_match: 4 + override_unlocked_retention: true update_mask {} )pb"; google::storage::v2::UpdateObjectRequest expected; @@ -699,6 +717,7 @@ TEST(GrpcObjectRequestParser, UpdateObjectRequestAllOptions) { storage::Generation(7), storage::IfGenerationMatch(1), storage::IfGenerationNotMatch(2), storage::IfMetagenerationMatch(3), storage::IfMetagenerationNotMatch(4), + storage::OverrideUnlockedRetention(true), storage::PredefinedAcl("projectPrivate"), storage::EncryptionKey::FromBinaryKey("01234567"), storage::Projection("full"), storage::UserProject("test-user-project"), @@ -713,7 +732,7 @@ TEST(GrpcObjectRequestParser, UpdateObjectRequestAllOptions) { UnorderedElementsAre("acl", "content_encoding", "content_disposition", "cache_control", "content_language", "content_type", "metadata", "temporary_hold", "event_based_hold", - "custom_time", "contexts")); + "custom_time", "contexts", "retention")); // Clear the paths, which we already compared, and test the rest actual->mutable_update_mask()->clear_paths(); EXPECT_THAT(*actual, IsProtoEqual(expected)); @@ -1463,6 +1482,88 @@ TEST(GrpcObjectRequestParser, MaybeFinalizeUploadChunkRequest) { } } +TEST(GrpcObjectRequestParser, UpdateObjectRequestRetentionMask) { + struct TestCase { + std::optional override_unlocked; + bool expected_override; + bool expect_retention_path; + } const cases[] = { + {std::nullopt, false, false}, + {false, false, false}, + {true, true, true}, + }; + for (TestCase const& test : cases) { + SCOPED_TRACE( + test.override_unlocked.has_value() + ? (*test.override_unlocked ? "override=true" : "override=false") + : "override=unset"); + storage::internal::UpdateObjectRequest req("bucket-name", "object-name", + storage::ObjectMetadata{}); + if (test.override_unlocked.has_value()) { + req.set_multiple_options( + storage::OverrideUnlockedRetention(*test.override_unlocked)); + } + StatusOr actual = ToProto(req); + ASSERT_STATUS_OK(actual); + EXPECT_EQ(actual->override_unlocked_retention(), test.expected_override); + EXPECT_FALSE(actual->object().has_retention()); + if (test.expect_retention_path) { + EXPECT_THAT(actual->update_mask().paths(), + testing::Contains("retention")); + } else { + EXPECT_THAT(actual->update_mask().paths(), + testing::Not(testing::Contains("retention"))); + } + } +} + +TEST(GrpcObjectRequestParser, ObjectRequestInvalidRetentionMode) { + storage::ObjectMetadata const invalid_metadata = + storage::ObjectMetadata{}.set_retention(storage::ObjectRetention{ + "test-only-invalid", std::chrono::system_clock::time_point{} + + std::chrono::seconds(1643126687)}); + + storage::internal::InsertObjectMediaRequest insert_req("bucket-name", + "object-name", kText); + insert_req.set_multiple_options( + storage::WithObjectMetadata(invalid_metadata)); + EXPECT_THAT(ToProto(insert_req), StatusIs(StatusCode::kInvalidArgument)); + + storage::internal::ResumableUploadRequest resumable_req("bucket-name", + "object-name"); + resumable_req.set_multiple_options( + storage::WithObjectMetadata(invalid_metadata)); + EXPECT_THAT(ToProto(resumable_req), StatusIs(StatusCode::kInvalidArgument)); + + storage::internal::CopyObjectRequest copy_req( + "source-bucket", "source-object", "bucket-name", "object-name"); + copy_req.set_multiple_options(storage::WithObjectMetadata(invalid_metadata)); + EXPECT_THAT(ToProto(copy_req), StatusIs(StatusCode::kInvalidArgument)); + + storage::internal::RewriteObjectRequest rewrite_req( + "source-bucket", "source-object", "bucket-name", "object-name", + "test-rewrite-token"); + rewrite_req.set_multiple_options( + storage::WithObjectMetadata(invalid_metadata)); + EXPECT_THAT(ToProto(rewrite_req), StatusIs(StatusCode::kInvalidArgument)); + + storage::internal::ComposeObjectRequest compose_req("bucket-name", {}, + "object-name"); + compose_req.set_multiple_options( + storage::WithObjectMetadata(invalid_metadata)); + EXPECT_THAT(ToProto(compose_req), StatusIs(StatusCode::kInvalidArgument)); + + storage::internal::UpdateObjectRequest update_req( + "bucket-name", "object-name", invalid_metadata); + EXPECT_THAT(ToProto(update_req), StatusIs(StatusCode::kInvalidArgument)); + + storage::internal::PatchObjectRequest patch_req( + "bucket-name", "object-name", + storage::ObjectMetadataPatchBuilder{}.SetRetention( + invalid_metadata.retention())); + EXPECT_THAT(ToProto(patch_req), StatusIs(StatusCode::kInvalidArgument)); +} + } // namespace GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END } // namespace storage_internal diff --git a/google/cloud/storage/internal/grpc/stub.cc b/google/cloud/storage/internal/grpc/stub.cc index 17a1b4bb210b0..8bcc20f36979e 100644 --- a/google/cloud/storage/internal/grpc/stub.cc +++ b/google/cloud/storage/internal/grpc/stub.cc @@ -407,7 +407,8 @@ StatusOr GrpcStub::InsertObjectMedia( StatusOr GrpcStub::CopyObject( rest_internal::RestContext& context, Options const& options, storage::internal::CopyObjectRequest const& request) { - auto proto = ToProto(request); + StatusOr proto = ToProto(request); + if (!proto) return std::move(proto).status(); grpc::ClientContext ctx; ApplyQueryParameters(ctx, options, request); ApplyRestContextHeaders(ctx, context); diff --git a/google/cloud/storage/internal/grpc/stub_test.cc b/google/cloud/storage/internal/grpc/stub_test.cc index 1837a7ed81e68..9ad6771316e07 100644 --- a/google/cloud/storage/internal/grpc/stub_test.cc +++ b/google/cloud/storage/internal/grpc/stub_test.cc @@ -557,6 +557,24 @@ TEST_F(GrpcClientTest, CopyObjectTooLarge) { EXPECT_THAT(response.status(), StatusIs(StatusCode::kOutOfRange)); } +TEST_F(GrpcClientTest, CopyObjectInvalidRetentionMode) { + auto mock = std::make_shared(); + EXPECT_CALL(*mock, RewriteObject).Times(0); + std::unique_ptr client = CreateTestClient(mock); + rest_internal::RestContext context = TestContext(); + storage::ObjectMetadata const invalid_metadata = + storage::ObjectMetadata{}.set_retention(storage::ObjectRetention{ + "test-only-invalid", std::chrono::system_clock::time_point{} + + std::chrono::seconds(1643126687)}); + StatusOr response = client->CopyObject( + context, TestOptions(), + storage::internal::CopyObjectRequest("test-source-bucket", + "test-source-object", "test-bucket", + "test-object") + .set_multiple_options(storage::WithObjectMetadata(invalid_metadata))); + EXPECT_THAT(response.status(), StatusIs(StatusCode::kInvalidArgument)); +} + TEST_F(GrpcClientTest, GetObjectMetadata) { auto mock = std::make_shared(); EXPECT_CALL(*mock, GetObject)