Repository navigation
Expand file tree
/
Copy pathssl_integration_test.cc
More file actions
399 lines (354 loc) · 17 KB
/
Copy pathssl_integration_test.cc
File metadata and controls
399 lines (354 loc) · 17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#include "ssl_integration_test.h"
#include <memory>
#include <string>
#include "envoy/config/transport_socket/capture/v2alpha/capture.pb.h"
#include "envoy/data/tap/v2alpha/capture.pb.h"
#include "common/event/dispatcher_impl.h"
#include "common/network/connection_impl.h"
#include "common/network/utility.h"
#include "common/ssl/context_config_impl.h"
#include "common/ssl/context_manager_impl.h"
#include "test/test_common/network_utility.h"
#include "test/test_common/utility.h"
#include "absl/strings/match.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "integration.h"
#include "utility.h"
using testing::Return;
namespace Envoy {
namespace Ssl {
void SslIntegrationTestBase::initialize() {
config_helper_.addSslConfig(server_ecdsa_cert_, server_tlsv1_3_);
HttpIntegrationTest::initialize();
context_manager_ = std::make_unique<ContextManagerImpl>(timeSystem());
registerTestServerPorts({"http"});
}
void SslIntegrationTestBase::TearDown() {
HttpIntegrationTest::cleanupUpstreamAndDownstream();
codec_client_.reset();
context_manager_.reset();
}
Network::ClientConnectionPtr
SslIntegrationTestBase::makeSslClientConnection(const ClientSslTransportOptions& options) {
Network::Address::InstanceConstSharedPtr address = getSslAddress(version_, lookupPort("http"));
if (debug_with_s_client_) {
const std::string s_client_cmd = TestEnvironment::substitute(
"openssl s_client -connect " + address->asString() +
" -showcerts -debug -msg -CAfile "
"{{ test_rundir }}/test/config/integration/certs/cacert.pem "
"-servername lyft.com -cert "
"{{ test_rundir }}/test/config/integration/certs/clientcert.pem "
"-key "
"{{ test_rundir }}/test/config/integration/certs/clientkey.pem ",
version_);
ENVOY_LOG_MISC(debug, "Executing {}", s_client_cmd);
RELEASE_ASSERT(::system(s_client_cmd.c_str()) == 0, "");
}
auto client_transport_socket_factory_ptr =
createClientSslTransportSocketFactory(options, *context_manager_);
return dispatcher_->createClientConnection(
address, Network::Address::InstanceConstSharedPtr(),
client_transport_socket_factory_ptr->createTransportSocket({}), nullptr);
}
void SslIntegrationTestBase::checkStats() {
const uint32_t expected_handshakes = debug_with_s_client_ ? 2 : 1;
Stats::CounterSharedPtr counter = test_server_->counter(listenerStatPrefix("ssl.handshake"));
EXPECT_EQ(expected_handshakes, counter->value());
counter->reset();
}
INSTANTIATE_TEST_CASE_P(IpVersions, SslIntegrationTest,
testing::ValuesIn(TestEnvironment::getIpVersionsForTest()),
TestUtility::ipTestParamsToString);
TEST_P(SslIntegrationTest, RouterRequestAndResponseWithGiantBodyBuffer) {
ConnectionCreationFunction creator = [&]() -> Network::ClientConnectionPtr {
return makeSslClientConnection({});
};
testRouterRequestAndResponseWithBody(16 * 1024 * 1024, 16 * 1024 * 1024, false, &creator);
checkStats();
}
TEST_P(SslIntegrationTest, RouterRequestAndResponseWithBodyNoBuffer) {
ConnectionCreationFunction creator = [&]() -> Network::ClientConnectionPtr {
return makeSslClientConnection({});
};
testRouterRequestAndResponseWithBody(1024, 512, false, &creator);
checkStats();
}
TEST_P(SslIntegrationTest, RouterRequestAndResponseWithBodyNoBufferHttp2) {
setDownstreamProtocol(Http::CodecClient::Type::HTTP2);
config_helper_.setClientCodec(
envoy::config::filter::network::http_connection_manager::v2::HttpConnectionManager::AUTO);
ConnectionCreationFunction creator = [&]() -> Network::ClientConnectionPtr {
return makeSslClientConnection(ClientSslTransportOptions().setAlpn(true));
};
testRouterRequestAndResponseWithBody(1024, 512, false, &creator);
checkStats();
}
TEST_P(SslIntegrationTest, RouterRequestAndResponseWithBodyNoBufferVerifySAN) {
ConnectionCreationFunction creator = [&]() -> Network::ClientConnectionPtr {
return makeSslClientConnection(ClientSslTransportOptions().setSan(true));
};
testRouterRequestAndResponseWithBody(1024, 512, false, &creator);
checkStats();
}
TEST_P(SslIntegrationTest, RouterRequestAndResponseWithBodyNoBufferHttp2VerifySAN) {
setDownstreamProtocol(Http::CodecClient::Type::HTTP2);
ConnectionCreationFunction creator = [&]() -> Network::ClientConnectionPtr {
return makeSslClientConnection(ClientSslTransportOptions().setAlpn(true).setSan(true));
};
testRouterRequestAndResponseWithBody(1024, 512, false, &creator);
checkStats();
}
TEST_P(SslIntegrationTest, RouterHeaderOnlyRequestAndResponse) {
ConnectionCreationFunction creator = [&]() -> Network::ClientConnectionPtr {
return makeSslClientConnection({});
};
testRouterHeaderOnlyRequestAndResponse(true, &creator);
checkStats();
}
TEST_P(SslIntegrationTest, RouterUpstreamDisconnectBeforeResponseComplete) {
ConnectionCreationFunction creator = [&]() -> Network::ClientConnectionPtr {
return makeSslClientConnection({});
};
testRouterUpstreamDisconnectBeforeResponseComplete(&creator);
checkStats();
}
TEST_P(SslIntegrationTest, RouterDownstreamDisconnectBeforeRequestComplete) {
ConnectionCreationFunction creator = [&]() -> Network::ClientConnectionPtr {
return makeSslClientConnection({});
};
testRouterDownstreamDisconnectBeforeRequestComplete(&creator);
checkStats();
}
TEST_P(SslIntegrationTest, RouterDownstreamDisconnectBeforeResponseComplete) {
#ifdef __APPLE__
// Skip this test on OS X: we can't detect the early close on OS X, and we
// won't clean up the upstream connection until it times out. See #4294.
if (downstream_protocol_ == Http::CodecClient::Type::HTTP1) {
return;
}
#endif
ConnectionCreationFunction creator = [&]() -> Network::ClientConnectionPtr {
return makeSslClientConnection({});
};
testRouterDownstreamDisconnectBeforeResponseComplete(&creator);
checkStats();
}
// This test must be here vs integration_admin_test so that it tests a server with loaded certs.
TEST_P(SslIntegrationTest, AdminCertEndpoint) {
initialize();
BufferingStreamDecoderPtr response = IntegrationUtil::makeSingleRequest(
lookupPort("admin"), "GET", "/certs", "", downstreamProtocol(), version_);
EXPECT_TRUE(response->complete());
EXPECT_STREQ("200", response->headers().Status()->value().c_str());
}
// Validate certificate selection across different certificate types and client TLS versions.
class SslCertficateIntegrationTest
: public SslIntegrationTestBase,
public testing::TestWithParam<std::tuple<Network::Address::IpVersion,
envoy::api::v2::auth::TlsParameters_TlsProtocol>> {
public:
SslCertficateIntegrationTest() : SslIntegrationTestBase(std::get<0>(GetParam())) {
server_tlsv1_3_ = true;
}
Network::ClientConnectionPtr
makeSslClientConnection(const ClientSslTransportOptions& options) override {
ClientSslTransportOptions modified_options{options};
modified_options.setTlsVersion(tls_version_);
return SslIntegrationTestBase::makeSslClientConnection(modified_options);
}
void TearDown() override { SslIntegrationTestBase::TearDown(); };
ClientSslTransportOptions rsaOnlyClientOptions() {
if (tls_version_ == envoy::api::v2::auth::TlsParameters::TLSv1_3) {
return ClientSslTransportOptions().setSigningAlgorithmsForTest("rsa_pss_rsae_sha256");
} else {
return ClientSslTransportOptions().setCipherSuites({"ECDHE-RSA-AES128-GCM-SHA256"});
}
}
ClientSslTransportOptions ecdsaOnlyClientOptions() {
if (tls_version_ == envoy::api::v2::auth::TlsParameters::TLSv1_3) {
return ClientSslTransportOptions().setSigningAlgorithmsForTest("ecdsa_secp256r1_sha256");
} else {
return ClientSslTransportOptions().setCipherSuites({"ECDHE-ECDSA-AES128-GCM-SHA256"});
}
}
static std::string ipClientVersionTestParamsToString(
const testing::TestParamInfo<
std::tuple<Network::Address::IpVersion, envoy::api::v2::auth::TlsParameters_TlsProtocol>>&
params) {
return fmt::format("{}_TLSv1_{}",
std::get<0>(params.param) == Network::Address::IpVersion::v4 ? "IPv4"
: "IPv6",
std::get<1>(params.param) - 1);
}
const envoy::api::v2::auth::TlsParameters_TlsProtocol tls_version_{std::get<1>(GetParam())};
};
INSTANTIATE_TEST_CASE_P(
IpVersionsClientVersions, SslCertficateIntegrationTest,
testing::Combine(testing::ValuesIn(TestEnvironment::getIpVersionsForTest()),
testing::Values(envoy::api::v2::auth::TlsParameters::TLSv1_2,
envoy::api::v2::auth::TlsParameters::TLSv1_3)),
SslCertficateIntegrationTest::ipClientVersionTestParamsToString);
// Server with an RSA certificate and a client with RSA/ECDSA cipher suites works.
TEST_P(SslCertficateIntegrationTest, ServerRsa) {
server_ecdsa_cert_ = false;
ConnectionCreationFunction creator = [&]() -> Network::ClientConnectionPtr {
return makeSslClientConnection({});
};
testRouterRequestAndResponseWithBody(1024, 512, false, &creator);
checkStats();
}
// Server with an ECDSA certificate and a client with RSA/ECDSA cipher suites works.
TEST_P(SslCertficateIntegrationTest, ServerEcdsa) {
server_ecdsa_cert_ = true;
ConnectionCreationFunction creator = [&]() -> Network::ClientConnectionPtr {
return makeSslClientConnection({});
};
testRouterRequestAndResponseWithBody(1024, 512, false, &creator);
checkStats();
}
// Server with an RSA certificate and a client with only RSA cipher suites works.
TEST_P(SslCertficateIntegrationTest, ClientRsaOnly) {
server_ecdsa_cert_ = false;
ConnectionCreationFunction creator = [&]() -> Network::ClientConnectionPtr {
return makeSslClientConnection(rsaOnlyClientOptions());
};
testRouterRequestAndResponseWithBody(1024, 512, false, &creator);
checkStats();
}
// Server has only an ECDSA certificate, client is only RSA capable, leads to a connection fail.
TEST_P(SslCertficateIntegrationTest, ServerEcdsaClientRsaOnly) {
server_ecdsa_cert_ = true;
initialize();
auto codec_client = makeRawHttpConnection(makeSslClientConnection(rsaOnlyClientOptions()));
EXPECT_FALSE(codec_client->connected());
Stats::CounterSharedPtr counter =
test_server_->counter(listenerStatPrefix("ssl.connection_error"));
EXPECT_EQ(1U, counter->value());
counter->reset();
}
// Server has only an RSA certificate, client is only ECDSA capable, leads to connection fail.
TEST_P(SslCertficateIntegrationTest, ServerEcdsaClientEcdsaOnly) {
server_ecdsa_cert_ = false;
initialize();
EXPECT_FALSE(
makeRawHttpConnection(makeSslClientConnection(ecdsaOnlyClientOptions()))->connected());
Stats::CounterSharedPtr counter =
test_server_->counter(listenerStatPrefix("ssl.connection_error"));
EXPECT_EQ(1U, counter->value());
counter->reset();
}
class SslCaptureIntegrationTest : public SslIntegrationTest {
public:
void initialize() override {
config_helper_.addConfigModifier([this](envoy::config::bootstrap::v2::Bootstrap& bootstrap) {
auto* filter_chain =
bootstrap.mutable_static_resources()->mutable_listeners(0)->mutable_filter_chains(0);
// Configure inner SSL transport socket based on existing config.
envoy::api::v2::core::TransportSocket ssl_transport_socket;
ssl_transport_socket.set_name("tls");
MessageUtil::jsonConvert(filter_chain->tls_context(), *ssl_transport_socket.mutable_config());
// Configure outer capture transport socket.
auto* transport_socket = filter_chain->mutable_transport_socket();
transport_socket->set_name("envoy.transport_sockets.capture");
envoy::config::transport_socket::capture::v2alpha::Capture capture_config;
auto* file_sink = capture_config.mutable_file_sink();
file_sink->set_path_prefix(path_prefix_);
file_sink->set_format(
text_format_ ? envoy::config::transport_socket::capture::v2alpha::FileSink::PROTO_TEXT
: envoy::config::transport_socket::capture::v2alpha::FileSink::PROTO_BINARY);
capture_config.mutable_transport_socket()->MergeFrom(ssl_transport_socket);
MessageUtil::jsonConvert(capture_config, *transport_socket->mutable_config());
// Nuke TLS context from legacy location.
filter_chain->clear_tls_context();
// Rest of TLS initialization.
});
SslIntegrationTest::initialize();
// This confuses our socket counting.
debug_with_s_client_ = false;
}
std::string path_prefix_ = TestEnvironment::temporaryPath("ssl_trace");
bool text_format_{};
};
INSTANTIATE_TEST_CASE_P(IpVersions, SslCaptureIntegrationTest,
testing::ValuesIn(TestEnvironment::getIpVersionsForTest()),
TestUtility::ipTestParamsToString);
// Validate two back-to-back requests with binary proto output.
TEST_P(SslCaptureIntegrationTest, TwoRequestsWithBinaryProto) {
initialize();
ConnectionCreationFunction creator = [&]() -> Network::ClientConnectionPtr {
return makeSslClientConnection({});
};
// First request (ID will be +1 since the client will also bump).
const uint64_t first_id = Network::ConnectionImpl::nextGlobalIdForTest() + 1;
codec_client_ = makeHttpConnection(creator());
Http::TestHeaderMapImpl post_request_headers{
{":method", "POST"}, {":path", "/test/long/url"}, {":scheme", "http"},
{":authority", "host"}, {"x-lyft-user-id", "123"}, {"x-forwarded-for", "10.0.0.1"}};
auto response =
sendRequestAndWaitForResponse(post_request_headers, 128, default_response_headers_, 256);
EXPECT_TRUE(upstream_request_->complete());
EXPECT_EQ(128, upstream_request_->bodyLength());
ASSERT_TRUE(response->complete());
EXPECT_STREQ("200", response->headers().Status()->value().c_str());
EXPECT_EQ(256, response->body().size());
checkStats();
envoy::api::v2::core::Address expected_local_address;
Network::Utility::addressToProtobufAddress(*codec_client_->connection()->remoteAddress(),
expected_local_address);
envoy::api::v2::core::Address expected_remote_address;
Network::Utility::addressToProtobufAddress(*codec_client_->connection()->localAddress(),
expected_remote_address);
codec_client_->close();
test_server_->waitForCounterGe("http.config_test.downstream_cx_destroy", 1);
envoy::data::tap::v2alpha::Trace trace;
MessageUtil::loadFromFile(fmt::format("{}_{}.pb", path_prefix_, first_id), trace);
// Validate general expected properties in the trace.
EXPECT_EQ(first_id, trace.connection().id());
EXPECT_THAT(expected_local_address, ProtoEq(trace.connection().local_address()));
EXPECT_THAT(expected_remote_address, ProtoEq(trace.connection().remote_address()));
ASSERT_GE(trace.events().size(), 2);
EXPECT_TRUE(absl::StartsWith(trace.events(0).read().data(), "POST /test/long/url HTTP/1.1"));
EXPECT_TRUE(absl::StartsWith(trace.events(1).write().data(), "HTTP/1.1 200 OK"));
// Verify a second request hits a different file.
const uint64_t second_id = Network::ConnectionImpl::nextGlobalIdForTest() + 1;
codec_client_ = makeHttpConnection(creator());
Http::TestHeaderMapImpl get_request_headers{
{":method", "GET"}, {":path", "/test/long/url"}, {":scheme", "http"},
{":authority", "host"}, {"x-lyft-user-id", "123"}, {"x-forwarded-for", "10.0.0.1"}};
response =
sendRequestAndWaitForResponse(get_request_headers, 128, default_response_headers_, 256);
EXPECT_TRUE(upstream_request_->complete());
EXPECT_EQ(128, upstream_request_->bodyLength());
ASSERT_TRUE(response->complete());
EXPECT_STREQ("200", response->headers().Status()->value().c_str());
EXPECT_EQ(256, response->body().size());
checkStats();
codec_client_->close();
test_server_->waitForCounterGe("http.config_test.downstream_cx_destroy", 2);
MessageUtil::loadFromFile(fmt::format("{}_{}.pb", path_prefix_, second_id), trace);
// Validate second connection ID.
EXPECT_EQ(second_id, trace.connection().id());
ASSERT_GE(trace.events().size(), 2);
EXPECT_TRUE(absl::StartsWith(trace.events(0).read().data(), "GET /test/long/url HTTP/1.1"));
EXPECT_TRUE(absl::StartsWith(trace.events(1).write().data(), "HTTP/1.1 200 OK"));
}
// Validate a single request with text proto output.
TEST_P(SslCaptureIntegrationTest, RequestWithTextProto) {
text_format_ = true;
ConnectionCreationFunction creator = [&]() -> Network::ClientConnectionPtr {
return makeSslClientConnection({});
};
const uint64_t id = Network::ConnectionImpl::nextGlobalIdForTest() + 1;
testRouterRequestAndResponseWithBody(1024, 512, false, &creator);
checkStats();
codec_client_->close();
test_server_->waitForCounterGe("http.config_test.downstream_cx_destroy", 1);
envoy::data::tap::v2alpha::Trace trace;
MessageUtil::loadFromFile(fmt::format("{}_{}.pb_text", path_prefix_, id), trace);
// Test some obvious properties.
EXPECT_TRUE(absl::StartsWith(trace.events(0).read().data(), "POST /test/long/url HTTP/1.1"));
EXPECT_TRUE(absl::StartsWith(trace.events(1).write().data(), "HTTP/1.1 200 OK"));
}
} // namespace Ssl
} // namespace Envoy