blob: 6e68f59e6ac13f54615de2ca240adab6d51117f7 [file] [log] [blame]
yang-g7d2a3e12016-02-18 15:41:56 -08001
2/*
3 *
4 * Copyright 2016, Google Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following disclaimer
15 * in the documentation and/or other materials provided with the
16 * distribution.
17 * * Neither the name of Google Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived from
19 * this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 */
34
35#include "test/cpp/util/test_credentials_provider.h"
36
Dan Born1b5a2642016-02-24 18:52:39 -080037#include <unordered_map>
38
Dan Born53a94ef2016-02-24 15:44:43 -080039#include <grpc++/impl/sync.h>
Craig Tillerf40df232016-03-25 13:38:14 -070040#include <grpc/support/sync.h>
Dan Born53a94ef2016-02-24 15:44:43 -080041
yang-g7d2a3e12016-02-18 15:41:56 -080042#include "test/core/end2end/data/ssl_test_data.h"
43
Vijay Paia63271c2016-06-15 12:56:38 -070044namespace grpc {
Dan Bornf30941c2016-02-24 14:17:26 -080045namespace {
46
Dan Born1b5a2642016-02-24 18:52:39 -080047using grpc::testing::CredentialTypeProvider;
48
49// Provide test credentials. Thread-safe.
50class CredentialsProvider {
51 public:
52 virtual ~CredentialsProvider() {}
53
54 virtual void AddSecureType(
55 const grpc::string& type,
56 std::unique_ptr<CredentialTypeProvider> type_provider) = 0;
57 virtual std::shared_ptr<ChannelCredentials> GetChannelCredentials(
58 const grpc::string& type, ChannelArguments* args) = 0;
59 virtual std::shared_ptr<ServerCredentials> GetServerCredentials(
60 const grpc::string& type) = 0;
61 virtual std::vector<grpc::string> GetSecureCredentialsTypeList() = 0;
62};
Dan Bornf30941c2016-02-24 14:17:26 -080063
64class DefaultCredentialsProvider : public CredentialsProvider {
65 public:
Vijay Paiefa84302016-06-15 10:23:34 -070066 ~DefaultCredentialsProvider() GRPC_OVERRIDE {}
Dan Bornf30941c2016-02-24 14:17:26 -080067
Vijay Paiefa84302016-06-15 10:23:34 -070068 void AddSecureType(const grpc::string& type,
69 std::unique_ptr<CredentialTypeProvider> type_provider)
70 GRPC_OVERRIDE {
Dan Born1b5a2642016-02-24 18:52:39 -080071 // This clobbers any existing entry for type, except the defaults, which
72 // can't be clobbered.
73 grpc::unique_lock<grpc::mutex> lock(mu_);
Vijay Paiefa84302016-06-15 10:23:34 -070074 auto it = std::find(added_secure_type_names_.begin(),
75 added_secure_type_names_.end(), type);
76 if (it == added_secure_type_names_.end()) {
77 added_secure_type_names_.push_back(type);
78 added_secure_type_providers_.push_back(std::move(type_provider));
79 } else {
80 added_secure_type_providers_[it - added_secure_type_names_.begin()] =
81 std::move(type_provider);
82 }
Dan Born1b5a2642016-02-24 18:52:39 -080083 }
84
Dan Bornf30941c2016-02-24 14:17:26 -080085 std::shared_ptr<ChannelCredentials> GetChannelCredentials(
Vijay Paiefa84302016-06-15 10:23:34 -070086 const grpc::string& type, ChannelArguments* args) GRPC_OVERRIDE {
Dan Bornf30941c2016-02-24 14:17:26 -080087 if (type == grpc::testing::kInsecureCredentialsType) {
88 return InsecureChannelCredentials();
89 } else if (type == grpc::testing::kTlsCredentialsType) {
90 SslCredentialsOptions ssl_opts = {test_root_cert, "", ""};
91 args->SetSslTargetNameOverride("foo.test.google.fr");
92 return SslCredentials(ssl_opts);
93 } else {
Dan Born1b5a2642016-02-24 18:52:39 -080094 grpc::unique_lock<grpc::mutex> lock(mu_);
Vijay Paiefa84302016-06-15 10:23:34 -070095 auto it(std::find(added_secure_type_names_.begin(),
96 added_secure_type_names_.end(), type));
97 if (it == added_secure_type_names_.end()) {
Dan Born1b5a2642016-02-24 18:52:39 -080098 gpr_log(GPR_ERROR, "Unsupported credentials type %s.", type.c_str());
Vijay Pai12bf3802016-06-15 11:24:10 -070099 return nullptr;
Dan Born1b5a2642016-02-24 18:52:39 -0800100 }
Vijay Paiefa84302016-06-15 10:23:34 -0700101 return added_secure_type_providers_[it - added_secure_type_names_.begin()]
102 ->GetChannelCredentials(args);
Dan Bornf30941c2016-02-24 14:17:26 -0800103 }
Dan Bornf30941c2016-02-24 14:17:26 -0800104 }
105
106 std::shared_ptr<ServerCredentials> GetServerCredentials(
Vijay Paiefa84302016-06-15 10:23:34 -0700107 const grpc::string& type) GRPC_OVERRIDE {
Dan Bornf30941c2016-02-24 14:17:26 -0800108 if (type == grpc::testing::kInsecureCredentialsType) {
109 return InsecureServerCredentials();
110 } else if (type == grpc::testing::kTlsCredentialsType) {
111 SslServerCredentialsOptions::PemKeyCertPair pkcp = {test_server1_key,
112 test_server1_cert};
113 SslServerCredentialsOptions ssl_opts;
114 ssl_opts.pem_root_certs = "";
115 ssl_opts.pem_key_cert_pairs.push_back(pkcp);
116 return SslServerCredentials(ssl_opts);
117 } else {
Dan Born1b5a2642016-02-24 18:52:39 -0800118 grpc::unique_lock<grpc::mutex> lock(mu_);
Vijay Paiefa84302016-06-15 10:23:34 -0700119 auto it(std::find(added_secure_type_names_.begin(),
120 added_secure_type_names_.end(), type));
121 if (it == added_secure_type_names_.end()) {
Dan Born1b5a2642016-02-24 18:52:39 -0800122 gpr_log(GPR_ERROR, "Unsupported credentials type %s.", type.c_str());
Vijay Pai12bf3802016-06-15 11:24:10 -0700123 return nullptr;
Dan Born1b5a2642016-02-24 18:52:39 -0800124 }
Vijay Paiefa84302016-06-15 10:23:34 -0700125 return added_secure_type_providers_[it - added_secure_type_names_.begin()]
126 ->GetServerCredentials();
Dan Bornf30941c2016-02-24 14:17:26 -0800127 }
Dan Bornf30941c2016-02-24 14:17:26 -0800128 }
Vijay Paiefa84302016-06-15 10:23:34 -0700129 std::vector<grpc::string> GetSecureCredentialsTypeList() GRPC_OVERRIDE {
Dan Bornf30941c2016-02-24 14:17:26 -0800130 std::vector<grpc::string> types;
131 types.push_back(grpc::testing::kTlsCredentialsType);
Dan Born1b5a2642016-02-24 18:52:39 -0800132 grpc::unique_lock<grpc::mutex> lock(mu_);
Vijay Paiefa84302016-06-15 10:23:34 -0700133 for (auto it = added_secure_type_names_.begin();
134 it != added_secure_type_names_.end(); it++) {
135 types.push_back(*it);
Dan Born1b5a2642016-02-24 18:52:39 -0800136 }
Dan Bornf30941c2016-02-24 14:17:26 -0800137 return types;
138 }
Dan Born1b5a2642016-02-24 18:52:39 -0800139
140 private:
141 grpc::mutex mu_;
Vijay Paiefa84302016-06-15 10:23:34 -0700142 std::vector<grpc::string> added_secure_type_names_;
143 std::vector<std::unique_ptr<CredentialTypeProvider>>
144 added_secure_type_providers_;
Dan Bornf30941c2016-02-24 14:17:26 -0800145};
146
Dan Born1b5a2642016-02-24 18:52:39 -0800147gpr_once g_once_init_provider = GPR_ONCE_INIT;
Vijay Pai12bf3802016-06-15 11:24:10 -0700148CredentialsProvider* g_provider = nullptr;
Dan Bornf30941c2016-02-24 14:17:26 -0800149
Craig Tiller7fe08a22016-02-29 20:17:48 -0800150void CreateDefaultProvider() { g_provider = new DefaultCredentialsProvider; }
Dan Bornf30941c2016-02-24 14:17:26 -0800151
152CredentialsProvider* GetProvider() {
Dan Born1b5a2642016-02-24 18:52:39 -0800153 gpr_once_init(&g_once_init_provider, &CreateDefaultProvider);
Dan Bornf30941c2016-02-24 14:17:26 -0800154 return g_provider;
155}
156
157} // namespace
158
yang-g7d2a3e12016-02-18 15:41:56 -0800159namespace testing {
160
Dan Born1b5a2642016-02-24 18:52:39 -0800161void AddSecureType(const grpc::string& type,
162 std::unique_ptr<CredentialTypeProvider> type_provider) {
163 GetProvider()->AddSecureType(type, std::move(type_provider));
Dan Bornf30941c2016-02-24 14:17:26 -0800164}
yang-g12a0a2c2016-02-19 00:22:20 -0800165
yang-g7d2a3e12016-02-18 15:41:56 -0800166std::shared_ptr<ChannelCredentials> GetChannelCredentials(
yang-g17197dd2016-02-19 00:04:22 -0800167 const grpc::string& type, ChannelArguments* args) {
Dan Bornf30941c2016-02-24 14:17:26 -0800168 return GetProvider()->GetChannelCredentials(type, args);
yang-g7d2a3e12016-02-18 15:41:56 -0800169}
170
171std::shared_ptr<ServerCredentials> GetServerCredentials(
yang-g17197dd2016-02-19 00:04:22 -0800172 const grpc::string& type) {
Dan Bornf30941c2016-02-24 14:17:26 -0800173 return GetProvider()->GetServerCredentials(type);
yang-g7d2a3e12016-02-18 15:41:56 -0800174}
175
yang-g4c8aed32016-02-19 00:19:39 -0800176std::vector<grpc::string> GetSecureCredentialsTypeList() {
Dan Bornf30941c2016-02-24 14:17:26 -0800177 return GetProvider()->GetSecureCredentialsTypeList();
yang-g4c8aed32016-02-19 00:19:39 -0800178}
179
yang-g7d2a3e12016-02-18 15:41:56 -0800180} // namespace testing
181} // namespace grpc