blob: 0456b9666710ceb0e4fb13ce1929d8653ef78cd9 [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
Vijay Pai320ed132016-11-01 17:16:55 -070037#include <mutex>
Dan Born1b5a2642016-02-24 18:52:39 -080038#include <unordered_map>
39
David Garcia Quintasc79b0652016-07-27 21:11:58 -070040#include <grpc/support/log.h>
Craig Tillerf40df232016-03-25 13:38:14 -070041#include <grpc/support/sync.h>
Dan Born53a94ef2016-02-24 15:44:43 -080042
yang-g7d2a3e12016-02-18 15:41:56 -080043#include "test/core/end2end/data/ssl_test_data.h"
44
Vijay Paia63271c2016-06-15 12:56:38 -070045namespace grpc {
Dan Bornf30941c2016-02-24 14:17:26 -080046namespace {
47
Dan Born1b5a2642016-02-24 18:52:39 -080048using grpc::testing::CredentialTypeProvider;
49
50// Provide test credentials. Thread-safe.
51class CredentialsProvider {
52 public:
53 virtual ~CredentialsProvider() {}
54
55 virtual void AddSecureType(
56 const grpc::string& type,
57 std::unique_ptr<CredentialTypeProvider> type_provider) = 0;
58 virtual std::shared_ptr<ChannelCredentials> GetChannelCredentials(
59 const grpc::string& type, ChannelArguments* args) = 0;
60 virtual std::shared_ptr<ServerCredentials> GetServerCredentials(
61 const grpc::string& type) = 0;
62 virtual std::vector<grpc::string> GetSecureCredentialsTypeList() = 0;
63};
Dan Bornf30941c2016-02-24 14:17:26 -080064
65class DefaultCredentialsProvider : public CredentialsProvider {
66 public:
Vijay Paic0b2acb2016-11-01 16:31:56 -070067 ~DefaultCredentialsProvider() override {}
Dan Bornf30941c2016-02-24 14:17:26 -080068
Vijay Pai713c7b82016-11-01 16:33:18 -070069 void AddSecureType(
70 const grpc::string& type,
71 std::unique_ptr<CredentialTypeProvider> type_provider) override {
Dan Born1b5a2642016-02-24 18:52:39 -080072 // This clobbers any existing entry for type, except the defaults, which
73 // can't be clobbered.
Vijay Pai320ed132016-11-01 17:16:55 -070074 std::unique_lock<std::mutex> lock(mu_);
Vijay Paiefa84302016-06-15 10:23:34 -070075 auto it = std::find(added_secure_type_names_.begin(),
76 added_secure_type_names_.end(), type);
77 if (it == added_secure_type_names_.end()) {
78 added_secure_type_names_.push_back(type);
79 added_secure_type_providers_.push_back(std::move(type_provider));
80 } else {
81 added_secure_type_providers_[it - added_secure_type_names_.begin()] =
82 std::move(type_provider);
83 }
Dan Born1b5a2642016-02-24 18:52:39 -080084 }
85
Dan Bornf30941c2016-02-24 14:17:26 -080086 std::shared_ptr<ChannelCredentials> GetChannelCredentials(
Vijay Paic0b2acb2016-11-01 16:31:56 -070087 const grpc::string& type, ChannelArguments* args) override {
Dan Bornf30941c2016-02-24 14:17:26 -080088 if (type == grpc::testing::kInsecureCredentialsType) {
89 return InsecureChannelCredentials();
90 } else if (type == grpc::testing::kTlsCredentialsType) {
91 SslCredentialsOptions ssl_opts = {test_root_cert, "", ""};
92 args->SetSslTargetNameOverride("foo.test.google.fr");
93 return SslCredentials(ssl_opts);
94 } else {
Vijay Pai320ed132016-11-01 17:16:55 -070095 std::unique_lock<std::mutex> lock(mu_);
Vijay Paiefa84302016-06-15 10:23:34 -070096 auto it(std::find(added_secure_type_names_.begin(),
97 added_secure_type_names_.end(), type));
98 if (it == added_secure_type_names_.end()) {
Dan Born1b5a2642016-02-24 18:52:39 -080099 gpr_log(GPR_ERROR, "Unsupported credentials type %s.", type.c_str());
Vijay Pai12bf3802016-06-15 11:24:10 -0700100 return nullptr;
Dan Born1b5a2642016-02-24 18:52:39 -0800101 }
Vijay Paiefa84302016-06-15 10:23:34 -0700102 return added_secure_type_providers_[it - added_secure_type_names_.begin()]
103 ->GetChannelCredentials(args);
Dan Bornf30941c2016-02-24 14:17:26 -0800104 }
Dan Bornf30941c2016-02-24 14:17:26 -0800105 }
106
107 std::shared_ptr<ServerCredentials> GetServerCredentials(
Vijay Paic0b2acb2016-11-01 16:31:56 -0700108 const grpc::string& type) override {
Dan Bornf30941c2016-02-24 14:17:26 -0800109 if (type == grpc::testing::kInsecureCredentialsType) {
110 return InsecureServerCredentials();
111 } else if (type == grpc::testing::kTlsCredentialsType) {
112 SslServerCredentialsOptions::PemKeyCertPair pkcp = {test_server1_key,
113 test_server1_cert};
114 SslServerCredentialsOptions ssl_opts;
115 ssl_opts.pem_root_certs = "";
116 ssl_opts.pem_key_cert_pairs.push_back(pkcp);
117 return SslServerCredentials(ssl_opts);
118 } else {
Vijay Pai320ed132016-11-01 17:16:55 -0700119 std::unique_lock<std::mutex> lock(mu_);
Vijay Paiefa84302016-06-15 10:23:34 -0700120 auto it(std::find(added_secure_type_names_.begin(),
121 added_secure_type_names_.end(), type));
122 if (it == added_secure_type_names_.end()) {
Dan Born1b5a2642016-02-24 18:52:39 -0800123 gpr_log(GPR_ERROR, "Unsupported credentials type %s.", type.c_str());
Vijay Pai12bf3802016-06-15 11:24:10 -0700124 return nullptr;
Dan Born1b5a2642016-02-24 18:52:39 -0800125 }
Vijay Paiefa84302016-06-15 10:23:34 -0700126 return added_secure_type_providers_[it - added_secure_type_names_.begin()]
127 ->GetServerCredentials();
Dan Bornf30941c2016-02-24 14:17:26 -0800128 }
Dan Bornf30941c2016-02-24 14:17:26 -0800129 }
Vijay Paic0b2acb2016-11-01 16:31:56 -0700130 std::vector<grpc::string> GetSecureCredentialsTypeList() override {
Dan Bornf30941c2016-02-24 14:17:26 -0800131 std::vector<grpc::string> types;
132 types.push_back(grpc::testing::kTlsCredentialsType);
Vijay Pai320ed132016-11-01 17:16:55 -0700133 std::unique_lock<std::mutex> lock(mu_);
Vijay Paiefa84302016-06-15 10:23:34 -0700134 for (auto it = added_secure_type_names_.begin();
135 it != added_secure_type_names_.end(); it++) {
136 types.push_back(*it);
Dan Born1b5a2642016-02-24 18:52:39 -0800137 }
Dan Bornf30941c2016-02-24 14:17:26 -0800138 return types;
139 }
Dan Born1b5a2642016-02-24 18:52:39 -0800140
141 private:
Vijay Pai320ed132016-11-01 17:16:55 -0700142 std::mutex mu_;
Vijay Paiefa84302016-06-15 10:23:34 -0700143 std::vector<grpc::string> added_secure_type_names_;
144 std::vector<std::unique_ptr<CredentialTypeProvider>>
145 added_secure_type_providers_;
Dan Bornf30941c2016-02-24 14:17:26 -0800146};
147
Dan Born1b5a2642016-02-24 18:52:39 -0800148gpr_once g_once_init_provider = GPR_ONCE_INIT;
Vijay Pai12bf3802016-06-15 11:24:10 -0700149CredentialsProvider* g_provider = nullptr;
Dan Bornf30941c2016-02-24 14:17:26 -0800150
Craig Tiller7fe08a22016-02-29 20:17:48 -0800151void CreateDefaultProvider() { g_provider = new DefaultCredentialsProvider; }
Dan Bornf30941c2016-02-24 14:17:26 -0800152
153CredentialsProvider* GetProvider() {
Dan Born1b5a2642016-02-24 18:52:39 -0800154 gpr_once_init(&g_once_init_provider, &CreateDefaultProvider);
Dan Bornf30941c2016-02-24 14:17:26 -0800155 return g_provider;
156}
157
158} // namespace
159
yang-g7d2a3e12016-02-18 15:41:56 -0800160namespace testing {
161
Dan Born1b5a2642016-02-24 18:52:39 -0800162void AddSecureType(const grpc::string& type,
163 std::unique_ptr<CredentialTypeProvider> type_provider) {
164 GetProvider()->AddSecureType(type, std::move(type_provider));
Dan Bornf30941c2016-02-24 14:17:26 -0800165}
yang-g12a0a2c2016-02-19 00:22:20 -0800166
yang-g7d2a3e12016-02-18 15:41:56 -0800167std::shared_ptr<ChannelCredentials> GetChannelCredentials(
yang-g17197dd2016-02-19 00:04:22 -0800168 const grpc::string& type, ChannelArguments* args) {
Dan Bornf30941c2016-02-24 14:17:26 -0800169 return GetProvider()->GetChannelCredentials(type, args);
yang-g7d2a3e12016-02-18 15:41:56 -0800170}
171
172std::shared_ptr<ServerCredentials> GetServerCredentials(
yang-g17197dd2016-02-19 00:04:22 -0800173 const grpc::string& type) {
Dan Bornf30941c2016-02-24 14:17:26 -0800174 return GetProvider()->GetServerCredentials(type);
yang-g7d2a3e12016-02-18 15:41:56 -0800175}
176
yang-g4c8aed32016-02-19 00:19:39 -0800177std::vector<grpc::string> GetSecureCredentialsTypeList() {
Dan Bornf30941c2016-02-24 14:17:26 -0800178 return GetProvider()->GetSecureCredentialsTypeList();
yang-g4c8aed32016-02-19 00:19:39 -0800179}
180
yang-g7d2a3e12016-02-18 15:41:56 -0800181} // namespace testing
182} // namespace grpc