Debian Codegraph @

src/core/lib/security/authorization/rbac_policy.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_RBAC_POLICY_H
16 #define GRPC_CORE_LIB_SECURITY_AUTHORIZATION_RBAC_POLICY_H
17
18 #include <grpc/support/port_platform.h>
19
20 #include <stdint.h>
21
22 #include <map>
23 #include <memory>
24 #include <string>
25 #include <vector>
26
27 #include "absl/types/optional.h"
28
29 #include "src/core/lib/matchers/matchers.h"
30
31 namespace grpc_core {
32
33 // Represents Envoy RBAC Proto. [See
34 // https://github.com/envoyproxy/envoy/blob/release/v1.17/api/envoy/config/rbac/v3/rbac.proto]
35 struct Rbac {
36 enum class Action {
37 kAllow,
38 kDeny,
39 };
40
41 struct CidrRange {
42 CidrRange() = default;
43 CidrRange(std::string address_prefix, uint32_t prefix_len);
44
45 CidrRange(CidrRange&& other) noexcept;
46 CidrRange& operator=(CidrRange&& other) noexcept;
47
48 std::string ToString() const;
49
50 std::string address_prefix;
51 uint32_t prefix_len;
52 };
53
54 // TODO(ashithasantosh): Support for destination_port_range.
55 struct Permission {
56 enum class RuleType {
57 kAnd,
58 kOr,
59 kNot,
60 kAny,
61 kHeader,
62 kPath,
63 kDestIp,
64 kDestPort,
65 kMetadata,
66 kReqServerName,
67 };
68
69 static Permission MakeAndPermission(
70 std::vector<std::unique_ptr<Permission>> permissions);
71 static Permission MakeOrPermission(
72 std::vector<std::unique_ptr<Permission>> permissions);
73 static Permission MakeNotPermission(Permission permission);
74 static Permission MakeAnyPermission();
75 static Permission MakeHeaderPermission(HeaderMatcher header_matcher);
76 static Permission MakePathPermission(StringMatcher string_matcher);
77 static Permission MakeDestIpPermission(CidrRange ip);
78 static Permission MakeDestPortPermission(int port);
79 // All the other fields in MetadataMatcher are ignored except invert.
80 static Permission MakeMetadataPermission(bool invert);
81 static Permission MakeReqServerNamePermission(StringMatcher string_matcher);
82
83 Permission() = default;
84
85 Permission(Permission&& other) noexcept;
86 Permission& operator=(Permission&& other) noexcept;
87
88 std::string ToString() const;
89
90 RuleType type = RuleType::kAnd;
91 HeaderMatcher header_matcher;
92 StringMatcher string_matcher;
93 CidrRange ip;
94 int port;
95 // For type kAnd/kOr/kNot. For kNot type, the vector will have only one
96 // element.
97 std::vector<std::unique_ptr<Permission>> permissions;
98 // For kMetadata
99 bool invert = false;
100 };
101
102 struct Principal {
103 enum class RuleType {
104 kAnd,
105 kOr,
106 kNot,
107 kAny,
108 kPrincipalName,
109 kSourceIp,
110 kDirectRemoteIp,
111 kRemoteIp,
112 kHeader,
113 kPath,
114 kMetadata,
115 };
116
117 static Principal MakeAndPrincipal(
118 std::vector<std::unique_ptr<Principal>> principals);
119 static Principal MakeOrPrincipal(
120 std::vector<std::unique_ptr<Principal>> principals);
121 static Principal MakeNotPrincipal(Principal principal);
122 static Principal MakeAnyPrincipal();
123 static Principal MakeAuthenticatedPrincipal(
124 absl::optional<StringMatcher> string_matcher);
125 static Principal MakeSourceIpPrincipal(CidrRange ip);
126 static Principal MakeDirectRemoteIpPrincipal(CidrRange ip);
127 static Principal MakeRemoteIpPrincipal(CidrRange ip);
128 static Principal MakeHeaderPrincipal(HeaderMatcher header_matcher);
129 static Principal MakePathPrincipal(StringMatcher string_matcher);
130 // All the other fields in MetadataMatcher are ignored except invert.
131 static Principal MakeMetadataPrincipal(bool invert);
132
133 Principal() = default;
134
135 Principal(Principal&& other) noexcept;
136 Principal& operator=(Principal&& other) noexcept;
137
138 std::string ToString() const;
139
140 RuleType type = RuleType::kAnd;
141 HeaderMatcher header_matcher;
142 absl::optional<StringMatcher> string_matcher;
143 CidrRange ip;
144 // For type kAnd/kOr/kNot. For kNot type, the vector will have only one
145 // element.
146 std::vector<std::unique_ptr<Principal>> principals;
147 // For kMetadata
148 bool invert = false;
149 };
150
151 struct Policy {
152 Policy() = default;
153 Policy(Permission permissions, Principal principals);
154
155 Policy(Policy&& other) noexcept;
156 Policy& operator=(Policy&& other) noexcept;
157
158 std::string ToString() const;
159
160 Permission permissions;
161 Principal principals;
162 };
163
164 Rbac() = default;
165 Rbac(Rbac::Action action, std::map<std::string, Policy> policies);
166
167 Rbac(Rbac&& other) noexcept;
168 Rbac& operator=(Rbac&& other) noexcept;
169
170 std::string ToString() const;
171
172 Action action;
173 std::map<std::string, Policy> policies;
174 };
175
176 } // namespace grpc_core
177
178 #endif /* GRPC_CORE_LIB_SECURITY_AUTHORIZATION_RBAC_POLICY_H */