grpc_authorization_policy_provider.h
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 #ifndef GRPC_CORE_LIB_SECURITY_AUTHORIZATION_GRPC_AUTHORIZATION_POLICY_PROVIDER_H
16 #define GRPC_CORE_LIB_SECURITY_AUTHORIZATION_GRPC_AUTHORIZATION_POLICY_PROVIDER_H
17 
19 
20 #include <functional>
21 #include <memory>
22 #include <string>
23 
24 #include "absl/base/thread_annotations.h"
25 #include "absl/status/status.h"
26 #include "absl/status/statusor.h"
27 #include "absl/strings/string_view.h"
28 
29 #include <grpc/grpc_security.h>
30 #include <grpc/support/sync.h>
31 
34 #include "src/core/lib/gprpp/thd.h"
38 
39 namespace grpc_core {
40 
41 // Provider class will get gRPC Authorization policy from string during
42 // initialization. This policy will be translated to Envoy RBAC policies and
43 // used to initialize allow and deny AuthorizationEngine objects. This provider
44 // will return the same authorization engines everytime.
47  public:
49  Create(absl::string_view authz_policy);
50 
51  // Use factory method "Create" to create an instance of
52  // StaticDataAuthorizationPolicyProvider.
54 
56  return {allow_engine_, deny_engine_};
57  }
58 
59  void Orphan() override {}
60 
61  private:
64 };
65 
66 // Provider class will get gRPC Authorization policy from provided file path.
67 // This policy will be translated to Envoy RBAC policies and used to initialize
68 // allow and deny AuthorizationEngine objects. This provider will periodically
69 // load file contents in specified path, and upon modification update the engine
70 // instances with new policy configuration. During reload if the file contents
71 // are invalid or there are I/O errors, we will skip that particular update and
72 // log error status. The authorization decisions will be made using the latest
73 // valid policy.
76  public:
78  Create(absl::string_view authz_policy_path,
79  unsigned int refresh_interval_sec);
80 
81  // Use factory method "Create" to create an instance of
82  // FileWatcherAuthorizationPolicyProvider.
84  unsigned int refresh_interval_sec,
86 
88  std::function<void(bool contents_changed, absl::Status Status)> cb);
89 
90  void Orphan() override;
91 
93  MutexLock lock(&mu_);
94  return {allow_engine_, deny_engine_};
95  }
96 
97  private:
98  // Force an update from the file system regardless of the interval.
100 
103  unsigned int refresh_interval_sec_;
104 
105  std::unique_ptr<Thread> refresh_thread_;
107 
109  // Callback is executed on every reload. This is useful for testing purpose.
110  std::function<void(bool contents_changed, absl::Status status)> cb_
111  ABSL_GUARDED_BY(mu_) = nullptr;
112  // Engines created using authz_policy_.
115 };
116 
117 } // namespace grpc_core
118 
119 #endif // GRPC_CORE_LIB_SECURITY_AUTHORIZATION_GRPC_AUTHORIZATION_POLICY_PROVIDER_H
grpc_core::FileWatcherAuthorizationPolicyProvider
Definition: grpc_authorization_policy_provider.h:74
grpc_core::FileWatcherAuthorizationPolicyProvider::refresh_thread_
std::unique_ptr< Thread > refresh_thread_
Definition: grpc_authorization_policy_provider.h:105
grpc_core::StaticDataAuthorizationPolicyProvider::allow_engine_
RefCountedPtr< AuthorizationEngine > allow_engine_
Definition: grpc_authorization_policy_provider.h:62
grpc_core::FileWatcherAuthorizationPolicyProvider::FileWatcherAuthorizationPolicyProvider
FileWatcherAuthorizationPolicyProvider(absl::string_view authz_policy_path, unsigned int refresh_interval_sec, absl::Status *status)
Definition: grpc_authorization_policy_provider.cc:97
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::MutexLock
Definition: src/core/lib/gprpp/sync.h:88
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
grpc_core::StaticDataAuthorizationPolicyProvider::StaticDataAuthorizationPolicyProvider
StaticDataAuthorizationPolicyProvider(RbacPolicies policies)
Definition: grpc_authorization_policy_provider.cc:54
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
authorization_engine.h
status
absl::Status status
Definition: rls.cc:251
grpc_authorization_policy_provider
Definition: src/core/lib/security/authorization/authorization_policy_provider.h:30
grpc_core::FileWatcherAuthorizationPolicyProvider::ForceUpdate
absl::Status ForceUpdate()
Definition: grpc_authorization_policy_provider.cc:139
grpc_security.h
grpc_core::FileWatcherAuthorizationPolicyProvider::mu_
Mutex mu_
Definition: grpc_authorization_policy_provider.h:108
grpc_authorization_policy_provider::AuthorizationEngines
Definition: src/core/lib/security/authorization/authorization_policy_provider.h:40
grpc_core::StaticDataAuthorizationPolicyProvider::deny_engine_
RefCountedPtr< AuthorizationEngine > deny_engine_
Definition: grpc_authorization_policy_provider.h:63
grpc_core::RbacPolicies
Definition: rbac_translator.h:27
grpc_core::StaticDataAuthorizationPolicyProvider::Create
static absl::StatusOr< RefCountedPtr< grpc_authorization_policy_provider > > Create(absl::string_view authz_policy)
Definition: grpc_authorization_policy_provider.cc:45
grpc_core::RefCountedPtr
Definition: ref_counted_ptr.h:35
cb_
TestPickArgsCallback cb_
Definition: test_lb_policies.cc:120
grpc_core::FileWatcherAuthorizationPolicyProvider::shutdown_event_
gpr_event shutdown_event_
Definition: grpc_authorization_policy_provider.h:106
grpc_core::StaticDataAuthorizationPolicyProvider::engines
AuthorizationEngines engines() override
Definition: grpc_authorization_policy_provider.h:55
grpc_core::FileWatcherAuthorizationPolicyProvider::authz_policy_path_
std::string authz_policy_path_
Definition: grpc_authorization_policy_provider.h:101
grpc_core::FileWatcherAuthorizationPolicyProvider::file_contents_
std::string file_contents_
Definition: grpc_authorization_policy_provider.h:102
grpc_core::StaticDataAuthorizationPolicyProvider
Definition: grpc_authorization_policy_provider.h:45
grpc_core::FileWatcherAuthorizationPolicyProvider::engines
AuthorizationEngines engines() override
Definition: grpc_authorization_policy_provider.h:92
grpc_core::FileWatcherAuthorizationPolicyProvider::refresh_interval_sec_
unsigned int refresh_interval_sec_
Definition: grpc_authorization_policy_provider.h:103
grpc_core::Mutex
Definition: src/core/lib/gprpp/sync.h:61
gpr_event
Definition: impl/codegen/sync_generic.h:31
absl::Status
Definition: third_party/abseil-cpp/absl/status/status.h:424
rbac_translator.h
thd.h
grpc::protobuf::util::Status
GRPC_CUSTOM_UTIL_STATUS Status
Definition: include/grpcpp/impl/codegen/config_protobuf.h:93
grpc_core::FileWatcherAuthorizationPolicyProvider::SetCallbackForTesting
void SetCallbackForTesting(std::function< void(bool contents_changed, absl::Status Status)> cb)
Definition: grpc_authorization_policy_provider.cc:133
ref_counted_ptr.h
grpc_core::FileWatcherAuthorizationPolicyProvider::ABSL_GUARDED_BY
std::function< void(bool contents_changed, absl::Status status)> cb_ ABSL_GUARDED_BY(mu_)
grpc_core::StaticDataAuthorizationPolicyProvider::Orphan
void Orphan() override
Definition: grpc_authorization_policy_provider.h:59
absl::StatusOr
Definition: abseil-cpp/absl/status/statusor.h:187
grpc_core::FileWatcherAuthorizationPolicyProvider::Create
static absl::StatusOr< RefCountedPtr< grpc_authorization_policy_provider > > Create(absl::string_view authz_policy_path, unsigned int refresh_interval_sec)
Definition: grpc_authorization_policy_provider.cc:86
function
std::function< bool(GrpcTool *, int, const char **, const CliCredentials &, GrpcToolOutputCallback)> function
Definition: grpc_tool.cc:250
grpc_core::FileWatcherAuthorizationPolicyProvider::Orphan
void Orphan() override
Definition: grpc_authorization_policy_provider.cc:179
sync.h
authorization_policy_provider.h
sync.h
cb
OPENSSL_EXPORT pem_password_cb * cb
Definition: pem.h:351
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:59:47