client_helper.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
20 
21 #include <fstream>
22 #include <memory>
23 #include <sstream>
24 
25 #include "absl/flags/declare.h"
26 #include "absl/flags/flag.h"
27 #include "absl/strings/match.h"
28 
29 #include <grpc/grpc.h>
30 #include <grpc/support/alloc.h>
31 #include <grpc/support/log.h>
32 #include <grpcpp/channel.h>
33 #include <grpcpp/create_channel.h>
35 
36 #include "src/core/lib/slice/b64.h"
41 
42 ABSL_DECLARE_FLAG(bool, use_alts);
43 ABSL_DECLARE_FLAG(bool, use_tls);
45 ABSL_DECLARE_FLAG(bool, use_test_ca);
48 ABSL_DECLARE_FLAG(std::string, server_host_override);
49 ABSL_DECLARE_FLAG(std::string, test_case);
50 ABSL_DECLARE_FLAG(std::string, default_service_account);
51 ABSL_DECLARE_FLAG(std::string, service_account_key_file);
52 ABSL_DECLARE_FLAG(std::string, oauth_scope);
53 
54 namespace grpc {
55 namespace testing {
56 
58  static std::string json_key;
59  if (json_key.empty()) {
60  std::ifstream json_key_file(absl::GetFlag(FLAGS_service_account_key_file));
61  std::stringstream key_stream;
62  key_stream << json_key_file.rdbuf();
63  json_key = key_stream.str();
64  }
65  return json_key;
66 }
67 
69  std::shared_ptr<CallCredentials> creds = GoogleComputeEngineCredentials();
70  SecureCallCredentials* secure_creds =
71  dynamic_cast<SecureCallCredentials*>(creds.get());
72  GPR_ASSERT(secure_creds != nullptr);
73  grpc_call_credentials* c_creds = secure_creds->GetRawCreds();
74  char* token = grpc_test_fetch_oauth2_token_with_credentials(c_creds);
75  GPR_ASSERT(token != nullptr);
76  gpr_log(GPR_INFO, "Get raw oauth2 access token: %s", token);
77  std::string access_token(token + sizeof("Bearer ") - 1);
78  gpr_free(token);
79  return access_token;
80 }
81 
83  std::unordered_map<std::string, std::function<bool()>>* /*actions*/) {}
84 
85 std::shared_ptr<Channel> CreateChannelForTestCase(
86  const std::string& test_case,
87  std::vector<
88  std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>
89  interceptor_creators) {
90  std::string server_uri = absl::GetFlag(FLAGS_server_host);
91  int32_t port = absl::GetFlag(FLAGS_server_port);
92  if (port != 0) {
94  }
95  std::shared_ptr<CallCredentials> creds;
96  if (test_case == "compute_engine_creds") {
97  creds = absl::GetFlag(FLAGS_custom_credentials_type) ==
98  "google_default_credentials"
99  ? nullptr
101  } else if (test_case == "jwt_token_creds") {
103  std::chrono::seconds token_lifetime = std::chrono::hours(1);
104  creds = absl::GetFlag(FLAGS_custom_credentials_type) ==
105  "google_default_credentials"
106  ? nullptr
108  token_lifetime.count());
109  } else if (test_case == "oauth2_auth_token") {
110  creds = absl::GetFlag(FLAGS_custom_credentials_type) ==
111  "google_default_credentials"
112  ? nullptr
114  } else if (test_case == "pick_first_unary") {
115  ChannelArguments channel_args;
116  // allow the LB policy to be configured with service config
118  return CreateTestChannel(
119  server_uri, absl::GetFlag(FLAGS_custom_credentials_type),
120  absl::GetFlag(FLAGS_server_host_override),
121  !absl::GetFlag(FLAGS_use_test_ca), creds, channel_args);
122  }
123  if (absl::GetFlag(FLAGS_custom_credentials_type).empty()) {
124  transport_security security_type =
125  absl::GetFlag(FLAGS_use_alts)
126  ? ALTS
127  : (absl::GetFlag(FLAGS_use_tls) ? TLS : INSECURE);
129  absl::GetFlag(FLAGS_server_host_override),
130  security_type, !absl::GetFlag(FLAGS_use_test_ca),
131  creds, std::move(interceptor_creators));
132  } else {
133  if (interceptor_creators.empty()) {
134  return CreateTestChannel(
135  server_uri, absl::GetFlag(FLAGS_custom_credentials_type), creds);
136  } else {
138  absl::GetFlag(FLAGS_custom_credentials_type),
139  creds, std::move(interceptor_creators));
140  }
141  }
142 }
143 
145  const grpc::string_ref& key,
146  const grpc::string_ref& value) {
147  auto key_str = std::string(key.begin(), key.end());
148  auto value_str = std::string(value.begin(), value.end());
149  if (absl::EndsWith(key_str, "-bin")) {
150  auto converted =
151  grpc_base64_encode(value_str.c_str(), value_str.length(), 0, 0);
152  value_str = std::string(converted);
153  gpr_free(converted);
154  }
155  gpr_log(GPR_ERROR, "%s %s: %s", prefix.c_str(), key_str.c_str(),
156  value_str.c_str());
157 }
158 
161  if (methods->QueryInterceptionHookPoint(
163  auto initial_metadata = methods->GetRecvInitialMetadata();
164 
165  for (const auto& entry : *initial_metadata) {
166  log_metadata_entry("GRPC_INITIAL_METADATA", entry.first, entry.second);
167  }
168  }
169 
170  if (methods->QueryInterceptionHookPoint(
172  auto trailing_metadata = methods->GetRecvTrailingMetadata();
173  for (const auto& entry : *trailing_metadata) {
174  log_metadata_entry("GRPC_TRAILING_METADATA", entry.first, entry.second);
175  }
176 
177  auto status = methods->GetRecvStatus();
178  gpr_log(GPR_ERROR, "GRPC_STATUS %d", status->error_code());
179  gpr_log(GPR_ERROR, "GRPC_ERROR_MESSAGE %s",
180  status->error_message().c_str());
181  }
182 
183  methods->Proceed();
184 }
185 
186 } // namespace testing
187 } // namespace grpc
GRPC_ARG_SERVICE_CONFIG_DISABLE_RESOLUTION
#define GRPC_ARG_SERVICE_CONFIG_DISABLE_RESOLUTION
Definition: grpc_types.h:306
test_credentials_provider.h
grpc::string_ref
Definition: grpcpp/impl/codegen/string_ref.h:43
absl::time_internal::cctz::seconds
std::chrono::duration< std::int_fast64_t > seconds
Definition: abseil-cpp/absl/time/internal/cctz/include/cctz/time_zone.h:40
GPR_INFO
#define GPR_INFO
Definition: include/grpc/impl/codegen/log.h:56
testing
Definition: aws_request_signer_test.cc:25
grpc::status
auto status
Definition: cpp/client/credentials_test.cc:200
grpc::experimental::InterceptionHookPoints::POST_RECV_STATUS
@ POST_RECV_STATUS
log.h
absl::StrAppend
void StrAppend(std::string *dest, const AlphaNum &a)
Definition: abseil-cpp/absl/strings/str_cat.cc:193
client_helper.h
grpc::gpr_free
gpr_free(creds_file_name)
grpc
Definition: grpcpp/alarm.h:33
grpc::experimental::InterceptionHookPoints::POST_RECV_INITIAL_METADATA
@ POST_RECV_INITIAL_METADATA
The following two are for all clients and servers.
server_port
static int server_port
Definition: bad_server_response_test.cc:86
grpc::testing::transport_security
transport_security
Definition: create_test_channel.h:34
grpc::experimental::InterceptorBatchMethods::GetRecvTrailingMetadata
virtual std::multimap< grpc::string_ref, grpc::string_ref > * GetRecvTrailingMetadata()=0
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
secure_credentials.h
grpc_call_credentials
Definition: src/core/lib/security/credentials/credentials.h:189
grpc::experimental::InterceptorBatchMethods::QueryInterceptionHookPoint
virtual bool QueryInterceptionHookPoint(InterceptionHookPoints type)=0
grpc::testing::GetOauth2AccessToken
std::string GetOauth2AccessToken()
Definition: client_helper.cc:68
run_xds_tests.server_uri
string server_uri
Definition: run_xds_tests.py:3320
grpc::SecureCallCredentials
Definition: secure_credentials.h:72
create_test_channel.h
grpc::testing::UpdateActions
void UpdateActions(std::unordered_map< std::string, std::function< bool()>> *)
Definition: client_helper.cc:82
grpc::ServiceAccountJWTAccessCredentials
std::shared_ptr< CallCredentials > ServiceAccountJWTAccessCredentials(const grpc::string &json_key, long token_lifetime_seconds=kMaxAuthTokenLifetimeSecs)
grpc_status._async.trailing_metadata
trailing_metadata
Definition: grpcio_status/grpc_status/_async.py:36
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
grpc.h
custom_credentials_type
std::string custom_credentials_type("INSECURE_CREDENTIALS")
oauth2_utils.h
b64.h
channel.h
ABSL_DECLARE_FLAG
ABSL_DECLARE_FLAG(bool, use_alts)
absl::GetFlag
ABSL_MUST_USE_RESULT T GetFlag(const absl::Flag< T > &flag)
Definition: abseil-cpp/absl/flags/flag.h:98
grpc::testing::GetServiceAccountJsonKey
std::string GetServiceAccountJsonKey()
Definition: client_helper.cc:57
grpc::CreateTestChannel
std::shared_ptr< Channel > CreateTestChannel(const std::string &server, const std::string &cred_type, const std::string &override_hostname, bool use_prod_roots, const std::shared_ptr< CallCredentials > &creds, const ChannelArguments &args)
Definition: create_test_channel.cc:88
GPR_ERROR
#define GPR_ERROR
Definition: include/grpc/impl/codegen/log.h:57
grpc_base64_encode
char * grpc_base64_encode(const void *vdata, size_t data_size, int url_safe, int multiline)
Definition: b64.cc:59
grpc::experimental::InterceptorBatchMethods
Definition: impl/codegen/interceptor.h:98
key_str
static char * key_str(grpc_error_strs which)
Definition: error.cc:796
google_benchmark.example.empty
def empty(state)
Definition: example.py:31
grpc::experimental::InterceptorBatchMethods::GetRecvInitialMetadata
virtual std::multimap< grpc::string_ref, grpc::string_ref > * GetRecvInitialMetadata()=0
grpc::testing::MetadataAndStatusLoggerInterceptor::Intercept
void Intercept(experimental::InterceptorBatchMethods *methods) override
Definition: client_helper.cc:159
tests.unit._exit_scenarios.port
port
Definition: _exit_scenarios.py:179
grpc::testing::ALTS
@ ALTS
Definition: create_test_channel.h:34
grpc::SecureCallCredentials::GetRawCreds
grpc_call_credentials * GetRawCreds()
Definition: secure_credentials.h:79
value
const char * value
Definition: hpack_parser_table.cc:165
grpc::ChannelArguments
Definition: grpcpp/support/channel_arguments.h:39
credentials.h
http2_server_health_check.server_host
server_host
Definition: http2_server_health_check.py:27
key
const char * key
Definition: hpack_parser_table.cc:164
grpc::testing::INSECURE
@ INSECURE
Definition: create_test_channel.h:34
grpc::testing::CreateChannelForTestCase
std::shared_ptr< Channel > CreateChannelForTestCase(const std::string &test_case, std::vector< std::unique_ptr< experimental::ClientInterceptorFactoryInterface >> interceptor_creators)
Definition: client_helper.cc:85
alloc.h
prefix
static const char prefix[]
Definition: head_of_line_blocking.cc:28
grpc::ChannelArguments::SetInt
void SetInt(const std::string &key, int value)
Set an integer argument value under key.
Definition: channel_arguments.cc:174
grpc::AccessTokenCredentials
std::shared_ptr< CallCredentials > AccessTokenCredentials(const grpc::string &access_token)
function
std::function< bool(GrpcTool *, int, const char **, const CliCredentials &, GrpcToolOutputCallback)> function
Definition: grpc_tool.cc:250
int32_t
signed int int32_t
Definition: stdint-msvc2008.h:77
grpc::experimental::InterceptorBatchMethods::Proceed
virtual void Proceed()=0
to_string
static bool to_string(zval *from)
Definition: protobuf/php/ext/google/protobuf/convert.c:333
grpc::GoogleComputeEngineCredentials
std::shared_ptr< CallCredentials > GoogleComputeEngineCredentials()
Definition: secure_credentials.cc:325
absl::EndsWith
bool EndsWith(absl::string_view text, absl::string_view suffix) noexcept
Definition: third_party/abseil-cpp/absl/strings/match.h:68
create_channel.h
grpc::experimental::InterceptorBatchMethods::GetRecvStatus
virtual Status * GetRecvStatus()=0
grpc::testing::log_metadata_entry
static void log_metadata_entry(const std::string &prefix, const grpc::string_ref &key, const grpc::string_ref &value)
Definition: client_helper.cc:144
grpc::testing::TLS
@ TLS
Definition: create_test_channel.h:34
grpc_test_fetch_oauth2_token_with_credentials
char * grpc_test_fetch_oauth2_token_with_credentials(grpc_call_credentials *creds)
Definition: oauth2_utils.cc:42


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:55