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 | |
Dan Born | 1b5a264 | 2016-02-24 18:52:39 -0800 | [diff] [blame] | 37 | #include <unordered_map> |
| 38 | |
Dan Born | 53a94ef | 2016-02-24 15:44:43 -0800 | [diff] [blame] | 39 | #include <grpc/support/sync.h> |
| 40 | #include <grpc++/impl/sync.h> |
| 41 | |
yang-g | 7d2a3e1 | 2016-02-18 15:41:56 -0800 | [diff] [blame] | 42 | #include "test/core/end2end/data/ssl_test_data.h" |
| 43 | |
Dan Born | f30941c | 2016-02-24 14:17:26 -0800 | [diff] [blame] | 44 | namespace { |
| 45 | |
| 46 | using grpc::ChannelArguments; |
| 47 | using grpc::ChannelCredentials; |
| 48 | using grpc::InsecureChannelCredentials; |
| 49 | using grpc::InsecureServerCredentials; |
| 50 | using grpc::ServerCredentials; |
| 51 | using grpc::SslCredentialsOptions; |
| 52 | using grpc::SslServerCredentialsOptions; |
Dan Born | 1b5a264 | 2016-02-24 18:52:39 -0800 | [diff] [blame] | 53 | using grpc::testing::CredentialTypeProvider; |
| 54 | |
| 55 | // Provide test credentials. Thread-safe. |
| 56 | class 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 Born | f30941c | 2016-02-24 14:17:26 -0800 | [diff] [blame] | 69 | |
| 70 | class DefaultCredentialsProvider : public CredentialsProvider { |
| 71 | public: |
| 72 | ~DefaultCredentialsProvider() override {} |
| 73 | |
Dan Born | 1b5a264 | 2016-02-24 18:52:39 -0800 | [diff] [blame] | 74 | 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 Born | f30941c | 2016-02-24 14:17:26 -0800 | [diff] [blame] | 83 | 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 Born | 1b5a264 | 2016-02-24 18:52:39 -0800 | [diff] [blame] | 92 | 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 Born | f30941c | 2016-02-24 14:17:26 -0800 | [diff] [blame] | 99 | } |
Dan Born | f30941c | 2016-02-24 14:17:26 -0800 | [diff] [blame] | 100 | } |
| 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 Born | 1b5a264 | 2016-02-24 18:52:39 -0800 | [diff] [blame] | 114 | 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 Born | f30941c | 2016-02-24 14:17:26 -0800 | [diff] [blame] | 121 | } |
Dan Born | f30941c | 2016-02-24 14:17:26 -0800 | [diff] [blame] | 122 | } |
| 123 | std::vector<grpc::string> GetSecureCredentialsTypeList() override { |
| 124 | std::vector<grpc::string> types; |
| 125 | types.push_back(grpc::testing::kTlsCredentialsType); |
Dan Born | 1b5a264 | 2016-02-24 18:52:39 -0800 | [diff] [blame] | 126 | 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 Born | f30941c | 2016-02-24 14:17:26 -0800 | [diff] [blame] | 130 | return types; |
| 131 | } |
Dan Born | 1b5a264 | 2016-02-24 18:52:39 -0800 | [diff] [blame] | 132 | |
| 133 | private: |
| 134 | grpc::mutex mu_; |
| 135 | std::unordered_map<grpc::string, std::unique_ptr<CredentialTypeProvider> > |
| 136 | added_secure_types_; |
Dan Born | f30941c | 2016-02-24 14:17:26 -0800 | [diff] [blame] | 137 | }; |
| 138 | |
Dan Born | 1b5a264 | 2016-02-24 18:52:39 -0800 | [diff] [blame] | 139 | gpr_once g_once_init_provider = GPR_ONCE_INIT; |
Dan Born | f30941c | 2016-02-24 14:17:26 -0800 | [diff] [blame] | 140 | CredentialsProvider* g_provider = nullptr; |
| 141 | |
Craig Tiller | 7fe08a2 | 2016-02-29 20:17:48 -0800 | [diff] [blame] | 142 | void CreateDefaultProvider() { g_provider = new DefaultCredentialsProvider; } |
Dan Born | f30941c | 2016-02-24 14:17:26 -0800 | [diff] [blame] | 143 | |
| 144 | CredentialsProvider* GetProvider() { |
Dan Born | 1b5a264 | 2016-02-24 18:52:39 -0800 | [diff] [blame] | 145 | gpr_once_init(&g_once_init_provider, &CreateDefaultProvider); |
Dan Born | f30941c | 2016-02-24 14:17:26 -0800 | [diff] [blame] | 146 | return g_provider; |
| 147 | } |
| 148 | |
| 149 | } // namespace |
| 150 | |
yang-g | 7d2a3e1 | 2016-02-18 15:41:56 -0800 | [diff] [blame] | 151 | namespace grpc { |
| 152 | namespace testing { |
| 153 | |
Dan Born | 1b5a264 | 2016-02-24 18:52:39 -0800 | [diff] [blame] | 154 | void AddSecureType(const grpc::string& type, |
| 155 | std::unique_ptr<CredentialTypeProvider> type_provider) { |
| 156 | GetProvider()->AddSecureType(type, std::move(type_provider)); |
Dan Born | f30941c | 2016-02-24 14:17:26 -0800 | [diff] [blame] | 157 | } |
yang-g | 12a0a2c | 2016-02-19 00:22:20 -0800 | [diff] [blame] | 158 | |
yang-g | 7d2a3e1 | 2016-02-18 15:41:56 -0800 | [diff] [blame] | 159 | std::shared_ptr<ChannelCredentials> GetChannelCredentials( |
yang-g | 17197dd | 2016-02-19 00:04:22 -0800 | [diff] [blame] | 160 | const grpc::string& type, ChannelArguments* args) { |
Dan Born | f30941c | 2016-02-24 14:17:26 -0800 | [diff] [blame] | 161 | return GetProvider()->GetChannelCredentials(type, args); |
yang-g | 7d2a3e1 | 2016-02-18 15:41:56 -0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | std::shared_ptr<ServerCredentials> GetServerCredentials( |
yang-g | 17197dd | 2016-02-19 00:04:22 -0800 | [diff] [blame] | 165 | const grpc::string& type) { |
Dan Born | f30941c | 2016-02-24 14:17:26 -0800 | [diff] [blame] | 166 | return GetProvider()->GetServerCredentials(type); |
yang-g | 7d2a3e1 | 2016-02-18 15:41:56 -0800 | [diff] [blame] | 167 | } |
| 168 | |
yang-g | 4c8aed3 | 2016-02-19 00:19:39 -0800 | [diff] [blame] | 169 | std::vector<grpc::string> GetSecureCredentialsTypeList() { |
Dan Born | f30941c | 2016-02-24 14:17:26 -0800 | [diff] [blame] | 170 | return GetProvider()->GetSecureCredentialsTypeList(); |
yang-g | 4c8aed3 | 2016-02-19 00:19:39 -0800 | [diff] [blame] | 171 | } |
| 172 | |
yang-g | 7d2a3e1 | 2016-02-18 15:41:56 -0800 | [diff] [blame] | 173 | } // namespace testing |
| 174 | } // namespace grpc |