Debian Codegraph @

test/cpp/interop/client_helper.cc

raw
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/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>
34 #include <grpcpp/security/credentials.h>
35
36 #include "src/core/lib/slice/b64.h"
37 #include "src/cpp/client/secure_credentials.h"
38 #include "test/core/security/oauth2_utils.h"
39 #include "test/cpp/util/create_test_channel.h"
40 #include "test/cpp/util/test_credentials_provider.h"
41
42 ABSL_DECLARE_FLAG(bool, use_alts);
43 ABSL_DECLARE_FLAG(bool, use_tls);
44 ABSL_DECLARE_FLAG(std::string, custom_credentials_type);
45 ABSL_DECLARE_FLAG(bool, use_test_ca);
46 ABSL_DECLARE_FLAG(int32_t, server_port);
47 ABSL_DECLARE_FLAG(std::string, server_host);
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
57 std::string GetServiceAccountJsonKey() {
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
68 std::string GetOauth2AccessToken() {
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
82 void UpdateActions(
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) {
93 absl::StrAppend(&server_uri, ":", std::to_string(port));
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
100 : GoogleComputeEngineCredentials();
101 } else if (test_case == "jwt_token_creds") {
102 std::string json_key = GetServiceAccountJsonKey();
103 std::chrono::seconds token_lifetime = std::chrono::hours(1);
104 creds = absl::GetFlag(FLAGS_custom_credentials_type) ==
105 "google_default_credentials"
106 ? nullptr
107 : ServiceAccountJWTAccessCredentials(json_key,
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
113 : AccessTokenCredentials(GetOauth2AccessToken());
114 } else if (test_case == "pick_first_unary") {
115 ChannelArguments channel_args;
116 // allow the LB policy to be configured with service config
117 channel_args.SetInt(GRPC_ARG_SERVICE_CONFIG_DISABLE_RESOLUTION, 0);
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);
128 return CreateTestChannel(server_uri,
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 {
137 return CreateTestChannel(server_uri,
138 absl::GetFlag(FLAGS_custom_credentials_type),
139 creds, std::move(interceptor_creators));
140 }
141 }
142 }
143
144 static void log_metadata_entry(const std::string& prefix,
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
159 void MetadataAndStatusLoggerInterceptor::Intercept(
160 experimental::InterceptorBatchMethods* methods) {
161 if (methods->QueryInterceptionHookPoint(
162 experimental::InterceptionHookPoints::POST_RECV_INITIAL_METADATA)) {
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(
171 experimental::InterceptionHookPoints::POST_RECV_STATUS)) {
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