Debian Codegraph @

src/core/lib/security/authorization/rbac_translator.cc

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 #include <grpc/support/port_platform.h>
16
17 #include "src/core/lib/security/authorization/rbac_translator.h"
18
19 #include <stddef.h>
20
21 #include <algorithm>
22 #include <map>
23 #include <memory>
24 #include <string>
25 #include <utility>
26 #include <vector>
27
28 #include "absl/status/status.h"
29 #include "absl/strings/match.h"
30 #include "absl/strings/str_cat.h"
31 #include "absl/strings/str_format.h"
32 #include "absl/strings/strip.h"
33
34 #include "src/core/lib/gpr/useful.h"
35 #include "src/core/lib/json/json.h"
36 #include "src/core/lib/matchers/matchers.h"
37
38 namespace grpc_core {
39
40 namespace {
41
42 absl::string_view GetMatcherType(absl::string_view value,
43 StringMatcher::Type* type) {
44 if (value == "*") {
45 *type = StringMatcher::Type::kSafeRegex;
46 // Presence match checks for non empty strings.
47 return ".+";
48 } else if (absl::StartsWith(value, "*")) {
49 *type = StringMatcher::Type::kSuffix;
50 return absl::StripPrefix(value, "*");
51 } else if (absl::EndsWith(value, "*")) {
52 *type = StringMatcher::Type::kPrefix;
53 return absl::StripSuffix(value, "*");
54 }
55 *type = StringMatcher::Type::kExact;
56 return value;
57 }
58
59 absl::StatusOr<StringMatcher> GetStringMatcher(absl::string_view value) {
60 StringMatcher::Type type;
61 absl::string_view matcher = GetMatcherType(value, &type);
62 return StringMatcher::Create(type, matcher);
63 }
64
65 absl::StatusOr<HeaderMatcher> GetHeaderMatcher(absl::string_view name,
66 absl::string_view value) {
67 StringMatcher::Type type;
68 absl::string_view matcher = GetMatcherType(value, &type);
69 return HeaderMatcher::Create(name, static_cast<HeaderMatcher::Type>(type),
70 matcher);
71 }
72
73 bool IsUnsupportedHeader(absl::string_view header_name) {
74 static const char* const kUnsupportedHeaders[] = {"host",
75 "connection",
76 "keep-alive",
77 "proxy-authenticate",
78 "proxy-authorization",
79 "te",
80 "trailer",
81 "transfer-encoding",
82 "upgrade"};
83 for (size_t i = 0; i < GPR_ARRAY_SIZE(kUnsupportedHeaders); ++i) {
84 if (absl::EqualsIgnoreCase(header_name, kUnsupportedHeaders[i])) {
85 return true;
86 }
87 }
88 return false;
89 }
90
91 absl::StatusOr<Rbac::Principal> ParsePrincipalsArray(const Json& json) {
92 std::vector<std::unique_ptr<Rbac::Principal>> principal_names;
93 for (size_t i = 0; i < json.array_value().size(); ++i) {
94 const Json& child = json.array_value().at(i);
95 if (child.type() != Json::Type::STRING) {
96 return absl::InvalidArgumentError(
97 absl::StrCat("\"principals\" ", i, ": is not a string."));
98 }
99 auto matcher_or = GetStringMatcher(child.string_value());
100 if (!matcher_or.ok()) {
101 return absl::Status(matcher_or.status().code(),
102 absl::StrCat("\"principals\" ", i, ": ",
103 matcher_or.status().message()));
104 }
105 principal_names.push_back(std::make_unique<Rbac::Principal>(
106 Rbac::Principal::MakeAuthenticatedPrincipal(
107 std::move(matcher_or.value()))));
108 }
109 return Rbac::Principal::MakeOrPrincipal(std::move(principal_names));
110 }
111
112 absl::StatusOr<Rbac::Principal> ParsePeer(const Json& json) {
113 std::vector<std::unique_ptr<Rbac::Principal>> peer;
114 auto it = json.object_value().find("principals");
115 if (it != json.object_value().end()) {
116 if (it->second.type() != Json::Type::ARRAY) {
117 return absl::InvalidArgumentError("\"principals\" is not an array.");
118 }
119 auto principal_names_or = ParsePrincipalsArray(it->second);
120 if (!principal_names_or.ok()) return principal_names_or.status();
121 if (!principal_names_or.value().principals.empty()) {
122 peer.push_back(std::make_unique<Rbac::Principal>(
123 std::move(principal_names_or.value())));
124 }
125 }
126 if (peer.empty()) {
127 return Rbac::Principal::MakeAnyPrincipal();
128 }
129 return Rbac::Principal::MakeAndPrincipal(std::move(peer));
130 }
131
132 absl::StatusOr<Rbac::Permission> ParseHeaderValues(
133 const Json& json, absl::string_view header_name) {
134 if (json.array_value().empty()) {
135 return absl::InvalidArgumentError("\"values\" list is empty.");
136 }
137 std::vector<std::unique_ptr<Rbac::Permission>> values;
138 for (size_t i = 0; i < json.array_value().size(); ++i) {
139 const Json& child = json.array_value().at(i);
140 if (child.type() != Json::Type::STRING) {
141 return absl::InvalidArgumentError(
142 absl::StrCat("\"values\" ", i, ": is not a string."));
143 }
144 auto matcher_or = GetHeaderMatcher(header_name, child.string_value());
145 if (!matcher_or.ok()) {
146 return absl::Status(
147 matcher_or.status().code(),
148 absl::StrCat("\"values\" ", i, ": ", matcher_or.status().message()));
149 }
150 values.push_back(std::make_unique<Rbac::Permission>(
151 Rbac::Permission::MakeHeaderPermission(std::move(matcher_or.value()))));
152 }
153 return Rbac::Permission::MakeOrPermission(std::move(values));
154 }
155
156 absl::StatusOr<Rbac::Permission> ParseHeaders(const Json& json) {
157 auto it = json.object_value().find("key");
158 if (it == json.object_value().end()) {
159 return absl::InvalidArgumentError("\"key\" is not present.");
160 }
161 if (it->second.type() != Json::Type::STRING) {
162 return absl::InvalidArgumentError("\"key\" is not a string.");
163 }
164 absl::string_view header_name = it->second.string_value();
165 if (absl::StartsWith(header_name, ":") ||
166 absl::StartsWith(header_name, "grpc-") ||
167 IsUnsupportedHeader(header_name)) {
168 return absl::InvalidArgumentError(
169 absl::StrFormat("Unsupported \"key\" %s.", header_name));
170 }
171 it = json.object_value().find("values");
172 if (it == json.object_value().end()) {
173 return absl::InvalidArgumentError("\"values\" is not present.");
174 }
175 if (it->second.type() != Json::Type::ARRAY) {
176 return absl::InvalidArgumentError("\"values\" is not an array.");
177 }
178 return ParseHeaderValues(it->second, header_name);
179 }
180
181 absl::StatusOr<Rbac::Permission> ParseHeadersArray(const Json& json) {
182 std::vector<std::unique_ptr<Rbac::Permission>> headers;
183 for (size_t i = 0; i < json.array_value().size(); ++i) {
184 const Json& child = json.array_value().at(i);
185 if (child.type() != Json::Type::OBJECT) {
186 return absl::InvalidArgumentError(
187 absl::StrCat("\"headers\" ", i, ": is not an object."));
188 }
189 auto headers_or = ParseHeaders(child);
190 if (!headers_or.ok()) {
191 return absl::Status(
192 headers_or.status().code(),
193 absl::StrCat("\"headers\" ", i, ": ", headers_or.status().message()));
194 }
195 headers.push_back(
196 std::make_unique<Rbac::Permission>(std::move(headers_or.value())));
197 }
198 return Rbac::Permission::MakeAndPermission(std::move(headers));
199 }
200
201 absl::StatusOr<Rbac::Permission> ParsePathsArray(const Json& json) {
202 std::vector<std::unique_ptr<Rbac::Permission>> paths;
203 for (size_t i = 0; i < json.array_value().size(); ++i) {
204 const Json& child = json.array_value().at(i);
205 if (child.type() != Json::Type::STRING) {
206 return absl::InvalidArgumentError(
207 absl::StrCat("\"paths\" ", i, ": is not a string."));
208 }
209 auto matcher_or = GetStringMatcher(child.string_value());
210 if (!matcher_or.ok()) {
211 return absl::Status(
212 matcher_or.status().code(),
213 absl::StrCat("\"paths\" ", i, ": ", matcher_or.status().message()));
214 }
215 paths.push_back(std::make_unique<Rbac::Permission>(
216 Rbac::Permission::MakePathPermission(std::move(matcher_or.value()))));
217 }
218 return Rbac::Permission::MakeOrPermission(std::move(paths));
219 }
220
221 absl::StatusOr<Rbac::Permission> ParseRequest(const Json& json) {
222 std::vector<std::unique_ptr<Rbac::Permission>> request;
223 auto it = json.object_value().find("paths");
224 if (it != json.object_value().end()) {
225 if (it->second.type() != Json::Type::ARRAY) {
226 return absl::InvalidArgumentError("\"paths\" is not an array.");
227 }
228 auto paths_or = ParsePathsArray(it->second);
229 if (!paths_or.ok()) return paths_or.status();
230 if (!paths_or.value().permissions.empty()) {
231 request.push_back(
232 std::make_unique<Rbac::Permission>(std::move(paths_or.value())));
233 }
234 }
235 it = json.object_value().find("headers");
236 if (it != json.object_value().end()) {
237 if (it->second.type() != Json::Type::ARRAY) {
238 return absl::InvalidArgumentError("\"headers\" is not an array.");
239 }
240 auto headers_or = ParseHeadersArray(it->second);
241 if (!headers_or.ok()) return headers_or.status();
242 if (!headers_or.value().permissions.empty()) {
243 request.push_back(
244 std::make_unique<Rbac::Permission>(std::move(headers_or.value())));
245 }
246 }
247 if (request.empty()) {
248 return Rbac::Permission::MakeAnyPermission();
249 }
250 return Rbac::Permission::MakeAndPermission(std::move(request));
251 }
252
253 absl::StatusOr<Rbac::Policy> ParseRules(const Json& json) {
254 Rbac::Principal principals;
255 auto it = json.object_value().find("source");
256 if (it != json.object_value().end()) {
257 if (it->second.type() != Json::Type::OBJECT) {
258 return absl::InvalidArgumentError("\"source\" is not an object.");
259 }
260 auto peer_or = ParsePeer(it->second);
261 if (!peer_or.ok()) return peer_or.status();
262 principals = std::move(peer_or.value());
263 } else {
264 principals = Rbac::Principal::MakeAnyPrincipal();
265 }
266 Rbac::Permission permissions;
267 it = json.object_value().find("request");
268 if (it != json.object_value().end()) {
269 if (it->second.type() != Json::Type::OBJECT) {
270 return absl::InvalidArgumentError("\"request\" is not an object.");
271 }
272 auto request_or = ParseRequest(it->second);
273 if (!request_or.ok()) return request_or.status();
274 permissions = std::move(request_or.value());
275 } else {
276 permissions = Rbac::Permission::MakeAnyPermission();
277 }
278 return Rbac::Policy(std::move(permissions), std::move(principals));
279 }
280
281 absl::StatusOr<std::map<std::string, Rbac::Policy>> ParseRulesArray(
282 const Json& json, absl::string_view name) {
283 std::map<std::string, Rbac::Policy> policies;
284 for (size_t i = 0; i < json.array_value().size(); ++i) {
285 const Json& child = json.array_value().at(i);
286 if (child.type() != Json::Type::OBJECT) {
287 return absl::InvalidArgumentError(
288 absl::StrCat("rules ", i, ": is not an object."));
289 }
290 auto it = child.object_value().find("name");
291 if (it == child.object_value().end()) {
292 return absl::InvalidArgumentError(
293 absl::StrCat("rules ", i, ": \"name\" is not present."));
294 }
295 if (it->second.type() != Json::Type::STRING) {
296 return absl::InvalidArgumentError(
297 absl::StrCat("rules ", i, ": \"name\" is not a string."));
298 }
299 std::string policy_name =
300 std::string(name) + "_" + it->second.string_value();
301 auto policy_or = ParseRules(child);
302 if (!policy_or.ok()) {
303 return absl::Status(
304 policy_or.status().code(),
305 absl::StrCat("rules ", i, ": ", policy_or.status().message()));
306 }
307 policies[policy_name] = std::move(policy_or.value());
308 }
309 return std::move(policies);
310 }
311
312 absl::StatusOr<Rbac> ParseDenyRulesArray(const Json& json,
313 absl::string_view name) {
314 auto policies_or = ParseRulesArray(json, name);
315 if (!policies_or.ok()) return policies_or.status();
316 return Rbac(Rbac::Action::kDeny, std::move(policies_or.value()));
317 }
318
319 absl::StatusOr<Rbac> ParseAllowRulesArray(const Json& json,
320 absl::string_view name) {
321 auto policies_or = ParseRulesArray(json, name);
322 if (!policies_or.ok()) return policies_or.status();
323 return Rbac(Rbac::Action::kAllow, std::move(policies_or.value()));
324 }
325
326 } // namespace
327
328 absl::StatusOr<RbacPolicies> GenerateRbacPolicies(
329 absl::string_view authz_policy) {
330 auto json = Json::Parse(authz_policy);
331 if (!json.ok()) {
332 return absl::InvalidArgumentError(
333 absl::StrCat("Failed to parse gRPC authorization policy. Error: ",
334 json.status().ToString()));
335 }
336 if (json->type() != Json::Type::OBJECT) {
337 return absl::InvalidArgumentError(
338 "SDK authorization policy is not an object.");
339 }
340 auto it = json->mutable_object()->find("name");
341 if (it == json->mutable_object()->end()) {
342 return absl::InvalidArgumentError("\"name\" field is not present.");
343 }
344 if (it->second.type() != Json::Type::STRING) {
345 return absl::InvalidArgumentError("\"name\" is not a string.");
346 }
347 absl::string_view name = it->second.string_value();
348 RbacPolicies rbac_policies;
349 it = json->mutable_object()->find("deny_rules");
350 if (it != json->mutable_object()->end()) {
351 if (it->second.type() != Json::Type::ARRAY) {
352 return absl::InvalidArgumentError("\"deny_rules\" is not an array.");
353 }
354 auto deny_policy_or = ParseDenyRulesArray(it->second, name);
355 if (!deny_policy_or.ok()) {
356 return absl::Status(
357 deny_policy_or.status().code(),
358 absl::StrCat("deny_", deny_policy_or.status().message()));
359 }
360 rbac_policies.deny_policy = std::move(deny_policy_or.value());
361 } else {
362 rbac_policies.deny_policy.action = Rbac::Action::kDeny;
363 }
364 it = json->mutable_object()->find("allow_rules");
365 if (it == json->mutable_object()->end()) {
366 return absl::InvalidArgumentError("\"allow_rules\" is not present.");
367 }
368 if (it->second.type() != Json::Type::ARRAY) {
369 return absl::InvalidArgumentError("\"allow_rules\" is not an array.");
370 }
371 auto allow_policy_or = ParseAllowRulesArray(it->second, name);
372 if (!allow_policy_or.ok()) {
373 return absl::Status(
374 allow_policy_or.status().code(),
375 absl::StrCat("allow_", allow_policy_or.status().message()));
376 }
377 rbac_policies.allow_policy = std::move(allow_policy_or.value());
378 return std::move(rbac_policies);
379 }
380
381 } // namespace grpc_core