Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2015, Google Inc. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | #include "test/cpp/interop/client_helper.h" |
| 35 | |
| 36 | #include <fstream> |
| 37 | #include <memory> |
| 38 | #include <sstream> |
| 39 | |
| 40 | #include <unistd.h> |
| 41 | |
| 42 | #include <grpc/grpc.h> |
yang-g | be5f059 | 2015-07-13 11:11:50 -0700 | [diff] [blame] | 43 | #include <grpc/support/alloc.h> |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 44 | #include <grpc/support/log.h> |
| 45 | #include <gflags/gflags.h> |
| 46 | #include <grpc++/channel_arguments.h> |
| 47 | #include <grpc++/channel_interface.h> |
| 48 | #include <grpc++/create_channel.h> |
| 49 | #include <grpc++/credentials.h> |
| 50 | #include <grpc++/stream.h> |
David Garcia Quintas | 80f3995 | 2015-07-21 16:07:36 -0700 | [diff] [blame] | 51 | |
yang-g | be5f059 | 2015-07-13 11:11:50 -0700 | [diff] [blame] | 52 | #include "test/core/security/oauth2_utils.h" |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 53 | #include "test/cpp/util/create_test_channel.h" |
| 54 | |
David Garcia Quintas | 80f3995 | 2015-07-21 16:07:36 -0700 | [diff] [blame] | 55 | #include "src/core/surface/call.h" |
| 56 | #include "src/cpp/client/secure_credentials.h" |
| 57 | |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 58 | DECLARE_bool(enable_ssl); |
| 59 | DECLARE_bool(use_prod_roots); |
| 60 | DECLARE_int32(server_port); |
| 61 | DECLARE_string(server_host); |
| 62 | DECLARE_string(server_host_override); |
| 63 | DECLARE_string(test_case); |
| 64 | DECLARE_string(default_service_account); |
| 65 | DECLARE_string(service_account_key_file); |
| 66 | DECLARE_string(oauth_scope); |
| 67 | |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 68 | namespace grpc { |
| 69 | namespace testing { |
| 70 | |
yang-g | be5f059 | 2015-07-13 11:11:50 -0700 | [diff] [blame] | 71 | namespace { |
| 72 | std::shared_ptr<Credentials> CreateServiceAccountCredentials() { |
| 73 | GPR_ASSERT(FLAGS_enable_ssl); |
| 74 | grpc::string json_key = GetServiceAccountJsonKey(); |
| 75 | std::chrono::seconds token_lifetime = std::chrono::hours(1); |
| 76 | return ServiceAccountCredentials(json_key, FLAGS_oauth_scope, |
| 77 | token_lifetime.count()); |
| 78 | } |
| 79 | } // namespace |
| 80 | |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 81 | grpc::string GetServiceAccountJsonKey() { |
| 82 | static grpc::string json_key; |
| 83 | if (json_key.empty()) { |
| 84 | std::ifstream json_key_file(FLAGS_service_account_key_file); |
| 85 | std::stringstream key_stream; |
| 86 | key_stream << json_key_file.rdbuf(); |
| 87 | json_key = key_stream.str(); |
| 88 | } |
| 89 | return json_key; |
| 90 | } |
| 91 | |
yang-g | be5f059 | 2015-07-13 11:11:50 -0700 | [diff] [blame] | 92 | grpc::string GetOauth2AccessToken() { |
| 93 | std::shared_ptr<Credentials> creds = CreateServiceAccountCredentials(); |
| 94 | SecureCredentials* secure_creds = |
| 95 | dynamic_cast<SecureCredentials*>(creds.get()); |
| 96 | GPR_ASSERT(secure_creds != nullptr); |
| 97 | grpc_credentials* c_creds = secure_creds->GetRawCreds(); |
| 98 | char* token = grpc_test_fetch_oauth2_token_with_credentials(c_creds); |
| 99 | GPR_ASSERT(token != nullptr); |
| 100 | gpr_log(GPR_INFO, "Get raw oauth2 access token: %s", token); |
| 101 | grpc::string access_token(token + sizeof("Bearer ") - 1); |
| 102 | gpr_free(token); |
| 103 | return access_token; |
| 104 | } |
| 105 | |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 106 | std::shared_ptr<ChannelInterface> CreateChannelForTestCase( |
| 107 | const grpc::string& test_case) { |
| 108 | GPR_ASSERT(FLAGS_server_port); |
| 109 | const int host_port_buf_size = 1024; |
| 110 | char host_port[host_port_buf_size]; |
| 111 | snprintf(host_port, host_port_buf_size, "%s:%d", FLAGS_server_host.c_str(), |
| 112 | FLAGS_server_port); |
| 113 | |
| 114 | if (test_case == "service_account_creds") { |
yang-g | be5f059 | 2015-07-13 11:11:50 -0700 | [diff] [blame] | 115 | std::shared_ptr<Credentials> creds = CreateServiceAccountCredentials(); |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 116 | return CreateTestChannel(host_port, FLAGS_server_host_override, |
| 117 | FLAGS_enable_ssl, FLAGS_use_prod_roots, creds); |
| 118 | } else if (test_case == "compute_engine_creds") { |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 119 | std::shared_ptr<Credentials> creds; |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 120 | GPR_ASSERT(FLAGS_enable_ssl); |
| 121 | creds = ComputeEngineCredentials(); |
| 122 | return CreateTestChannel(host_port, FLAGS_server_host_override, |
| 123 | FLAGS_enable_ssl, FLAGS_use_prod_roots, creds); |
| 124 | } else if (test_case == "jwt_token_creds") { |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 125 | std::shared_ptr<Credentials> creds; |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 126 | GPR_ASSERT(FLAGS_enable_ssl); |
| 127 | grpc::string json_key = GetServiceAccountJsonKey(); |
Nicolas "Pixel" Noble | b7c2035 | 2015-04-11 01:27:32 +0200 | [diff] [blame] | 128 | std::chrono::seconds token_lifetime = std::chrono::hours(1); |
Julien Boeuf | fe4c3f4 | 2015-07-22 16:20:13 -0700 | [diff] [blame] | 129 | creds = |
| 130 | ServiceAccountJWTAccessCredentials(json_key, token_lifetime.count()); |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 131 | return CreateTestChannel(host_port, FLAGS_server_host_override, |
| 132 | FLAGS_enable_ssl, FLAGS_use_prod_roots, creds); |
yang-g | be5f059 | 2015-07-13 11:11:50 -0700 | [diff] [blame] | 133 | } else if (test_case == "oauth2_auth_token") { |
| 134 | grpc::string raw_token = GetOauth2AccessToken(); |
| 135 | std::shared_ptr<Credentials> creds = AccessTokenCredentials(raw_token); |
| 136 | return CreateTestChannel(host_port, FLAGS_server_host_override, |
| 137 | FLAGS_enable_ssl, FLAGS_use_prod_roots, creds); |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 138 | } else { |
| 139 | return CreateTestChannel(host_port, FLAGS_server_host_override, |
| 140 | FLAGS_enable_ssl, FLAGS_use_prod_roots); |
| 141 | } |
| 142 | } |
| 143 | |
David Garcia Quintas | 80f3995 | 2015-07-21 16:07:36 -0700 | [diff] [blame] | 144 | InteropClientContextInspector::InteropClientContextInspector( |
| 145 | const ::grpc::ClientContext& context) |
| 146 | : context_(context) {} |
| 147 | |
| 148 | grpc_compression_algorithm |
| 149 | InteropClientContextInspector::GetCallCompressionAlgorithm() const { |
| 150 | return grpc_call_get_compression_algorithm(context_.call_); |
| 151 | } |
| 152 | |
David Garcia Quintas | c899319 | 2015-07-22 09:10:39 -0700 | [diff] [blame] | 153 | gpr_uint32 InteropClientContextInspector::GetMessageFlags() const { |
| 154 | return grpc_call_get_message_flags(context_.call_); |
| 155 | } |
David Garcia Quintas | 80f3995 | 2015-07-21 16:07:36 -0700 | [diff] [blame] | 156 | |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 157 | } // namespace testing |
| 158 | } // namespace grpc |