grpc_authorization_policy_provider.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 
16 
18 
19 #include <stdint.h>
20 
21 #include <utility>
22 
23 #include "absl/memory/memory.h"
24 
25 #include <grpc/grpc_security.h>
27 #include <grpc/slice.h>
28 #include <grpc/status.h>
29 #include <grpc/support/log.h>
31 #include <grpc/support/time.h>
32 
39 
40 namespace grpc_core {
41 
42 extern TraceFlag grpc_authz_trace;
43 
46  auto policies_or = GenerateRbacPolicies(authz_policy);
47  if (!policies_or.ok()) {
48  return policies_or.status();
49  }
50  return MakeRefCounted<StaticDataAuthorizationPolicyProvider>(
51  std::move(*policies_or));
52 }
53 
55  RbacPolicies policies)
56  : allow_engine_(MakeRefCounted<GrpcAuthorizationEngine>(
57  std::move(policies.allow_policy))),
59  std::move(policies.deny_policy))) {}
60 
61 namespace {
62 
63 absl::StatusOr<std::string> ReadPolicyFromFile(absl::string_view policy_path) {
64  grpc_slice policy_slice = grpc_empty_slice();
66  grpc_load_file(std::string(policy_path).c_str(), 0, &policy_slice);
67  if (!GRPC_ERROR_IS_NONE(error)) {
71  return status;
72  }
73  std::string policy_contents(StringViewFromSlice(policy_slice));
74  grpc_slice_unref_internal(policy_slice);
75  return policy_contents;
76 }
77 
78 gpr_timespec TimeoutSecondsToDeadline(int64_t seconds) {
81 }
82 
83 } // namespace
84 
87  absl::string_view authz_policy_path, unsigned int refresh_interval_sec) {
88  GPR_ASSERT(!authz_policy_path.empty());
89  GPR_ASSERT(refresh_interval_sec > 0);
91  auto provider = MakeRefCounted<FileWatcherAuthorizationPolicyProvider>(
92  authz_policy_path, refresh_interval_sec, &status);
93  if (!status.ok()) return status;
94  return provider;
95 }
96 
98  absl::string_view authz_policy_path, unsigned int refresh_interval_sec,
100  : authz_policy_path_(std::string(authz_policy_path)),
101  refresh_interval_sec_(refresh_interval_sec) {
103  // Initial read is done synchronously.
104  *status = ForceUpdate();
105  if (!status->ok()) {
106  return;
107  }
108  auto thread_lambda = [](void* arg) {
111  GPR_ASSERT(provider != nullptr);
112  while (true) {
113  void* value = gpr_event_wait(
114  &provider->shutdown_event_,
115  TimeoutSecondsToDeadline(provider->refresh_interval_sec_));
116  if (value != nullptr) {
117  return;
118  }
119  absl::Status status = provider->ForceUpdate();
122  "authorization policy reload status. code=%d error_details=%s",
123  status.code(), std::string(status.message()).c_str());
124  }
125  }
126  };
127  refresh_thread_ = absl::make_unique<Thread>(
128  "FileWatcherAuthorizationPolicyProvider_refreshing_thread", thread_lambda,
129  WeakRef().release());
130  refresh_thread_->Start();
131 }
132 
134  std::function<void(bool contents_changed, absl::Status status)> cb) {
135  MutexLock lock(&mu_);
136  cb_ = std::move(cb);
137 }
138 
140  bool contents_changed = false;
141  auto done_early = [&](absl::Status status) {
142  MutexLock lock(&mu_);
143  if (cb_ != nullptr) {
144  cb_(contents_changed, status);
145  }
146  return status;
147  };
148  absl::StatusOr<std::string> file_contents =
149  ReadPolicyFromFile(authz_policy_path_);
150  if (!file_contents.ok()) {
151  return done_early(file_contents.status());
152  }
153  if (file_contents_ == *file_contents) {
154  return done_early(absl::OkStatus());
155  }
156  file_contents_ = std::move(*file_contents);
157  contents_changed = true;
158  auto rbac_policies_or = GenerateRbacPolicies(file_contents_);
159  if (!rbac_policies_or.ok()) {
160  return done_early(rbac_policies_or.status());
161  }
162  MutexLock lock(&mu_);
163  allow_engine_ = MakeRefCounted<GrpcAuthorizationEngine>(
164  std::move(rbac_policies_or->allow_policy));
165  deny_engine_ = MakeRefCounted<GrpcAuthorizationEngine>(
166  std::move(rbac_policies_or->deny_policy));
167  if (cb_ != nullptr) {
168  cb_(contents_changed, absl::OkStatus());
169  }
172  "authorization policy reload status: successfully loaded new "
173  "policy\n%s",
174  file_contents_.c_str());
175  }
176  return absl::OkStatus();
177 }
178 
180  gpr_event_set(&shutdown_event_, reinterpret_cast<void*>(1));
181  if (refresh_thread_ != nullptr) {
182  refresh_thread_->Join();
183  }
184 }
185 
186 } // namespace grpc_core
187 
188 // Wrapper APIs declared in grpc_security.h
189 
192  const char* authz_policy, grpc_status_code* code,
193  const char** error_details) {
194  GPR_ASSERT(authz_policy != nullptr);
195  auto provider_or =
197  if (!provider_or.ok()) {
198  *code = static_cast<grpc_status_code>(provider_or.status().code());
199  *error_details =
200  gpr_strdup(std::string(provider_or.status().message()).c_str());
201  return nullptr;
202  }
203  return provider_or->release();
204 }
205 
208  const char* authz_policy_path, unsigned int refresh_interval_sec,
209  grpc_status_code* code, const char** error_details) {
210  GPR_ASSERT(authz_policy_path != nullptr);
212  authz_policy_path, refresh_interval_sec);
213  if (!provider_or.ok()) {
214  *code = static_cast<grpc_status_code>(provider_or.status().code());
215  *error_details =
216  gpr_strdup(std::string(provider_or.status().message()).c_str());
217  return nullptr;
218  }
219  return provider_or->release();
220 }
221 
224  if (provider != nullptr) provider->Unref();
225 }
trace.h
grpc_core::FileWatcherAuthorizationPolicyProvider
Definition: grpc_authorization_policy_provider.h:74
absl::InvalidArgumentError
Status InvalidArgumentError(absl::string_view message)
Definition: third_party/abseil-cpp/absl/status/status.cc:351
GPR_TIMESPAN
@ GPR_TIMESPAN
Definition: gpr_types.h:45
absl::time_internal::cctz::seconds
std::chrono::duration< std::int_fast64_t > seconds
Definition: abseil-cpp/absl/time/internal/cctz/include/cctz/time_zone.h:40
GPR_INFO
#define GPR_INFO
Definition: include/grpc/impl/codegen/log.h:56
grpc_core::MakeRefCounted
RefCountedPtr< T > MakeRefCounted(Args &&... args)
Definition: ref_counted_ptr.h:335
grpc_core::FileWatcherAuthorizationPolicyProvider::refresh_thread_
std::unique_ptr< Thread > refresh_thread_
Definition: grpc_authorization_policy_provider.h:105
log.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
load_file.h
slice.h
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::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
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
error
grpc_error_handle error
Definition: retry_filter.cc:499
grpc_status_code
grpc_status_code
Definition: include/grpc/impl/codegen/status.h:28
absl::OkStatus
Status OkStatus()
Definition: third_party/abseil-cpp/absl/status/status.h:882
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::DualRefCounted< grpc_authorization_policy_provider >::WeakRef
WeakRefCountedPtr< grpc_authorization_policy_provider > WeakRef() GRPC_MUST_USE_RESULT
Definition: dual_ref_counted.h:149
time.h
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_TRACE_FLAG_ENABLED
#define GRPC_TRACE_FLAG_ENABLED(f)
Definition: debug/trace.h:114
grpc_core::GrpcAuthorizationEngine
Definition: grpc_authorization_engine.h:39
grpc_authorization_policy_provider_static_data_create
grpc_authorization_policy_provider * grpc_authorization_policy_provider_static_data_create(const char *authz_policy, grpc_status_code *code, const char **error_details)
Definition: grpc_authorization_policy_provider.cc:191
grpc_core::RbacPolicies
Definition: rbac_translator.h:27
status.h
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
string_util.h
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
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
cb_
TestPickArgsCallback cb_
Definition: test_lb_policies.cc:120
gen_stats_data.c_str
def c_str(s, encoding='ascii')
Definition: gen_stats_data.py:38
grpc_core::FileWatcherAuthorizationPolicyProvider::shutdown_event_
gpr_event shutdown_event_
Definition: grpc_authorization_policy_provider.h:106
grpc_authorization_policy_provider.h
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
absl::Status::message
absl::string_view message() const
Definition: third_party/abseil-cpp/absl/status/status.h:806
arg
Definition: cmdline.cc:40
grpc_empty_slice
GPRAPI grpc_slice grpc_empty_slice(void)
Definition: slice/slice.cc:42
grpc_core::FileWatcherAuthorizationPolicyProvider::authz_policy_path_
std::string authz_policy_path_
Definition: grpc_authorization_policy_provider.h:101
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
grpc_core::GenerateRbacPolicies
absl::StatusOr< RbacPolicies > GenerateRbacPolicies(absl::string_view authz_policy)
Definition: rbac_translator.cc:331
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
error.h
grpc_core::FileWatcherAuthorizationPolicyProvider::file_contents_
std::string file_contents_
Definition: grpc_authorization_policy_provider.h:102
slice_internal.h
GPR_ERROR
#define GPR_ERROR
Definition: include/grpc/impl/codegen/log.h:57
grpc_core::DualRefCounted::Unref
void Unref()
Definition: dual_ref_counted.h:63
gpr_now
GPRAPI gpr_timespec gpr_now(gpr_clock_type clock)
stdint.h
gpr_event_wait
GPRAPI void * gpr_event_wait(gpr_event *ev, gpr_timespec abs_deadline)
Definition: sync.cc:73
gpr_types.h
value
const char * value
Definition: hpack_parser_table.cc:165
absl::StatusOr::ok
ABSL_MUST_USE_RESULT bool ok() const
Definition: abseil-cpp/absl/status/statusor.h:491
grpc_authorization_engine.h
gpr_time_add
GPRAPI gpr_timespec gpr_time_add(gpr_timespec a, gpr_timespec b)
Definition: src/core/lib/gpr/time.cc:135
grpc_authorization_policy_provider_file_watcher_create
grpc_authorization_policy_provider * grpc_authorization_policy_provider_file_watcher_create(const char *authz_policy_path, unsigned int refresh_interval_sec, grpc_status_code *code, const char **error_details)
Definition: grpc_authorization_policy_provider.cc:207
grpc_error_std_string
std::string grpc_error_std_string(grpc_error_handle error)
Definition: error.cc:944
absl::Status
Definition: third_party/abseil-cpp/absl/status/status.h:424
grpc_authorization_policy_provider_release
void grpc_authorization_policy_provider_release(grpc_authorization_policy_provider *provider)
Definition: grpc_authorization_policy_provider.cc:222
std
Definition: grpcpp/impl/codegen/async_unary_call.h:407
grpc_core::WeakRefCountedPtr
Definition: ref_counted_ptr.h:185
grpc_core::FileWatcherAuthorizationPolicyProvider::SetCallbackForTesting
void SetCallbackForTesting(std::function< void(bool contents_changed, absl::Status Status)> cb)
Definition: grpc_authorization_policy_provider.cc:133
arg
struct arg arg
slice_refcount.h
absl::Status::ok
ABSL_MUST_USE_RESULT bool ok() const
Definition: third_party/abseil-cpp/absl/status/status.h:802
GRPC_ERROR_UNREF
#define GRPC_ERROR_UNREF(err)
Definition: error.h:262
release
return ret release()
Definition: doc/python/sphinx/conf.py:37
gpr_strdup
GPRAPI char * gpr_strdup(const char *src)
Definition: string.cc:39
absl::string_view::empty
constexpr bool empty() const noexcept
Definition: abseil-cpp/absl/strings/string_view.h:292
absl::StatusOr
Definition: abseil-cpp/absl/status/statusor.h:187
code
Definition: bloaty/third_party/zlib/contrib/infback9/inftree9.h:24
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
gpr_timespec
Definition: gpr_types.h:50
grpc_error
Definition: error_internal.h:42
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
absl::Status::code
absl::StatusCode code() const
Definition: third_party/abseil-cpp/absl/status/status.cc:233
grpc_core::grpc_authz_trace
TraceFlag grpc_authz_trace
cb
OPENSSL_EXPORT pem_password_cb * cb
Definition: pem.h:351
grpc_slice_unref_internal
void grpc_slice_unref_internal(const grpc_slice &slice)
Definition: slice_refcount.h:39
absl::StatusOr::status
const Status & status() const &
Definition: abseil-cpp/absl/status/statusor.h:678
gpr_time_from_seconds
GPRAPI gpr_timespec gpr_time_from_seconds(int64_t s, gpr_clock_type clock_type)
Definition: src/core/lib/gpr/time.cc:123
GRPC_ERROR_IS_NONE
#define GRPC_ERROR_IS_NONE(err)
Definition: error.h:241
port_platform.h


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