security/authorization/matchers.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 <string>
21 
22 #include "absl/memory/memory.h"
23 #include "absl/strings/string_view.h"
24 
26 #include <grpc/support/log.h>
27 
31 
32 namespace grpc_core {
33 
34 std::unique_ptr<AuthorizationMatcher> AuthorizationMatcher::Create(
35  Rbac::Permission permission) {
36  switch (permission.type) {
38  std::vector<std::unique_ptr<AuthorizationMatcher>> matchers;
39  for (const auto& rule : permission.permissions) {
41  }
42  return absl::make_unique<AndAuthorizationMatcher>(std::move(matchers));
43  }
45  std::vector<std::unique_ptr<AuthorizationMatcher>> matchers;
46  for (const auto& rule : permission.permissions) {
48  }
49  return absl::make_unique<OrAuthorizationMatcher>(std::move(matchers));
50  }
52  return absl::make_unique<NotAuthorizationMatcher>(
55  return absl::make_unique<AlwaysAuthorizationMatcher>();
57  return absl::make_unique<HeaderAuthorizationMatcher>(
58  std::move(permission.header_matcher));
60  return absl::make_unique<PathAuthorizationMatcher>(
61  std::move(permission.string_matcher));
63  return absl::make_unique<IpAuthorizationMatcher>(
66  return absl::make_unique<PortAuthorizationMatcher>(permission.port);
68  return absl::make_unique<MetadataAuthorizationMatcher>(permission.invert);
70  return absl::make_unique<ReqServerNameAuthorizationMatcher>(
71  std::move(permission.string_matcher));
72  }
73  return nullptr;
74 }
75 
76 std::unique_ptr<AuthorizationMatcher> AuthorizationMatcher::Create(
77  Rbac::Principal principal) {
78  switch (principal.type) {
80  std::vector<std::unique_ptr<AuthorizationMatcher>> matchers;
81  for (const auto& id : principal.principals) {
83  }
84  return absl::make_unique<AndAuthorizationMatcher>(std::move(matchers));
85  }
87  std::vector<std::unique_ptr<AuthorizationMatcher>> matchers;
88  for (const auto& id : principal.principals) {
90  }
91  return absl::make_unique<OrAuthorizationMatcher>(std::move(matchers));
92  }
94  return absl::make_unique<NotAuthorizationMatcher>(
97  return absl::make_unique<AlwaysAuthorizationMatcher>();
99  return absl::make_unique<AuthenticatedAuthorizationMatcher>(
100  std::move(principal.string_matcher));
102  return absl::make_unique<IpAuthorizationMatcher>(
105  return absl::make_unique<IpAuthorizationMatcher>(
107  std::move(principal.ip));
109  return absl::make_unique<IpAuthorizationMatcher>(
112  return absl::make_unique<HeaderAuthorizationMatcher>(
113  std::move(principal.header_matcher));
115  return absl::make_unique<PathAuthorizationMatcher>(
116  std::move(principal.string_matcher.value()));
118  return absl::make_unique<MetadataAuthorizationMatcher>(principal.invert);
119  }
120  return nullptr;
121 }
122 
124  for (const auto& matcher : matchers_) {
125  if (!matcher->Matches(args)) {
126  return false;
127  }
128  }
129  return true;
130 }
131 
133  for (const auto& matcher : matchers_) {
134  if (matcher->Matches(args)) {
135  return true;
136  }
137  }
138  return false;
139 }
140 
142  return !matcher_->Matches(args);
143 }
144 
146  std::string concatenated_value;
147  return matcher_.Match(
148  args.GetHeaderValue(matcher_.name(), &concatenated_value));
149 }
150 
152  : type_(type), prefix_len_(range.prefix_len) {
154  grpc_string_to_sockaddr(&subnet_address_, range.address_prefix.c_str(),
155  /*port does not matter here*/ 0);
156  if (GRPC_ERROR_IS_NONE(error)) {
158  } else {
159  gpr_log(GPR_DEBUG, "CidrRange address %s is not IPv4/IPv6. Error: %s",
160  range.address_prefix.c_str(), grpc_error_std_string(error).c_str());
161  }
163 }
164 
166  grpc_resolved_address address;
167  switch (type_) {
168  case Type::kDestIp: {
169  address = args.GetLocalAddress();
170  break;
171  }
172  case Type::kSourceIp:
174  case Type::kRemoteIp: {
175  address = args.GetPeerAddress();
176  break;
177  }
178  default:
179  return false;
180  }
182 }
183 
185  return port_ == args.GetLocalPort();
186 }
187 
189  const EvaluateArgs& args) const {
190  if (args.GetTransportSecurityType() != GRPC_SSL_TRANSPORT_SECURITY_TYPE &&
191  args.GetTransportSecurityType() != GRPC_TLS_TRANSPORT_SECURITY_TYPE) {
192  // Connection is not authenticated.
193  return false;
194  }
195  if (!matcher_.has_value()) {
196  // Allows any authenticated user.
197  return true;
198  }
199  std::vector<absl::string_view> uri_sans = args.GetUriSans();
200  if (!uri_sans.empty()) {
201  for (const auto& uri : uri_sans) {
202  if (matcher_->Match(uri)) {
203  return true;
204  }
205  }
206  }
207  std::vector<absl::string_view> dns_sans = args.GetDnsSans();
208  if (!dns_sans.empty()) {
209  for (const auto& dns : dns_sans) {
210  if (matcher_->Match(dns)) {
211  return true;
212  }
213  }
214  }
215  return matcher_->Match(args.GetSubject());
216 }
217 
219  // Currently we only support matching against an empty string.
220  return matcher_.Match("");
221 }
222 
224  absl::string_view path = args.GetPath();
225  if (!path.empty()) {
226  return matcher_.Match(path);
227  }
228  return false;
229 }
230 
232  return permissions_->Matches(args) && principals_->Matches(args);
233 }
234 
235 } // namespace grpc_core
grpc_core::HeaderAuthorizationMatcher::Matches
bool Matches(const EvaluateArgs &args) const override
Definition: security/authorization/matchers.cc:145
grpc_core::EvaluateArgs
Definition: evaluate_args.h:34
grpc_core::HeaderMatcher::Match
bool Match(const absl::optional< absl::string_view > &value) const
Definition: matchers/matchers.cc:290
log.h
grpc_core::AuthorizationMatcher::Create
static std::unique_ptr< AuthorizationMatcher > Create(Rbac::Permission permission)
Definition: security/authorization/matchers.cc:34
sockaddr_utils.h
grpc_core::Rbac::Principal::invert
bool invert
Definition: rbac_policy.h:148
grpc_core::PolicyAuthorizationMatcher::principals_
std::unique_ptr< AuthorizationMatcher > principals_
Definition: security/authorization/matchers.h:213
grpc_core::PortAuthorizationMatcher::Matches
bool Matches(const EvaluateArgs &args) const override
Definition: security/authorization/matchers.cc:184
capstone.range
range
Definition: third_party/bloaty/third_party/capstone/bindings/python/capstone/__init__.py:6
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
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
matchers
XdsRouteConfigResource::Route::Matchers matchers
Definition: xds_server_config_fetcher.cc:317
grpc_core::Rbac::Permission::RuleType::kNot
@ kNot
grpc_core::Rbac::Principal::RuleType::kSourceIp
@ kSourceIp
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
type_
std::string type_
Definition: client_channel_stress_test.cc:212
grpc_core::Rbac::Permission::RuleType::kPath
@ kPath
GRPC_SSL_TRANSPORT_SECURITY_TYPE
#define GRPC_SSL_TRANSPORT_SECURITY_TYPE
Definition: grpc_security_constants.h:27
grpc_resolved_address
Definition: resolved_address.h:34
grpc_core::IpAuthorizationMatcher::prefix_len_
const uint32_t prefix_len_
Definition: security/authorization/matchers.h:145
grpc_core::Rbac::Permission::RuleType::kDestIp
@ kDestIp
grpc_core::Rbac::Permission::RuleType::kMetadata
@ kMetadata
grpc_core::PathAuthorizationMatcher::matcher_
const StringMatcher matcher_
Definition: security/authorization/matchers.h:195
grpc_core::IpAuthorizationMatcher::Type::kDirectRemoteIp
@ kDirectRemoteIp
check_documentation.path
path
Definition: check_documentation.py:57
grpc_core::Rbac::Principal::RuleType::kRemoteIp
@ kRemoteIp
grpc_core::Rbac::Permission::header_matcher
HeaderMatcher header_matcher
Definition: rbac_policy.h:91
grpc_core::Rbac::Principal::RuleType::kAny
@ kAny
grpc_core::IpAuthorizationMatcher::subnet_address_
grpc_resolved_address subnet_address_
Definition: security/authorization/matchers.h:144
grpc_core::Rbac::Principal::RuleType::kHeader
@ kHeader
grpc_core::Rbac::Principal::RuleType::kOr
@ kOr
grpc_core::Rbac::Permission::RuleType::kHeader
@ kHeader
grpc_core::IpAuthorizationMatcher::IpAuthorizationMatcher
IpAuthorizationMatcher(Type type, Rbac::CidrRange range)
Definition: security/authorization/matchers.cc:151
grpc_core::PolicyAuthorizationMatcher::permissions_
std::unique_ptr< AuthorizationMatcher > permissions_
Definition: security/authorization/matchers.h:212
grpc_core::PathAuthorizationMatcher::Matches
bool Matches(const EvaluateArgs &args) const override
Definition: security/authorization/matchers.cc:223
grpc_core::IpAuthorizationMatcher::type_
const Type type_
Definition: security/authorization/matchers.h:142
grpc_sockaddr_match_subnet
bool grpc_sockaddr_match_subnet(const grpc_resolved_address *address, const grpc_resolved_address *subnet_address, uint32_t mask_bits)
Definition: sockaddr_utils.cc:415
parse_address.h
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
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
gen_stats_data.c_str
def c_str(s, encoding='ascii')
Definition: gen_stats_data.py:38
Type
Definition: bloaty/third_party/protobuf/src/google/protobuf/type.pb.h:182
grpc_core::Rbac::Permission::RuleType::kAny
@ kAny
grpc_core::ReqServerNameAuthorizationMatcher::matcher_
const StringMatcher matcher_
Definition: security/authorization/matchers.h:183
grpc_string_to_sockaddr
grpc_error_handle grpc_string_to_sockaddr(grpc_resolved_address *out, const char *addr, int port)
Definition: parse_address.cc:320
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
grpc_core::Rbac::Principal::string_matcher
absl::optional< StringMatcher > string_matcher
Definition: rbac_policy.h:142
grpc_core::AuthenticatedAuthorizationMatcher::matcher_
const absl::optional< StringMatcher > matcher_
Definition: security/authorization/matchers.h:169
matchers.h
grpc_core::HeaderAuthorizationMatcher::matcher_
const HeaderMatcher matcher_
Definition: security/authorization/matchers.h:124
grpc_core::IpAuthorizationMatcher::Type::kDestIp
@ kDestIp
grpc_core::Rbac::Principal
Definition: rbac_policy.h:102
error.h
grpc_core::OrAuthorizationMatcher::Matches
bool Matches(const EvaluateArgs &args) const override
Definition: security/authorization/matchers.cc:132
grpc_core::Rbac::Permission::RuleType::kOr
@ kOr
grpc_core::Rbac::Principal::principals
std::vector< std::unique_ptr< Principal > > principals
Definition: rbac_policy.h:146
grpc_core::StringMatcher::Match
bool Match(absl::string_view value) const
Definition: matchers/matchers.cc:107
grpc_core::Rbac::Permission::type
RuleType type
Definition: rbac_policy.h:90
grpc_core::PolicyAuthorizationMatcher::Matches
bool Matches(const EvaluateArgs &args) const override
Definition: security/authorization/matchers.cc:231
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::NotAuthorizationMatcher::matcher_
std::unique_ptr< AuthorizationMatcher > matcher_
Definition: security/authorization/matchers.h:96
grpc_core::Rbac::Principal::RuleType::kNot
@ kNot
grpc_core::IpAuthorizationMatcher::Matches
bool Matches(const EvaluateArgs &args) const override
Definition: security/authorization/matchers.cc:165
grpc_core::Rbac::Permission::RuleType::kReqServerName
@ kReqServerName
grpc_error_std_string
std::string grpc_error_std_string(grpc_error_handle error)
Definition: error.cc:944
grpc_core::PortAuthorizationMatcher::port_
const int port_
Definition: security/authorization/matchers.h:156
grpc_core::Rbac::CidrRange
Definition: rbac_policy.h:41
grpc_core::OrAuthorizationMatcher::matchers_
std::vector< std::unique_ptr< AuthorizationMatcher > > matchers_
Definition: security/authorization/matchers.h:83
grpc_core::AndAuthorizationMatcher::Matches
bool Matches(const EvaluateArgs &args) const override
Definition: security/authorization/matchers.cc:123
grpc_core::Rbac::Permission::ip
CidrRange ip
Definition: rbac_policy.h:93
grpc_security_constants.h
grpc_core::Rbac::Permission::RuleType::kDestPort
@ kDestPort
grpc_core::Rbac::Principal::type
RuleType type
Definition: rbac_policy.h:140
GRPC_ERROR_UNREF
#define GRPC_ERROR_UNREF(err)
Definition: error.h:262
grpc_core::Rbac::Principal::RuleType::kMetadata
@ kMetadata
grpc_core::IpAuthorizationMatcher::Type::kSourceIp
@ kSourceIp
GRPC_TLS_TRANSPORT_SECURITY_TYPE
#define GRPC_TLS_TRANSPORT_SECURITY_TYPE
Definition: grpc_security_constants.h:28
grpc_core::HeaderMatcher::name
const std::string & name() const
Definition: matchers/matchers.h:125
GPR_DEBUG
#define GPR_DEBUG
Definition: include/grpc/impl/codegen/log.h:55
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::ip
CidrRange ip
Definition: rbac_policy.h:143
grpc_error
Definition: error_internal.h:42
grpc_core::AndAuthorizationMatcher::matchers_
std::vector< std::unique_ptr< AuthorizationMatcher > > matchers_
Definition: security/authorization/matchers.h:71
grpc_core::AuthenticatedAuthorizationMatcher::Matches
bool Matches(const EvaluateArgs &args) const override
Definition: security/authorization/matchers.cc:188
grpc_core::Rbac::Permission::RuleType::kAnd
@ kAnd
grpc_core::Rbac::Permission::invert
bool invert
Definition: rbac_policy.h:99
grpc_core::Rbac::Principal::RuleType::kDirectRemoteIp
@ kDirectRemoteIp
grpc_core::IpAuthorizationMatcher::Type::kRemoteIp
@ kRemoteIp
grpc_core::NotAuthorizationMatcher::Matches
bool Matches(const EvaluateArgs &args) const override
Definition: security/authorization/matchers.cc:141
grpc_sockaddr_mask_bits
void grpc_sockaddr_mask_bits(grpc_resolved_address *address, uint32_t mask_bits)
Definition: sockaddr_utils.cc:363
grpc_core::Rbac::Principal::RuleType::kPath
@ kPath
grpc_core::ReqServerNameAuthorizationMatcher::Matches
bool Matches(const EvaluateArgs &) const override
Definition: security/authorization/matchers.cc:218
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:59:22