blob: 61b46d25aa041a08f32dd901ec8b82b1ee02f786 [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
yang-g9e2f90c2015-08-21 15:35:03 -070036#include <unistd.h>
37
Yang Gaoa4002072015-04-09 23:25:21 -070038#include <fstream>
39#include <memory>
40#include <sstream>
41
Yang Gaoa4002072015-04-09 23:25:21 -070042#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>
yang-g8c2be9f2015-08-19 16:28:09 -070046#include <grpc++/channel.h>
Yang Gaoa4002072015-04-09 23:25:21 -070047#include <grpc++/create_channel.h>
Julien Boeuf5be92a32015-08-28 16:28:18 -070048#include <grpc++/security/credentials.h>
Yang Gaoa4002072015-04-09 23:25:21 -070049
David Garcia Quintas80f39952015-07-21 16:07:36 -070050#include "src/cpp/client/secure_credentials.h"
yang-g9e2f90c2015-08-21 15:35:03 -070051#include "test/core/security/oauth2_utils.h"
52#include "test/cpp/util/create_test_channel.h"
David Garcia Quintas80f39952015-07-21 16:07:36 -070053
yang-g035cf092015-09-18 12:11:05 -070054DECLARE_bool(use_tls);
yang-g92981d52015-10-06 14:45:01 -070055DECLARE_bool(use_test_ca);
Yang Gaoa4002072015-04-09 23:25:21 -070056DECLARE_int32(server_port);
57DECLARE_string(server_host);
58DECLARE_string(server_host_override);
59DECLARE_string(test_case);
60DECLARE_string(default_service_account);
61DECLARE_string(service_account_key_file);
62DECLARE_string(oauth_scope);
63
Yang Gaoa4002072015-04-09 23:25:21 -070064namespace grpc {
65namespace testing {
66
67grpc::string GetServiceAccountJsonKey() {
68 static grpc::string json_key;
69 if (json_key.empty()) {
70 std::ifstream json_key_file(FLAGS_service_account_key_file);
71 std::stringstream key_stream;
72 key_stream << json_key_file.rdbuf();
73 json_key = key_stream.str();
74 }
75 return json_key;
76}
77
yang-gbe5f0592015-07-13 11:11:50 -070078grpc::string GetOauth2AccessToken() {
Julien Boeuf510a9202015-08-25 21:51:07 -070079 std::shared_ptr<Credentials> creds = GoogleComputeEngineCredentials();
yang-gbe5f0592015-07-13 11:11:50 -070080 SecureCredentials* secure_creds =
81 dynamic_cast<SecureCredentials*>(creds.get());
82 GPR_ASSERT(secure_creds != nullptr);
83 grpc_credentials* c_creds = secure_creds->GetRawCreds();
84 char* token = grpc_test_fetch_oauth2_token_with_credentials(c_creds);
85 GPR_ASSERT(token != nullptr);
86 gpr_log(GPR_INFO, "Get raw oauth2 access token: %s", token);
87 grpc::string access_token(token + sizeof("Bearer ") - 1);
88 gpr_free(token);
89 return access_token;
90}
91
yang-g8c2be9f2015-08-19 16:28:09 -070092std::shared_ptr<Channel> CreateChannelForTestCase(
Yang Gaoa4002072015-04-09 23:25:21 -070093 const grpc::string& test_case) {
94 GPR_ASSERT(FLAGS_server_port);
95 const int host_port_buf_size = 1024;
96 char host_port[host_port_buf_size];
97 snprintf(host_port, host_port_buf_size, "%s:%d", FLAGS_server_host.c_str(),
98 FLAGS_server_port);
99
Julien Boeuf510a9202015-08-25 21:51:07 -0700100 if (test_case == "compute_engine_creds") {
Yang Gaoa8938922015-05-14 11:51:07 -0700101 std::shared_ptr<Credentials> creds;
yang-g035cf092015-09-18 12:11:05 -0700102 GPR_ASSERT(FLAGS_use_tls);
Julien Boeuf510a9202015-08-25 21:51:07 -0700103 creds = GoogleComputeEngineCredentials();
Yang Gaoa4002072015-04-09 23:25:21 -0700104 return CreateTestChannel(host_port, FLAGS_server_host_override,
yang-g92981d52015-10-06 14:45:01 -0700105 FLAGS_use_tls, !FLAGS_use_test_ca, creds);
Yang Gaoa4002072015-04-09 23:25:21 -0700106 } else if (test_case == "jwt_token_creds") {
Yang Gaoa8938922015-05-14 11:51:07 -0700107 std::shared_ptr<Credentials> creds;
yang-g035cf092015-09-18 12:11:05 -0700108 GPR_ASSERT(FLAGS_use_tls);
Yang Gaoa4002072015-04-09 23:25:21 -0700109 grpc::string json_key = GetServiceAccountJsonKey();
Nicolas "Pixel" Nobleb7c20352015-04-11 01:27:32 +0200110 std::chrono::seconds token_lifetime = std::chrono::hours(1);
Julien Boeuffe4c3f42015-07-22 16:20:13 -0700111 creds =
112 ServiceAccountJWTAccessCredentials(json_key, token_lifetime.count());
Yang Gaoa4002072015-04-09 23:25:21 -0700113 return CreateTestChannel(host_port, FLAGS_server_host_override,
yang-g92981d52015-10-06 14:45:01 -0700114 FLAGS_use_tls, !FLAGS_use_test_ca, creds);
yang-gbe5f0592015-07-13 11:11:50 -0700115 } else if (test_case == "oauth2_auth_token") {
116 grpc::string raw_token = GetOauth2AccessToken();
117 std::shared_ptr<Credentials> creds = AccessTokenCredentials(raw_token);
118 return CreateTestChannel(host_port, FLAGS_server_host_override,
yang-g92981d52015-10-06 14:45:01 -0700119 FLAGS_use_tls, !FLAGS_use_test_ca, creds);
Yang Gaoa4002072015-04-09 23:25:21 -0700120 } else {
121 return CreateTestChannel(host_port, FLAGS_server_host_override,
yang-g92981d52015-10-06 14:45:01 -0700122 FLAGS_use_tls, !FLAGS_use_test_ca);
Yang Gaoa4002072015-04-09 23:25:21 -0700123 }
124}
125
Yang Gaoa4002072015-04-09 23:25:21 -0700126} // namespace testing
127} // namespace grpc