yang-g | 7d2a3e1 | 2016-02-18 15:41:56 -0800 | [diff] [blame] | 1 | |
| 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 Pai | 320ed13 | 2016-11-01 17:16:55 -0700 | [diff] [blame] | 37 | #include <mutex> |
Dan Born | 1b5a264 | 2016-02-24 18:52:39 -0800 | [diff] [blame] | 38 | #include <unordered_map> |
| 39 | |
David Garcia Quintas | c79b065 | 2016-07-27 21:11:58 -0700 | [diff] [blame] | 40 | #include <grpc/support/log.h> |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 41 | #include <grpc/support/sync.h> |
Dan Born | 53a94ef | 2016-02-24 15:44:43 -0800 | [diff] [blame] | 42 | |
yang-g | 7d2a3e1 | 2016-02-18 15:41:56 -0800 | [diff] [blame] | 43 | #include "test/core/end2end/data/ssl_test_data.h" |
| 44 | |
Vijay Pai | a63271c | 2016-06-15 12:56:38 -0700 | [diff] [blame] | 45 | namespace grpc { |
yang-g | cc59102 | 2017-01-11 11:10:43 -0800 | [diff] [blame^] | 46 | namespace testing { |
Dan Born | f30941c | 2016-02-24 14:17:26 -0800 | [diff] [blame] | 47 | namespace { |
| 48 | |
Dan Born | f30941c | 2016-02-24 14:17:26 -0800 | [diff] [blame] | 49 | class DefaultCredentialsProvider : public CredentialsProvider { |
| 50 | public: |
Vijay Pai | c0b2acb | 2016-11-01 16:31:56 -0700 | [diff] [blame] | 51 | ~DefaultCredentialsProvider() override {} |
Dan Born | f30941c | 2016-02-24 14:17:26 -0800 | [diff] [blame] | 52 | |
Vijay Pai | 713c7b8 | 2016-11-01 16:33:18 -0700 | [diff] [blame] | 53 | void AddSecureType( |
| 54 | const grpc::string& type, |
| 55 | std::unique_ptr<CredentialTypeProvider> type_provider) override { |
Dan Born | 1b5a264 | 2016-02-24 18:52:39 -0800 | [diff] [blame] | 56 | // This clobbers any existing entry for type, except the defaults, which |
| 57 | // can't be clobbered. |
Vijay Pai | 320ed13 | 2016-11-01 17:16:55 -0700 | [diff] [blame] | 58 | std::unique_lock<std::mutex> lock(mu_); |
Vijay Pai | efa8430 | 2016-06-15 10:23:34 -0700 | [diff] [blame] | 59 | 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 Born | 1b5a264 | 2016-02-24 18:52:39 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Dan Born | f30941c | 2016-02-24 14:17:26 -0800 | [diff] [blame] | 70 | std::shared_ptr<ChannelCredentials> GetChannelCredentials( |
Vijay Pai | c0b2acb | 2016-11-01 16:31:56 -0700 | [diff] [blame] | 71 | const grpc::string& type, ChannelArguments* args) override { |
Dan Born | f30941c | 2016-02-24 14:17:26 -0800 | [diff] [blame] | 72 | 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 Pai | 320ed13 | 2016-11-01 17:16:55 -0700 | [diff] [blame] | 79 | std::unique_lock<std::mutex> lock(mu_); |
Vijay Pai | efa8430 | 2016-06-15 10:23:34 -0700 | [diff] [blame] | 80 | 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 Born | 1b5a264 | 2016-02-24 18:52:39 -0800 | [diff] [blame] | 83 | gpr_log(GPR_ERROR, "Unsupported credentials type %s.", type.c_str()); |
Vijay Pai | 12bf380 | 2016-06-15 11:24:10 -0700 | [diff] [blame] | 84 | return nullptr; |
Dan Born | 1b5a264 | 2016-02-24 18:52:39 -0800 | [diff] [blame] | 85 | } |
Vijay Pai | efa8430 | 2016-06-15 10:23:34 -0700 | [diff] [blame] | 86 | return added_secure_type_providers_[it - added_secure_type_names_.begin()] |
| 87 | ->GetChannelCredentials(args); |
Dan Born | f30941c | 2016-02-24 14:17:26 -0800 | [diff] [blame] | 88 | } |
Dan Born | f30941c | 2016-02-24 14:17:26 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | std::shared_ptr<ServerCredentials> GetServerCredentials( |
Vijay Pai | c0b2acb | 2016-11-01 16:31:56 -0700 | [diff] [blame] | 92 | const grpc::string& type) override { |
Dan Born | f30941c | 2016-02-24 14:17:26 -0800 | [diff] [blame] | 93 | 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 Pai | 320ed13 | 2016-11-01 17:16:55 -0700 | [diff] [blame] | 103 | std::unique_lock<std::mutex> lock(mu_); |
Vijay Pai | efa8430 | 2016-06-15 10:23:34 -0700 | [diff] [blame] | 104 | 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 Born | 1b5a264 | 2016-02-24 18:52:39 -0800 | [diff] [blame] | 107 | gpr_log(GPR_ERROR, "Unsupported credentials type %s.", type.c_str()); |
Vijay Pai | 12bf380 | 2016-06-15 11:24:10 -0700 | [diff] [blame] | 108 | return nullptr; |
Dan Born | 1b5a264 | 2016-02-24 18:52:39 -0800 | [diff] [blame] | 109 | } |
Vijay Pai | efa8430 | 2016-06-15 10:23:34 -0700 | [diff] [blame] | 110 | return added_secure_type_providers_[it - added_secure_type_names_.begin()] |
| 111 | ->GetServerCredentials(); |
Dan Born | f30941c | 2016-02-24 14:17:26 -0800 | [diff] [blame] | 112 | } |
Dan Born | f30941c | 2016-02-24 14:17:26 -0800 | [diff] [blame] | 113 | } |
Vijay Pai | c0b2acb | 2016-11-01 16:31:56 -0700 | [diff] [blame] | 114 | std::vector<grpc::string> GetSecureCredentialsTypeList() override { |
Dan Born | f30941c | 2016-02-24 14:17:26 -0800 | [diff] [blame] | 115 | std::vector<grpc::string> types; |
| 116 | types.push_back(grpc::testing::kTlsCredentialsType); |
Vijay Pai | 320ed13 | 2016-11-01 17:16:55 -0700 | [diff] [blame] | 117 | std::unique_lock<std::mutex> lock(mu_); |
Vijay Pai | efa8430 | 2016-06-15 10:23:34 -0700 | [diff] [blame] | 118 | for (auto it = added_secure_type_names_.begin(); |
| 119 | it != added_secure_type_names_.end(); it++) { |
| 120 | types.push_back(*it); |
Dan Born | 1b5a264 | 2016-02-24 18:52:39 -0800 | [diff] [blame] | 121 | } |
Dan Born | f30941c | 2016-02-24 14:17:26 -0800 | [diff] [blame] | 122 | return types; |
| 123 | } |
Dan Born | 1b5a264 | 2016-02-24 18:52:39 -0800 | [diff] [blame] | 124 | |
| 125 | private: |
Vijay Pai | 320ed13 | 2016-11-01 17:16:55 -0700 | [diff] [blame] | 126 | std::mutex mu_; |
Vijay Pai | efa8430 | 2016-06-15 10:23:34 -0700 | [diff] [blame] | 127 | std::vector<grpc::string> added_secure_type_names_; |
| 128 | std::vector<std::unique_ptr<CredentialTypeProvider>> |
| 129 | added_secure_type_providers_; |
Dan Born | f30941c | 2016-02-24 14:17:26 -0800 | [diff] [blame] | 130 | }; |
| 131 | |
Vijay Pai | 12bf380 | 2016-06-15 11:24:10 -0700 | [diff] [blame] | 132 | CredentialsProvider* g_provider = nullptr; |
Dan Born | f30941c | 2016-02-24 14:17:26 -0800 | [diff] [blame] | 133 | |
Craig Tiller | 3b45b8d | 2016-11-29 12:31:14 -0800 | [diff] [blame] | 134 | } // namespace |
| 135 | |
yang-g | cc59102 | 2017-01-11 11:10:43 -0800 | [diff] [blame^] | 136 | CredentialsProvider* GetCredentialsProvider() { |
| 137 | if (g_provider == nullptr) { |
| 138 | g_provider = new DefaultCredentialsProvider; |
| 139 | } |
| 140 | return g_provider; |
Craig Tiller | 3b45b8d | 2016-11-29 12:31:14 -0800 | [diff] [blame] | 141 | } |
| 142 | |
yang-g | cc59102 | 2017-01-11 11:10:43 -0800 | [diff] [blame^] | 143 | void SetCredentialsProvider(CredentialsProvider* provider) { |
| 144 | // For now, forbids overriding provider. |
| 145 | GPR_ASSERT(g_provider == nullptr); |
| 146 | g_provider = provider; |
yang-g | 4c8aed3 | 2016-02-19 00:19:39 -0800 | [diff] [blame] | 147 | } |
| 148 | |
yang-g | 7d2a3e1 | 2016-02-18 15:41:56 -0800 | [diff] [blame] | 149 | } // namespace testing |
| 150 | } // namespace grpc |