Debian Codegraph @

test/cpp/end2end/grpc_authz_end2end_test.cc

raw
1 // Copyright 2021 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <gmock/gmock.h>
16 #include <gtest/gtest.h>
17
18 #include <grpcpp/channel.h>
19 #include <grpcpp/client_context.h>
20 #include <grpcpp/create_channel.h>
21 #include <grpcpp/security/authorization_policy_provider.h>
22 #include <grpcpp/server.h>
23 #include <grpcpp/server_builder.h>
24
25 #include "src/core/lib/iomgr/load_file.h"
26 #include "src/core/lib/security/authorization/grpc_authorization_policy_provider.h"
27 #include "src/core/lib/security/credentials/fake/fake_credentials.h"
28 #include "src/cpp/client/secure_credentials.h"
29 #include "src/cpp/server/secure_server_credentials.h"
30 #include "src/proto/grpc/testing/echo.grpc.pb.h"
31 #include "test/core/util/port.h"
32 #include "test/core/util/test_config.h"
33 #include "test/core/util/tls_utils.h"
34 #include "test/cpp/end2end/test_service_impl.h"
35
36 namespace grpc {
37 namespace testing {
38 namespace {
39
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";
45
46 constexpr char kMessage[] = "Hello";
47
48 std::string ReadFile(const char* file_path) {
49 grpc_slice slice;
50 GPR_ASSERT(
51 GRPC_LOG_IF_ERROR("load_file", grpc_load_file(file_path, 0, &slice)));
52 std::string file_contents(grpc_core::StringViewFromSlice(slice));
53 grpc_slice_unref(slice);
54 return file_contents;
55 }
56
57 class GrpcAuthzEnd2EndTest : public ::testing::Test {
58 protected:
59 GrpcAuthzEnd2EndTest()
60 : server_address_(
61 absl::StrCat("localhost:", grpc_pick_unused_port_or_die())) {
62 std::string root_cert = ReadFile(kCaCertPath);
63 std::string identity_cert = ReadFile(kServerCertPath);
64 std::string private_key = ReadFile(kServerKeyPath);
65 std::vector<experimental::IdentityKeyCertPair>
66 server_identity_key_cert_pairs = {{private_key, identity_cert}};
67 grpc::experimental::TlsServerCredentialsOptions server_options(
68 std::make_shared<grpc::experimental::StaticDataCertificateProvider>(
69 root_cert, server_identity_key_cert_pairs));
70 server_options.watch_root_certs();
71 server_options.watch_identity_key_cert_pairs();
72 server_options.set_cert_request_type(
73 GRPC_SSL_REQUEST_CLIENT_CERTIFICATE_AND_VERIFY);
74 server_creds_ = grpc::experimental::TlsServerCredentials(server_options);
75 std::vector<experimental::IdentityKeyCertPair>
76 channel_identity_key_cert_pairs = {
77 {ReadFile(kClientKeyPath), ReadFile(kClientCertPath)}};
78 grpc::experimental::TlsChannelCredentialsOptions channel_options;
79 channel_options.set_certificate_provider(
80 std::make_shared<grpc::experimental::StaticDataCertificateProvider>(
81 ReadFile(kCaCertPath), channel_identity_key_cert_pairs));
82 channel_options.watch_identity_key_cert_pairs();
83 channel_options.watch_root_certs();
84 channel_creds_ = grpc::experimental::TlsCredentials(channel_options);
85 }
86
87 ~GrpcAuthzEnd2EndTest() override { server_->Shutdown(); }
88
89 // Replaces existing credentials with insecure credentials.
90 void UseInsecureCredentials() {
91 server_creds_ = InsecureServerCredentials();
92 channel_creds_ = InsecureChannelCredentials();
93 }
94
95 // Creates server with gRPC authorization enabled when provider is not null.
96 void InitServer(
97 std::shared_ptr<experimental::AuthorizationPolicyProviderInterface>
98 provider) {
99 ServerBuilder builder;
100 builder.AddListeningPort(server_address_, std::move(server_creds_));
101 builder.experimental().SetAuthorizationPolicyProvider(std::move(provider));
102 builder.RegisterService(&service_);
103 server_ = builder.BuildAndStart();
104 }
105
106 std::shared_ptr<experimental::AuthorizationPolicyProviderInterface>
107 CreateStaticAuthzPolicyProvider(const std::string& policy) {
108 grpc::Status status;
109 auto provider = experimental::StaticDataAuthorizationPolicyProvider::Create(
110 policy, &status);
111 EXPECT_TRUE(status.ok());
112 return provider;
113 }
114
115 std::shared_ptr<experimental::AuthorizationPolicyProviderInterface>
116 CreateFileWatcherAuthzPolicyProvider(const std::string& policy_path,
117 unsigned int refresh_interval_sec) {
118 grpc::Status status;
119 auto provider =
120 experimental::FileWatcherAuthorizationPolicyProvider::Create(
121 policy_path, refresh_interval_sec, &status);
122 EXPECT_TRUE(status.ok());
123 return provider;
124 }
125
126 std::shared_ptr<Channel> BuildChannel() {
127 ChannelArguments args;
128 // Override target name for host name check
129 args.SetSslTargetNameOverride("foo.test.google.fr");
130 return grpc::CreateCustomChannel(server_address_, channel_creds_, args);
131 }
132
133 grpc::Status SendRpc(const std::shared_ptr<Channel>& channel,
134 ClientContext* context,
135 grpc::testing::EchoResponse* response = nullptr) {
136 auto stub = grpc::testing::EchoTestService::NewStub(channel);
137 grpc::testing::EchoRequest request;
138 request.set_message(kMessage);
139 return stub->Echo(context, request, response);
140 }
141
142 std::string server_address_;
143 TestServiceImpl service_;
144 std::unique_ptr<Server> server_;
145 std::shared_ptr<ServerCredentials> server_creds_;
146 std::shared_ptr<ChannelCredentials> channel_creds_;
147 };
148
149 TEST_F(GrpcAuthzEnd2EndTest,
150 StaticInitAllowsRpcRequestNoMatchInDenyMatchInAllow) {
151 std::string policy =
152 "{"
153 " \"name\": \"authz\","
154 " \"allow_rules\": ["
155 " {"
156 " \"name\": \"allow_echo\","
157 " \"request\": {"
158 " \"paths\": ["
159 " \"*/Echo\""
160 " ],"
161 " \"headers\": ["
162 " {"
163 " \"key\": \"key-foo\","
164 " \"values\": [\"foo1\", \"foo2\"]"
165 " },"
166 " {"
167 " \"key\": \"key-bar\","
168 " \"values\": [\"bar1\"]"
169 " }"
170 " ]"
171 " }"
172 " }"
173 " ],"
174 " \"deny_rules\": ["
175 " {"
176 " \"name\": \"deny_clientstreamingecho\","
177 " \"request\": {"
178 " \"paths\": ["
179 " \"*/ClientStreamingEcho\""
180 " ]"
181 " }"
182 " }"
183 " ]"
184 "}";
185 InitServer(CreateStaticAuthzPolicyProvider(policy));
186 auto channel = BuildChannel();
187 ClientContext context;
188 context.AddMetadata("key-foo", "foo2");
189 context.AddMetadata("key-bar", "bar1");
190 context.AddMetadata("key-baz", "baz1");
191 grpc::testing::EchoResponse resp;
192 grpc::Status status = SendRpc(channel, &context, &resp);
193 EXPECT_TRUE(status.ok());
194 EXPECT_EQ(resp.message(), kMessage);
195 }
196
197 TEST_F(GrpcAuthzEnd2EndTest, StaticInitDeniesRpcRequestNoMatchInAllowAndDeny) {
198 std::string policy =
199 "{"
200 " \"name\": \"authz\","
201 " \"allow_rules\": ["
202 " {"
203 " \"name\": \"allow_foo\","
204 " \"request\": {"
205 " \"paths\": ["
206 " \"*/foo\""
207 " ]"
208 " }"
209 " }"
210 " ],"
211 " \"deny_rules\": ["
212 " {"
213 " \"name\": \"deny_bar\","
214 " \"source\": {"
215 " \"principals\": ["
216 " \"bar\""
217 " ]"
218 " }"
219 " }"
220 " ]"
221 "}";
222 InitServer(CreateStaticAuthzPolicyProvider(policy));
223 auto channel = BuildChannel();
224 ClientContext context;
225 grpc::testing::EchoResponse resp;
226 grpc::Status status = SendRpc(channel, &context, &resp);
227 EXPECT_EQ(status.error_code(), grpc::StatusCode::PERMISSION_DENIED);
228 EXPECT_EQ(status.error_message(), "Unauthorized RPC request rejected.");
229 EXPECT_TRUE(resp.message().empty());
230 }
231
232 TEST_F(GrpcAuthzEnd2EndTest,
233 StaticInitDeniesRpcRequestMatchInDenyMatchInAllow) {
234 std::string policy =
235 "{"
236 " \"name\": \"authz\","
237 " \"allow_rules\": ["
238 " {"
239 " \"name\": \"allow_all\""
240 " }"
241 " ],"
242 " \"deny_rules\": ["
243 " {"
244 " \"name\": \"deny_echo\","
245 " \"request\": {"
246 " \"paths\": ["
247 " \"*/Echo\""
248 " ]"
249 " }"
250 " }"
251 " ]"
252 "}";
253 InitServer(CreateStaticAuthzPolicyProvider(policy));
254 auto channel = BuildChannel();
255 ClientContext context;
256 grpc::testing::EchoResponse resp;
257 grpc::Status status = SendRpc(channel, &context, &resp);
258 EXPECT_EQ(status.error_code(), grpc::StatusCode::PERMISSION_DENIED);
259 EXPECT_EQ(status.error_message(), "Unauthorized RPC request rejected.");
260 EXPECT_TRUE(resp.message().empty());
261 }
262
263 TEST_F(GrpcAuthzEnd2EndTest,
264 StaticInitDeniesRpcRequestMatchInDenyNoMatchInAllow) {
265 std::string policy =
266 "{"
267 " \"name\": \"authz\","
268 " \"allow_rules\": ["
269 " {"
270 " \"name\": \"allow_clientstreamingecho\","
271 " \"request\": {"
272 " \"paths\": ["
273 " \"*/ClientStreamingEcho\""
274 " ]"
275 " }"
276 " }"
277 " ],"
278 " \"deny_rules\": ["
279 " {"
280 " \"name\": \"deny_echo\","
281 " \"request\": {"
282 " \"paths\": ["
283 " \"*/Echo\""
284 " ]"
285 " }"
286 " }"
287 " ]"
288 "}";
289 InitServer(CreateStaticAuthzPolicyProvider(policy));
290 auto channel = BuildChannel();
291 ClientContext context;
292 grpc::testing::EchoResponse resp;
293 grpc::Status status = SendRpc(channel, &context, &resp);
294 EXPECT_EQ(status.error_code(), grpc::StatusCode::PERMISSION_DENIED);
295 EXPECT_EQ(status.error_message(), "Unauthorized RPC request rejected.");
296 EXPECT_TRUE(resp.message().empty());
297 }
298
299 TEST_F(GrpcAuthzEnd2EndTest, StaticInitAllowsRpcRequestEmptyDenyMatchInAllow) {
300 std::string policy =
301 "{"
302 " \"name\": \"authz\","
303 " \"allow_rules\": ["
304 " {"
305 " \"name\": \"allow_echo\","
306 " \"request\": {"
307 " \"paths\": ["
308 " \"*\""
309 " ],"
310 " \"headers\": ["
311 " {"
312 " \"key\": \"key-foo\","
313 " \"values\": [\"foo1\", \"foo2\"]"
314 " },"
315 " {"
316 " \"key\": \"key-bar\","
317 " \"values\": [\"bar1\"]"
318 " }"
319 " ]"
320 " }"
321 " }"
322 " ]"
323 "}";
324 InitServer(CreateStaticAuthzPolicyProvider(policy));
325 auto channel = BuildChannel();
326 ClientContext context;
327 context.AddMetadata("key-foo", "foo2");
328 context.AddMetadata("key-bar", "bar1");
329 context.AddMetadata("key-baz", "baz1");
330 grpc::testing::EchoResponse resp;
331 grpc::Status status = SendRpc(channel, &context, &resp);
332 EXPECT_TRUE(status.ok());
333 EXPECT_EQ(resp.message(), kMessage);
334 }
335
336 TEST_F(GrpcAuthzEnd2EndTest,
337 StaticInitDeniesRpcRequestEmptyDenyNoMatchInAllow) {
338 std::string policy =
339 "{"
340 " \"name\": \"authz\","
341 " \"allow_rules\": ["
342 " {"
343 " \"name\": \"allow_echo\","
344 " \"request\": {"
345 " \"paths\": ["
346 " \"*/Echo\""
347 " ],"
348 " \"headers\": ["
349 " {"
350 " \"key\": \"key-foo\","
351 " \"values\": [\"foo1\"]"
352 " }"
353 " ]"
354 " }"
355 " }"
356 " ]"
357 "}";
358 InitServer(CreateStaticAuthzPolicyProvider(policy));
359 auto channel = BuildChannel();
360 ClientContext context;
361 context.AddMetadata("key-bar", "bar1");
362 grpc::testing::EchoResponse resp;
363 grpc::Status status = SendRpc(channel, &context, &resp);
364 EXPECT_EQ(status.error_code(), grpc::StatusCode::PERMISSION_DENIED);
365 EXPECT_EQ(status.error_message(), "Unauthorized RPC request rejected.");
366 EXPECT_TRUE(resp.message().empty());
367 }
368
369 TEST_F(
370 GrpcAuthzEnd2EndTest,
371 StaticInitDeniesRpcRequestWithPrincipalsFieldOnUnauthenticatedConnection) {
372 std::string policy =
373 "{"
374 " \"name\": \"authz\","
375 " \"allow_rules\": ["
376 " {"
377 " \"name\": \"allow_mtls\","
378 " \"source\": {"
379 " \"principals\": [\"*\"]"
380 " }"
381 " }"
382 " ]"
383 "}";
384 UseInsecureCredentials();
385 InitServer(CreateStaticAuthzPolicyProvider(policy));
386 auto channel = BuildChannel();
387 ClientContext context;
388 grpc::testing::EchoResponse resp;
389 grpc::Status status = SendRpc(channel, &context, &resp);
390 EXPECT_EQ(status.error_code(), grpc::StatusCode::PERMISSION_DENIED);
391 EXPECT_EQ(status.error_message(), "Unauthorized RPC request rejected.");
392 EXPECT_TRUE(resp.message().empty());
393 }
394
395 TEST_F(GrpcAuthzEnd2EndTest,
396 StaticInitAllowsRpcRequestWithPrincipalsFieldOnAuthenticatedConnection) {
397 std::string policy =
398 "{"
399 " \"name\": \"authz\","
400 " \"allow_rules\": ["
401 " {"
402 " \"name\": \"allow_mtls\","
403 " \"source\": {"
404 " \"principals\": [\"*\"]"
405 " }"
406 " }"
407 " ]"
408 "}";
409 InitServer(CreateStaticAuthzPolicyProvider(policy));
410 auto channel = BuildChannel();
411 ClientContext context;
412 grpc::testing::EchoResponse resp;
413 grpc::Status status = SendRpc(channel, &context, &resp);
414 EXPECT_TRUE(status.ok());
415 EXPECT_EQ(resp.message(), kMessage);
416 }
417
418 TEST_F(GrpcAuthzEnd2EndTest,
419 FileWatcherInitAllowsRpcRequestNoMatchInDenyMatchInAllow) {
420 std::string policy =
421 "{"
422 " \"name\": \"authz\","
423 " \"allow_rules\": ["
424 " {"
425 " \"name\": \"allow_echo\","
426 " \"request\": {"
427 " \"paths\": ["
428 " \"*/Echo\""
429 " ],"
430 " \"headers\": ["
431 " {"
432 " \"key\": \"key-foo\","
433 " \"values\": [\"foo1\", \"foo2\"]"
434 " },"
435 " {"
436 " \"key\": \"key-bar\","
437 " \"values\": [\"bar1\"]"
438 " }"
439 " ]"
440 " }"
441 " }"
442 " ],"
443 " \"deny_rules\": ["
444 " {"
445 " \"name\": \"deny_clientstreamingecho\","
446 " \"request\": {"
447 " \"paths\": ["
448 " \"*/ClientStreamingEcho\""
449 " ]"
450 " }"
451 " }"
452 " ]"
453 "}";
454 grpc_core::testing::TmpFile tmp_policy(policy);
455 InitServer(CreateFileWatcherAuthzPolicyProvider(tmp_policy.name(), 5));
456 auto channel = BuildChannel();
457 ClientContext context;
458 context.AddMetadata("key-foo", "foo2");
459 context.AddMetadata("key-bar", "bar1");
460 context.AddMetadata("key-baz", "baz1");
461 grpc::testing::EchoResponse resp;
462 grpc::Status status = SendRpc(channel, &context, &resp);
463 EXPECT_TRUE(status.ok());
464 EXPECT_EQ(resp.message(), kMessage);
465 }
466
467 TEST_F(GrpcAuthzEnd2EndTest,
468 FileWatcherInitDeniesRpcRequestNoMatchInAllowAndDeny) {
469 std::string policy =
470 "{"
471 " \"name\": \"authz\","
472 " \"allow_rules\": ["
473 " {"
474 " \"name\": \"allow_foo\","
475 " \"request\": {"
476 " \"paths\": ["
477 " \"*/foo\""
478 " ]"
479 " }"
480 " }"
481 " ],"
482 " \"deny_rules\": ["
483 " {"
484 " \"name\": \"deny_bar\","
485 " \"source\": {"
486 " \"principals\": ["
487 " \"bar\""
488 " ]"
489 " }"
490 " }"
491 " ]"
492 "}";
493 grpc_core::testing::TmpFile tmp_policy(policy);
494 InitServer(CreateFileWatcherAuthzPolicyProvider(tmp_policy.name(), 5));
495 auto channel = BuildChannel();
496 ClientContext context;
497 grpc::testing::EchoResponse resp;
498 grpc::Status status = SendRpc(channel, &context, &resp);
499 EXPECT_EQ(status.error_code(), grpc::StatusCode::PERMISSION_DENIED);
500 EXPECT_EQ(status.error_message(), "Unauthorized RPC request rejected.");
501 EXPECT_TRUE(resp.message().empty());
502 }
503
504 TEST_F(GrpcAuthzEnd2EndTest,
505 FileWatcherInitDeniesRpcRequestMatchInDenyMatchInAllow) {
506 std::string policy =
507 "{"
508 " \"name\": \"authz\","
509 " \"allow_rules\": ["
510 " {"
511 " \"name\": \"allow_all\""
512 " }"
513 " ],"
514 " \"deny_rules\": ["
515 " {"
516 " \"name\": \"deny_echo\","
517 " \"request\": {"
518 " \"paths\": ["
519 " \"*/Echo\""
520 " ]"
521 " }"
522 " }"
523 " ]"
524 "}";
525 grpc_core::testing::TmpFile tmp_policy(policy);
526 InitServer(CreateFileWatcherAuthzPolicyProvider(tmp_policy.name(), 5));
527 auto channel = BuildChannel();
528 ClientContext context;
529 grpc::testing::EchoResponse resp;
530 grpc::Status status = SendRpc(channel, &context, &resp);
531 EXPECT_EQ(status.error_code(), grpc::StatusCode::PERMISSION_DENIED);
532 EXPECT_EQ(status.error_message(), "Unauthorized RPC request rejected.");
533 EXPECT_TRUE(resp.message().empty());
534 }
535
536 TEST_F(GrpcAuthzEnd2EndTest,
537 FileWatcherInitDeniesRpcRequestMatchInDenyNoMatchInAllow) {
538 std::string policy =
539 "{"
540 " \"name\": \"authz\","
541 " \"allow_rules\": ["
542 " {"
543 " \"name\": \"allow_clientstreamingecho\","
544 " \"request\": {"
545 " \"paths\": ["
546 " \"*/ClientStreamingEcho\""
547 " ]"
548 " }"
549 " }"
550 " ],"
551 " \"deny_rules\": ["
552 " {"
553 " \"name\": \"deny_echo\","
554 " \"request\": {"
555 " \"paths\": ["
556 " \"*/Echo\""
557 " ]"
558 " }"
559 " }"
560 " ]"
561 "}";
562 grpc_core::testing::TmpFile tmp_policy(policy);
563 InitServer(CreateFileWatcherAuthzPolicyProvider(tmp_policy.name(), 5));
564 auto channel = BuildChannel();
565 ClientContext context;
566 grpc::testing::EchoResponse resp;
567 grpc::Status status = SendRpc(channel, &context, &resp);
568 EXPECT_EQ(status.error_code(), grpc::StatusCode::PERMISSION_DENIED);
569 EXPECT_EQ(status.error_message(), "Unauthorized RPC request rejected.");
570 EXPECT_TRUE(resp.message().empty());
571 }
572
573 TEST_F(GrpcAuthzEnd2EndTest,
574 FileWatcherInitAllowsRpcRequestEmptyDenyMatchInAllow) {
575 std::string policy =
576 "{"
577 " \"name\": \"authz\","
578 " \"allow_rules\": ["
579 " {"
580 " \"name\": \"allow_echo\","
581 " \"request\": {"
582 " \"paths\": ["
583 " \"*/Echo\""
584 " ],"
585 " \"headers\": ["
586 " {"
587 " \"key\": \"key-foo\","
588 " \"values\": [\"foo1\", \"foo2\"]"
589 " },"
590 " {"
591 " \"key\": \"key-bar\","
592 " \"values\": [\"bar1\"]"
593 " }"
594 " ]"
595 " }"
596 " }"
597 " ]"
598 "}";
599 grpc_core::testing::TmpFile tmp_policy(policy);
600 InitServer(CreateFileWatcherAuthzPolicyProvider(tmp_policy.name(), 5));
601 auto channel = BuildChannel();
602 ClientContext context;
603 context.AddMetadata("key-foo", "foo2");
604 context.AddMetadata("key-bar", "bar1");
605 context.AddMetadata("key-baz", "baz1");
606 grpc::testing::EchoResponse resp;
607 grpc::Status status = SendRpc(channel, &context, &resp);
608 EXPECT_TRUE(status.ok());
609 EXPECT_EQ(resp.message(), kMessage);
610 }
611
612 TEST_F(GrpcAuthzEnd2EndTest,
613 FileWatcherInitDeniesRpcRequestEmptyDenyNoMatchInAllow) {
614 std::string policy =
615 "{"
616 " \"name\": \"authz\","
617 " \"allow_rules\": ["
618 " {"
619 " \"name\": \"allow_echo\","
620 " \"request\": {"
621 " \"paths\": ["
622 " \"*/Echo\""
623 " ],"
624 " \"headers\": ["
625 " {"
626 " \"key\": \"key-foo\","
627 " \"values\": [\"foo1\"]"
628 " }"
629 " ]"
630 " }"
631 " }"
632 " ]"
633 "}";
634 grpc_core::testing::TmpFile tmp_policy(policy);
635 InitServer(CreateFileWatcherAuthzPolicyProvider(tmp_policy.name(), 5));
636 auto channel = BuildChannel();
637 ClientContext context;
638 context.AddMetadata("key-bar", "bar1");
639 grpc::testing::EchoResponse resp;
640 grpc::Status status = SendRpc(channel, &context, &resp);
641 EXPECT_EQ(status.error_code(), grpc::StatusCode::PERMISSION_DENIED);
642 EXPECT_EQ(status.error_message(), "Unauthorized RPC request rejected.");
643 EXPECT_TRUE(resp.message().empty());
644 }
645
646 TEST_F(GrpcAuthzEnd2EndTest, FileWatcherValidPolicyRefresh) {
647 std::string policy =
648 "{"
649 " \"name\": \"authz\","
650 " \"allow_rules\": ["
651 " {"
652 " \"name\": \"allow_echo\","
653 " \"request\": {"
654 " \"paths\": ["
655 " \"*/Echo\""
656 " ]"
657 " }"
658 " }"
659 " ]"
660 "}";
661 grpc_core::testing::TmpFile tmp_policy(policy);
662 auto provider = CreateFileWatcherAuthzPolicyProvider(tmp_policy.name(), 1);
663 InitServer(provider);
664 auto channel = BuildChannel();
665 ClientContext context1;
666 grpc::testing::EchoResponse resp1;
667 grpc::Status status = SendRpc(channel, &context1, &resp1);
668 EXPECT_TRUE(status.ok());
669 EXPECT_EQ(resp1.message(), kMessage);
670 gpr_event on_reload_done;
671 gpr_event_init(&on_reload_done);
672 std::function<void(bool contents_changed, absl::Status status)> callback =
673 [&on_reload_done](bool contents_changed, absl::Status status) {
674 if (contents_changed) {
675 EXPECT_TRUE(status.ok());
676 gpr_event_set(&on_reload_done, reinterpret_cast<void*>(1));
677 }
678 };
679 dynamic_cast<grpc_core::FileWatcherAuthorizationPolicyProvider*>(
680 provider->c_provider())
681 ->SetCallbackForTesting(std::move(callback));
682 // Replace the existing policy with a new authorization policy.
683 policy =
684 "{"
685 " \"name\": \"authz\","
686 " \"allow_rules\": ["
687 " {"
688 " \"name\": \"allow_foo\","
689 " \"request\": {"
690 " \"paths\": ["
691 " \"*/foo\""
692 " ]"
693 " }"
694 " }"
695 " ],"
696 " \"deny_rules\": ["
697 " {"
698 " \"name\": \"deny_echo\","
699 " \"request\": {"
700 " \"paths\": ["
701 " \"*/Echo\""
702 " ]"
703 " }"
704 " }"
705 " ]"
706 "}";
707 tmp_policy.RewriteFile(policy);
708 // Wait for the provider's refresh thread to read the updated files.
709 ASSERT_EQ(
710 gpr_event_wait(&on_reload_done, gpr_inf_future(GPR_CLOCK_MONOTONIC)),
711 reinterpret_cast<void*>(1));
712 ClientContext context2;
713 grpc::testing::EchoResponse resp2;
714 status = SendRpc(channel, &context2, &resp2);
715 EXPECT_EQ(status.error_code(), grpc::StatusCode::PERMISSION_DENIED);
716 EXPECT_EQ(status.error_message(), "Unauthorized RPC request rejected.");
717 EXPECT_TRUE(resp2.message().empty());
718 dynamic_cast<grpc_core::FileWatcherAuthorizationPolicyProvider*>(
719 provider->c_provider())
720 ->SetCallbackForTesting(nullptr);
721 }
722
723 TEST_F(GrpcAuthzEnd2EndTest, FileWatcherInvalidPolicyRefreshSkipsReload) {
724 std::string policy =
725 "{"
726 " \"name\": \"authz\","
727 " \"allow_rules\": ["
728 " {"
729 " \"name\": \"allow_echo\","
730 " \"request\": {"
731 " \"paths\": ["
732 " \"*/Echo\""
733 " ]"
734 " }"
735 " }"
736 " ]"
737 "}";
738 grpc_core::testing::TmpFile tmp_policy(policy);
739 auto provider = CreateFileWatcherAuthzPolicyProvider(tmp_policy.name(), 1);
740 InitServer(provider);
741 auto channel = BuildChannel();
742 ClientContext context1;
743 grpc::testing::EchoResponse resp1;
744 grpc::Status status = SendRpc(channel, &context1, &resp1);
745 EXPECT_TRUE(status.ok());
746 EXPECT_EQ(resp1.message(), kMessage);
747 gpr_event on_reload_done;
748 gpr_event_init(&on_reload_done);
749 std::function<void(bool contents_changed, absl::Status status)> callback =
750 [&on_reload_done](bool contents_changed, absl::Status status) {
751 if (contents_changed) {
752 EXPECT_EQ(status.code(), absl::StatusCode::kInvalidArgument);
753 EXPECT_EQ(status.message(), "\"name\" field is not present.");
754 gpr_event_set(&on_reload_done, reinterpret_cast<void*>(1));
755 }
756 };
757 dynamic_cast<grpc_core::FileWatcherAuthorizationPolicyProvider*>(
758 provider->c_provider())
759 ->SetCallbackForTesting(std::move(callback));
760 // Replaces existing policy with an invalid authorization policy.
761 policy = "{}";
762 tmp_policy.RewriteFile(policy);
763 // Wait for the provider's refresh thread to read the updated files.
764 ASSERT_EQ(
765 gpr_event_wait(&on_reload_done, gpr_inf_future(GPR_CLOCK_MONOTONIC)),
766 reinterpret_cast<void*>(1));
767 ClientContext context2;
768 grpc::testing::EchoResponse resp2;
769 status = SendRpc(channel, &context2, &resp2);
770 EXPECT_TRUE(status.ok());
771 EXPECT_EQ(resp2.message(), kMessage);
772 dynamic_cast<grpc_core::FileWatcherAuthorizationPolicyProvider*>(
773 provider->c_provider())
774 ->SetCallbackForTesting(nullptr);
775 }
776
777 TEST_F(GrpcAuthzEnd2EndTest, FileWatcherRecoversFromFailure) {
778 std::string policy =
779 "{"
780 " \"name\": \"authz\","
781 " \"allow_rules\": ["
782 " {"
783 " \"name\": \"allow_echo\","
784 " \"request\": {"
785 " \"paths\": ["
786 " \"*/Echo\""
787 " ]"
788 " }"
789 " }"
790 " ]"
791 "}";
792 grpc_core::testing::TmpFile tmp_policy(policy);
793 auto provider = CreateFileWatcherAuthzPolicyProvider(tmp_policy.name(), 1);
794 InitServer(provider);
795 auto channel = BuildChannel();
796 ClientContext context1;
797 grpc::testing::EchoResponse resp1;
798 grpc::Status status = SendRpc(channel, &context1, &resp1);
799 EXPECT_TRUE(status.ok());
800 EXPECT_EQ(resp1.message(), kMessage);
801 gpr_event on_first_reload_done;
802 gpr_event_init(&on_first_reload_done);
803 std::function<void(bool contents_changed, absl::Status status)> callback1 =
804 [&on_first_reload_done](bool contents_changed, absl::Status status) {
805 if (contents_changed) {
806 EXPECT_EQ(status.code(), absl::StatusCode::kInvalidArgument);
807 EXPECT_EQ(status.message(), "\"name\" field is not present.");
808 gpr_event_set(&on_first_reload_done, reinterpret_cast<void*>(1));
809 }
810 };
811 dynamic_cast<grpc_core::FileWatcherAuthorizationPolicyProvider*>(
812 provider->c_provider())
813 ->SetCallbackForTesting(std::move(callback1));
814 // Replaces existing policy with an invalid authorization policy.
815 policy = "{}";
816 tmp_policy.RewriteFile(policy);
817 // Wait for the provider's refresh thread to read the updated files.
818 ASSERT_EQ(gpr_event_wait(&on_first_reload_done,
819 gpr_inf_future(GPR_CLOCK_MONOTONIC)),
820 reinterpret_cast<void*>(1));
821 ClientContext context2;
822 grpc::testing::EchoResponse resp2;
823 status = SendRpc(channel, &context2, &resp2);
824 EXPECT_TRUE(status.ok());
825 EXPECT_EQ(resp2.message(), kMessage);
826 gpr_event on_second_reload_done;
827 gpr_event_init(&on_second_reload_done);
828 std::function<void(bool contents_changed, absl::Status status)> callback2 =
829 [&on_second_reload_done](bool contents_changed, absl::Status status) {
830 if (contents_changed) {
831 EXPECT_TRUE(status.ok());
832 gpr_event_set(&on_second_reload_done, reinterpret_cast<void*>(1));
833 }
834 };
835 dynamic_cast<grpc_core::FileWatcherAuthorizationPolicyProvider*>(
836 provider->c_provider())
837 ->SetCallbackForTesting(std::move(callback2));
838 // Replace the existing invalid policy with a valid authorization policy.
839 policy =
840 "{"
841 " \"name\": \"authz\","
842 " \"allow_rules\": ["
843 " {"
844 " \"name\": \"allow_foo\","
845 " \"request\": {"
846 " \"paths\": ["
847 " \"*/foo\""
848 " ]"
849 " }"
850 " }"
851 " ],"
852 " \"deny_rules\": ["
853 " {"
854 " \"name\": \"deny_echo\","
855 " \"request\": {"
856 " \"paths\": ["
857 " \"*/Echo\""
858 " ]"
859 " }"
860 " }"
861 " ]"
862 "}";
863 tmp_policy.RewriteFile(policy);
864 // Wait for the provider's refresh thread to read the updated files.
865 ASSERT_EQ(gpr_event_wait(&on_second_reload_done,
866 gpr_inf_future(GPR_CLOCK_MONOTONIC)),
867 reinterpret_cast<void*>(1));
868 ClientContext context3;
869 grpc::testing::EchoResponse resp3;
870 status = SendRpc(channel, &context3, &resp3);
871 EXPECT_EQ(status.error_code(), grpc::StatusCode::PERMISSION_DENIED);
872 EXPECT_EQ(status.error_message(), "Unauthorized RPC request rejected.");
873 EXPECT_TRUE(resp3.message().empty());
874 dynamic_cast<grpc_core::FileWatcherAuthorizationPolicyProvider*>(
875 provider->c_provider())
876 ->SetCallbackForTesting(nullptr);
877 }
878
879 } // namespace
880 } // namespace testing
881 } // namespace grpc
882
883 int main(int argc, char** argv) {
884 ::testing::InitGoogleTest(&argc, argv);
885 grpc::testing::TestEnvironment env(&argc, argv);
886 const auto result = RUN_ALL_TESTS();
887 return result;
888 }