xref: /aosp_15_r20/external/grpc-grpc/test/cpp/interop/client_helper.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 
19 #include "test/cpp/interop/client_helper.h"
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/escaping.h"
28 #include "absl/strings/match.h"
29 
30 #include <grpc/grpc.h>
31 #include <grpc/support/alloc.h>
32 #include <grpc/support/log.h>
33 #include <grpcpp/channel.h>
34 #include <grpcpp/create_channel.h>
35 #include <grpcpp/security/credentials.h>
36 
37 #include "test/core/security/oauth2_utils.h"
38 #include "test/cpp/util/create_test_channel.h"
39 #include "test/cpp/util/test_credentials_provider.h"
40 
41 ABSL_DECLARE_FLAG(std::string, custom_credentials_type);
42 ABSL_DECLARE_FLAG(std::string, default_service_account);
43 ABSL_DECLARE_FLAG(std::string, oauth_scope);
44 ABSL_DECLARE_FLAG(std::string, service_account_key_file);
45 ABSL_DECLARE_FLAG(std::string, server_host);
46 ABSL_DECLARE_FLAG(std::string, server_host_override);
47 ABSL_DECLARE_FLAG(int32_t, server_port);
48 ABSL_DECLARE_FLAG(std::string, test_case);
49 ABSL_DECLARE_FLAG(bool, use_alts);
50 ABSL_DECLARE_FLAG(bool, use_test_ca);
51 ABSL_DECLARE_FLAG(bool, use_tls);
52 
53 namespace grpc {
54 namespace testing {
55 
GetServiceAccountJsonKey()56 std::string GetServiceAccountJsonKey() {
57   static std::string json_key;
58   if (json_key.empty()) {
59     std::ifstream json_key_file(absl::GetFlag(FLAGS_service_account_key_file));
60     std::stringstream key_stream;
61     key_stream << json_key_file.rdbuf();
62     json_key = key_stream.str();
63   }
64   return json_key;
65 }
66 
GetOauth2AccessToken()67 std::string GetOauth2AccessToken() {
68   std::shared_ptr<CallCredentials> creds = GoogleComputeEngineCredentials();
69   char* token = grpc_test_fetch_oauth2_token_with_credentials(creds->c_creds_);
70   GPR_ASSERT(token != nullptr);
71   gpr_log(GPR_INFO, "Get raw oauth2 access token: %s", token);
72   std::string access_token(token + sizeof("Bearer ") - 1);
73   gpr_free(token);
74   return access_token;
75 }
76 
UpdateActions(std::unordered_map<std::string,std::function<bool ()>> *)77 void UpdateActions(
78     std::unordered_map<std::string, std::function<bool()>>* /*actions*/) {}
79 
CreateChannelForTestCase(const std::string & test_case,std::vector<std::unique_ptr<experimental::ClientInterceptorFactoryInterface>> interceptor_creators,ChannelArguments channel_args)80 std::shared_ptr<Channel> CreateChannelForTestCase(
81     const std::string& test_case,
82     std::vector<
83         std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>
84         interceptor_creators,
85     ChannelArguments channel_args) {
86   std::string server_uri = absl::GetFlag(FLAGS_server_host);
87   int32_t port = absl::GetFlag(FLAGS_server_port);
88   if (port != 0) {
89     absl::StrAppend(&server_uri, ":", std::to_string(port));
90   }
91   std::shared_ptr<CallCredentials> creds;
92   if (test_case == "compute_engine_creds") {
93     creds = absl::GetFlag(FLAGS_custom_credentials_type) ==
94                     "google_default_credentials"
95                 ? nullptr
96                 : GoogleComputeEngineCredentials();
97   } else if (test_case == "jwt_token_creds") {
98     std::string json_key = GetServiceAccountJsonKey();
99     std::chrono::seconds token_lifetime = std::chrono::hours(1);
100     creds = absl::GetFlag(FLAGS_custom_credentials_type) ==
101                     "google_default_credentials"
102                 ? nullptr
103                 : ServiceAccountJWTAccessCredentials(json_key,
104                                                      token_lifetime.count());
105   } else if (test_case == "oauth2_auth_token") {
106     creds = absl::GetFlag(FLAGS_custom_credentials_type) ==
107                     "google_default_credentials"
108                 ? nullptr
109                 : AccessTokenCredentials(GetOauth2AccessToken());
110   } else if (test_case == "pick_first_unary") {
111     // allow the LB policy to be configured with service config
112     channel_args.SetInt(GRPC_ARG_SERVICE_CONFIG_DISABLE_RESOLUTION, 0);
113     return CreateTestChannel(
114         server_uri, absl::GetFlag(FLAGS_custom_credentials_type),
115         absl::GetFlag(FLAGS_server_host_override),
116         !absl::GetFlag(FLAGS_use_test_ca), creds, channel_args);
117   }
118   if (absl::GetFlag(FLAGS_custom_credentials_type).empty()) {
119     transport_security security_type =
120         absl::GetFlag(FLAGS_use_alts)
121             ? ALTS
122             : (absl::GetFlag(FLAGS_use_tls) ? TLS : INSECURE);
123     return CreateTestChannel(
124         server_uri, absl::GetFlag(FLAGS_server_host_override), security_type,
125         !absl::GetFlag(FLAGS_use_test_ca), creds, channel_args,
126         std::move(interceptor_creators));
127   } else {
128     if (interceptor_creators.empty()) {
129       return CreateTestChannel(server_uri,
130                                absl::GetFlag(FLAGS_custom_credentials_type), "",
131                                false, creds, channel_args);
132     } else {
133       return CreateTestChannel(
134           server_uri, absl::GetFlag(FLAGS_custom_credentials_type), creds,
135           std::move(interceptor_creators), channel_args);
136     }
137   }
138 }
139 
log_metadata_entry(const std::string & prefix,const grpc::string_ref & key,const grpc::string_ref & value)140 static void log_metadata_entry(const std::string& prefix,
141                                const grpc::string_ref& key,
142                                const grpc::string_ref& value) {
143   std::string key_str(key.begin(), key.end());
144   std::string value_str(value.begin(), value.end());
145   if (absl::EndsWith(key_str, "-bin")) {
146     value_str = absl::Base64Escape(value_str);
147   }
148   gpr_log(GPR_ERROR, "%s %s: %s", prefix.c_str(), key_str.c_str(),
149           value_str.c_str());
150 }
151 
Intercept(experimental::InterceptorBatchMethods * methods)152 void MetadataAndStatusLoggerInterceptor::Intercept(
153     experimental::InterceptorBatchMethods* methods) {
154   if (methods->QueryInterceptionHookPoint(
155           experimental::InterceptionHookPoints::POST_RECV_INITIAL_METADATA)) {
156     auto initial_metadata = methods->GetRecvInitialMetadata();
157 
158     for (const auto& entry : *initial_metadata) {
159       log_metadata_entry("GRPC_INITIAL_METADATA", entry.first, entry.second);
160     }
161   }
162 
163   if (methods->QueryInterceptionHookPoint(
164           experimental::InterceptionHookPoints::POST_RECV_STATUS)) {
165     auto trailing_metadata = methods->GetRecvTrailingMetadata();
166     for (const auto& entry : *trailing_metadata) {
167       log_metadata_entry("GRPC_TRAILING_METADATA", entry.first, entry.second);
168     }
169 
170     auto status = methods->GetRecvStatus();
171     gpr_log(GPR_ERROR, "GRPC_STATUS %d", status->error_code());
172     gpr_log(GPR_ERROR, "GRPC_ERROR_MESSAGE %s",
173             status->error_message().c_str());
174   }
175 
176   methods->Proceed();
177 }
178 
179 }  // namespace testing
180 }  // namespace grpc
181