blob: 65d320576733e346e8749daf5cc17dd5cfecc098 [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/support/sync.h>
40#include <grpc++/impl/sync.h>
41
yang-g7d2a3e12016-02-18 15:41:56 -080042#include "test/core/end2end/data/ssl_test_data.h"
43
Dan Bornf30941c2016-02-24 14:17:26 -080044namespace {
45
46using grpc::ChannelArguments;
47using grpc::ChannelCredentials;
48using grpc::InsecureChannelCredentials;
49using grpc::InsecureServerCredentials;
50using grpc::ServerCredentials;
51using grpc::SslCredentialsOptions;
52using grpc::SslServerCredentialsOptions;
Dan Born1b5a2642016-02-24 18:52:39 -080053using grpc::testing::CredentialTypeProvider;
54
55// Provide test credentials. Thread-safe.
56class CredentialsProvider {
57 public:
58 virtual ~CredentialsProvider() {}
59
60 virtual void AddSecureType(
61 const grpc::string& type,
62 std::unique_ptr<CredentialTypeProvider> type_provider) = 0;
63 virtual std::shared_ptr<ChannelCredentials> GetChannelCredentials(
64 const grpc::string& type, ChannelArguments* args) = 0;
65 virtual std::shared_ptr<ServerCredentials> GetServerCredentials(
66 const grpc::string& type) = 0;
67 virtual std::vector<grpc::string> GetSecureCredentialsTypeList() = 0;
68};
Dan Bornf30941c2016-02-24 14:17:26 -080069
70class DefaultCredentialsProvider : public CredentialsProvider {
71 public:
72 ~DefaultCredentialsProvider() override {}
73
Dan Born1b5a2642016-02-24 18:52:39 -080074 void AddSecureType(
75 const grpc::string& type,
76 std::unique_ptr<CredentialTypeProvider> type_provider) override {
77 // This clobbers any existing entry for type, except the defaults, which
78 // can't be clobbered.
79 grpc::unique_lock<grpc::mutex> lock(mu_);
80 added_secure_types_[type] = std::move(type_provider);
81 }
82
Dan Bornf30941c2016-02-24 14:17:26 -080083 std::shared_ptr<ChannelCredentials> GetChannelCredentials(
84 const grpc::string& type, ChannelArguments* args) override {
85 if (type == grpc::testing::kInsecureCredentialsType) {
86 return InsecureChannelCredentials();
87 } else if (type == grpc::testing::kTlsCredentialsType) {
88 SslCredentialsOptions ssl_opts = {test_root_cert, "", ""};
89 args->SetSslTargetNameOverride("foo.test.google.fr");
90 return SslCredentials(ssl_opts);
91 } else {
Dan Born1b5a2642016-02-24 18:52:39 -080092 grpc::unique_lock<grpc::mutex> lock(mu_);
93 auto it(added_secure_types_.find(type));
94 if (it == added_secure_types_.end()) {
95 gpr_log(GPR_ERROR, "Unsupported credentials type %s.", type.c_str());
96 return nullptr;
97 }
98 return it->second->GetChannelCredentials(args);
Dan Bornf30941c2016-02-24 14:17:26 -080099 }
Dan Bornf30941c2016-02-24 14:17:26 -0800100 }
101
102 std::shared_ptr<ServerCredentials> GetServerCredentials(
103 const grpc::string& type) override {
104 if (type == grpc::testing::kInsecureCredentialsType) {
105 return InsecureServerCredentials();
106 } else if (type == grpc::testing::kTlsCredentialsType) {
107 SslServerCredentialsOptions::PemKeyCertPair pkcp = {test_server1_key,
108 test_server1_cert};
109 SslServerCredentialsOptions ssl_opts;
110 ssl_opts.pem_root_certs = "";
111 ssl_opts.pem_key_cert_pairs.push_back(pkcp);
112 return SslServerCredentials(ssl_opts);
113 } else {
Dan Born1b5a2642016-02-24 18:52:39 -0800114 grpc::unique_lock<grpc::mutex> lock(mu_);
115 auto it(added_secure_types_.find(type));
116 if (it == added_secure_types_.end()) {
117 gpr_log(GPR_ERROR, "Unsupported credentials type %s.", type.c_str());
118 return nullptr;
119 }
120 return it->second->GetServerCredentials();
Dan Bornf30941c2016-02-24 14:17:26 -0800121 }
Dan Bornf30941c2016-02-24 14:17:26 -0800122 }
123 std::vector<grpc::string> GetSecureCredentialsTypeList() override {
124 std::vector<grpc::string> types;
125 types.push_back(grpc::testing::kTlsCredentialsType);
Dan Born1b5a2642016-02-24 18:52:39 -0800126 grpc::unique_lock<grpc::mutex> lock(mu_);
127 for (const auto& type_pair : added_secure_types_) {
128 types.push_back(type_pair.first);
129 }
Dan Bornf30941c2016-02-24 14:17:26 -0800130 return types;
131 }
Dan Born1b5a2642016-02-24 18:52:39 -0800132
133 private:
134 grpc::mutex mu_;
135 std::unordered_map<grpc::string, std::unique_ptr<CredentialTypeProvider> >
136 added_secure_types_;
Dan Bornf30941c2016-02-24 14:17:26 -0800137};
138
Dan Born1b5a2642016-02-24 18:52:39 -0800139gpr_once g_once_init_provider = GPR_ONCE_INIT;
Dan Bornf30941c2016-02-24 14:17:26 -0800140CredentialsProvider* g_provider = nullptr;
141
Dan Born1b5a2642016-02-24 18:52:39 -0800142void CreateDefaultProvider() {
143 g_provider = new DefaultCredentialsProvider;
Dan Bornf30941c2016-02-24 14:17:26 -0800144}
145
146CredentialsProvider* GetProvider() {
Dan Born1b5a2642016-02-24 18:52:39 -0800147 gpr_once_init(&g_once_init_provider, &CreateDefaultProvider);
Dan Bornf30941c2016-02-24 14:17:26 -0800148 return g_provider;
149}
150
151} // namespace
152
yang-g7d2a3e12016-02-18 15:41:56 -0800153namespace grpc {
154namespace testing {
155
Dan Born1b5a2642016-02-24 18:52:39 -0800156void AddSecureType(const grpc::string& type,
157 std::unique_ptr<CredentialTypeProvider> type_provider) {
158 GetProvider()->AddSecureType(type, std::move(type_provider));
Dan Bornf30941c2016-02-24 14:17:26 -0800159}
yang-g12a0a2c2016-02-19 00:22:20 -0800160
yang-g7d2a3e12016-02-18 15:41:56 -0800161std::shared_ptr<ChannelCredentials> GetChannelCredentials(
yang-g17197dd2016-02-19 00:04:22 -0800162 const grpc::string& type, ChannelArguments* args) {
Dan Bornf30941c2016-02-24 14:17:26 -0800163 return GetProvider()->GetChannelCredentials(type, args);
yang-g7d2a3e12016-02-18 15:41:56 -0800164}
165
166std::shared_ptr<ServerCredentials> GetServerCredentials(
yang-g17197dd2016-02-19 00:04:22 -0800167 const grpc::string& type) {
Dan Bornf30941c2016-02-24 14:17:26 -0800168 return GetProvider()->GetServerCredentials(type);
yang-g7d2a3e12016-02-18 15:41:56 -0800169}
170
yang-g4c8aed32016-02-19 00:19:39 -0800171std::vector<grpc::string> GetSecureCredentialsTypeList() {
Dan Bornf30941c2016-02-24 14:17:26 -0800172 return GetProvider()->GetSecureCredentialsTypeList();
yang-g4c8aed32016-02-19 00:19:39 -0800173}
174
yang-g7d2a3e12016-02-18 15:41:56 -0800175} // namespace testing
176} // namespace grpc