blob: 76561007c486391697693eee656f6487f18a45bc [file] [log] [blame]
yang-g7d2a3e12016-02-18 15:41:56 -08001
2/*
3 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02004 * Copyright 2016 gRPC authors.
yang-g7d2a3e12016-02-18 15:41:56 -08005 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02006 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
yang-g7d2a3e12016-02-18 15:41:56 -08009 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020010 * http://www.apache.org/licenses/LICENSE-2.0
yang-g7d2a3e12016-02-18 15:41:56 -080011 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020012 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
yang-g7d2a3e12016-02-18 15:41:56 -080017 *
18 */
19
20#include "test/cpp/util/test_credentials_provider.h"
21
Vijay Pai320ed132016-11-01 17:16:55 -070022#include <mutex>
Dan Born1b5a2642016-02-24 18:52:39 -080023#include <unordered_map>
24
David Garcia Quintasc79b0652016-07-27 21:11:58 -070025#include <grpc/support/log.h>
Craig Tillerf40df232016-03-25 13:38:14 -070026#include <grpc/support/sync.h>
Dan Born53a94ef2016-02-24 15:44:43 -080027
yang-g7d2a3e12016-02-18 15:41:56 -080028#include "test/core/end2end/data/ssl_test_data.h"
29
Vijay Paia63271c2016-06-15 12:56:38 -070030namespace grpc {
yang-gcc591022017-01-11 11:10:43 -080031namespace testing {
Dan Bornf30941c2016-02-24 14:17:26 -080032namespace {
33
Dan Bornf30941c2016-02-24 14:17:26 -080034class DefaultCredentialsProvider : public CredentialsProvider {
35 public:
Vijay Paic0b2acb2016-11-01 16:31:56 -070036 ~DefaultCredentialsProvider() override {}
Dan Bornf30941c2016-02-24 14:17:26 -080037
Vijay Pai713c7b82016-11-01 16:33:18 -070038 void AddSecureType(
39 const grpc::string& type,
40 std::unique_ptr<CredentialTypeProvider> type_provider) override {
Dan Born1b5a2642016-02-24 18:52:39 -080041 // This clobbers any existing entry for type, except the defaults, which
42 // can't be clobbered.
Vijay Pai320ed132016-11-01 17:16:55 -070043 std::unique_lock<std::mutex> lock(mu_);
Vijay Paiefa84302016-06-15 10:23:34 -070044 auto it = std::find(added_secure_type_names_.begin(),
45 added_secure_type_names_.end(), type);
46 if (it == added_secure_type_names_.end()) {
47 added_secure_type_names_.push_back(type);
48 added_secure_type_providers_.push_back(std::move(type_provider));
49 } else {
50 added_secure_type_providers_[it - added_secure_type_names_.begin()] =
51 std::move(type_provider);
52 }
Dan Born1b5a2642016-02-24 18:52:39 -080053 }
54
Dan Bornf30941c2016-02-24 14:17:26 -080055 std::shared_ptr<ChannelCredentials> GetChannelCredentials(
Vijay Paic0b2acb2016-11-01 16:31:56 -070056 const grpc::string& type, ChannelArguments* args) override {
Dan Bornf30941c2016-02-24 14:17:26 -080057 if (type == grpc::testing::kInsecureCredentialsType) {
58 return InsecureChannelCredentials();
59 } else if (type == grpc::testing::kTlsCredentialsType) {
60 SslCredentialsOptions ssl_opts = {test_root_cert, "", ""};
61 args->SetSslTargetNameOverride("foo.test.google.fr");
62 return SslCredentials(ssl_opts);
63 } else {
Vijay Pai320ed132016-11-01 17:16:55 -070064 std::unique_lock<std::mutex> lock(mu_);
Vijay Paiefa84302016-06-15 10:23:34 -070065 auto it(std::find(added_secure_type_names_.begin(),
66 added_secure_type_names_.end(), type));
67 if (it == added_secure_type_names_.end()) {
Dan Born1b5a2642016-02-24 18:52:39 -080068 gpr_log(GPR_ERROR, "Unsupported credentials type %s.", type.c_str());
Vijay Pai12bf3802016-06-15 11:24:10 -070069 return nullptr;
Dan Born1b5a2642016-02-24 18:52:39 -080070 }
Vijay Paiefa84302016-06-15 10:23:34 -070071 return added_secure_type_providers_[it - added_secure_type_names_.begin()]
72 ->GetChannelCredentials(args);
Dan Bornf30941c2016-02-24 14:17:26 -080073 }
Dan Bornf30941c2016-02-24 14:17:26 -080074 }
75
76 std::shared_ptr<ServerCredentials> GetServerCredentials(
Vijay Paic0b2acb2016-11-01 16:31:56 -070077 const grpc::string& type) override {
Dan Bornf30941c2016-02-24 14:17:26 -080078 if (type == grpc::testing::kInsecureCredentialsType) {
79 return InsecureServerCredentials();
80 } else if (type == grpc::testing::kTlsCredentialsType) {
81 SslServerCredentialsOptions::PemKeyCertPair pkcp = {test_server1_key,
82 test_server1_cert};
83 SslServerCredentialsOptions ssl_opts;
84 ssl_opts.pem_root_certs = "";
85 ssl_opts.pem_key_cert_pairs.push_back(pkcp);
86 return SslServerCredentials(ssl_opts);
87 } else {
Vijay Pai320ed132016-11-01 17:16:55 -070088 std::unique_lock<std::mutex> lock(mu_);
Vijay Paiefa84302016-06-15 10:23:34 -070089 auto it(std::find(added_secure_type_names_.begin(),
90 added_secure_type_names_.end(), type));
91 if (it == added_secure_type_names_.end()) {
Dan Born1b5a2642016-02-24 18:52:39 -080092 gpr_log(GPR_ERROR, "Unsupported credentials type %s.", type.c_str());
Vijay Pai12bf3802016-06-15 11:24:10 -070093 return nullptr;
Dan Born1b5a2642016-02-24 18:52:39 -080094 }
Vijay Paiefa84302016-06-15 10:23:34 -070095 return added_secure_type_providers_[it - added_secure_type_names_.begin()]
96 ->GetServerCredentials();
Dan Bornf30941c2016-02-24 14:17:26 -080097 }
Dan Bornf30941c2016-02-24 14:17:26 -080098 }
Vijay Paic0b2acb2016-11-01 16:31:56 -070099 std::vector<grpc::string> GetSecureCredentialsTypeList() override {
Dan Bornf30941c2016-02-24 14:17:26 -0800100 std::vector<grpc::string> types;
101 types.push_back(grpc::testing::kTlsCredentialsType);
Vijay Pai320ed132016-11-01 17:16:55 -0700102 std::unique_lock<std::mutex> lock(mu_);
Vijay Paiefa84302016-06-15 10:23:34 -0700103 for (auto it = added_secure_type_names_.begin();
104 it != added_secure_type_names_.end(); it++) {
105 types.push_back(*it);
Dan Born1b5a2642016-02-24 18:52:39 -0800106 }
Dan Bornf30941c2016-02-24 14:17:26 -0800107 return types;
108 }
Dan Born1b5a2642016-02-24 18:52:39 -0800109
110 private:
Vijay Pai320ed132016-11-01 17:16:55 -0700111 std::mutex mu_;
Vijay Paiefa84302016-06-15 10:23:34 -0700112 std::vector<grpc::string> added_secure_type_names_;
113 std::vector<std::unique_ptr<CredentialTypeProvider>>
114 added_secure_type_providers_;
Dan Bornf30941c2016-02-24 14:17:26 -0800115};
116
Vijay Pai12bf3802016-06-15 11:24:10 -0700117CredentialsProvider* g_provider = nullptr;
Dan Bornf30941c2016-02-24 14:17:26 -0800118
Craig Tiller3b45b8d2016-11-29 12:31:14 -0800119} // namespace
120
yang-gcc591022017-01-11 11:10:43 -0800121CredentialsProvider* GetCredentialsProvider() {
122 if (g_provider == nullptr) {
123 g_provider = new DefaultCredentialsProvider;
124 }
125 return g_provider;
Craig Tiller3b45b8d2016-11-29 12:31:14 -0800126}
127
yang-gcc591022017-01-11 11:10:43 -0800128void SetCredentialsProvider(CredentialsProvider* provider) {
129 // For now, forbids overriding provider.
130 GPR_ASSERT(g_provider == nullptr);
131 g_provider = provider;
yang-g4c8aed32016-02-19 00:19:39 -0800132}
133
yang-g7d2a3e12016-02-18 15:41:56 -0800134} // namespace testing
135} // namespace grpc