rbac_policy.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 <algorithm>
20 #include <type_traits>
21 #include <utility>
22 
23 #include "absl/memory/memory.h"
24 #include "absl/strings/str_format.h"
25 #include "absl/strings/str_join.h"
26 
27 namespace grpc_core {
28 
29 //
30 // Rbac
31 //
32 
33 Rbac::Rbac(Rbac::Action action, std::map<std::string, Policy> policies)
34  : action(action), policies(std::move(policies)) {}
35 
36 Rbac::Rbac(Rbac&& other) noexcept
37  : action(other.action), policies(std::move(other.policies)) {}
38 
39 Rbac& Rbac::operator=(Rbac&& other) noexcept {
40  action = other.action;
41  policies = std::move(other.policies);
42  return *this;
43 }
44 
46  std::vector<std::string> contents;
47  contents.push_back(absl::StrFormat(
48  "Rbac action=%s{", action == Rbac::Action::kAllow ? "Allow" : "Deny"));
49  for (const auto& p : policies) {
50  contents.push_back(absl::StrFormat("{\n policy_name=%s\n%s\n}", p.first,
51  p.second.ToString()));
52  }
53  contents.push_back("}");
54  return absl::StrJoin(contents, "\n");
55 }
56 
57 //
58 // CidrRange
59 //
60 
61 Rbac::CidrRange::CidrRange(std::string address_prefix, uint32_t prefix_len)
62  : address_prefix(std::move(address_prefix)), prefix_len(prefix_len) {}
63 
65  : address_prefix(std::move(other.address_prefix)),
66  prefix_len(other.prefix_len) {}
67 
69  address_prefix = std::move(other.address_prefix);
70  prefix_len = other.prefix_len;
71  return *this;
72 }
73 
75  return absl::StrFormat("CidrRange{address_prefix=%s,prefix_len=%d}",
76  address_prefix, prefix_len);
77 }
78 
79 //
80 // Permission
81 //
82 
84  std::vector<std::unique_ptr<Permission>> permissions) {
85  Permission permission;
86  permission.type = Permission::RuleType::kAnd;
87  permission.permissions = std::move(permissions);
88  return permission;
89 }
90 
92  std::vector<std::unique_ptr<Permission>> permissions) {
93  Permission permission;
94  permission.type = Permission::RuleType::kOr;
95  permission.permissions = std::move(permissions);
96  return permission;
97 }
98 
100  Permission not_permission;
101  not_permission.type = Permission::RuleType::kNot;
102  not_permission.permissions.push_back(
103  absl::make_unique<Rbac::Permission>(std::move(permission)));
104  return not_permission;
105 }
106 
108  Permission permission;
109  permission.type = Permission::RuleType::kAny;
110  return permission;
111 }
112 
114  HeaderMatcher header_matcher) {
115  Permission permission;
116  permission.type = Permission::RuleType::kHeader;
117  permission.header_matcher = std::move(header_matcher);
118  return permission;
119 }
120 
122  StringMatcher string_matcher) {
123  Permission permission;
124  permission.type = Permission::RuleType::kPath;
125  permission.string_matcher = std::move(string_matcher);
126  return permission;
127 }
128 
130  Permission permission;
131  permission.type = Permission::RuleType::kDestIp;
132  permission.ip = std::move(ip);
133  return permission;
134 }
135 
137  Permission permission;
139  permission.port = port;
140  return permission;
141 }
142 
144  Permission permission;
146  permission.invert = invert;
147  return permission;
148 }
149 
151  StringMatcher string_matcher) {
152  Permission permission;
154  permission.string_matcher = std::move(string_matcher);
155  return permission;
156 }
157 
159  : type(other.type), invert(other.invert) {
160  switch (type) {
161  case RuleType::kAnd:
162  case RuleType::kOr:
163  case RuleType::kNot:
164  permissions = std::move(other.permissions);
165  break;
166  case RuleType::kAny:
167  break;
168  case RuleType::kHeader:
169  header_matcher = std::move(other.header_matcher);
170  break;
171  case RuleType::kPath:
172  case RuleType::kReqServerName:
173  string_matcher = std::move(other.string_matcher);
174  break;
175  case RuleType::kDestIp:
176  ip = std::move(other.ip);
177  break;
178  default:
179  port = other.port;
180  }
181 }
182 
184  Rbac::Permission&& other) noexcept {
185  type = other.type;
186  invert = other.invert;
187  switch (type) {
188  case RuleType::kAnd:
189  case RuleType::kOr:
190  case RuleType::kNot:
191  permissions = std::move(other.permissions);
192  break;
193  case RuleType::kAny:
194  break;
195  case RuleType::kHeader:
196  header_matcher = std::move(other.header_matcher);
197  break;
198  case RuleType::kPath:
199  case RuleType::kReqServerName:
200  string_matcher = std::move(other.string_matcher);
201  break;
202  case RuleType::kDestIp:
203  ip = std::move(other.ip);
204  break;
205  default:
206  port = other.port;
207  }
208  return *this;
209 }
210 
212  switch (type) {
213  case RuleType::kAnd: {
214  std::vector<std::string> contents;
215  contents.reserve(permissions.size());
216  for (const auto& permission : permissions) {
217  contents.push_back(permission->ToString());
218  }
219  return absl::StrFormat("and=[%s]", absl::StrJoin(contents, ","));
220  }
221  case RuleType::kOr: {
222  std::vector<std::string> contents;
223  contents.reserve(permissions.size());
224  for (const auto& permission : permissions) {
225  contents.push_back(permission->ToString());
226  }
227  return absl::StrFormat("or=[%s]", absl::StrJoin(contents, ","));
228  }
229  case RuleType::kNot:
230  return absl::StrFormat("not %s", permissions[0]->ToString());
231  case RuleType::kAny:
232  return "any";
233  case RuleType::kHeader:
234  return absl::StrFormat("header=%s", header_matcher.ToString());
235  case RuleType::kPath:
236  return absl::StrFormat("path=%s", string_matcher.ToString());
237  case RuleType::kDestIp:
238  return absl::StrFormat("dest_ip=%s", ip.ToString());
239  case RuleType::kDestPort:
240  return absl::StrFormat("dest_port=%d", port);
241  case RuleType::kMetadata:
242  return absl::StrFormat("%smetadata", invert ? "invert " : "");
243  case RuleType::kReqServerName:
244  return absl::StrFormat("requested_server_name=%s",
245  string_matcher.ToString());
246  default:
247  return "";
248  }
249 }
250 
251 //
252 // Principal
253 //
254 
256  std::vector<std::unique_ptr<Principal>> principals) {
257  Principal principal;
258  principal.type = Principal::RuleType::kAnd;
259  principal.principals = std::move(principals);
260  return principal;
261 }
262 
264  std::vector<std::unique_ptr<Principal>> principals) {
265  Principal principal;
266  principal.type = Principal::RuleType::kOr;
267  principal.principals = std::move(principals);
268  return principal;
269 }
270 
272  Principal not_principal;
273  not_principal.type = Principal::RuleType::kNot;
274  not_principal.principals.push_back(
275  absl::make_unique<Rbac::Principal>(std::move(principal)));
276  return not_principal;
277 }
278 
280  Principal principal;
281  principal.type = Principal::RuleType::kAny;
282  return principal;
283 }
284 
286  absl::optional<StringMatcher> string_matcher) {
287  Principal principal;
289  principal.string_matcher = std::move(string_matcher);
290  return principal;
291 }
292 
294  Principal principal;
296  principal.ip = std::move(ip);
297  return principal;
298 }
299 
301  Principal principal;
303  principal.ip = std::move(ip);
304  return principal;
305 }
306 
308  Principal principal;
310  principal.ip = std::move(ip);
311  return principal;
312 }
313 
315  HeaderMatcher header_matcher) {
316  Principal principal;
318  principal.header_matcher = std::move(header_matcher);
319  return principal;
320 }
321 
323  StringMatcher string_matcher) {
324  Principal principal;
325  principal.type = Principal::RuleType::kPath;
326  principal.string_matcher = std::move(string_matcher);
327  return principal;
328 }
329 
331  Principal principal;
333  principal.invert = invert;
334  return principal;
335 }
336 
338  : type(other.type), invert(other.invert) {
339  switch (type) {
340  case RuleType::kAnd:
341  case RuleType::kOr:
342  case RuleType::kNot:
343  principals = std::move(other.principals);
344  break;
345  case RuleType::kAny:
346  break;
347  case RuleType::kHeader:
348  header_matcher = std::move(other.header_matcher);
349  break;
350  case RuleType::kPrincipalName:
351  case RuleType::kPath:
352  string_matcher = std::move(other.string_matcher);
353  break;
354  default:
355  ip = std::move(other.ip);
356  }
357 }
358 
360  type = other.type;
361  invert = other.invert;
362  switch (type) {
363  case RuleType::kAnd:
364  case RuleType::kOr:
365  case RuleType::kNot:
366  principals = std::move(other.principals);
367  break;
368  case RuleType::kAny:
369  break;
370  case RuleType::kHeader:
371  header_matcher = std::move(other.header_matcher);
372  break;
373  case RuleType::kPrincipalName:
374  case RuleType::kPath:
375  string_matcher = std::move(other.string_matcher);
376  break;
377  default:
378  ip = std::move(other.ip);
379  }
380  return *this;
381 }
382 
384  switch (type) {
385  case RuleType::kAnd: {
386  std::vector<std::string> contents;
387  contents.reserve(principals.size());
388  for (const auto& principal : principals) {
389  contents.push_back(principal->ToString());
390  }
391  return absl::StrFormat("and=[%s]", absl::StrJoin(contents, ","));
392  }
393  case RuleType::kOr: {
394  std::vector<std::string> contents;
395  contents.reserve(principals.size());
396  for (const auto& principal : principals) {
397  contents.push_back(principal->ToString());
398  }
399  return absl::StrFormat("or=[%s]", absl::StrJoin(contents, ","));
400  }
401  case RuleType::kNot:
402  return absl::StrFormat("not %s", principals[0]->ToString());
403  case RuleType::kAny:
404  return "any";
405  case RuleType::kPrincipalName:
406  return absl::StrFormat("principal_name=%s", string_matcher->ToString());
407  case RuleType::kSourceIp:
408  return absl::StrFormat("source_ip=%s", ip.ToString());
409  case RuleType::kDirectRemoteIp:
410  return absl::StrFormat("direct_remote_ip=%s", ip.ToString());
411  case RuleType::kRemoteIp:
412  return absl::StrFormat("remote_ip=%s", ip.ToString());
413  case RuleType::kHeader:
414  return absl::StrFormat("header=%s", header_matcher.ToString());
415  case RuleType::kPath:
416  return absl::StrFormat("path=%s", string_matcher->ToString());
417  case RuleType::kMetadata:
418  return absl::StrFormat("%smetadata", invert ? "invert " : "");
419  default:
420  return "";
421  }
422 }
423 
424 //
425 // Policy
426 //
427 
428 Rbac::Policy::Policy(Permission permissions, Principal principals)
429  : permissions(std::move(permissions)), principals(std::move(principals)) {}
430 
432  : permissions(std::move(other.permissions)),
433  principals(std::move(other.principals)) {}
434 
436  permissions = std::move(other.permissions);
437  principals = std::move(other.principals);
438  return *this;
439 }
440 
442  return absl::StrFormat(
443  " Policy {\n Permissions{%s}\n Principals{%s}\n }",
444  permissions.ToString(), principals.ToString());
445 }
446 
447 } // namespace grpc_core
grpc_core::Rbac::operator=
Rbac & operator=(Rbac &&other) noexcept
Definition: rbac_policy.cc:39
grpc_core::Rbac::Principal::MakeHeaderPrincipal
static Principal MakeHeaderPrincipal(HeaderMatcher header_matcher)
Definition: rbac_policy.cc:314
grpc_core::Rbac::Permission::MakePathPermission
static Permission MakePathPermission(StringMatcher string_matcher)
Definition: rbac_policy.cc:121
grpc_core::Rbac::Permission::MakeHeaderPermission
static Permission MakeHeaderPermission(HeaderMatcher header_matcher)
Definition: rbac_policy.cc:113
grpc_core::Rbac::Principal::invert
bool invert
Definition: rbac_policy.h:148
grpc_core::Rbac::Permission::MakeAnyPermission
static Permission MakeAnyPermission()
Definition: rbac_policy.cc:107
grpc_core::Rbac::Permission::MakeAndPermission
static Permission MakeAndPermission(std::vector< std::unique_ptr< Permission >> permissions)
Definition: rbac_policy.cc:83
grpc_core::Rbac::Principal::MakeAnyPrincipal
static Principal MakeAnyPrincipal()
Definition: rbac_policy.cc:279
absl::StrFormat
ABSL_MUST_USE_RESULT std::string StrFormat(const FormatSpec< Args... > &format, const Args &... args)
Definition: abseil-cpp/absl/strings/str_format.h:338
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::Rbac::Principal::RuleType::kPrincipalName
@ kPrincipalName
grpc_core::Rbac::Permission::permissions
std::vector< std::unique_ptr< Permission > > permissions
Definition: rbac_policy.h:97
grpc_core::Rbac::Permission::RuleType::kNot
@ kNot
grpc_core::Rbac::Principal::RuleType::kSourceIp
@ kSourceIp
grpc_core::Rbac::ToString
std::string ToString() const
Definition: rbac_policy.cc:45
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
grpc_core::Rbac::Permission::RuleType::kPath
@ kPath
grpc_core::Rbac::Permission::RuleType::kDestIp
@ kDestIp
grpc_core::Rbac::Permission::MakeNotPermission
static Permission MakeNotPermission(Permission permission)
Definition: rbac_policy.cc:99
grpc_core::Rbac::Permission::RuleType::kMetadata
@ kMetadata
grpc_core::Rbac::Principal::operator=
Principal & operator=(Principal &&other) noexcept
Definition: rbac_policy.cc:359
grpc_core::Rbac::Principal::RuleType::kRemoteIp
@ kRemoteIp
grpc_core::Rbac::CidrRange::CidrRange
CidrRange()=default
grpc_core::Rbac::Permission::header_matcher
HeaderMatcher header_matcher
Definition: rbac_policy.h:91
grpc_core::Rbac::Policy
Definition: rbac_policy.h:151
grpc_core::HeaderMatcher
Definition: matchers/matchers.h:79
grpc_core::Rbac::Principal::RuleType::kAny
@ kAny
grpc_core::Rbac::Principal::MakePathPrincipal
static Principal MakePathPrincipal(StringMatcher string_matcher)
Definition: rbac_policy.cc:322
grpc_core::Rbac::Principal::RuleType::kHeader
@ kHeader
grpc_core::Rbac::Principal::RuleType::kOr
@ kOr
grpc_core::Rbac::Principal::MakeNotPrincipal
static Principal MakeNotPrincipal(Principal principal)
Definition: rbac_policy.cc:271
grpc_core::Rbac::Permission::RuleType::kHeader
@ kHeader
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
grpc_core::Rbac::Principal::RuleType::kAnd
@ kAnd
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
absl::StrJoin
std::string StrJoin(Iterator start, Iterator end, absl::string_view sep, Formatter &&fmt)
Definition: abseil-cpp/absl/strings/str_join.h:239
grpc_core::Rbac::Action
Action
Definition: rbac_policy.h:36
grpc_core::Rbac::Principal::MakeAndPrincipal
static Principal MakeAndPrincipal(std::vector< std::unique_ptr< Principal >> principals)
Definition: rbac_policy.cc:255
grpc_core::Rbac::Permission::RuleType::kAny
@ kAny
grpc_core::Rbac::action
Action action
Definition: rbac_policy.h:172
grpc_core::Rbac::Principal::ToString
std::string ToString() const
Definition: rbac_policy.cc:383
grpc_core::Rbac::Permission::ToString
std::string ToString() const
Definition: rbac_policy.cc:211
grpc_core::Rbac::Principal::string_matcher
absl::optional< StringMatcher > string_matcher
Definition: rbac_policy.h:142
grpc_core::Rbac::Policy::operator=
Policy & operator=(Policy &&other) noexcept
Definition: rbac_policy.cc:435
absl::optional
Definition: abseil-cpp/absl/types/internal/optional.h:61
rbac_policy.h
grpc_core::Rbac::Principal
Definition: rbac_policy.h:102
grpc_core::Rbac::Permission::RuleType::kOr
@ kOr
grpc_core::Rbac::Principal::Principal
Principal()=default
grpc_core::Rbac::Permission::operator=
Permission & operator=(Permission &&other) noexcept
Definition: rbac_policy.cc:183
grpc_core::Rbac::Principal::principals
std::vector< std::unique_ptr< Principal > > principals
Definition: rbac_policy.h:146
grpc_core::Rbac::Permission::type
RuleType type
Definition: rbac_policy.h:90
grpc_core::Rbac::Permission::Permission
Permission()=default
tests.unit._exit_scenarios.port
port
Definition: _exit_scenarios.py:179
grpc_core::Rbac::Permission::string_matcher
StringMatcher string_matcher
Definition: rbac_policy.h:92
grpc_core::Rbac::Principal::header_matcher
HeaderMatcher header_matcher
Definition: rbac_policy.h:141
grpc_core::StringMatcher
Definition: matchers/matchers.h:32
grpc_core::Rbac
Definition: rbac_policy.h:35
grpc_core::Rbac::Policy::ToString
std::string ToString() const
Definition: rbac_policy.cc:441
grpc_core::Rbac::Principal::RuleType::kNot
@ kNot
grpc_core::Rbac::Rbac
Rbac()=default
contents
string_view contents
Definition: elf.cc:597
grpc_core::Rbac::Permission::RuleType::kReqServerName
@ kReqServerName
grpc_core::Rbac::Policy::Policy
Policy()=default
client.action
action
Definition: examples/python/xds/client.py:49
grpc_core::Rbac::Principal::MakeAuthenticatedPrincipal
static Principal MakeAuthenticatedPrincipal(absl::optional< StringMatcher > string_matcher)
Definition: rbac_policy.cc:285
grpc_core::Rbac::policies
std::map< std::string, Policy > policies
Definition: rbac_policy.h:173
grpc_core::Rbac::Permission::MakeOrPermission
static Permission MakeOrPermission(std::vector< std::unique_ptr< Permission >> permissions)
Definition: rbac_policy.cc:91
grpc_core::Rbac::Action::kAllow
@ kAllow
std
Definition: grpcpp/impl/codegen/async_unary_call.h:407
grpc_core::Rbac::Principal::MakeRemoteIpPrincipal
static Principal MakeRemoteIpPrincipal(CidrRange ip)
Definition: rbac_policy.cc:307
grpc_core::Rbac::CidrRange
Definition: rbac_policy.h:41
grpc_core::Rbac::Permission::MakeReqServerNamePermission
static Permission MakeReqServerNamePermission(StringMatcher string_matcher)
Definition: rbac_policy.cc:150
grpc_core::Rbac::Permission::ip
CidrRange ip
Definition: rbac_policy.h:93
grpc_core::Rbac::Permission::RuleType::kDestPort
@ kDestPort
grpc_core::Rbac::Principal::MakeSourceIpPrincipal
static Principal MakeSourceIpPrincipal(CidrRange ip)
Definition: rbac_policy.cc:293
grpc_core::Rbac::Principal::type
RuleType type
Definition: rbac_policy.h:140
grpc_core::Rbac::Principal::RuleType::kMetadata
@ kMetadata
grpc_core::Rbac::Permission::MakeDestIpPermission
static Permission MakeDestIpPermission(CidrRange ip)
Definition: rbac_policy.cc:129
grpc_core::Rbac::CidrRange::ToString
std::string ToString() const
Definition: rbac_policy.cc:74
grpc_core::Rbac::Principal::MakeMetadataPrincipal
static Principal MakeMetadataPrincipal(bool invert)
Definition: rbac_policy.cc:330
grpc_core::Rbac::Permission::port
int port
Definition: rbac_policy.h:94
grpc_core::Rbac::Permission
Definition: rbac_policy.h:55
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
grpc_core::Rbac::Principal::MakeDirectRemoteIpPrincipal
static Principal MakeDirectRemoteIpPrincipal(CidrRange ip)
Definition: rbac_policy.cc:300
grpc_core::Rbac::Permission::MakeDestPortPermission
static Permission MakeDestPortPermission(int port)
Definition: rbac_policy.cc:136
grpc_core::Rbac::Principal::MakeOrPrincipal
static Principal MakeOrPrincipal(std::vector< std::unique_ptr< Principal >> principals)
Definition: rbac_policy.cc:263
grpc_core::Rbac::Principal::ip
CidrRange ip
Definition: rbac_policy.h:143
grpc_core::Rbac::Permission::RuleType::kAnd
@ kAnd
grpc_core::Rbac::Permission::MakeMetadataPermission
static Permission MakeMetadataPermission(bool invert)
Definition: rbac_policy.cc:143
grpc_core::Rbac::Permission::invert
bool invert
Definition: rbac_policy.h:99
grpc_core::Rbac::Principal::RuleType::kDirectRemoteIp
@ kDirectRemoteIp
grpc_core::Rbac::Principal::RuleType::kPath
@ kPath
grpc_core::Rbac::CidrRange::operator=
CidrRange & operator=(CidrRange &&other) noexcept
Definition: rbac_policy.cc:68
port_platform.h


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