security/authorization/matchers.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_MATCHERS_H
16 #define GRPC_CORE_LIB_SECURITY_AUTHORIZATION_MATCHERS_H
17 
19 
20 #include <stdint.h>
21 
22 #include <memory>
23 #include <utility>
24 #include <vector>
25 
26 #include "absl/types/optional.h"
27 
32 
33 namespace grpc_core {
34 
35 // Describes the rules for matching permission or principal.
37  public:
38  virtual ~AuthorizationMatcher() = default;
39 
40  // Returns whether or not the permission/principal matches the rules of the
41  // matcher.
42  virtual bool Matches(const EvaluateArgs& args) const = 0;
43 
44  // Creates an instance of a matcher based off the rules defined in Permission
45  // config.
46  static std::unique_ptr<AuthorizationMatcher> Create(
47  Rbac::Permission permission);
48 
49  // Creates an instance of a matcher based off the rules defined in Principal
50  // config.
51  static std::unique_ptr<AuthorizationMatcher> Create(
52  Rbac::Principal principal);
53 };
54 
56  public:
57  explicit AlwaysAuthorizationMatcher() = default;
58 
59  bool Matches(const EvaluateArgs&) const override { return true; }
60 };
61 
63  public:
65  std::vector<std::unique_ptr<AuthorizationMatcher>> matchers)
66  : matchers_(std::move(matchers)) {}
67 
68  bool Matches(const EvaluateArgs& args) const override;
69 
70  private:
71  std::vector<std::unique_ptr<AuthorizationMatcher>> matchers_;
72 };
73 
75  public:
77  std::vector<std::unique_ptr<AuthorizationMatcher>> matchers)
78  : matchers_(std::move(matchers)) {}
79 
80  bool Matches(const EvaluateArgs& args) const override;
81 
82  private:
83  std::vector<std::unique_ptr<AuthorizationMatcher>> matchers_;
84 };
85 
86 // Negates matching the provided permission/principal.
88  public:
90  std::unique_ptr<AuthorizationMatcher> matcher)
91  : matcher_(std::move(matcher)) {}
92 
93  bool Matches(const EvaluateArgs& args) const override;
94 
95  private:
96  std::unique_ptr<AuthorizationMatcher> matcher_;
97 };
98 
100  public:
101  explicit MetadataAuthorizationMatcher(bool invert) : invert_(invert) {}
102 
103  // In RBAC, metadata refers to the Envoy metadata which has no relation to
104  // gRPC metadata. Envoy metadata is a generic state shared between filters,
105  // which has no gRPC equivalent. RBAC implementations in gRPC will treat Envoy
106  // metadata as an empty map. Since ValueMatcher can only match if a value is
107  // present (even NullMatch), the metadata matcher will not match unless invert
108  // is set to true.
109  bool Matches(const EvaluateArgs&) const override { return invert_; }
110 
111  private:
112  const bool invert_;
113 };
114 
115 // Perform a match against HTTP headers.
117  public:
119  : matcher_(std::move(matcher)) {}
120 
121  bool Matches(const EvaluateArgs& args) const override;
122 
123  private:
125 };
126 
127 // Perform a match against IP Cidr Range.
129  public:
130  enum class Type {
131  kDestIp,
132  kSourceIp,
134  kRemoteIp,
135  };
136 
138 
139  bool Matches(const EvaluateArgs& args) const override;
140 
141  private:
142  const Type type_;
143  // Subnet masked address.
146 };
147 
148 // Perform a match against port number of the destination (local) address.
150  public:
152 
153  bool Matches(const EvaluateArgs& args) const override;
154 
155  private:
156  const int port_;
157 };
158 
159 // Matches the principal name as described in the peer certificate. Uses URI SAN
160 // or DNS SAN in that order, otherwise uses subject field.
162  public:
164  : matcher_(std::move(auth)) {}
165 
166  bool Matches(const EvaluateArgs& args) const override;
167 
168  private:
170 };
171 
172 // Perform a match against the request server from the client's connection
173 // request. This is typically TLS SNI. Currently unsupported.
175  public:
177  StringMatcher requested_server_name)
178  : matcher_(std::move(requested_server_name)) {}
179 
180  bool Matches(const EvaluateArgs&) const override;
181 
182  private:
184 };
185 
186 // Perform a match against the path header of HTTP request.
188  public:
190  : matcher_(std::move(path)) {}
191 
192  bool Matches(const EvaluateArgs& args) const override;
193 
194  private:
196 };
197 
198 // Performs a match for policy field in RBAC, which is a collection of
199 // permission and principal matchers. Policy matches iff, we find a match in one
200 // of its permissions and a match in one of its principals.
202  public:
204  : permissions_(
205  AuthorizationMatcher::Create(std::move(policy.permissions))),
206  principals_(
207  AuthorizationMatcher::Create(std::move(policy.principals))) {}
208 
209  bool Matches(const EvaluateArgs& args) const override;
210 
211  private:
212  std::unique_ptr<AuthorizationMatcher> permissions_;
213  std::unique_ptr<AuthorizationMatcher> principals_;
214 };
215 
216 } // namespace grpc_core
217 
218 #endif // GRPC_CORE_LIB_SECURITY_AUTHORIZATION_MATCHERS_H
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::AlwaysAuthorizationMatcher::Matches
bool Matches(const EvaluateArgs &) const override
Definition: security/authorization/matchers.h:59
grpc_core::AuthorizationMatcher::Create
static std::unique_ptr< AuthorizationMatcher > Create(Rbac::Permission permission)
Definition: security/authorization/matchers.cc:34
grpc_core::PolicyAuthorizationMatcher::principals_
std::unique_ptr< AuthorizationMatcher > principals_
Definition: security/authorization/matchers.h:213
grpc_core::NotAuthorizationMatcher
Definition: security/authorization/matchers.h:87
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::MetadataAuthorizationMatcher
Definition: security/authorization/matchers.h:99
grpc_core::ReqServerNameAuthorizationMatcher::ReqServerNameAuthorizationMatcher
ReqServerNameAuthorizationMatcher(StringMatcher requested_server_name)
Definition: security/authorization/matchers.h:176
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::PathAuthorizationMatcher::PathAuthorizationMatcher
PathAuthorizationMatcher(StringMatcher path)
Definition: security/authorization/matchers.h:189
matchers
XdsRouteConfigResource::Route::Matchers matchers
Definition: xds_server_config_fetcher.cc:317
grpc_core::IpAuthorizationMatcher
Definition: security/authorization/matchers.h:128
grpc_core::AuthorizationMatcher::~AuthorizationMatcher
virtual ~AuthorizationMatcher()=default
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::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::OrAuthorizationMatcher
Definition: security/authorization/matchers.h:74
grpc_core::AuthenticatedAuthorizationMatcher
Definition: security/authorization/matchers.h:161
resolved_address.h
grpc_core::Rbac::Policy
Definition: rbac_policy.h:151
grpc_core::HeaderMatcher
Definition: matchers/matchers.h:79
grpc_core::IpAuthorizationMatcher::subnet_address_
grpc_resolved_address subnet_address_
Definition: security/authorization/matchers.h:144
evaluate_args.h
grpc_core::PolicyAuthorizationMatcher
Definition: security/authorization/matchers.h:201
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
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
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
grpc_core::MetadataAuthorizationMatcher::Matches
bool Matches(const EvaluateArgs &) const override
Definition: security/authorization/matchers.h:109
grpc_core::AuthorizationMatcher::Matches
virtual bool Matches(const EvaluateArgs &args) const =0
Type
Definition: bloaty/third_party/protobuf/src/google/protobuf/type.pb.h:182
grpc_core::AuthenticatedAuthorizationMatcher::AuthenticatedAuthorizationMatcher
AuthenticatedAuthorizationMatcher(absl::optional< StringMatcher > auth)
Definition: security/authorization/matchers.h:163
grpc_core::ReqServerNameAuthorizationMatcher::matcher_
const StringMatcher matcher_
Definition: security/authorization/matchers.h:183
grpc_core::AlwaysAuthorizationMatcher
Definition: security/authorization/matchers.h:55
grpc_core::NotAuthorizationMatcher::NotAuthorizationMatcher
NotAuthorizationMatcher(std::unique_ptr< AuthorizationMatcher > matcher)
Definition: security/authorization/matchers.h:89
grpc_core::AuthenticatedAuthorizationMatcher::matcher_
const absl::optional< StringMatcher > matcher_
Definition: security/authorization/matchers.h:169
grpc_core::HeaderAuthorizationMatcher::matcher_
const HeaderMatcher matcher_
Definition: security/authorization/matchers.h:124
matchers.h
grpc_core::IpAuthorizationMatcher::Type::kDestIp
@ kDestIp
absl::optional
Definition: abseil-cpp/absl/types/internal/optional.h:61
grpc_core::AndAuthorizationMatcher::AndAuthorizationMatcher
AndAuthorizationMatcher(std::vector< std::unique_ptr< AuthorizationMatcher >> matchers)
Definition: security/authorization/matchers.h:64
rbac_policy.h
grpc_core::Rbac::Principal
Definition: rbac_policy.h:102
grpc_core::AndAuthorizationMatcher
Definition: security/authorization/matchers.h:62
grpc_core::OrAuthorizationMatcher::Matches
bool Matches(const EvaluateArgs &args) const override
Definition: security/authorization/matchers.cc:132
grpc_core::AlwaysAuthorizationMatcher::AlwaysAuthorizationMatcher
AlwaysAuthorizationMatcher()=default
stdint.h
tests.unit._exit_scenarios.port
port
Definition: _exit_scenarios.py:179
grpc_core::PolicyAuthorizationMatcher::Matches
bool Matches(const EvaluateArgs &args) const override
Definition: security/authorization/matchers.cc:231
grpc_core::StringMatcher
Definition: matchers/matchers.h:32
grpc_core::NotAuthorizationMatcher::matcher_
std::unique_ptr< AuthorizationMatcher > matcher_
Definition: security/authorization/matchers.h:96
grpc_core::PolicyAuthorizationMatcher::PolicyAuthorizationMatcher
PolicyAuthorizationMatcher(Rbac::Policy policy)
Definition: security/authorization/matchers.h:203
grpc_core::IpAuthorizationMatcher::Matches
bool Matches(const EvaluateArgs &args) const override
Definition: security/authorization/matchers.cc:165
grpc_core::PortAuthorizationMatcher
Definition: security/authorization/matchers.h:149
std
Definition: grpcpp/impl/codegen/async_unary_call.h:407
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::HeaderAuthorizationMatcher
Definition: security/authorization/matchers.h:116
grpc_core::PortAuthorizationMatcher::PortAuthorizationMatcher
PortAuthorizationMatcher(int port)
Definition: security/authorization/matchers.h:151
grpc_core::PathAuthorizationMatcher
Definition: security/authorization/matchers.h:187
grpc_core::MetadataAuthorizationMatcher::MetadataAuthorizationMatcher
MetadataAuthorizationMatcher(bool invert)
Definition: security/authorization/matchers.h:101
grpc_core::IpAuthorizationMatcher::Type::kSourceIp
@ kSourceIp
grpc_core::OrAuthorizationMatcher::OrAuthorizationMatcher
OrAuthorizationMatcher(std::vector< std::unique_ptr< AuthorizationMatcher >> matchers)
Definition: security/authorization/matchers.h:76
grpc_core::ReqServerNameAuthorizationMatcher
Definition: security/authorization/matchers.h:174
grpc_core::Rbac::Permission
Definition: rbac_policy.h:55
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
grpc_core::HeaderAuthorizationMatcher::HeaderAuthorizationMatcher
HeaderAuthorizationMatcher(HeaderMatcher matcher)
Definition: security/authorization/matchers.h:118
grpc_core::MetadataAuthorizationMatcher::invert_
const bool invert_
Definition: security/authorization/matchers.h:112
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::IpAuthorizationMatcher::Type::kRemoteIp
@ kRemoteIp
grpc_core::AuthorizationMatcher
Definition: security/authorization/matchers.h:36
grpc_core::NotAuthorizationMatcher::Matches
bool Matches(const EvaluateArgs &args) const override
Definition: security/authorization/matchers.cc:141
grpc_core::ReqServerNameAuthorizationMatcher::Matches
bool Matches(const EvaluateArgs &) const override
Definition: security/authorization/matchers.cc:218
port_platform.h


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