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(GCS+gRPC): implement To/FromProto for HmacKeyMetadata #8926

Merged
merged 2 commits into from
May 9, 2022
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
2 changes: 2 additions & 0 deletions google/cloud/storage/google_cloud_cpp_storage_grpc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ google_cloud_cpp_storage_grpc_hdrs = [
"internal/grpc_client.h",
"internal/grpc_common_request_params.h",
"internal/grpc_configure_client_context.h",
"internal/grpc_hmac_key_metadata_parser.h",
"internal/grpc_object_access_control_parser.h",
"internal/grpc_object_metadata_parser.h",
"internal/grpc_object_read_source.h",
Expand All @@ -46,6 +47,7 @@ google_cloud_cpp_storage_grpc_srcs = [
"internal/grpc_bucket_metadata_parser.cc",
"internal/grpc_bucket_request_parser.cc",
"internal/grpc_client.cc",
"internal/grpc_hmac_key_metadata_parser.cc",
"internal/grpc_object_access_control_parser.cc",
"internal/grpc_object_metadata_parser.cc",
"internal/grpc_object_read_source.cc",
Expand Down
3 changes: 3 additions & 0 deletions google/cloud/storage/google_cloud_cpp_storage_grpc.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ else ()
internal/grpc_client.h
internal/grpc_common_request_params.h
internal/grpc_configure_client_context.h
internal/grpc_hmac_key_metadata_parser.cc
internal/grpc_hmac_key_metadata_parser.h
internal/grpc_object_access_control_parser.cc
internal/grpc_object_access_control_parser.h
internal/grpc_object_metadata_parser.cc
Expand Down Expand Up @@ -165,6 +167,7 @@ if (BUILD_TESTING AND GOOGLE_CLOUD_CPP_STORAGE_ENABLE_GRPC)
internal/grpc_client_read_object_test.cc
internal/grpc_client_test.cc
internal/grpc_configure_client_context_test.cc
internal/grpc_hmac_key_metadata_parser_test.cc
internal/grpc_object_access_control_parser_test.cc
internal/grpc_object_metadata_parser_test.cc
internal/grpc_object_read_source_test.cc
Expand Down
2 changes: 2 additions & 0 deletions google/cloud/storage/hmac_key_metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace storage {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
namespace internal {
struct HmacKeyMetadataParser;
struct GrpcHmacKeyMetadataParser;
} // namespace internal

/**
Expand Down Expand Up @@ -79,6 +80,7 @@ class HmacKeyMetadata {

private:
friend struct internal::HmacKeyMetadataParser;
friend struct internal::GrpcHmacKeyMetadataParser;
friend std::ostream& operator<<(std::ostream& os, HmacKeyMetadata const& rhs);

// Keep the fields in alphabetical order.
Expand Down
66 changes: 66 additions & 0 deletions google/cloud/storage/internal/grpc_hmac_key_metadata_parser.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "google/cloud/storage/internal/grpc_hmac_key_metadata_parser.h"
#include "google/cloud/internal/time_utils.h"
#include "absl/strings/string_view.h"
#include "absl/strings/strip.h"

namespace google {
namespace cloud {
namespace storage {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
namespace internal {
devbww marked this conversation as resolved.
Show resolved Hide resolved

HmacKeyMetadata GrpcHmacKeyMetadataParser::FromProto(
google::storage::v2::HmacKeyMetadata const& rhs) {
HmacKeyMetadata result;
result.id_ = rhs.id();
result.access_id_ = rhs.access_id();
// The protos use `projects/{project}` format, but the field may be absent or
// may have a project id (instead of number), so we need to do some parsing.
// We are forgiving here. It is better to drop one field rather than dropping
// the full message.
absl::string_view project = rhs.project();
absl::ConsumePrefix(&project, "projects/");
result.project_id_ = std::string(project);
result.service_account_email_ = rhs.service_account_email();
result.state_ = rhs.state();
result.time_created_ =
google::cloud::internal::ToChronoTimePoint(rhs.create_time());
result.updated_ =
google::cloud::internal::ToChronoTimePoint(rhs.update_time());
return result;
}

google::storage::v2::HmacKeyMetadata GrpcHmacKeyMetadataParser::ToProto(
HmacKeyMetadata const& rhs) {
google::storage::v2::HmacKeyMetadata result;
result.set_id(rhs.id());
result.set_access_id(rhs.access_id());
result.set_project("projects/" + rhs.project_id());
result.set_service_account_email(rhs.service_account_email());
result.set_state(rhs.state());
*result.mutable_create_time() =
google::cloud::internal::ToProtoTimestamp(rhs.time_created());
*result.mutable_update_time() =
google::cloud::internal::ToProtoTimestamp(rhs.updated());
return result;
}

} // namespace internal
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace storage
} // namespace cloud
} // namespace google
42 changes: 42 additions & 0 deletions google/cloud/storage/internal/grpc_hmac_key_metadata_parser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_GRPC_HMAC_KEY_METADATA_PARSER_H
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_GRPC_HMAC_KEY_METADATA_PARSER_H

#include "google/cloud/storage/hmac_key_metadata.h"
#include "google/cloud/storage/version.h"
#include "google/cloud/options.h"
#include <google/storage/v2/storage.pb.h>

namespace google {
namespace cloud {
namespace storage {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
namespace internal {

struct GrpcHmacKeyMetadataParser {
devbww marked this conversation as resolved.
Show resolved Hide resolved
static HmacKeyMetadata FromProto(
google::storage::v2::HmacKeyMetadata const& rhs);
static google::storage::v2::HmacKeyMetadata ToProto(
HmacKeyMetadata const& rhs);
};

} // namespace internal
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace storage
} // namespace cloud
} // namespace google

#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_GRPC_HMAC_KEY_METADATA_PARSER_H
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "google/cloud/storage/internal/grpc_hmac_key_metadata_parser.h"
#include "google/cloud/storage/internal/hmac_key_metadata_parser.h"
#include "google/cloud/internal/format_time_point.h"
#include "google/cloud/testing_util/is_proto_equal.h"
#include <google/protobuf/text_format.h>
#include <gmock/gmock.h>

namespace google {
namespace cloud {
namespace storage {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
namespace internal {
namespace {

namespace storage_proto = ::google::storage::v2;
using ::google::cloud::internal::FormatRfc3339;
using ::google::cloud::testing_util::IsProtoEqual;
using ::google::protobuf::TextFormat;

TEST(GrpcHmacKeyMetadataParser, Roundtrip) {
storage_proto::HmacKeyMetadata input;
auto constexpr kProtoText = R"pb(
id: "test-id"
access_id: "test-access-id"
project: "projects/test-project"
service_account_email: "test-account@test-project.test"
state: "INACTIVE"
create_time { seconds: 1652099696 nanos: 789000000 }
update_time { seconds: 1652186096 nanos: 789000000 })pb";
EXPECT_TRUE(TextFormat::ParseFromString(kProtoText, &input));

auto const actual = GrpcHmacKeyMetadataParser::FromProto(input);
EXPECT_EQ(actual.id(), "test-id");
EXPECT_EQ(actual.access_id(), "test-access-id");
EXPECT_EQ(actual.state(), "INACTIVE");
// To get the dates in RFC-3339 format I used:
// date --rfc-3339=seconds --date=@1652099696 # Create
// date --rfc-3339=seconds --date=@1652186096 # Update
EXPECT_EQ(FormatRfc3339(actual.time_created()), "2022-05-09T12:34:56.789Z");
EXPECT_EQ(FormatRfc3339(actual.updated()), "2022-05-10T12:34:56.789Z");
auto const output = GrpcHmacKeyMetadataParser::ToProto(actual);
EXPECT_THAT(output, IsProtoEqual(input));
}

} // namespace
} // namespace internal
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace storage
} // namespace cloud
} // namespace google
1 change: 1 addition & 0 deletions google/cloud/storage/storage_client_grpc_unit_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ storage_client_grpc_unit_tests = [
"internal/grpc_client_read_object_test.cc",
"internal/grpc_client_test.cc",
"internal/grpc_configure_client_context_test.cc",
"internal/grpc_hmac_key_metadata_parser_test.cc",
"internal/grpc_object_access_control_parser_test.cc",
"internal/grpc_object_metadata_parser_test.cc",
"internal/grpc_object_read_source_test.cc",
Expand Down