-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pubsub): implement GUAC for SchemaAdmin (#7436)
- Loading branch information
Showing
10 changed files
with
357 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright 2021 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 | ||
// | ||
// http://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/pubsub/internal/schema_auth.h" | ||
#include "google/cloud/internal/log_wrapper.h" | ||
|
||
namespace google { | ||
namespace cloud { | ||
namespace pubsub_internal { | ||
inline namespace GOOGLE_CLOUD_CPP_PUBSUB_NS { | ||
|
||
StatusOr<google::pubsub::v1::Schema> SchemaAuth::CreateSchema( | ||
grpc::ClientContext& context, | ||
google::pubsub::v1::CreateSchemaRequest const& request) { | ||
auto status = auth_->ConfigureContext(context); | ||
if (!status.ok()) return status; | ||
return child_->CreateSchema(context, request); | ||
} | ||
|
||
StatusOr<google::pubsub::v1::Schema> SchemaAuth::GetSchema( | ||
grpc::ClientContext& context, | ||
google::pubsub::v1::GetSchemaRequest const& request) { | ||
auto status = auth_->ConfigureContext(context); | ||
if (!status.ok()) return status; | ||
return child_->GetSchema(context, request); | ||
} | ||
|
||
StatusOr<google::pubsub::v1::ListSchemasResponse> SchemaAuth::ListSchemas( | ||
grpc::ClientContext& context, | ||
google::pubsub::v1::ListSchemasRequest const& request) { | ||
auto status = auth_->ConfigureContext(context); | ||
if (!status.ok()) return status; | ||
return child_->ListSchemas(context, request); | ||
} | ||
|
||
Status SchemaAuth::DeleteSchema( | ||
grpc::ClientContext& context, | ||
google::pubsub::v1::DeleteSchemaRequest const& request) { | ||
auto status = auth_->ConfigureContext(context); | ||
if (!status.ok()) return status; | ||
return child_->DeleteSchema(context, request); | ||
} | ||
|
||
StatusOr<google::pubsub::v1::ValidateSchemaResponse> SchemaAuth::ValidateSchema( | ||
grpc::ClientContext& context, | ||
google::pubsub::v1::ValidateSchemaRequest const& request) { | ||
auto status = auth_->ConfigureContext(context); | ||
if (!status.ok()) return status; | ||
return child_->ValidateSchema(context, request); | ||
} | ||
|
||
StatusOr<google::pubsub::v1::ValidateMessageResponse> | ||
SchemaAuth::ValidateMessage( | ||
grpc::ClientContext& context, | ||
google::pubsub::v1::ValidateMessageRequest const& request) { | ||
auto status = auth_->ConfigureContext(context); | ||
if (!status.ok()) return status; | ||
return child_->ValidateMessage(context, request); | ||
} | ||
|
||
} // namespace GOOGLE_CLOUD_CPP_PUBSUB_NS | ||
} // namespace pubsub_internal | ||
} // namespace cloud | ||
} // namespace google |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2021 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 | ||
// | ||
// http://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_PUBSUB_INTERNAL_SCHEMA_AUTH_H | ||
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_PUBSUB_INTERNAL_SCHEMA_AUTH_H | ||
|
||
#include "google/cloud/pubsub/internal/schema_stub.h" | ||
#include "google/cloud/pubsub/version.h" | ||
#include "google/cloud/internal/unified_grpc_credentials.h" | ||
#include <memory> | ||
|
||
namespace google { | ||
namespace cloud { | ||
namespace pubsub_internal { | ||
inline namespace GOOGLE_CLOUD_CPP_PUBSUB_NS { | ||
|
||
/** | ||
* A Decorator for `SchemaStub` that logs each request and response. | ||
*/ | ||
class SchemaAuth : public SchemaStub { | ||
public: | ||
SchemaAuth( | ||
std::shared_ptr<google::cloud::internal::GrpcAuthenticationStrategy> auth, | ||
std::shared_ptr<SchemaStub> child) | ||
: auth_(std::move(auth)), child_(std::move(child)) {} | ||
|
||
StatusOr<google::pubsub::v1::Schema> CreateSchema( | ||
grpc::ClientContext& context, | ||
google::pubsub::v1::CreateSchemaRequest const& request) override; | ||
StatusOr<google::pubsub::v1::Schema> GetSchema( | ||
grpc::ClientContext& context, | ||
google::pubsub::v1::GetSchemaRequest const& request) override; | ||
StatusOr<google::pubsub::v1::ListSchemasResponse> ListSchemas( | ||
grpc::ClientContext& context, | ||
google::pubsub::v1::ListSchemasRequest const& request) override; | ||
Status DeleteSchema( | ||
grpc::ClientContext& context, | ||
google::pubsub::v1::DeleteSchemaRequest const& request) override; | ||
StatusOr<google::pubsub::v1::ValidateSchemaResponse> ValidateSchema( | ||
grpc::ClientContext& context, | ||
google::pubsub::v1::ValidateSchemaRequest const& request) override; | ||
StatusOr<google::pubsub::v1::ValidateMessageResponse> ValidateMessage( | ||
grpc::ClientContext& context, | ||
google::pubsub::v1::ValidateMessageRequest const& request) override; | ||
|
||
private: | ||
std::shared_ptr<google::cloud::internal::GrpcAuthenticationStrategy> auth_; | ||
std::shared_ptr<SchemaStub> child_; | ||
}; | ||
|
||
} // namespace GOOGLE_CLOUD_CPP_PUBSUB_NS | ||
} // namespace pubsub_internal | ||
} // namespace cloud | ||
} // namespace google | ||
|
||
#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_PUBSUB_INTERNAL_SCHEMA_AUTH_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
// Copyright 2021 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 | ||
// | ||
// http://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/pubsub/internal/schema_auth.h" | ||
#include "google/cloud/pubsub/testing/mock_schema_stub.h" | ||
#include "google/cloud/testing_util/mock_grpc_authentication_strategy.h" | ||
#include "google/cloud/testing_util/status_matchers.h" | ||
#include "absl/memory/memory.h" | ||
#include <gmock/gmock.h> | ||
|
||
namespace google { | ||
namespace cloud { | ||
namespace pubsub_internal { | ||
inline namespace GOOGLE_CLOUD_CPP_PUBSUB_NS { | ||
namespace { | ||
|
||
using ::google::cloud::testing_util::MakeTypicalMockAuth; | ||
using ::google::cloud::testing_util::StatusIs; | ||
using ::testing::IsNull; | ||
using ::testing::Not; | ||
using ::testing::Return; | ||
|
||
TEST(SchemaAuthTest, CreateSchema) { | ||
auto mock = std::make_shared<pubsub_testing::MockSchemaStub>(); | ||
EXPECT_CALL(*mock, CreateSchema) | ||
.WillOnce(Return(Status(StatusCode::kPermissionDenied, "uh-oh"))); | ||
auto under_test = SchemaAuth(MakeTypicalMockAuth(), mock); | ||
grpc::ClientContext ctx; | ||
google::pubsub::v1::CreateSchemaRequest request; | ||
auto auth_failure = under_test.CreateSchema(ctx, request); | ||
EXPECT_THAT(ctx.credentials(), IsNull()); | ||
EXPECT_THAT(auth_failure, StatusIs(StatusCode::kInvalidArgument)); | ||
|
||
auto auth_success = under_test.CreateSchema(ctx, request); | ||
EXPECT_THAT(ctx.credentials(), Not(IsNull())); | ||
EXPECT_THAT(auth_success, StatusIs(StatusCode::kPermissionDenied)); | ||
} | ||
|
||
TEST(SchemaAuthTest, GetSchema) { | ||
auto mock = std::make_shared<pubsub_testing::MockSchemaStub>(); | ||
EXPECT_CALL(*mock, GetSchema) | ||
.WillOnce(Return(Status(StatusCode::kPermissionDenied, "uh-oh"))); | ||
auto under_test = SchemaAuth(MakeTypicalMockAuth(), mock); | ||
grpc::ClientContext ctx; | ||
google::pubsub::v1::GetSchemaRequest request; | ||
auto auth_failure = under_test.GetSchema(ctx, request); | ||
EXPECT_THAT(ctx.credentials(), IsNull()); | ||
EXPECT_THAT(auth_failure, StatusIs(StatusCode::kInvalidArgument)); | ||
|
||
auto auth_success = under_test.GetSchema(ctx, request); | ||
EXPECT_THAT(ctx.credentials(), Not(IsNull())); | ||
EXPECT_THAT(auth_success, StatusIs(StatusCode::kPermissionDenied)); | ||
} | ||
|
||
TEST(SchemaAuthTest, ListSchemas) { | ||
auto mock = std::make_shared<pubsub_testing::MockSchemaStub>(); | ||
EXPECT_CALL(*mock, ListSchemas) | ||
.WillOnce(Return(Status(StatusCode::kPermissionDenied, "uh-oh"))); | ||
auto under_test = SchemaAuth(MakeTypicalMockAuth(), mock); | ||
grpc::ClientContext ctx; | ||
google::pubsub::v1::ListSchemasRequest request; | ||
auto auth_failure = under_test.ListSchemas(ctx, request); | ||
EXPECT_THAT(ctx.credentials(), IsNull()); | ||
EXPECT_THAT(auth_failure, StatusIs(StatusCode::kInvalidArgument)); | ||
|
||
auto auth_success = under_test.ListSchemas(ctx, request); | ||
EXPECT_THAT(ctx.credentials(), Not(IsNull())); | ||
EXPECT_THAT(auth_success, StatusIs(StatusCode::kPermissionDenied)); | ||
} | ||
|
||
TEST(SchemaAuthTest, DeleteSchema) { | ||
auto mock = std::make_shared<pubsub_testing::MockSchemaStub>(); | ||
EXPECT_CALL(*mock, DeleteSchema) | ||
.WillOnce(Return(Status(StatusCode::kPermissionDenied, "uh-oh"))); | ||
auto under_test = SchemaAuth(MakeTypicalMockAuth(), mock); | ||
grpc::ClientContext ctx; | ||
google::pubsub::v1::DeleteSchemaRequest request; | ||
auto auth_failure = under_test.DeleteSchema(ctx, request); | ||
EXPECT_THAT(ctx.credentials(), IsNull()); | ||
EXPECT_THAT(auth_failure, StatusIs(StatusCode::kInvalidArgument)); | ||
|
||
auto auth_success = under_test.DeleteSchema(ctx, request); | ||
EXPECT_THAT(ctx.credentials(), Not(IsNull())); | ||
EXPECT_THAT(auth_success, StatusIs(StatusCode::kPermissionDenied)); | ||
} | ||
|
||
TEST(SchemaAuthTest, ValidateSchema) { | ||
auto mock = std::make_shared<pubsub_testing::MockSchemaStub>(); | ||
EXPECT_CALL(*mock, ValidateSchema) | ||
.WillOnce(Return(Status(StatusCode::kPermissionDenied, "uh-oh"))); | ||
auto under_test = SchemaAuth(MakeTypicalMockAuth(), mock); | ||
grpc::ClientContext ctx; | ||
google::pubsub::v1::ValidateSchemaRequest request; | ||
auto auth_failure = under_test.ValidateSchema(ctx, request); | ||
EXPECT_THAT(ctx.credentials(), IsNull()); | ||
EXPECT_THAT(auth_failure, StatusIs(StatusCode::kInvalidArgument)); | ||
|
||
auto auth_success = under_test.ValidateSchema(ctx, request); | ||
EXPECT_THAT(ctx.credentials(), Not(IsNull())); | ||
EXPECT_THAT(auth_success, StatusIs(StatusCode::kPermissionDenied)); | ||
} | ||
|
||
TEST(SchemaAuthTest, ValidateMessage) { | ||
auto mock = std::make_shared<pubsub_testing::MockSchemaStub>(); | ||
EXPECT_CALL(*mock, ValidateMessage) | ||
.WillOnce(Return(Status(StatusCode::kPermissionDenied, "uh-oh"))); | ||
auto under_test = SchemaAuth(MakeTypicalMockAuth(), mock); | ||
grpc::ClientContext ctx; | ||
google::pubsub::v1::ValidateMessageRequest request; | ||
auto auth_failure = under_test.ValidateMessage(ctx, request); | ||
EXPECT_THAT(ctx.credentials(), IsNull()); | ||
EXPECT_THAT(auth_failure, StatusIs(StatusCode::kInvalidArgument)); | ||
|
||
auto auth_success = under_test.ValidateMessage(ctx, request); | ||
EXPECT_THAT(ctx.credentials(), Not(IsNull())); | ||
EXPECT_THAT(auth_success, StatusIs(StatusCode::kPermissionDenied)); | ||
} | ||
|
||
} // namespace | ||
} // namespace GOOGLE_CLOUD_CPP_PUBSUB_NS | ||
} // namespace pubsub_internal | ||
} // namespace cloud | ||
} // namespace google |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.