grpc_authz_end2end_test.cc
Go to the documentation of this file.
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>
22 #include <grpcpp/server.h>
23 #include <grpcpp/server_builder.h>
24 
30 #include "src/proto/grpc/testing/echo.grpc.pb.h"
31 #include "test/core/util/port.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) {
50  GPR_ASSERT(
51  GRPC_LOG_IF_ERROR("load_file", grpc_load_file(file_path, 0, &slice)));
54  return file_contents;
55 }
56 
57 class GrpcAuthzEnd2EndTest : public ::testing::Test {
58  protected:
59  GrpcAuthzEnd2EndTest()
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}};
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(
75  std::vector<experimental::IdentityKeyCertPair>
76  channel_identity_key_cert_pairs = {
77  {ReadFile(kClientKeyPath), ReadFile(kClientCertPath)}};
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();
85  }
86 
87  ~GrpcAuthzEnd2EndTest() override { server_->Shutdown(); }
88 
89  // Replaces existing credentials with insecure credentials.
90  void UseInsecureCredentials() {
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) {
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) {
119  auto provider =
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");
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 
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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  };
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(
711  reinterpret_cast<void*>(1));
712  ClientContext context2;
713  grpc::testing::EchoResponse resp2;
714  status = SendRpc(channel, &context2, &resp2);
716  EXPECT_EQ(status.error_message(), "Unauthorized RPC request rejected.");
717  EXPECT_TRUE(resp2.message().empty());
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) {
753  EXPECT_EQ(status.message(), "\"name\" field is not present.");
754  gpr_event_set(&on_reload_done, reinterpret_cast<void*>(1));
755  }
756  };
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(
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);
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) {
807  EXPECT_EQ(status.message(), "\"name\" field is not present.");
808  gpr_event_set(&on_first_reload_done, reinterpret_cast<void*>(1));
809  }
810  };
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,
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  };
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,
867  reinterpret_cast<void*>(1));
868  ClientContext context3;
869  grpc::testing::EchoResponse resp3;
870  status = SendRpc(channel, &context3, &resp3);
872  EXPECT_EQ(status.error_message(), "Unauthorized RPC request rejected.");
873  EXPECT_TRUE(resp3.message().empty());
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 }
grpc_core::FileWatcherAuthorizationPolicyProvider
Definition: grpc_authorization_policy_provider.h:74
grpc_core::testing::TmpFile
Definition: test/core/util/tls_utils.h:27
grpc_slice_unref
GPRAPI void grpc_slice_unref(grpc_slice s)
Definition: slice_api.cc:32
server_
std::unique_ptr< Server > server_
Definition: grpc_authz_end2end_test.cc:144
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
testing
Definition: aws_request_signer_test.cc:25
grpc::status
auto status
Definition: cpp/client/credentials_test.cc:200
fake_credentials.h
port.h
tls_utils.h
grpc_load_file
grpc_error_handle grpc_load_file(const char *filename, int add_null_terminator, grpc_slice *output)
Definition: load_file.cc:33
generate.env
env
Definition: generate.py:37
absl::StrCat
std::string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: abseil-cpp/absl/strings/str_cat.cc:98
load_file.h
grpc
Definition: grpcpp/alarm.h:33
cpp.utils.ReadFile
def ReadFile(filename, print_error=True)
Definition: bloaty/third_party/googletest/googlemock/scripts/generator/cpp/utils.py:30
grpc::ASSERT_EQ
ASSERT_EQ(sizeof(valid_json), fwrite(valid_json, 1, sizeof(valid_json), creds_file))
benchmark.request
request
Definition: benchmark.py:77
grpc_core::StringViewFromSlice
absl::string_view StringViewFromSlice(const grpc_slice &slice)
Definition: slice_internal.h:93
gpr_event_set
GPRAPI void gpr_event_set(gpr_event *ev, void *value)
Definition: sync.cc:59
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
secure_credentials.h
GRPC_SSL_REQUEST_CLIENT_CERTIFICATE_AND_VERIFY
@ GRPC_SSL_REQUEST_CLIENT_CERTIFICATE_AND_VERIFY
Definition: grpc_security_constants.h:105
gpr_inf_future
GPRAPI gpr_timespec gpr_inf_future(gpr_clock_type type)
Definition: src/core/lib/gpr/time.cc:55
server_address_
std::string server_address_
Definition: grpc_authz_end2end_test.cc:142
grpc::experimental::TlsCredentialsOptions::watch_identity_key_cert_pairs
void watch_identity_key_cert_pairs()
Definition: tls_credentials_options.cc:57
GRPC_LOG_IF_ERROR
#define GRPC_LOG_IF_ERROR(what, error)
Definition: error.h:398
grpc::experimental::TlsServerCredentialsOptions
Definition: tls_credentials_options.h:136
async_greeter_client.stub
stub
Definition: hellostreamingworld/async_greeter_client.py:26
grpc::ClientContext::AddMetadata
void AddMetadata(const std::string &meta_key, const std::string &meta_value)
Definition: client_context.cc:121
testing::Test
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:402
test_service_impl.h
profile_analyzer.builder
builder
Definition: profile_analyzer.py:159
channel
wrapped_grpc_channel * channel
Definition: src/php/ext/grpc/call.h:33
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
channel_creds_
std::shared_ptr< ChannelCredentials > channel_creds_
Definition: grpc_authz_end2end_test.cc:146
grpc_authorization_policy_provider.h
grpc.StatusCode.PERMISSION_DENIED
tuple PERMISSION_DENIED
Definition: src/python/grpcio/grpc/__init__.py:268
slice
grpc_slice slice
Definition: src/core/lib/surface/server.cc:467
http2_server_health_check.resp
resp
Definition: http2_server_health_check.py:31
grpc::experimental::TlsServerCredentials
std::shared_ptr< ServerCredentials > TlsServerCredentials(const experimental::TlsServerCredentialsOptions &options)
Builds TLS ServerCredentials given TLS options.
Definition: secure_server_credentials.cc:153
grpc::experimental::TlsCredentialsOptions::watch_root_certs
void watch_root_certs()
Definition: tls_credentials_options.cc:47
channel.h
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
GPR_CLOCK_MONOTONIC
@ GPR_CLOCK_MONOTONIC
Definition: gpr_types.h:36
gpr_event_init
GPRAPI void gpr_event_init(gpr_event *ev)
Definition: sync.cc:54
callback
static void callback(void *arg, int status, int timeouts, struct hostent *host)
Definition: acountry.c:224
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
grpc_pick_unused_port_or_die
int grpc_pick_unused_port_or_die(void)
grpc::experimental::TlsChannelCredentialsOptions
Definition: tls_credentials_options.h:125
gpr_event_wait
GPRAPI void * gpr_event_wait(gpr_event *ev, gpr_timespec abs_deadline)
Definition: sync.cc:73
absl::StatusCode::kInvalidArgument
@ kInvalidArgument
server_creds_
std::shared_ptr< ServerCredentials > server_creds_
Definition: grpc_authz_end2end_test.cc:145
test_config.h
grpc.beta.implementations.server_options
def server_options(multi_method_implementation=None, request_deserializers=None, response_serializers=None, thread_pool=None, thread_pool_size=None, default_timeout=None, maximum_timeout=None)
Definition: implementations.py:258
secure_server_credentials.h
main
int main(int argc, char **argv)
Definition: grpc_authz_end2end_test.cc:883
gpr_event
Definition: impl/codegen/sync_generic.h:31
client_context.h
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
grpc::experimental::TlsCredentials
std::shared_ptr< ChannelCredentials > TlsCredentials(const TlsChannelCredentialsOptions &options)
Builds TLS Credentials given TLS options.
Definition: secure_credentials.cc:316
grpc::testing::TEST_F
TEST_F(ChannelArgumentsTest, SetInt)
Definition: channel_arguments_test.cc:134
absl::Status
Definition: third_party/abseil-cpp/absl/status/status.h:424
private_key
Definition: hrss.c:1885
asyncio_get_stats.response
response
Definition: asyncio_get_stats.py:28
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
grpc::experimental::StaticDataAuthorizationPolicyProvider::Create
static std::shared_ptr< StaticDataAuthorizationPolicyProvider > Create(const std::string &authz_policy, grpc::Status *status)
Definition: authorization_policy_provider.cc:29
grpc::CreateCustomChannel
std::shared_ptr< Channel > CreateCustomChannel(const grpc::string &target, const std::shared_ptr< ChannelCredentials > &creds, const ChannelArguments &args)
service_
TestServiceImpl service_
Definition: grpc_authz_end2end_test.cc:143
grpc::Status
Definition: include/grpcpp/impl/codegen/status.h:35
grpc::testing::EXPECT_EQ
EXPECT_EQ(options.token_exchange_service_uri, "https://foo/exchange")
grpc::experimental::FileWatcherAuthorizationPolicyProvider::Create
static std::shared_ptr< FileWatcherAuthorizationPolicyProvider > Create(const std::string &authz_policy_path, unsigned int refresh_interval_sec, grpc::Status *status)
Definition: authorization_policy_provider.cc:51
grpc::experimental::TlsCredentialsOptions::set_certificate_provider
void set_certificate_provider(std::shared_ptr< CertificateProviderInterface > certificate_provider)
Definition: tls_credentials_options.cc:38
callback2
static void callback2(void *arg, int status, int timeouts, struct hostent *host)
Definition: acountry.c:252
grpc::InsecureServerCredentials
std::shared_ptr< ServerCredentials > InsecureServerCredentials()
Definition: insecure_server_credentials.cc:52
context
grpc::ClientContext context
Definition: istio_echo_server_lib.cc:61
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
authorization_policy_provider.h
grpc::testing::EXPECT_TRUE
EXPECT_TRUE(grpc::experimental::StsCredentialsOptionsFromJson(minimum_valid_json, &options) .ok())
server.h
grpc::InsecureChannelCredentials
std::shared_ptr< ChannelCredentials > InsecureChannelCredentials()
Credentials for an unencrypted, unauthenticated channel.
Definition: cpp/client/insecure_credentials.cc:69
function
std::function< bool(GrpcTool *, int, const char **, const CliCredentials &, GrpcToolOutputCallback)> function
Definition: grpc_tool.cc:250
tests.interop.resources.private_key
def private_key()
Definition: interop/resources.py:29
TestServiceImpl
Definition: interop_server.cc:139
grpc::testing::SendRpc
static void SendRpc(grpc::testing::EchoTestService::Stub *stub, int num_rpcs, bool allow_exhaustion, gpr_atm *errors)
Definition: thread_stress_test.cc:277
server_builder.h
create_channel.h


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:44