Debian Codegraph @

src/core/lib/security/authorization/matchers.h

raw
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
18 #include <grpc/support/port_platform.h>
19
20 #include <stdint.h>
21
22 #include <memory>
23 #include <utility>
24 #include <vector>
25
26 #include "absl/types/optional.h"
27
28 #include "src/core/lib/iomgr/resolved_address.h"
29 #include "src/core/lib/matchers/matchers.h"
30 #include "src/core/lib/security/authorization/evaluate_args.h"
31 #include "src/core/lib/security/authorization/rbac_policy.h"
32
33 namespace grpc_core {
34
35 // Describes the rules for matching permission or principal.
36 class AuthorizationMatcher {
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
55 class AlwaysAuthorizationMatcher : public AuthorizationMatcher {
56 public:
57 explicit AlwaysAuthorizationMatcher() = default;
58
59 bool Matches(const EvaluateArgs&) const override { return true; }
60 };
61
62 class AndAuthorizationMatcher : public AuthorizationMatcher {
63 public:
64 explicit AndAuthorizationMatcher(
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
74 class OrAuthorizationMatcher : public AuthorizationMatcher {
75 public:
76 explicit OrAuthorizationMatcher(
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.
87 class NotAuthorizationMatcher : public AuthorizationMatcher {
88 public:
89 explicit NotAuthorizationMatcher(
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
99 class MetadataAuthorizationMatcher : public AuthorizationMatcher {
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.
116 class HeaderAuthorizationMatcher : public AuthorizationMatcher {
117 public:
118 explicit HeaderAuthorizationMatcher(HeaderMatcher matcher)
119 : matcher_(std::move(matcher)) {}
120
121 bool Matches(const EvaluateArgs& args) const override;
122
123 private:
124 const HeaderMatcher matcher_;
125 };
126
127 // Perform a match against IP Cidr Range.
128 class IpAuthorizationMatcher : public AuthorizationMatcher {
129 public:
130 enum class Type {
131 kDestIp,
132 kSourceIp,
133 kDirectRemoteIp,
134 kRemoteIp,
135 };
136
137 IpAuthorizationMatcher(Type type, Rbac::CidrRange range);
138
139 bool Matches(const EvaluateArgs& args) const override;
140
141 private:
142 const Type type_;
143 // Subnet masked address.
144 grpc_resolved_address subnet_address_;
145 const uint32_t prefix_len_;
146 };
147
148 // Perform a match against port number of the destination (local) address.
149 class PortAuthorizationMatcher : public AuthorizationMatcher {
150 public:
151 explicit PortAuthorizationMatcher(int port) : port_(port) {}
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.
161 class AuthenticatedAuthorizationMatcher : public AuthorizationMatcher {
162 public:
163 explicit AuthenticatedAuthorizationMatcher(absl::optional<StringMatcher> auth)
164 : matcher_(std::move(auth)) {}
165
166 bool Matches(const EvaluateArgs& args) const override;
167
168 private:
169 const absl::optional<StringMatcher> matcher_;
170 };
171
172 // Perform a match against the request server from the client's connection
173 // request. This is typically TLS SNI. Currently unsupported.
174 class ReqServerNameAuthorizationMatcher : public AuthorizationMatcher {
175 public:
176 explicit ReqServerNameAuthorizationMatcher(
177 StringMatcher requested_server_name)
178 : matcher_(std::move(requested_server_name)) {}
179
180 bool Matches(const EvaluateArgs&) const override;
181
182 private:
183 const StringMatcher matcher_;
184 };
185
186 // Perform a match against the path header of HTTP request.
187 class PathAuthorizationMatcher : public AuthorizationMatcher {
188 public:
189 explicit PathAuthorizationMatcher(StringMatcher path)
190 : matcher_(std::move(path)) {}
191
192 bool Matches(const EvaluateArgs& args) const override;
193
194 private:
195 const StringMatcher matcher_;
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.
201 class PolicyAuthorizationMatcher : public AuthorizationMatcher {
202 public:
203 explicit PolicyAuthorizationMatcher(Rbac::Policy policy)
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