15 #include <gmock/gmock.h>
16 #include <gtest/gtest.h>
30 #include "src/proto/grpc/testing/echo.grpc.pb.h"
40 constexpr
char kCaCertPath[] =
"src/core/tsi/test_creds/ca.pem";
41 constexpr
char kServerCertPath[] =
"src/core/tsi/test_creds/server1.pem";
42 constexpr
char kServerKeyPath[] =
"src/core/tsi/test_creds/server1.key";
43 constexpr
char kClientCertPath[] =
"src/core/tsi/test_creds/client.pem";
44 constexpr
char kClientKeyPath[] =
"src/core/tsi/test_creds/client.key";
46 constexpr
char kMessage[] =
"Hello";
59 GrpcAuthzEnd2EndTest()
65 std::vector<experimental::IdentityKeyCertPair>
66 server_identity_key_cert_pairs = {{
private_key, identity_cert}};
68 std::make_shared<grpc::experimental::StaticDataCertificateProvider>(
69 root_cert, server_identity_key_cert_pairs));
75 std::vector<experimental::IdentityKeyCertPair>
76 channel_identity_key_cert_pairs = {
80 std::make_shared<grpc::experimental::StaticDataCertificateProvider>(
81 ReadFile(kCaCertPath), channel_identity_key_cert_pairs));
87 ~GrpcAuthzEnd2EndTest()
override {
server_->Shutdown(); }
90 void UseInsecureCredentials() {
97 std::shared_ptr<experimental::AuthorizationPolicyProviderInterface>
106 std::shared_ptr<experimental::AuthorizationPolicyProviderInterface>
107 CreateStaticAuthzPolicyProvider(
const std::string& policy) {
115 std::shared_ptr<experimental::AuthorizationPolicyProviderInterface>
116 CreateFileWatcherAuthzPolicyProvider(
const std::string& policy_path,
117 unsigned int refresh_interval_sec) {
121 policy_path, refresh_interval_sec, &
status);
126 std::shared_ptr<Channel> BuildChannel() {
127 ChannelArguments
args;
129 args.SetSslTargetNameOverride(
"foo.test.google.fr");
135 grpc::testing::EchoResponse*
response =
nullptr) {
136 auto stub = grpc::testing::EchoTestService::NewStub(
channel);
137 grpc::testing::EchoRequest
request;
149 TEST_F(GrpcAuthzEnd2EndTest,
150 StaticInitAllowsRpcRequestNoMatchInDenyMatchInAllow) {
153 " \"name\": \"authz\","
154 " \"allow_rules\": ["
156 " \"name\": \"allow_echo\","
163 " \"key\": \"key-foo\","
164 " \"values\": [\"foo1\", \"foo2\"]"
167 " \"key\": \"key-bar\","
168 " \"values\": [\"bar1\"]"
176 " \"name\": \"deny_clientstreamingecho\","
179 " \"*/ClientStreamingEcho\""
185 InitServer(CreateStaticAuthzPolicyProvider(policy));
191 grpc::testing::EchoResponse
resp;
197 TEST_F(GrpcAuthzEnd2EndTest, StaticInitDeniesRpcRequestNoMatchInAllowAndDeny) {
200 " \"name\": \"authz\","
201 " \"allow_rules\": ["
203 " \"name\": \"allow_foo\","
213 " \"name\": \"deny_bar\","
222 InitServer(CreateStaticAuthzPolicyProvider(policy));
225 grpc::testing::EchoResponse
resp;
228 EXPECT_EQ(
status.error_message(),
"Unauthorized RPC request rejected.");
232 TEST_F(GrpcAuthzEnd2EndTest,
233 StaticInitDeniesRpcRequestMatchInDenyMatchInAllow) {
236 " \"name\": \"authz\","
237 " \"allow_rules\": ["
239 " \"name\": \"allow_all\""
244 " \"name\": \"deny_echo\","
253 InitServer(CreateStaticAuthzPolicyProvider(policy));
256 grpc::testing::EchoResponse
resp;
259 EXPECT_EQ(
status.error_message(),
"Unauthorized RPC request rejected.");
263 TEST_F(GrpcAuthzEnd2EndTest,
264 StaticInitDeniesRpcRequestMatchInDenyNoMatchInAllow) {
267 " \"name\": \"authz\","
268 " \"allow_rules\": ["
270 " \"name\": \"allow_clientstreamingecho\","
273 " \"*/ClientStreamingEcho\""
280 " \"name\": \"deny_echo\","
289 InitServer(CreateStaticAuthzPolicyProvider(policy));
292 grpc::testing::EchoResponse
resp;
295 EXPECT_EQ(
status.error_message(),
"Unauthorized RPC request rejected.");
299 TEST_F(GrpcAuthzEnd2EndTest, StaticInitAllowsRpcRequestEmptyDenyMatchInAllow) {
302 " \"name\": \"authz\","
303 " \"allow_rules\": ["
305 " \"name\": \"allow_echo\","
312 " \"key\": \"key-foo\","
313 " \"values\": [\"foo1\", \"foo2\"]"
316 " \"key\": \"key-bar\","
317 " \"values\": [\"bar1\"]"
324 InitServer(CreateStaticAuthzPolicyProvider(policy));
330 grpc::testing::EchoResponse
resp;
336 TEST_F(GrpcAuthzEnd2EndTest,
337 StaticInitDeniesRpcRequestEmptyDenyNoMatchInAllow) {
340 " \"name\": \"authz\","
341 " \"allow_rules\": ["
343 " \"name\": \"allow_echo\","
350 " \"key\": \"key-foo\","
351 " \"values\": [\"foo1\"]"
358 InitServer(CreateStaticAuthzPolicyProvider(policy));
362 grpc::testing::EchoResponse
resp;
365 EXPECT_EQ(
status.error_message(),
"Unauthorized RPC request rejected.");
370 GrpcAuthzEnd2EndTest,
371 StaticInitDeniesRpcRequestWithPrincipalsFieldOnUnauthenticatedConnection) {
374 " \"name\": \"authz\","
375 " \"allow_rules\": ["
377 " \"name\": \"allow_mtls\","
379 " \"principals\": [\"*\"]"
384 UseInsecureCredentials();
385 InitServer(CreateStaticAuthzPolicyProvider(policy));
388 grpc::testing::EchoResponse
resp;
391 EXPECT_EQ(
status.error_message(),
"Unauthorized RPC request rejected.");
395 TEST_F(GrpcAuthzEnd2EndTest,
396 StaticInitAllowsRpcRequestWithPrincipalsFieldOnAuthenticatedConnection) {
399 " \"name\": \"authz\","
400 " \"allow_rules\": ["
402 " \"name\": \"allow_mtls\","
404 " \"principals\": [\"*\"]"
409 InitServer(CreateStaticAuthzPolicyProvider(policy));
412 grpc::testing::EchoResponse
resp;
418 TEST_F(GrpcAuthzEnd2EndTest,
419 FileWatcherInitAllowsRpcRequestNoMatchInDenyMatchInAllow) {
422 " \"name\": \"authz\","
423 " \"allow_rules\": ["
425 " \"name\": \"allow_echo\","
432 " \"key\": \"key-foo\","
433 " \"values\": [\"foo1\", \"foo2\"]"
436 " \"key\": \"key-bar\","
437 " \"values\": [\"bar1\"]"
445 " \"name\": \"deny_clientstreamingecho\","
448 " \"*/ClientStreamingEcho\""
455 InitServer(CreateFileWatcherAuthzPolicyProvider(tmp_policy.name(), 5));
461 grpc::testing::EchoResponse
resp;
467 TEST_F(GrpcAuthzEnd2EndTest,
468 FileWatcherInitDeniesRpcRequestNoMatchInAllowAndDeny) {
471 " \"name\": \"authz\","
472 " \"allow_rules\": ["
474 " \"name\": \"allow_foo\","
484 " \"name\": \"deny_bar\","
494 InitServer(CreateFileWatcherAuthzPolicyProvider(tmp_policy.name(), 5));
497 grpc::testing::EchoResponse
resp;
500 EXPECT_EQ(
status.error_message(),
"Unauthorized RPC request rejected.");
504 TEST_F(GrpcAuthzEnd2EndTest,
505 FileWatcherInitDeniesRpcRequestMatchInDenyMatchInAllow) {
508 " \"name\": \"authz\","
509 " \"allow_rules\": ["
511 " \"name\": \"allow_all\""
516 " \"name\": \"deny_echo\","
526 InitServer(CreateFileWatcherAuthzPolicyProvider(tmp_policy.name(), 5));
529 grpc::testing::EchoResponse
resp;
532 EXPECT_EQ(
status.error_message(),
"Unauthorized RPC request rejected.");
536 TEST_F(GrpcAuthzEnd2EndTest,
537 FileWatcherInitDeniesRpcRequestMatchInDenyNoMatchInAllow) {
540 " \"name\": \"authz\","
541 " \"allow_rules\": ["
543 " \"name\": \"allow_clientstreamingecho\","
546 " \"*/ClientStreamingEcho\""
553 " \"name\": \"deny_echo\","
563 InitServer(CreateFileWatcherAuthzPolicyProvider(tmp_policy.name(), 5));
566 grpc::testing::EchoResponse
resp;
569 EXPECT_EQ(
status.error_message(),
"Unauthorized RPC request rejected.");
573 TEST_F(GrpcAuthzEnd2EndTest,
574 FileWatcherInitAllowsRpcRequestEmptyDenyMatchInAllow) {
577 " \"name\": \"authz\","
578 " \"allow_rules\": ["
580 " \"name\": \"allow_echo\","
587 " \"key\": \"key-foo\","
588 " \"values\": [\"foo1\", \"foo2\"]"
591 " \"key\": \"key-bar\","
592 " \"values\": [\"bar1\"]"
600 InitServer(CreateFileWatcherAuthzPolicyProvider(tmp_policy.name(), 5));
606 grpc::testing::EchoResponse
resp;
612 TEST_F(GrpcAuthzEnd2EndTest,
613 FileWatcherInitDeniesRpcRequestEmptyDenyNoMatchInAllow) {
616 " \"name\": \"authz\","
617 " \"allow_rules\": ["
619 " \"name\": \"allow_echo\","
626 " \"key\": \"key-foo\","
627 " \"values\": [\"foo1\"]"
635 InitServer(CreateFileWatcherAuthzPolicyProvider(tmp_policy.name(), 5));
639 grpc::testing::EchoResponse
resp;
642 EXPECT_EQ(
status.error_message(),
"Unauthorized RPC request rejected.");
646 TEST_F(GrpcAuthzEnd2EndTest, FileWatcherValidPolicyRefresh) {
649 " \"name\": \"authz\","
650 " \"allow_rules\": ["
652 " \"name\": \"allow_echo\","
662 auto provider = CreateFileWatcherAuthzPolicyProvider(tmp_policy.name(), 1);
663 InitServer(provider);
665 ClientContext context1;
666 grpc::testing::EchoResponse resp1;
674 if (contents_changed) {
680 provider->c_provider())
685 " \"name\": \"authz\","
686 " \"allow_rules\": ["
688 " \"name\": \"allow_foo\","
698 " \"name\": \"deny_echo\","
707 tmp_policy.RewriteFile(policy);
711 reinterpret_cast<void*
>(1));
712 ClientContext context2;
713 grpc::testing::EchoResponse resp2;
716 EXPECT_EQ(
status.error_message(),
"Unauthorized RPC request rejected.");
719 provider->c_provider())
720 ->SetCallbackForTesting(
nullptr);
723 TEST_F(GrpcAuthzEnd2EndTest, FileWatcherInvalidPolicyRefreshSkipsReload) {
726 " \"name\": \"authz\","
727 " \"allow_rules\": ["
729 " \"name\": \"allow_echo\","
739 auto provider = CreateFileWatcherAuthzPolicyProvider(tmp_policy.name(), 1);
740 InitServer(provider);
742 ClientContext context1;
743 grpc::testing::EchoResponse resp1;
751 if (contents_changed) {
758 provider->c_provider())
762 tmp_policy.RewriteFile(policy);
766 reinterpret_cast<void*
>(1));
767 ClientContext context2;
768 grpc::testing::EchoResponse resp2;
773 provider->c_provider())
774 ->SetCallbackForTesting(
nullptr);
777 TEST_F(GrpcAuthzEnd2EndTest, FileWatcherRecoversFromFailure) {
780 " \"name\": \"authz\","
781 " \"allow_rules\": ["
783 " \"name\": \"allow_echo\","
793 auto provider = CreateFileWatcherAuthzPolicyProvider(tmp_policy.name(), 1);
794 InitServer(provider);
796 ClientContext context1;
797 grpc::testing::EchoResponse resp1;
805 if (contents_changed) {
808 gpr_event_set(&on_first_reload_done,
reinterpret_cast<void*
>(1));
812 provider->c_provider())
813 ->SetCallbackForTesting(
std::move(callback1));
816 tmp_policy.RewriteFile(policy);
820 reinterpret_cast<void*
>(1));
821 ClientContext context2;
822 grpc::testing::EchoResponse resp2;
830 if (contents_changed) {
832 gpr_event_set(&on_second_reload_done,
reinterpret_cast<void*
>(1));
836 provider->c_provider())
841 " \"name\": \"authz\","
842 " \"allow_rules\": ["
844 " \"name\": \"allow_foo\","
854 " \"name\": \"deny_echo\","
863 tmp_policy.RewriteFile(policy);
867 reinterpret_cast<void*
>(1));
868 ClientContext context3;
869 grpc::testing::EchoResponse resp3;
872 EXPECT_EQ(
status.error_message(),
"Unauthorized RPC request rejected.");
875 provider->c_provider())
876 ->SetCallbackForTesting(
nullptr);
883 int main(
int argc,
char** argv) {