blob: 909b02a7019fe426733f140fbb4b06c5c3ca9576 [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 {
yang-gcc591022017-01-11 11:10:43 -080046namespace testing {
Dan Bornf30941c2016-02-24 14:17:26 -080047namespace {
48
Dan Bornf30941c2016-02-24 14:17:26 -080049class DefaultCredentialsProvider : public CredentialsProvider {
50 public:
Vijay Paic0b2acb2016-11-01 16:31:56 -070051 ~DefaultCredentialsProvider() override {}
Dan Bornf30941c2016-02-24 14:17:26 -080052
Vijay Pai713c7b82016-11-01 16:33:18 -070053 void AddSecureType(
54 const grpc::string& type,
55 std::unique_ptr<CredentialTypeProvider> type_provider) override {
Dan Born1b5a2642016-02-24 18:52:39 -080056 // This clobbers any existing entry for type, except the defaults, which
57 // can't be clobbered.
Vijay Pai320ed132016-11-01 17:16:55 -070058 std::unique_lock<std::mutex> lock(mu_);
Vijay Paiefa84302016-06-15 10:23:34 -070059 auto it = std::find(added_secure_type_names_.begin(),
60 added_secure_type_names_.end(), type);
61 if (it == added_secure_type_names_.end()) {
62 added_secure_type_names_.push_back(type);
63 added_secure_type_providers_.push_back(std::move(type_provider));
64 } else {
65 added_secure_type_providers_[it - added_secure_type_names_.begin()] =
66 std::move(type_provider);
67 }
Dan Born1b5a2642016-02-24 18:52:39 -080068 }
69
Dan Bornf30941c2016-02-24 14:17:26 -080070 std::shared_ptr<ChannelCredentials> GetChannelCredentials(
Vijay Paic0b2acb2016-11-01 16:31:56 -070071 const grpc::string& type, ChannelArguments* args) override {
Dan Bornf30941c2016-02-24 14:17:26 -080072 if (type == grpc::testing::kInsecureCredentialsType) {
73 return InsecureChannelCredentials();
74 } else if (type == grpc::testing::kTlsCredentialsType) {
75 SslCredentialsOptions ssl_opts = {test_root_cert, "", ""};
76 args->SetSslTargetNameOverride("foo.test.google.fr");
77 return SslCredentials(ssl_opts);
78 } else {
Vijay Pai320ed132016-11-01 17:16:55 -070079 std::unique_lock<std::mutex> lock(mu_);
Vijay Paiefa84302016-06-15 10:23:34 -070080 auto it(std::find(added_secure_type_names_.begin(),
81 added_secure_type_names_.end(), type));
82 if (it == added_secure_type_names_.end()) {
Dan Born1b5a2642016-02-24 18:52:39 -080083 gpr_log(GPR_ERROR, "Unsupported credentials type %s.", type.c_str());
Vijay Pai12bf3802016-06-15 11:24:10 -070084 return nullptr;
Dan Born1b5a2642016-02-24 18:52:39 -080085 }
Vijay Paiefa84302016-06-15 10:23:34 -070086 return added_secure_type_providers_[it - added_secure_type_names_.begin()]
87 ->GetChannelCredentials(args);
Dan Bornf30941c2016-02-24 14:17:26 -080088 }
Dan Bornf30941c2016-02-24 14:17:26 -080089 }
90
91 std::shared_ptr<ServerCredentials> GetServerCredentials(
Vijay Paic0b2acb2016-11-01 16:31:56 -070092 const grpc::string& type) override {
Dan Bornf30941c2016-02-24 14:17:26 -080093 if (type == grpc::testing::kInsecureCredentialsType) {
94 return InsecureServerCredentials();
95 } else if (type == grpc::testing::kTlsCredentialsType) {
96 SslServerCredentialsOptions::PemKeyCertPair pkcp = {test_server1_key,
97 test_server1_cert};
98 SslServerCredentialsOptions ssl_opts;
99 ssl_opts.pem_root_certs = "";
100 ssl_opts.pem_key_cert_pairs.push_back(pkcp);
101 return SslServerCredentials(ssl_opts);
102 } else {
Vijay Pai320ed132016-11-01 17:16:55 -0700103 std::unique_lock<std::mutex> lock(mu_);
Vijay Paiefa84302016-06-15 10:23:34 -0700104 auto it(std::find(added_secure_type_names_.begin(),
105 added_secure_type_names_.end(), type));
106 if (it == added_secure_type_names_.end()) {
Dan Born1b5a2642016-02-24 18:52:39 -0800107 gpr_log(GPR_ERROR, "Unsupported credentials type %s.", type.c_str());
Vijay Pai12bf3802016-06-15 11:24:10 -0700108 return nullptr;
Dan Born1b5a2642016-02-24 18:52:39 -0800109 }
Vijay Paiefa84302016-06-15 10:23:34 -0700110 return added_secure_type_providers_[it - added_secure_type_names_.begin()]
111 ->GetServerCredentials();
Dan Bornf30941c2016-02-24 14:17:26 -0800112 }
Dan Bornf30941c2016-02-24 14:17:26 -0800113 }
Vijay Paic0b2acb2016-11-01 16:31:56 -0700114 std::vector<grpc::string> GetSecureCredentialsTypeList() override {
Dan Bornf30941c2016-02-24 14:17:26 -0800115 std::vector<grpc::string> types;
116 types.push_back(grpc::testing::kTlsCredentialsType);
Vijay Pai320ed132016-11-01 17:16:55 -0700117 std::unique_lock<std::mutex> lock(mu_);
Vijay Paiefa84302016-06-15 10:23:34 -0700118 for (auto it = added_secure_type_names_.begin();
119 it != added_secure_type_names_.end(); it++) {
120 types.push_back(*it);
Dan Born1b5a2642016-02-24 18:52:39 -0800121 }
Dan Bornf30941c2016-02-24 14:17:26 -0800122 return types;
123 }
Dan Born1b5a2642016-02-24 18:52:39 -0800124
125 private:
Vijay Pai320ed132016-11-01 17:16:55 -0700126 std::mutex mu_;
Vijay Paiefa84302016-06-15 10:23:34 -0700127 std::vector<grpc::string> added_secure_type_names_;
128 std::vector<std::unique_ptr<CredentialTypeProvider>>
129 added_secure_type_providers_;
Dan Bornf30941c2016-02-24 14:17:26 -0800130};
131
Vijay Pai12bf3802016-06-15 11:24:10 -0700132CredentialsProvider* g_provider = nullptr;
Dan Bornf30941c2016-02-24 14:17:26 -0800133
Craig Tiller3b45b8d2016-11-29 12:31:14 -0800134} // namespace
135
yang-gcc591022017-01-11 11:10:43 -0800136CredentialsProvider* GetCredentialsProvider() {
137 if (g_provider == nullptr) {
138 g_provider = new DefaultCredentialsProvider;
139 }
140 return g_provider;
Craig Tiller3b45b8d2016-11-29 12:31:14 -0800141}
142
yang-gcc591022017-01-11 11:10:43 -0800143void SetCredentialsProvider(CredentialsProvider* provider) {
144 // For now, forbids overriding provider.
145 GPR_ASSERT(g_provider == nullptr);
146 g_provider = provider;
yang-g4c8aed32016-02-19 00:19:39 -0800147}
148
yang-g7d2a3e12016-02-18 15:41:56 -0800149} // namespace testing
150} // namespace grpc