1
2 /*
3 *
4 * Copyright 2016 gRPC authors.
5 *
6 * 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
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * 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.
17 *
18 */
19
20 #include "test/cpp/util/test_credentials_provider.h"
21
22 #include <mutex>
23 #include <unordered_map>
24
25 #include <grpc/support/log.h>
26 #include <grpc/support/sync.h>
27
28 #include "test/core/end2end/data/ssl_test_data.h"
29
30 namespace grpc {
31 namespace testing {
32 namespace {
33
34 class DefaultCredentialsProvider : public CredentialsProvider {
35 public:
~DefaultCredentialsProvider()36 ~DefaultCredentialsProvider() override {}
37
AddSecureType(const grpc::string & type,std::unique_ptr<CredentialTypeProvider> type_provider)38 void AddSecureType(
39 const grpc::string& type,
40 std::unique_ptr<CredentialTypeProvider> type_provider) override {
41 // This clobbers any existing entry for type, except the defaults, which
42 // can't be clobbered.
43 std::unique_lock<std::mutex> lock(mu_);
44 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 }
53 }
54
GetChannelCredentials(const grpc::string & type,ChannelArguments * args)55 std::shared_ptr<ChannelCredentials> GetChannelCredentials(
56 const grpc::string& type, ChannelArguments* args) override {
57 if (type == grpc::testing::kInsecureCredentialsType) {
58 return InsecureChannelCredentials();
59 } else if (type == grpc::testing::kAltsCredentialsType) {
60 grpc::experimental::AltsCredentialsOptions alts_opts;
61 return grpc::experimental::AltsCredentials(alts_opts);
62 } else if (type == grpc::testing::kTlsCredentialsType) {
63 SslCredentialsOptions ssl_opts = {test_root_cert, "", ""};
64 args->SetSslTargetNameOverride("foo.test.google.fr");
65 return SslCredentials(ssl_opts);
66 } else if (type == grpc::testing::kGoogleDefaultCredentialsType) {
67 return grpc::GoogleDefaultCredentials();
68 } else {
69 std::unique_lock<std::mutex> lock(mu_);
70 auto it(std::find(added_secure_type_names_.begin(),
71 added_secure_type_names_.end(), type));
72 if (it == added_secure_type_names_.end()) {
73 gpr_log(GPR_ERROR, "Unsupported credentials type %s.", type.c_str());
74 return nullptr;
75 }
76 return added_secure_type_providers_[it - added_secure_type_names_.begin()]
77 ->GetChannelCredentials(args);
78 }
79 }
80
GetServerCredentials(const grpc::string & type)81 std::shared_ptr<ServerCredentials> GetServerCredentials(
82 const grpc::string& type) override {
83 if (type == grpc::testing::kInsecureCredentialsType) {
84 return InsecureServerCredentials();
85 } else if (type == grpc::testing::kAltsCredentialsType) {
86 grpc::experimental::AltsServerCredentialsOptions alts_opts;
87 return grpc::experimental::AltsServerCredentials(alts_opts);
88 } else if (type == grpc::testing::kTlsCredentialsType) {
89 SslServerCredentialsOptions::PemKeyCertPair pkcp = {test_server1_key,
90 test_server1_cert};
91 SslServerCredentialsOptions ssl_opts;
92 ssl_opts.pem_root_certs = "";
93 ssl_opts.pem_key_cert_pairs.push_back(pkcp);
94 return SslServerCredentials(ssl_opts);
95 } else {
96 std::unique_lock<std::mutex> lock(mu_);
97 auto it(std::find(added_secure_type_names_.begin(),
98 added_secure_type_names_.end(), type));
99 if (it == added_secure_type_names_.end()) {
100 gpr_log(GPR_ERROR, "Unsupported credentials type %s.", type.c_str());
101 return nullptr;
102 }
103 return added_secure_type_providers_[it - added_secure_type_names_.begin()]
104 ->GetServerCredentials();
105 }
106 }
GetSecureCredentialsTypeList()107 std::vector<grpc::string> GetSecureCredentialsTypeList() override {
108 std::vector<grpc::string> types;
109 types.push_back(grpc::testing::kTlsCredentialsType);
110 std::unique_lock<std::mutex> lock(mu_);
111 for (auto it = added_secure_type_names_.begin();
112 it != added_secure_type_names_.end(); it++) {
113 types.push_back(*it);
114 }
115 return types;
116 }
117
118 private:
119 std::mutex mu_;
120 std::vector<grpc::string> added_secure_type_names_;
121 std::vector<std::unique_ptr<CredentialTypeProvider>>
122 added_secure_type_providers_;
123 };
124
125 CredentialsProvider* g_provider = nullptr;
126
127 } // namespace
128
GetCredentialsProvider()129 CredentialsProvider* GetCredentialsProvider() {
130 if (g_provider == nullptr) {
131 g_provider = new DefaultCredentialsProvider;
132 }
133 return g_provider;
134 }
135
SetCredentialsProvider(CredentialsProvider * provider)136 void SetCredentialsProvider(CredentialsProvider* provider) {
137 // For now, forbids overriding provider.
138 GPR_ASSERT(g_provider == nullptr);
139 g_provider = provider;
140 }
141
142 } // namespace testing
143 } // namespace grpc
144