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 | f30941c | 2016-02-24 14:17:26 -0800 | [diff] [blame^] | 37 | #include "include/grpc/impl/codegen/sync_posix.h" |
| 38 | #include "include/grpc++/impl/codegen/sync_no_cxx11.h" |
yang-g | 7d2a3e1 | 2016-02-18 15:41:56 -0800 | [diff] [blame] | 39 | #include "test/core/end2end/data/ssl_test_data.h" |
| 40 | |
Dan Born | f30941c | 2016-02-24 14:17:26 -0800 | [diff] [blame^] | 41 | namespace { |
| 42 | |
| 43 | using grpc::ChannelArguments; |
| 44 | using grpc::ChannelCredentials; |
| 45 | using grpc::InsecureChannelCredentials; |
| 46 | using grpc::InsecureServerCredentials; |
| 47 | using grpc::ServerCredentials; |
| 48 | using grpc::SslCredentialsOptions; |
| 49 | using grpc::SslServerCredentialsOptions; |
| 50 | using grpc::testing::CredentialsProvider; |
| 51 | |
| 52 | class DefaultCredentialsProvider : public CredentialsProvider { |
| 53 | public: |
| 54 | ~DefaultCredentialsProvider() override {} |
| 55 | |
| 56 | std::shared_ptr<ChannelCredentials> GetChannelCredentials( |
| 57 | const grpc::string& type, ChannelArguments* args) override { |
| 58 | if (type == grpc::testing::kInsecureCredentialsType) { |
| 59 | return InsecureChannelCredentials(); |
| 60 | } else if (type == grpc::testing::kTlsCredentialsType) { |
| 61 | SslCredentialsOptions ssl_opts = {test_root_cert, "", ""}; |
| 62 | args->SetSslTargetNameOverride("foo.test.google.fr"); |
| 63 | return SslCredentials(ssl_opts); |
| 64 | } else { |
| 65 | gpr_log(GPR_ERROR, "Unsupported credentials type %s.", type.c_str()); |
| 66 | } |
| 67 | return nullptr; |
| 68 | } |
| 69 | |
| 70 | std::shared_ptr<ServerCredentials> GetServerCredentials( |
| 71 | const grpc::string& type) override { |
| 72 | if (type == grpc::testing::kInsecureCredentialsType) { |
| 73 | return InsecureServerCredentials(); |
| 74 | } else if (type == grpc::testing::kTlsCredentialsType) { |
| 75 | SslServerCredentialsOptions::PemKeyCertPair pkcp = {test_server1_key, |
| 76 | test_server1_cert}; |
| 77 | SslServerCredentialsOptions ssl_opts; |
| 78 | ssl_opts.pem_root_certs = ""; |
| 79 | ssl_opts.pem_key_cert_pairs.push_back(pkcp); |
| 80 | return SslServerCredentials(ssl_opts); |
| 81 | } else { |
| 82 | gpr_log(GPR_ERROR, "Unsupported credentials type %s.", type.c_str()); |
| 83 | } |
| 84 | return nullptr; |
| 85 | } |
| 86 | std::vector<grpc::string> GetSecureCredentialsTypeList() override { |
| 87 | std::vector<grpc::string> types; |
| 88 | types.push_back(grpc::testing::kTlsCredentialsType); |
| 89 | return types; |
| 90 | } |
| 91 | }; |
| 92 | |
| 93 | gpr_once g_once_init_provider_mu = GPR_ONCE_INIT; |
| 94 | grpc::mutex* g_provider_mu; |
| 95 | CredentialsProvider* g_provider = nullptr; |
| 96 | |
| 97 | void InitProviderMu() { |
| 98 | g_provider_mu = new grpc::mutex; |
| 99 | } |
| 100 | |
| 101 | grpc::mutex& GetMu() { |
| 102 | gpr_once_init(&g_once_init_provider_mu, &InitProviderMu); |
| 103 | return *g_provider_mu; |
| 104 | } |
| 105 | |
| 106 | CredentialsProvider* GetProvider() { |
| 107 | grpc::unique_lock<grpc::mutex> lock(GetMu()); |
| 108 | if (g_provider == nullptr) { |
| 109 | g_provider = new DefaultCredentialsProvider; |
| 110 | } |
| 111 | return g_provider; |
| 112 | } |
| 113 | |
| 114 | } // namespace |
| 115 | |
yang-g | 7d2a3e1 | 2016-02-18 15:41:56 -0800 | [diff] [blame] | 116 | namespace grpc { |
| 117 | namespace testing { |
| 118 | |
Dan Born | f30941c | 2016-02-24 14:17:26 -0800 | [diff] [blame^] | 119 | // Note that it is not thread-safe to set a provider while concurrently using |
| 120 | // the previously set provider, as this deletes and replaces it. nullptr may be |
| 121 | // given to reset to the default. |
| 122 | void SetTestCredentialsProvider(std::unique_ptr<CredentialsProvider> provider) { |
| 123 | grpc::unique_lock<grpc::mutex> lock(GetMu()); |
| 124 | if (g_provider != nullptr) { |
| 125 | delete g_provider; |
| 126 | } |
| 127 | g_provider = provider.release(); |
| 128 | } |
yang-g | 12a0a2c | 2016-02-19 00:22:20 -0800 | [diff] [blame] | 129 | |
yang-g | 7d2a3e1 | 2016-02-18 15:41:56 -0800 | [diff] [blame] | 130 | std::shared_ptr<ChannelCredentials> GetChannelCredentials( |
yang-g | 17197dd | 2016-02-19 00:04:22 -0800 | [diff] [blame] | 131 | const grpc::string& type, ChannelArguments* args) { |
Dan Born | f30941c | 2016-02-24 14:17:26 -0800 | [diff] [blame^] | 132 | return GetProvider()->GetChannelCredentials(type, args); |
yang-g | 7d2a3e1 | 2016-02-18 15:41:56 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | std::shared_ptr<ServerCredentials> GetServerCredentials( |
yang-g | 17197dd | 2016-02-19 00:04:22 -0800 | [diff] [blame] | 136 | const grpc::string& type) { |
Dan Born | f30941c | 2016-02-24 14:17:26 -0800 | [diff] [blame^] | 137 | return GetProvider()->GetServerCredentials(type); |
yang-g | 7d2a3e1 | 2016-02-18 15:41:56 -0800 | [diff] [blame] | 138 | } |
| 139 | |
yang-g | 4c8aed3 | 2016-02-19 00:19:39 -0800 | [diff] [blame] | 140 | std::vector<grpc::string> GetSecureCredentialsTypeList() { |
Dan Born | f30941c | 2016-02-24 14:17:26 -0800 | [diff] [blame^] | 141 | return GetProvider()->GetSecureCredentialsTypeList(); |
yang-g | 4c8aed3 | 2016-02-19 00:19:39 -0800 | [diff] [blame] | 142 | } |
| 143 | |
yang-g | 7d2a3e1 | 2016-02-18 15:41:56 -0800 | [diff] [blame] | 144 | } // namespace testing |
| 145 | } // namespace grpc |