• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cstdio>
23 #include <fstream>
24 #include <iostream>
25 
26 #include <mutex>
27 #include <unordered_map>
28 
29 #include <gflags/gflags.h>
30 #include <grpc/support/log.h>
31 #include <grpc/support/sync.h>
32 #include <grpcpp/security/server_credentials.h>
33 
34 #include "test/core/end2end/data/ssl_test_data.h"
35 
36 DEFINE_string(tls_cert_file, "", "The TLS cert file used when --use_tls=true");
37 DEFINE_string(tls_key_file, "", "The TLS key file used when --use_tls=true");
38 
39 namespace grpc {
40 namespace testing {
41 namespace {
42 
ReadFile(const std::string & src_path)43 std::string ReadFile(const std::string& src_path) {
44   std::ifstream src;
45   src.open(src_path, std::ifstream::in | std::ifstream::binary);
46 
47   std::string contents;
48   src.seekg(0, std::ios::end);
49   contents.reserve(src.tellg());
50   src.seekg(0, std::ios::beg);
51   contents.assign((std::istreambuf_iterator<char>(src)),
52                   (std::istreambuf_iterator<char>()));
53   return contents;
54 }
55 
56 class DefaultCredentialsProvider : public CredentialsProvider {
57  public:
DefaultCredentialsProvider()58   DefaultCredentialsProvider() {
59     if (!FLAGS_tls_key_file.empty()) {
60       custom_server_key_ = ReadFile(FLAGS_tls_key_file);
61     }
62     if (!FLAGS_tls_cert_file.empty()) {
63       custom_server_cert_ = ReadFile(FLAGS_tls_cert_file);
64     }
65   }
~DefaultCredentialsProvider()66   ~DefaultCredentialsProvider() override {}
67 
AddSecureType(const std::string & type,std::unique_ptr<CredentialTypeProvider> type_provider)68   void AddSecureType(
69       const std::string& type,
70       std::unique_ptr<CredentialTypeProvider> type_provider) override {
71     // This clobbers any existing entry for type, except the defaults, which
72     // can't be clobbered.
73     std::unique_lock<std::mutex> lock(mu_);
74     auto it = std::find(added_secure_type_names_.begin(),
75                         added_secure_type_names_.end(), type);
76     if (it == added_secure_type_names_.end()) {
77       added_secure_type_names_.push_back(type);
78       added_secure_type_providers_.push_back(std::move(type_provider));
79     } else {
80       added_secure_type_providers_[it - added_secure_type_names_.begin()] =
81           std::move(type_provider);
82     }
83   }
84 
GetChannelCredentials(const std::string & type,ChannelArguments * args)85   std::shared_ptr<ChannelCredentials> GetChannelCredentials(
86       const std::string& type, ChannelArguments* args) override {
87     if (type == grpc::testing::kInsecureCredentialsType) {
88       return InsecureChannelCredentials();
89     } else if (type == grpc::testing::kAltsCredentialsType) {
90       grpc::experimental::AltsCredentialsOptions alts_opts;
91       return grpc::experimental::AltsCredentials(alts_opts);
92     } else if (type == grpc::testing::kTlsCredentialsType) {
93       SslCredentialsOptions ssl_opts = {test_root_cert, "", ""};
94       args->SetSslTargetNameOverride("foo.test.google.fr");
95       return grpc::SslCredentials(ssl_opts);
96     } else if (type == grpc::testing::kGoogleDefaultCredentialsType) {
97       return grpc::GoogleDefaultCredentials();
98     } else {
99       std::unique_lock<std::mutex> lock(mu_);
100       auto it(std::find(added_secure_type_names_.begin(),
101                         added_secure_type_names_.end(), type));
102       if (it == added_secure_type_names_.end()) {
103         gpr_log(GPR_ERROR, "Unsupported credentials type %s.", type.c_str());
104         return nullptr;
105       }
106       return added_secure_type_providers_[it - added_secure_type_names_.begin()]
107           ->GetChannelCredentials(args);
108     }
109   }
110 
GetServerCredentials(const std::string & type)111   std::shared_ptr<ServerCredentials> GetServerCredentials(
112       const std::string& type) override {
113     if (type == grpc::testing::kInsecureCredentialsType) {
114       return InsecureServerCredentials();
115     } else if (type == grpc::testing::kAltsCredentialsType) {
116       grpc::experimental::AltsServerCredentialsOptions alts_opts;
117       return grpc::experimental::AltsServerCredentials(alts_opts);
118     } else if (type == grpc::testing::kTlsCredentialsType) {
119       SslServerCredentialsOptions ssl_opts;
120       ssl_opts.pem_root_certs = "";
121       if (!custom_server_key_.empty() && !custom_server_cert_.empty()) {
122         SslServerCredentialsOptions::PemKeyCertPair pkcp = {
123             custom_server_key_, custom_server_cert_};
124         ssl_opts.pem_key_cert_pairs.push_back(pkcp);
125       } else {
126         SslServerCredentialsOptions::PemKeyCertPair pkcp = {test_server1_key,
127                                                             test_server1_cert};
128         ssl_opts.pem_key_cert_pairs.push_back(pkcp);
129       }
130       return SslServerCredentials(ssl_opts);
131     } else {
132       std::unique_lock<std::mutex> lock(mu_);
133       auto it(std::find(added_secure_type_names_.begin(),
134                         added_secure_type_names_.end(), type));
135       if (it == added_secure_type_names_.end()) {
136         gpr_log(GPR_ERROR, "Unsupported credentials type %s.", type.c_str());
137         return nullptr;
138       }
139       return added_secure_type_providers_[it - added_secure_type_names_.begin()]
140           ->GetServerCredentials();
141     }
142   }
GetSecureCredentialsTypeList()143   std::vector<std::string> GetSecureCredentialsTypeList() override {
144     std::vector<std::string> types;
145     types.push_back(grpc::testing::kTlsCredentialsType);
146     std::unique_lock<std::mutex> lock(mu_);
147     for (auto it = added_secure_type_names_.begin();
148          it != added_secure_type_names_.end(); it++) {
149       types.push_back(*it);
150     }
151     return types;
152   }
153 
154  private:
155   std::mutex mu_;
156   std::vector<std::string> added_secure_type_names_;
157   std::vector<std::unique_ptr<CredentialTypeProvider>>
158       added_secure_type_providers_;
159   std::string custom_server_key_;
160   std::string custom_server_cert_;
161 };
162 
163 CredentialsProvider* g_provider = nullptr;
164 
165 }  // namespace
166 
GetCredentialsProvider()167 CredentialsProvider* GetCredentialsProvider() {
168   if (g_provider == nullptr) {
169     g_provider = new DefaultCredentialsProvider;
170   }
171   return g_provider;
172 }
173 
SetCredentialsProvider(CredentialsProvider * provider)174 void SetCredentialsProvider(CredentialsProvider* provider) {
175   // For now, forbids overriding provider.
176   GPR_ASSERT(g_provider == nullptr);
177   g_provider = provider;
178 }
179 
180 }  // namespace testing
181 }  // namespace grpc
182