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