Debian Codegraph @

src/core/lib/security/authorization/evaluate_args.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/evaluate_args.h"
18
19 #include <string.h>
20
21 #include "absl/status/status.h"
22 #include "absl/status/statusor.h"
23 #include "absl/strings/match.h"
24 #include "absl/strings/numbers.h"
25
26 #include <grpc/grpc_security_constants.h>
27 #include <grpc/support/log.h>
28
29 #include "src/core/lib/address_utils/parse_address.h"
30 #include "src/core/lib/gprpp/host_port.h"
31 #include "src/core/lib/security/credentials/tls/tls_utils.h"
32 #include "src/core/lib/slice/slice.h"
33 #include "src/core/lib/uri/uri_parser.h"
34
35 namespace grpc_core {
36
37 namespace {
38
39 EvaluateArgs::PerChannelArgs::Address ParseEndpointUri(
40 absl::string_view uri_text) {
41 EvaluateArgs::PerChannelArgs::Address address;
42 absl::StatusOr<URI> uri = URI::Parse(uri_text);
43 if (!uri.ok()) {
44 gpr_log(GPR_DEBUG, "Failed to parse uri.");
45 return address;
46 }
47 absl::string_view host_view;
48 absl::string_view port_view;
49 if (!SplitHostPort(uri->path(), &host_view, &port_view)) {
50 gpr_log(GPR_DEBUG, "Failed to split %s into host and port.",
51 uri->path().c_str());
52 return address;
53 }
54 if (!absl::SimpleAtoi(port_view, &address.port)) {
55 gpr_log(GPR_DEBUG, "Port %s is out of range or null.",
56 std::string(port_view).c_str());
57 }
58 address.address_str = std::string(host_view);
59 auto resolved_address = StringToSockaddr(uri->path());
60 if (!resolved_address.ok()) {
61 gpr_log(GPR_DEBUG, "Address \"%s\" is not IPv4/IPv6. Error: %s",
62 uri->path().c_str(), resolved_address.status().ToString().c_str());
63 memset(&address.address, 0, sizeof(address.address));
64 } else {
65 address.address = *resolved_address;
66 }
67 return address;
68 }
69
70 } // namespace
71
72 EvaluateArgs::PerChannelArgs::PerChannelArgs(grpc_auth_context* auth_context,
73 grpc_endpoint* endpoint) {
74 if (auth_context != nullptr) {
75 transport_security_type = GetAuthPropertyValue(
76 auth_context, GRPC_TRANSPORT_SECURITY_TYPE_PROPERTY_NAME);
77 spiffe_id =
78 GetAuthPropertyValue(auth_context, GRPC_PEER_SPIFFE_ID_PROPERTY_NAME);
79 uri_sans = GetAuthPropertyArray(auth_context, GRPC_PEER_URI_PROPERTY_NAME);
80 dns_sans = GetAuthPropertyArray(auth_context, GRPC_PEER_DNS_PROPERTY_NAME);
81 common_name =
82 GetAuthPropertyValue(auth_context, GRPC_X509_CN_PROPERTY_NAME);
83 subject =
84 GetAuthPropertyValue(auth_context, GRPC_X509_SUBJECT_PROPERTY_NAME);
85 }
86 if (endpoint != nullptr) {
87 local_address = ParseEndpointUri(grpc_endpoint_get_local_address(endpoint));
88 peer_address = ParseEndpointUri(grpc_endpoint_get_peer(endpoint));
89 }
90 }
91
92 absl::string_view EvaluateArgs::GetPath() const {
93 if (metadata_ != nullptr) {
94 const auto* path = metadata_->get_pointer(HttpPathMetadata());
95 if (path != nullptr) {
96 return path->as_string_view();
97 }
98 }
99 return absl::string_view();
100 }
101
102 absl::string_view EvaluateArgs::GetAuthority() const {
103 absl::string_view authority;
104 if (metadata_ != nullptr) {
105 if (auto* authority_md = metadata_->get_pointer(HttpAuthorityMetadata())) {
106 authority = authority_md->as_string_view();
107 }
108 }
109 return authority;
110 }
111
112 absl::string_view EvaluateArgs::GetMethod() const {
113 if (metadata_ != nullptr) {
114 auto method_md = metadata_->get(HttpMethodMetadata());
115 if (method_md.has_value()) {
116 return HttpMethodMetadata::Encode(*method_md).as_string_view();
117 }
118 }
119 return absl::string_view();
120 }
121
122 absl::optional<absl::string_view> EvaluateArgs::GetHeaderValue(
123 absl::string_view key, std::string* concatenated_value) const {
124 if (metadata_ == nullptr) {
125 return absl::nullopt;
126 }
127 if (absl::EqualsIgnoreCase(key, "te")) {
128 return absl::nullopt;
129 }
130 if (absl::EqualsIgnoreCase(key, "host")) {
131 // Maps legacy host header to :authority.
132 return GetAuthority();
133 }
134 return metadata_->GetStringValue(key, concatenated_value);
135 }
136
137 grpc_resolved_address EvaluateArgs::GetLocalAddress() const {
138 if (channel_args_ == nullptr) {
139 return {};
140 }
141 return channel_args_->local_address.address;
142 }
143
144 absl::string_view EvaluateArgs::GetLocalAddressString() const {
145 if (channel_args_ == nullptr) {
146 return "";
147 }
148 return channel_args_->local_address.address_str;
149 }
150
151 int EvaluateArgs::GetLocalPort() const {
152 if (channel_args_ == nullptr) {
153 return 0;
154 }
155 return channel_args_->local_address.port;
156 }
157
158 grpc_resolved_address EvaluateArgs::GetPeerAddress() const {
159 if (channel_args_ == nullptr) {
160 return {};
161 }
162 return channel_args_->peer_address.address;
163 }
164
165 absl::string_view EvaluateArgs::GetPeerAddressString() const {
166 if (channel_args_ == nullptr) {
167 return "";
168 }
169 return channel_args_->peer_address.address_str;
170 }
171
172 int EvaluateArgs::GetPeerPort() const {
173 if (channel_args_ == nullptr) {
174 return 0;
175 }
176 return channel_args_->peer_address.port;
177 }
178
179 absl::string_view EvaluateArgs::GetTransportSecurityType() const {
180 if (channel_args_ == nullptr) {
181 return "";
182 }
183 return channel_args_->transport_security_type;
184 }
185
186 absl::string_view EvaluateArgs::GetSpiffeId() const {
187 if (channel_args_ == nullptr) {
188 return "";
189 }
190 return channel_args_->spiffe_id;
191 }
192
193 std::vector<absl::string_view> EvaluateArgs::GetUriSans() const {
194 if (channel_args_ == nullptr) {
195 return {};
196 }
197 return channel_args_->uri_sans;
198 }
199
200 std::vector<absl::string_view> EvaluateArgs::GetDnsSans() const {
201 if (channel_args_ == nullptr) {
202 return {};
203 }
204 return channel_args_->dns_sans;
205 }
206
207 absl::string_view EvaluateArgs::GetCommonName() const {
208 if (channel_args_ == nullptr) {
209 return "";
210 }
211 return channel_args_->common_name;
212 }
213
214 absl::string_view EvaluateArgs::GetSubject() const {
215 if (channel_args_ == nullptr) {
216 return "";
217 }
218 return channel_args_->subject;
219 }
220
221 } // namespace grpc_core