forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alpn_selection_integration_test.cc
211 lines (183 loc) · 9 KB
/
alpn_selection_integration_test.cc
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
#include "envoy/config/bootstrap/v3/bootstrap.pb.h"
#include "envoy/config/route/v3/route_components.pb.h"
#include "envoy/extensions/transport_sockets/tls/v3/cert.pb.h"
#include "source/common/http/utility.h"
#include "source/common/tls/context_config_impl.h"
#include "source/common/tls/context_impl.h"
#include "source/common/tls/server_ssl_socket.h"
#include "test/integration/http_integration.h"
#include "absl/strings/str_replace.h"
#include "gtest/gtest.h"
namespace Envoy {
class AlpnSelectionIntegrationTest : public testing::Test, public HttpIntegrationTest {
public:
AlpnSelectionIntegrationTest()
: HttpIntegrationTest(Http::CodecType::HTTP1, TestEnvironment::getIpVersionsForTest().front(),
ConfigHelper::httpProxyConfig()) {}
void initialize() override {
setDownstreamProtocol(Http::CodecType::HTTP1);
setUpstreamProtocol(use_h2_ ? Http::CodecType::HTTP2 : Http::CodecType::HTTP1);
config_helper_.addConfigModifier([&](envoy::config::bootstrap::v3::Bootstrap& bootstrap) {
auto* static_resources = bootstrap.mutable_static_resources();
auto* cluster = static_resources->mutable_clusters(0);
if (use_h2_) {
ConfigHelper::setHttp2(*cluster);
}
const std::string transport_socket_yaml = absl::StrFormat(
R"EOF(
name: tls
typed_config:
"@type": type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.UpstreamTlsContext
common_tls_context:
alpn_protocols: [ %s ]
tls_certificates:
- certificate_chain: { filename: "%s" }
private_key: { filename: "%s" }
)EOF",
absl::StrJoin(configured_alpn_, ","),
TestEnvironment::runfilesPath("test/config/integration/certs/clientcert.pem"),
TestEnvironment::runfilesPath("test/config/integration/certs/clientkey.pem"));
auto* transport_socket = cluster->mutable_transport_socket();
TestUtility::loadFromYaml(transport_socket_yaml, *transport_socket);
});
HttpIntegrationTest::initialize();
}
Network::DownstreamTransportSocketFactoryPtr createUpstreamSslContext() {
envoy::extensions::transport_sockets::tls::v3::DownstreamTlsContext tls_context;
const std::string yaml = absl::StrFormat(
R"EOF(
common_tls_context:
alpn_protocols: [%s]
tls_certificates:
- certificate_chain: { filename: "%s" }
private_key: { filename: "%s" }
validation_context:
trusted_ca: { filename: "%s" }
require_client_certificate: true
)EOF",
absl::StrJoin(upstream_alpn_, ","),
TestEnvironment::runfilesPath("test/config/integration/certs/upstreamcert.pem"),
TestEnvironment::runfilesPath("test/config/integration/certs/upstreamkey.pem"),
TestEnvironment::runfilesPath("test/config/integration/certs/cacert.pem"));
TestUtility::loadFromYaml(yaml, tls_context);
auto cfg = *Extensions::TransportSockets::Tls::ServerContextConfigImpl::create(
tls_context, factory_context_);
static auto* upstream_stats_store = new Stats::IsolatedStoreImpl();
return *Extensions::TransportSockets::Tls::ServerSslSocketFactory::create(
std::move(cfg), context_manager_, *upstream_stats_store->rootScope(),
std::vector<std::string>{});
}
void createUpstreams() override {
auto endpoint = upstream_address_fn_(0);
FakeUpstreamConfig config = upstreamConfig();
config.upstream_protocol_ = use_h2_ ? Http::CodecType::HTTP2 : Http::CodecType::HTTP1;
fake_upstreams_.emplace_back(
new FakeUpstream(createUpstreamSslContext(), endpoint->ip()->port(), version_, config));
}
bool use_h2_{};
std::vector<std::string> upstream_alpn_;
std::vector<std::string> configured_alpn_;
};
// No upstream ALPN is specified in the protocol, but we successfully negotiate h2 ALPN
// due to the default ALPN set through the HTTP/2 conn pool.
TEST_F(AlpnSelectionIntegrationTest, Http2UpstreamMatchingAlpn) {
use_h2_ = true;
upstream_alpn_.emplace_back(Http::Utility::AlpnNames::get().Http2);
initialize();
codec_client_ = makeHttpConnection(lookupPort("http"));
IntegrationStreamDecoderPtr response =
codec_client_->makeHeaderOnlyRequest(default_request_headers_);
waitForNextUpstreamRequest();
EXPECT_EQ(Http::Utility::AlpnNames::get().Http2,
fake_upstream_connection_->connection().nextProtocol());
upstream_request_->encodeHeaders(default_response_headers_, true);
ASSERT_TRUE(response->waitForEndStream());
EXPECT_EQ("200", response->headers().getStatusValue());
}
// No upstream ALPN is specified in the protocol and we fail to negotiate h2 ALPN
// since the upstream doesn't list h2 in its ALPN list. Note that the call still goes
// through because ALPN negotiation failure doesn't necessarily fail the call.
// TODO(snowp): We should actually fail the handshake in case of negotiation failure,
// fix that and update these tests.
TEST_F(AlpnSelectionIntegrationTest, Http2UpstreamMismatchingAlpn) {
use_h2_ = true;
upstream_alpn_.emplace_back(Http::Utility::AlpnNames::get().Http11);
initialize();
codec_client_ = makeHttpConnection(lookupPort("http"));
IntegrationStreamDecoderPtr response =
codec_client_->makeHeaderOnlyRequest(default_request_headers_);
waitForNextUpstreamRequest();
// No ALPN negotiated.
EXPECT_EQ("", fake_upstream_connection_->connection().nextProtocol());
upstream_request_->encodeHeaders(default_response_headers_, true);
ASSERT_TRUE(response->waitForEndStream());
EXPECT_EQ("200", response->headers().getStatusValue());
}
// The upstream supports h2,custom-alpn, and we configure the upstream TLS context to negotiate
// custom-alpn. No attempt to negotiate h2 should happen, so we should select custom-alpn.
TEST_F(AlpnSelectionIntegrationTest, Http2UpstreamConfiguredALPN) {
use_h2_ = true;
upstream_alpn_.emplace_back(Http::Utility::AlpnNames::get().Http2);
upstream_alpn_.emplace_back("custom-alpn");
configured_alpn_.emplace_back("custom-alpn");
initialize();
codec_client_ = makeHttpConnection(lookupPort("http"));
IntegrationStreamDecoderPtr response =
codec_client_->makeHeaderOnlyRequest(default_request_headers_);
waitForNextUpstreamRequest();
EXPECT_EQ("custom-alpn", fake_upstream_connection_->connection().nextProtocol());
upstream_request_->encodeHeaders(default_response_headers_, true);
ASSERT_TRUE(response->waitForEndStream());
EXPECT_EQ("200", response->headers().getStatusValue());
}
// No upstream ALPN is specified in the protocol, but we successfully negotiate http/1.1 ALPN
// due to the default ALPN set through the HTTP/1.1 conn pool.
TEST_F(AlpnSelectionIntegrationTest, Http11UpstreaMatchingAlpn) {
upstream_alpn_.emplace_back(Http::Utility::AlpnNames::get().Http11);
initialize();
codec_client_ = makeHttpConnection(lookupPort("http"));
IntegrationStreamDecoderPtr response =
codec_client_->makeHeaderOnlyRequest(default_request_headers_);
waitForNextUpstreamRequest();
EXPECT_EQ(Http::Utility::AlpnNames::get().Http11,
fake_upstream_connection_->connection().nextProtocol());
upstream_request_->encodeHeaders(default_response_headers_, true);
ASSERT_TRUE(response->waitForEndStream());
EXPECT_EQ("200", response->headers().getStatusValue());
}
// The upstream only lists h2 but we attempt to negotiate http/1.1 due to the default ALPN set by
// the conn pool. This results in no protocol being negotiated. Note that the call still goes
// through because ALPN negotiation failure doesn't necessarily fail the call.
TEST_F(AlpnSelectionIntegrationTest, Http11UpstreaMismatchingAlpn) {
upstream_alpn_.emplace_back(Http::Utility::AlpnNames::get().Http2);
initialize();
codec_client_ = makeHttpConnection(lookupPort("http"));
IntegrationStreamDecoderPtr response =
codec_client_->makeHeaderOnlyRequest(default_request_headers_);
waitForNextUpstreamRequest();
// No ALPN selected.
EXPECT_EQ("", fake_upstream_connection_->connection().nextProtocol());
upstream_request_->encodeHeaders(default_response_headers_, true);
ASSERT_TRUE(response->waitForEndStream());
EXPECT_EQ("200", response->headers().getStatusValue());
}
// The upstream supports http/1.1,custom-alpn, and we configure the upstream TLS context to
// negotiate custom-alpn. No attempt to negotiate http/1.1 should happen, so we should select
// custom-alpn.
// TODO(snowp): We should actually fail the handshake in case of negotiation failure,
// fix that and update these tests.
TEST_F(AlpnSelectionIntegrationTest, Http11UpstreamConfiguredALPN) {
upstream_alpn_.emplace_back(Http::Utility::AlpnNames::get().Http11);
upstream_alpn_.emplace_back("custom-alpn");
configured_alpn_.emplace_back("custom-alpn");
initialize();
codec_client_ = makeHttpConnection(lookupPort("http"));
IntegrationStreamDecoderPtr response =
codec_client_->makeHeaderOnlyRequest(default_request_headers_);
waitForNextUpstreamRequest();
EXPECT_EQ("custom-alpn", fake_upstream_connection_->connection().nextProtocol());
upstream_request_->encodeHeaders(default_response_headers_, true);
ASSERT_TRUE(response->waitForEndStream());
EXPECT_EQ("200", response->headers().getStatusValue());
}
} // namespace Envoy