1 //
2 //
3 // Copyright 2023 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18 #include <gmock/gmock.h>
19 #include <grpc/credentials.h>
20 #include <grpc/grpc_security.h>
21 #include <grpcpp/channel.h>
22 #include <grpcpp/client_context.h>
23 #include <grpcpp/create_channel.h>
24 #include <grpcpp/server.h>
25 #include <grpcpp/server_builder.h>
26 #include <gtest/gtest.h>
27
28 #include <memory>
29
30 #include "absl/log/log.h"
31 #include "absl/synchronization/notification.h"
32 #include "test/core/test_util/port.h"
33 #include "test/core/test_util/test_config.h"
34 #include "test/core/test_util/tls_utils.h"
35 #include "test/cpp/end2end/test_service_impl.h"
36
37 namespace grpc {
38 namespace testing {
39 namespace {
40
41 constexpr char kCaCertPath[] = "src/core/tsi/test_creds/ca.pem";
42 constexpr char kServerCertPath[] = "src/core/tsi/test_creds/server1.pem";
43 constexpr char kServerKeyPath[] = "src/core/tsi/test_creds/server1.key";
44 constexpr char kClientCertPath[] = "src/core/tsi/test_creds/client.pem";
45 constexpr char kClientKeyPath[] = "src/core/tsi/test_creds/client.key";
46 constexpr char kMessage[] = "Hello";
47
48 class SslCredentialsTest : public ::testing::Test {
49 protected:
RunServer(absl::Notification * notification)50 void RunServer(absl::Notification* notification) {
51 std::string root_cert = grpc_core::testing::GetFileContents(kCaCertPath);
52 grpc::SslServerCredentialsOptions::PemKeyCertPair key_cert_pair = {
53 grpc_core::testing::GetFileContents(kServerKeyPath),
54 grpc_core::testing::GetFileContents(kServerCertPath)};
55 // TODO(gtcooke94) Parametrize this test for TLS and mTLS as well
56 grpc::SslServerCredentialsOptions ssl_options;
57 ssl_options.pem_key_cert_pairs.push_back(key_cert_pair);
58 ssl_options.pem_root_certs = root_cert;
59 ssl_options.force_client_auth = true;
60
61 grpc::ServerBuilder builder;
62 TestServiceImpl service_;
63
64 builder.AddListeningPort(server_addr_,
65 grpc::SslServerCredentials(ssl_options));
66 builder.RegisterService("foo.test.google.fr", &service_);
67 server_ = builder.BuildAndStart();
68 notification->Notify();
69 server_->Wait();
70 }
71
TearDown()72 void TearDown() override {
73 if (server_ != nullptr) {
74 server_->Shutdown();
75 server_thread_->join();
76 delete server_thread_;
77 }
78 }
79
80 TestServiceImpl service_;
81 std::unique_ptr<Server> server_ = nullptr;
82 std::thread* server_thread_ = nullptr;
83 std::string server_addr_;
84 };
85
DoRpc(const std::string & server_addr,const SslCredentialsOptions & ssl_options,grpc_ssl_session_cache * cache,bool expect_session_reuse)86 void DoRpc(const std::string& server_addr,
87 const SslCredentialsOptions& ssl_options,
88 grpc_ssl_session_cache* cache, bool expect_session_reuse) {
89 ChannelArguments channel_args;
90 channel_args.SetPointer(std::string(GRPC_SSL_SESSION_CACHE_ARG), cache);
91 channel_args.SetSslTargetNameOverride("foo.test.google.fr");
92
93 std::shared_ptr<Channel> channel = grpc::CreateCustomChannel(
94 server_addr, grpc::SslCredentials(ssl_options), channel_args);
95
96 auto stub = grpc::testing::EchoTestService::NewStub(channel);
97 grpc::testing::EchoRequest request;
98 grpc::testing::EchoResponse response;
99 request.set_message(kMessage);
100 ClientContext context;
101 context.set_deadline(grpc_timeout_seconds_to_deadline(/*time_s=*/60));
102 grpc::Status result = stub->Echo(&context, request, &response);
103 EXPECT_TRUE(result.ok());
104 if (!result.ok()) {
105 LOG(ERROR) << result.error_message() << ", " << result.error_details();
106 }
107 EXPECT_EQ(response.message(), kMessage);
108 std::shared_ptr<const AuthContext> auth_context = context.auth_context();
109 std::vector<grpc::string_ref> properties =
110 auth_context->FindPropertyValues(GRPC_SSL_SESSION_REUSED_PROPERTY);
111 ASSERT_EQ(properties.size(), 1u);
112 if (expect_session_reuse) {
113 EXPECT_EQ("true", ToString(properties[0]));
114 } else {
115 EXPECT_EQ("false", ToString(properties[0]));
116 }
117 }
118
TEST_F(SslCredentialsTest,SequentialResumption)119 TEST_F(SslCredentialsTest, SequentialResumption) {
120 server_addr_ = absl::StrCat("localhost:",
121 std::to_string(grpc_pick_unused_port_or_die()));
122 absl::Notification notification;
123 server_thread_ = new std::thread([&]() { RunServer(¬ification); });
124 notification.WaitForNotification();
125
126 std::string root_cert = grpc_core::testing::GetFileContents(kCaCertPath);
127 std::string client_key = grpc_core::testing::GetFileContents(kClientKeyPath);
128 std::string client_cert =
129 grpc_core::testing::GetFileContents(kClientCertPath);
130 grpc::SslCredentialsOptions ssl_options;
131 ssl_options.pem_root_certs = root_cert;
132 ssl_options.pem_private_key = client_key;
133 ssl_options.pem_cert_chain = client_cert;
134
135 grpc_ssl_session_cache* cache = grpc_ssl_session_cache_create_lru(16);
136
137 DoRpc(server_addr_, ssl_options, cache, /*expect_session_reuse=*/false);
138 for (int i = 0; i < 10; i++) {
139 DoRpc(server_addr_, ssl_options, cache, /*expect_session_reuse=*/true);
140 }
141
142 grpc_ssl_session_cache_destroy(cache);
143 }
144
TEST_F(SslCredentialsTest,ConcurrentResumption)145 TEST_F(SslCredentialsTest, ConcurrentResumption) {
146 server_addr_ = absl::StrCat("localhost:",
147 std::to_string(grpc_pick_unused_port_or_die()));
148 absl::Notification notification;
149 server_thread_ = new std::thread([&]() { RunServer(¬ification); });
150 notification.WaitForNotification();
151
152 std::string root_cert = grpc_core::testing::GetFileContents(kCaCertPath);
153 std::string client_key = grpc_core::testing::GetFileContents(kClientKeyPath);
154 std::string client_cert =
155 grpc_core::testing::GetFileContents(kClientCertPath);
156 grpc::SslCredentialsOptions ssl_options;
157 ssl_options.pem_root_certs = root_cert;
158 ssl_options.pem_private_key = client_key;
159 ssl_options.pem_cert_chain = client_cert;
160
161 grpc_ssl_session_cache* cache = grpc_ssl_session_cache_create_lru(16);
162
163 DoRpc(server_addr_, ssl_options, cache, /*expect_session_reuse=*/false);
164 std::vector<std::thread> threads;
165 threads.reserve(10);
166 for (int i = 0; i < 10; i++) {
167 threads.push_back(std::thread([&]() {
168 DoRpc(server_addr_, ssl_options, cache, /*expect_session_reuse=*/true);
169 }));
170 }
171 for (auto& t : threads) {
172 t.join();
173 }
174
175 grpc_ssl_session_cache_destroy(cache);
176 }
177
TEST_F(SslCredentialsTest,ResumptionFailsDueToNoCapacityInCache)178 TEST_F(SslCredentialsTest, ResumptionFailsDueToNoCapacityInCache) {
179 server_addr_ = absl::StrCat("localhost:",
180 std::to_string(grpc_pick_unused_port_or_die()));
181 absl::Notification notification;
182 server_thread_ = new std::thread([&]() { RunServer(¬ification); });
183 notification.WaitForNotification();
184
185 std::string root_cert = grpc_core::testing::GetFileContents(kCaCertPath);
186 std::string client_key = grpc_core::testing::GetFileContents(kClientKeyPath);
187 std::string client_cert =
188 grpc_core::testing::GetFileContents(kClientCertPath);
189 grpc::SslCredentialsOptions ssl_options;
190 ssl_options.pem_root_certs = root_cert;
191 ssl_options.pem_private_key = client_key;
192 ssl_options.pem_cert_chain = client_cert;
193
194 grpc_ssl_session_cache* cache = grpc_ssl_session_cache_create_lru(0);
195
196 DoRpc(server_addr_, ssl_options, cache, /*expect_session_reuse=*/false);
197 DoRpc(server_addr_, ssl_options, cache, /*expect_session_reuse=*/false);
198
199 grpc_ssl_session_cache_destroy(cache);
200 }
201
202 } // namespace
203 } // namespace testing
204 } // namespace grpc
205
main(int argc,char ** argv)206 int main(int argc, char** argv) {
207 grpc::testing::TestEnvironment env(&argc, argv);
208 ::testing::InitGoogleTest(&argc, argv);
209 int ret = RUN_ALL_TESTS();
210 return ret;
211 }
212