blob: be652a4add4712296fa999ef7c48f98015922814 [file] [log] [blame]
Yang Gaoa4002072015-04-09 23:25:21 -07001/*
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-gbe5f0592015-07-13 11:11:50 -070043#include <grpc/support/alloc.h>
Yang Gaoa4002072015-04-09 23:25:21 -070044#include <grpc/support/log.h>
45#include <gflags/gflags.h>
46#include <grpc++/channel_arguments.h>
yang-g8c2be9f2015-08-19 16:28:09 -070047#include <grpc++/channel.h>
Yang Gaoa4002072015-04-09 23:25:21 -070048#include <grpc++/create_channel.h>
49#include <grpc++/credentials.h>
50#include <grpc++/stream.h>
David Garcia Quintas80f39952015-07-21 16:07:36 -070051
yang-gbe5f0592015-07-13 11:11:50 -070052#include "test/core/security/oauth2_utils.h"
Yang Gaoa4002072015-04-09 23:25:21 -070053#include "test/cpp/util/create_test_channel.h"
54
David Garcia Quintas80f39952015-07-21 16:07:36 -070055#include "src/cpp/client/secure_credentials.h"
56
Yang Gaoa4002072015-04-09 23:25:21 -070057DECLARE_bool(enable_ssl);
58DECLARE_bool(use_prod_roots);
59DECLARE_int32(server_port);
60DECLARE_string(server_host);
61DECLARE_string(server_host_override);
62DECLARE_string(test_case);
63DECLARE_string(default_service_account);
64DECLARE_string(service_account_key_file);
65DECLARE_string(oauth_scope);
66
Yang Gaoa4002072015-04-09 23:25:21 -070067namespace grpc {
68namespace testing {
69
yang-gbe5f0592015-07-13 11:11:50 -070070namespace {
71std::shared_ptr<Credentials> CreateServiceAccountCredentials() {
72 GPR_ASSERT(FLAGS_enable_ssl);
73 grpc::string json_key = GetServiceAccountJsonKey();
74 std::chrono::seconds token_lifetime = std::chrono::hours(1);
75 return ServiceAccountCredentials(json_key, FLAGS_oauth_scope,
76 token_lifetime.count());
77}
78} // namespace
79
Yang Gaoa4002072015-04-09 23:25:21 -070080grpc::string GetServiceAccountJsonKey() {
81 static grpc::string json_key;
82 if (json_key.empty()) {
83 std::ifstream json_key_file(FLAGS_service_account_key_file);
84 std::stringstream key_stream;
85 key_stream << json_key_file.rdbuf();
86 json_key = key_stream.str();
87 }
88 return json_key;
89}
90
yang-gbe5f0592015-07-13 11:11:50 -070091grpc::string GetOauth2AccessToken() {
92 std::shared_ptr<Credentials> creds = CreateServiceAccountCredentials();
93 SecureCredentials* secure_creds =
94 dynamic_cast<SecureCredentials*>(creds.get());
95 GPR_ASSERT(secure_creds != nullptr);
96 grpc_credentials* c_creds = secure_creds->GetRawCreds();
97 char* token = grpc_test_fetch_oauth2_token_with_credentials(c_creds);
98 GPR_ASSERT(token != nullptr);
99 gpr_log(GPR_INFO, "Get raw oauth2 access token: %s", token);
100 grpc::string access_token(token + sizeof("Bearer ") - 1);
101 gpr_free(token);
102 return access_token;
103}
104
yang-g8c2be9f2015-08-19 16:28:09 -0700105std::shared_ptr<Channel> CreateChannelForTestCase(
Yang Gaoa4002072015-04-09 23:25:21 -0700106 const grpc::string& test_case) {
107 GPR_ASSERT(FLAGS_server_port);
108 const int host_port_buf_size = 1024;
109 char host_port[host_port_buf_size];
110 snprintf(host_port, host_port_buf_size, "%s:%d", FLAGS_server_host.c_str(),
111 FLAGS_server_port);
112
113 if (test_case == "service_account_creds") {
yang-gbe5f0592015-07-13 11:11:50 -0700114 std::shared_ptr<Credentials> creds = CreateServiceAccountCredentials();
Yang Gaoa4002072015-04-09 23:25:21 -0700115 return CreateTestChannel(host_port, FLAGS_server_host_override,
116 FLAGS_enable_ssl, FLAGS_use_prod_roots, creds);
117 } else if (test_case == "compute_engine_creds") {
Yang Gaoa8938922015-05-14 11:51:07 -0700118 std::shared_ptr<Credentials> creds;
Yang Gaoa4002072015-04-09 23:25:21 -0700119 GPR_ASSERT(FLAGS_enable_ssl);
120 creds = ComputeEngineCredentials();
121 return CreateTestChannel(host_port, FLAGS_server_host_override,
122 FLAGS_enable_ssl, FLAGS_use_prod_roots, creds);
123 } else if (test_case == "jwt_token_creds") {
Yang Gaoa8938922015-05-14 11:51:07 -0700124 std::shared_ptr<Credentials> creds;
Yang Gaoa4002072015-04-09 23:25:21 -0700125 GPR_ASSERT(FLAGS_enable_ssl);
126 grpc::string json_key = GetServiceAccountJsonKey();
Nicolas "Pixel" Nobleb7c20352015-04-11 01:27:32 +0200127 std::chrono::seconds token_lifetime = std::chrono::hours(1);
Julien Boeuffe4c3f42015-07-22 16:20:13 -0700128 creds =
129 ServiceAccountJWTAccessCredentials(json_key, token_lifetime.count());
Yang Gaoa4002072015-04-09 23:25:21 -0700130 return CreateTestChannel(host_port, FLAGS_server_host_override,
131 FLAGS_enable_ssl, FLAGS_use_prod_roots, creds);
yang-gbe5f0592015-07-13 11:11:50 -0700132 } else if (test_case == "oauth2_auth_token") {
133 grpc::string raw_token = GetOauth2AccessToken();
134 std::shared_ptr<Credentials> creds = AccessTokenCredentials(raw_token);
135 return CreateTestChannel(host_port, FLAGS_server_host_override,
136 FLAGS_enable_ssl, FLAGS_use_prod_roots, creds);
Yang Gaoa4002072015-04-09 23:25:21 -0700137 } else {
138 return CreateTestChannel(host_port, FLAGS_server_host_override,
139 FLAGS_enable_ssl, FLAGS_use_prod_roots);
140 }
141}
142
Yang Gaoa4002072015-04-09 23:25:21 -0700143} // namespace testing
144} // namespace grpc