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 53 grpc::string GetServiceAccountJsonKey() { 54 static grpc::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 64 grpc::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 grpc::string access_token(token + sizeof("Bearer ") - 1); 74 gpr_free(token); 75 return access_token; 76 } 77 78 void UpdateActions( 79 std::unordered_map<grpc::string, std::function<bool()>>* actions) {} 80 81 std::shared_ptr<Channel> CreateChannelForTestCase( 82 const grpc::string& test_case) { 83 GPR_ASSERT(FLAGS_server_port); 84 const int host_port_buf_size = 1024; 85 char host_port[host_port_buf_size]; 86 snprintf(host_port, host_port_buf_size, "%s:%d", FLAGS_server_host.c_str(), 87 FLAGS_server_port); 88 89 std::shared_ptr<CallCredentials> creds; 90 if (test_case == "compute_engine_creds") { 91 creds = FLAGS_custom_credentials_type == "google_default_credentials" 92 ? nullptr 93 : GoogleComputeEngineCredentials(); 94 } else if (test_case == "jwt_token_creds") { 95 grpc::string json_key = GetServiceAccountJsonKey(); 96 std::chrono::seconds token_lifetime = std::chrono::hours(1); 97 creds = FLAGS_custom_credentials_type == "google_default_credentials" 98 ? nullptr 99 : ServiceAccountJWTAccessCredentials(json_key, 100 token_lifetime.count()); 101 } else if (test_case == "oauth2_auth_token") { 102 creds = FLAGS_custom_credentials_type == "google_default_credentials" 103 ? nullptr 104 : AccessTokenCredentials(GetOauth2AccessToken()); 105 } 106 if (FLAGS_custom_credentials_type.empty()) { 107 transport_security security_type = 108 FLAGS_use_alts ? ALTS : (FLAGS_use_tls ? TLS : INSECURE); 109 return CreateTestChannel(host_port, FLAGS_server_host_override, 110 security_type, !FLAGS_use_test_ca, creds); 111 } else { 112 return CreateTestChannel(host_port, FLAGS_custom_credentials_type, creds); 113 } 114 } 115 116 } // namespace testing 117 } // namespace grpc 118