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 <gflags/gflags.h>
26 #include <grpc/grpc.h>
27 #include <grpc/support/alloc.h>
28 #include <grpc/support/log.h>
29 #include <grpcpp/channel.h>
30 #include <grpcpp/create_channel.h>
31 #include <grpcpp/security/credentials.h>
32
33 #include "src/cpp/client/secure_credentials.h"
34 #include "test/core/security/oauth2_utils.h"
35 #include "test/cpp/util/create_test_channel.h"
36 #include "test/cpp/util/test_credentials_provider.h"
37
38 DECLARE_bool(use_alts);
39 DECLARE_bool(use_tls);
40 DECLARE_string(custom_credentials_type);
41 DECLARE_bool(use_test_ca);
42 DECLARE_int32(server_port);
43 DECLARE_string(server_host);
44 DECLARE_string(server_host_override);
45 DECLARE_string(test_case);
46 DECLARE_string(default_service_account);
47 DECLARE_string(service_account_key_file);
48 DECLARE_string(oauth_scope);
49
50 namespace grpc {
51 namespace testing {
52
GetServiceAccountJsonKey()53 std::string GetServiceAccountJsonKey() {
54 static std::string json_key;
55 if (json_key.empty()) {
56 std::ifstream json_key_file(FLAGS_service_account_key_file);
57 std::stringstream key_stream;
58 key_stream << json_key_file.rdbuf();
59 json_key = key_stream.str();
60 }
61 return json_key;
62 }
63
GetOauth2AccessToken()64 std::string GetOauth2AccessToken() {
65 std::shared_ptr<CallCredentials> creds = GoogleComputeEngineCredentials();
66 SecureCallCredentials* secure_creds =
67 dynamic_cast<SecureCallCredentials*>(creds.get());
68 GPR_ASSERT(secure_creds != nullptr);
69 grpc_call_credentials* c_creds = secure_creds->GetRawCreds();
70 char* token = grpc_test_fetch_oauth2_token_with_credentials(c_creds);
71 GPR_ASSERT(token != nullptr);
72 gpr_log(GPR_INFO, "Get raw oauth2 access token: %s", token);
73 std::string access_token(token + sizeof("Bearer ") - 1);
74 gpr_free(token);
75 return access_token;
76 }
77
UpdateActions(std::unordered_map<std::string,std::function<bool ()>> *)78 void UpdateActions(
79 std::unordered_map<std::string, std::function<bool()>>* /*actions*/) {}
80
CreateChannelForTestCase(const std::string & test_case,std::vector<std::unique_ptr<experimental::ClientInterceptorFactoryInterface>> interceptor_creators)81 std::shared_ptr<Channel> CreateChannelForTestCase(
82 const std::string& test_case,
83 std::vector<
84 std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>
85 interceptor_creators) {
86 GPR_ASSERT(FLAGS_server_port);
87 const int host_port_buf_size = 1024;
88 char host_port[host_port_buf_size];
89 snprintf(host_port, host_port_buf_size, "%s:%d", FLAGS_server_host.c_str(),
90 FLAGS_server_port);
91
92 std::shared_ptr<CallCredentials> creds;
93 if (test_case == "compute_engine_creds") {
94 creds = FLAGS_custom_credentials_type == "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 = FLAGS_custom_credentials_type == "google_default_credentials"
101 ? nullptr
102 : ServiceAccountJWTAccessCredentials(json_key,
103 token_lifetime.count());
104 } else if (test_case == "oauth2_auth_token") {
105 creds = FLAGS_custom_credentials_type == "google_default_credentials"
106 ? nullptr
107 : AccessTokenCredentials(GetOauth2AccessToken());
108 } else if (test_case == "pick_first_unary") {
109 ChannelArguments channel_args;
110 // allow the LB policy to be configured with service config
111 channel_args.SetInt(GRPC_ARG_SERVICE_CONFIG_DISABLE_RESOLUTION, 0);
112 return CreateTestChannel(host_port, FLAGS_custom_credentials_type,
113 FLAGS_server_host_override, !FLAGS_use_test_ca,
114 creds, channel_args);
115 }
116 if (FLAGS_custom_credentials_type.empty()) {
117 transport_security security_type =
118 FLAGS_use_alts ? ALTS : (FLAGS_use_tls ? TLS : INSECURE);
119 return CreateTestChannel(host_port, FLAGS_server_host_override,
120 security_type, !FLAGS_use_test_ca, creds,
121 std::move(interceptor_creators));
122 } else {
123 if (interceptor_creators.empty()) {
124 return CreateTestChannel(host_port, FLAGS_custom_credentials_type, creds);
125 } else {
126 return CreateTestChannel(host_port, FLAGS_custom_credentials_type, creds,
127 std::move(interceptor_creators));
128 }
129 }
130 }
131
132 } // namespace testing
133 } // namespace grpc
134