evaluate_args.cc
Go to the documentation of this file.
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 
16 
18 
19 #include "absl/status/statusor.h"
20 #include "absl/strings/match.h"
21 #include "absl/strings/numbers.h"
22 
24 #include <grpc/support/log.h>
25 
32 
33 namespace grpc_core {
34 
35 namespace {
36 
38  absl::string_view uri_text) {
40  absl::StatusOr<URI> uri = URI::Parse(uri_text);
41  if (!uri.ok()) {
42  gpr_log(GPR_DEBUG, "Failed to parse uri.");
43  return address;
44  }
45  absl::string_view host_view;
46  absl::string_view port_view;
47  if (!SplitHostPort(uri->path(), &host_view, &port_view)) {
48  gpr_log(GPR_DEBUG, "Failed to split %s into host and port.",
49  uri->path().c_str());
50  return address;
51  }
52  if (!absl::SimpleAtoi(port_view, &address.port)) {
53  gpr_log(GPR_DEBUG, "Port %s is out of range or null.",
54  std::string(port_view).c_str());
55  }
56  address.address_str = std::string(host_view);
58  &address.address, address.address_str.c_str(), address.port);
59  if (!GRPC_ERROR_IS_NONE(error)) {
60  gpr_log(GPR_DEBUG, "Address %s is not IPv4/IPv6. Error: %s",
61  address.address_str.c_str(), grpc_error_std_string(error).c_str());
62  }
64  return address;
65 }
66 
67 } // namespace
68 
70  grpc_endpoint* endpoint) {
71  if (auth_context != nullptr) {
74  spiffe_id =
78  common_name =
80  subject =
82  }
83  if (endpoint != nullptr) {
84  local_address = ParseEndpointUri(grpc_endpoint_get_local_address(endpoint));
85  peer_address = ParseEndpointUri(grpc_endpoint_get_peer(endpoint));
86  }
87 }
88 
90  if (metadata_ != nullptr) {
91  const auto* path = metadata_->get_pointer(HttpPathMetadata());
92  if (path != nullptr) {
93  return path->as_string_view();
94  }
95  }
96  return absl::string_view();
97 }
98 
100  absl::string_view authority;
101  if (metadata_ != nullptr) {
102  if (auto* authority_md = metadata_->get_pointer(HttpAuthorityMetadata())) {
103  authority = authority_md->as_string_view();
104  }
105  }
106  return authority;
107 }
108 
110  if (metadata_ != nullptr) {
111  auto method_md = metadata_->get(HttpMethodMetadata());
112  if (method_md.has_value()) {
113  return HttpMethodMetadata::Encode(*method_md).as_string_view();
114  }
115  }
116  return absl::string_view();
117 }
118 
120  absl::string_view key, std::string* concatenated_value) const {
121  if (metadata_ == nullptr) {
122  return absl::nullopt;
123  }
124  if (absl::EqualsIgnoreCase(key, "te")) {
125  return absl::nullopt;
126  }
127  if (absl::EqualsIgnoreCase(key, "host")) {
128  // Maps legacy host header to :authority.
129  return GetAuthority();
130  }
131  return metadata_->GetStringValue(key, concatenated_value);
132 }
133 
135  if (channel_args_ == nullptr) {
136  return {};
137  }
139 }
140 
142  if (channel_args_ == nullptr) {
143  return "";
144  }
146 }
147 
149  if (channel_args_ == nullptr) {
150  return 0;
151  }
153 }
154 
156  if (channel_args_ == nullptr) {
157  return {};
158  }
160 }
161 
163  if (channel_args_ == nullptr) {
164  return "";
165  }
167 }
168 
170  if (channel_args_ == nullptr) {
171  return 0;
172  }
174 }
175 
177  if (channel_args_ == nullptr) {
178  return "";
179  }
181 }
182 
184  if (channel_args_ == nullptr) {
185  return "";
186  }
187  return channel_args_->spiffe_id;
188 }
189 
190 std::vector<absl::string_view> EvaluateArgs::GetUriSans() const {
191  if (channel_args_ == nullptr) {
192  return {};
193  }
194  return channel_args_->uri_sans;
195 }
196 
197 std::vector<absl::string_view> EvaluateArgs::GetDnsSans() const {
198  if (channel_args_ == nullptr) {
199  return {};
200  }
201  return channel_args_->dns_sans;
202 }
203 
205  if (channel_args_ == nullptr) {
206  return "";
207  }
208  return channel_args_->common_name;
209 }
210 
212  if (channel_args_ == nullptr) {
213  return "";
214  }
215  return channel_args_->subject;
216 }
217 
218 } // namespace grpc_core
GRPC_PEER_DNS_PROPERTY_NAME
#define GRPC_PEER_DNS_PROPERTY_NAME
Definition: grpc_security_constants.h:48
slice.h
grpc_auth_context
Definition: security_context.h:63
log.h
grpc_core::EvaluateArgs::GetPeerAddressString
absl::string_view GetPeerAddressString() const
Definition: evaluate_args.cc:162
grpc_core::EvaluateArgs::PerChannelArgs::dns_sans
std::vector< absl::string_view > dns_sans
Definition: evaluate_args.h:52
grpc_core::MetadataMap::get
absl::optional< typename Which::ValueType > get(Which) const
Definition: metadata_batch.h:1067
grpc_core::HttpMethodMetadata
Definition: metadata_batch.h:136
grpc_core::GetAuthPropertyArray
std::vector< absl::string_view > GetAuthPropertyArray(grpc_auth_context *context, const char *property_name)
Definition: src/core/lib/security/credentials/tls/tls_utils.cc:109
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::MetadataMap::get_pointer
const metadata_detail::Value< Which >::StorageType * get_pointer(Which) const
Definition: metadata_batch.h:1039
grpc_core::HttpPathMetadata
Definition: metadata_batch.h:262
grpc_core::EvaluateArgs::PerChannelArgs::Address::port
int port
Definition: evaluate_args.h:44
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
error
grpc_error_handle error
Definition: retry_filter.cc:499
grpc_endpoint_get_peer
absl::string_view grpc_endpoint_get_peer(grpc_endpoint *ep)
Definition: endpoint.cc:55
grpc_core::EvaluateArgs::PerChannelArgs::uri_sans
std::vector< absl::string_view > uri_sans
Definition: evaluate_args.h:51
grpc_resolved_address
Definition: resolved_address.h:34
grpc_core::SplitHostPort
bool SplitHostPort(absl::string_view name, absl::string_view *host, absl::string_view *port)
Definition: host_port.cc:88
check_documentation.path
path
Definition: check_documentation.py:57
grpc_core::EvaluateArgs::PerChannelArgs::Address::address
grpc_resolved_address address
Definition: evaluate_args.h:41
grpc_core::EvaluateArgs::PerChannelArgs::common_name
absl::string_view common_name
Definition: evaluate_args.h:53
grpc_core::URI::Parse
static absl::StatusOr< URI > Parse(absl::string_view uri_text)
Definition: uri_parser.cc:209
grpc_core::EvaluateArgs::GetLocalAddressString
absl::string_view GetLocalAddressString() const
Definition: evaluate_args.cc:141
grpc_core::EvaluateArgs::GetAuthority
absl::string_view GetAuthority() const
Definition: evaluate_args.cc:99
grpc_core::MetadataMap::GetStringValue
absl::optional< absl::string_view > GetStringValue(absl::string_view name, std::string *buffer) const
Definition: metadata_batch.h:1100
evaluate_args.h
grpc_core::URI::path
const std::string & path() const
Definition: uri_parser.h:70
grpc_core::EvaluateArgs::PerChannelArgs::transport_security_type
absl::string_view transport_security_type
Definition: evaluate_args.h:49
absl::SimpleAtoi
ABSL_NAMESPACE_BEGIN ABSL_MUST_USE_RESULT bool SimpleAtoi(absl::string_view str, int_type *out)
Definition: abseil-cpp/absl/strings/numbers.h:271
parse_address.h
GRPC_TRANSPORT_SECURITY_TYPE_PROPERTY_NAME
#define GRPC_TRANSPORT_SECURITY_TYPE_PROPERTY_NAME
Definition: grpc_security_constants.h:26
GRPC_PEER_SPIFFE_ID_PROPERTY_NAME
#define GRPC_PEER_SPIFFE_ID_PROPERTY_NAME
Definition: grpc_security_constants.h:49
gen_stats_data.c_str
def c_str(s, encoding='ascii')
Definition: gen_stats_data.py:38
grpc_core::EvaluateArgs::GetLocalPort
int GetLocalPort() const
Definition: evaluate_args.cc:148
grpc_core::HttpAuthorityMetadata
Definition: metadata_batch.h:256
grpc_string_to_sockaddr
grpc_error_handle grpc_string_to_sockaddr(grpc_resolved_address *out, const char *addr, int port)
Definition: parse_address.cc:320
grpc_core::EvaluateArgs::PerChannelArgs::local_address
Address local_address
Definition: evaluate_args.h:55
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
grpc_core::EvaluateArgs::GetPath
absl::string_view GetPath() const
Definition: evaluate_args.cc:89
GRPC_X509_SUBJECT_PROPERTY_NAME
#define GRPC_X509_SUBJECT_PROPERTY_NAME
Definition: grpc_security_constants.h:31
absl::optional< absl::string_view >
absl::EqualsIgnoreCase
ABSL_NAMESPACE_BEGIN bool EqualsIgnoreCase(absl::string_view piece1, absl::string_view piece2) noexcept
Definition: abseil-cpp/absl/strings/match.cc:22
GRPC_X509_CN_PROPERTY_NAME
#define GRPC_X509_CN_PROPERTY_NAME
Definition: grpc_security_constants.h:30
grpc_core::EvaluateArgs::GetMethod
absl::string_view GetMethod() const
Definition: evaluate_args.cc:109
grpc_core::EvaluateArgs::PerChannelArgs::PerChannelArgs
PerChannelArgs(grpc_auth_context *auth_context, grpc_endpoint *endpoint)
Definition: evaluate_args.cc:69
grpc_core::slice_detail::BaseSlice::as_string_view
absl::string_view as_string_view() const
Definition: src/core/lib/slice/slice.h:89
grpc_core::EvaluateArgs::channel_args_
PerChannelArgs * channel_args_
Definition: evaluate_args.h:90
error.h
grpc_core::HttpMethodMetadata::Encode
static StaticSlice Encode(ValueType x)
Definition: metadata_batch.cc:199
host_port.h
grpc_core::EvaluateArgs::metadata_
grpc_metadata_batch * metadata_
Definition: evaluate_args.h:89
grpc_core::EvaluateArgs::GetPeerPort
int GetPeerPort() const
Definition: evaluate_args.cc:169
framework.rpc.grpc_channelz.Address
Address
Definition: grpc_channelz.py:50
grpc_core::EvaluateArgs::PerChannelArgs::subject
absl::string_view subject
Definition: evaluate_args.h:54
absl::StatusOr::ok
ABSL_MUST_USE_RESULT bool ok() const
Definition: abseil-cpp/absl/status/statusor.h:491
key
const char * key
Definition: hpack_parser_table.cc:164
grpc_error_std_string
std::string grpc_error_std_string(grpc_error_handle error)
Definition: error.cc:944
grpc_core::GetAuthPropertyValue
absl::string_view GetAuthPropertyValue(grpc_auth_context *context, const char *property_name)
Definition: src/core/lib/security/credentials/tls/tls_utils.cc:93
string_view
absl::string_view string_view
Definition: attr.cc:22
grpc_core::EvaluateArgs::GetPeerAddress
grpc_resolved_address GetPeerAddress() const
Definition: evaluate_args.cc:155
grpc_core::EvaluateArgs::GetLocalAddress
grpc_resolved_address GetLocalAddress() const
Definition: evaluate_args.cc:134
grpc_security_constants.h
grpc_core::EvaluateArgs::PerChannelArgs::Address::address_str
std::string address_str
Definition: evaluate_args.h:43
GRPC_ERROR_UNREF
#define GRPC_ERROR_UNREF(err)
Definition: error.h:262
grpc_core::EvaluateArgs::PerChannelArgs::spiffe_id
absl::string_view spiffe_id
Definition: evaluate_args.h:50
GPR_DEBUG
#define GPR_DEBUG
Definition: include/grpc/impl/codegen/log.h:55
grpc_core::EvaluateArgs::GetSpiffeId
absl::string_view GetSpiffeId() const
Definition: evaluate_args.cc:183
grpc_core::EvaluateArgs::GetUriSans
std::vector< absl::string_view > GetUriSans() const
Definition: evaluate_args.cc:190
grpc_core::EvaluateArgs::GetTransportSecurityType
absl::string_view GetTransportSecurityType() const
Definition: evaluate_args.cc:176
absl::StatusOr
Definition: abseil-cpp/absl/status/statusor.h:187
uri_parser.h
grpc_core::EvaluateArgs::GetDnsSans
std::vector< absl::string_view > GetDnsSans() const
Definition: evaluate_args.cc:197
grpc_endpoint_get_local_address
absl::string_view grpc_endpoint_get_local_address(grpc_endpoint *ep)
Definition: endpoint.cc:59
grpc_error
Definition: error_internal.h:42
grpc_core::EvaluateArgs::GetSubject
absl::string_view GetSubject() const
Definition: evaluate_args.cc:211
grpc_core::EvaluateArgs::GetCommonName
absl::string_view GetCommonName() const
Definition: evaluate_args.cc:204
GRPC_PEER_URI_PROPERTY_NAME
#define GRPC_PEER_URI_PROPERTY_NAME
Definition: grpc_security_constants.h:50
grpc_endpoint
Definition: endpoint.h:105
tls_utils.h
grpc_core::EvaluateArgs::GetHeaderValue
absl::optional< absl::string_view > GetHeaderValue(absl::string_view key, std::string *concatenated_value) const
Definition: evaluate_args.cc:119
GRPC_ERROR_IS_NONE
#define GRPC_ERROR_IS_NONE(err)
Definition: error.h:241
grpc_core::EvaluateArgs::PerChannelArgs::peer_address
Address peer_address
Definition: evaluate_args.h:56
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:59:16