|
| 1 | +#include "spiffe_validator_integration_test.h" |
| 2 | + |
| 3 | +#include <memory> |
| 4 | + |
| 5 | +#include "extensions/transport_sockets/tls/context_manager_impl.h" |
| 6 | + |
| 7 | +#include "test/integration/integration.h" |
| 8 | + |
| 9 | +#include "gtest/gtest.h" |
| 10 | + |
| 11 | +namespace Envoy { |
| 12 | +namespace Ssl { |
| 13 | + |
| 14 | +void SslSPIFFECertValidatorIntegrationTest::initialize() { |
| 15 | + config_helper_.addSslConfig( |
| 16 | + ConfigHelper::ServerSslOptions().setRsaCert(true).setTlsV13(true).setCustomValidatorConfig( |
| 17 | + custom_validator_config_)); |
| 18 | + HttpIntegrationTest::initialize(); |
| 19 | + |
| 20 | + context_manager_ = |
| 21 | + std::make_unique<Extensions::TransportSockets::Tls::ContextManagerImpl>(timeSystem()); |
| 22 | + registerTestServerPorts({"http"}); |
| 23 | +} |
| 24 | + |
| 25 | +void SslSPIFFECertValidatorIntegrationTest::TearDown() { |
| 26 | + HttpIntegrationTest::cleanupUpstreamAndDownstream(); |
| 27 | + codec_client_.reset(); |
| 28 | + context_manager_.reset(); |
| 29 | +} |
| 30 | + |
| 31 | +Network::ClientConnectionPtr SslSPIFFECertValidatorIntegrationTest::makeSslClientConnection( |
| 32 | + const ClientSslTransportOptions& options) { |
| 33 | + ClientSslTransportOptions modified_options{options}; |
| 34 | + modified_options.setTlsVersion(tls_version_); |
| 35 | + |
| 36 | + Network::Address::InstanceConstSharedPtr address = getSslAddress(version_, lookupPort("http")); |
| 37 | + auto client_transport_socket_factory_ptr = |
| 38 | + createClientSslTransportSocketFactory(modified_options, *context_manager_, *api_); |
| 39 | + return dispatcher_->createClientConnection( |
| 40 | + address, Network::Address::InstanceConstSharedPtr(), |
| 41 | + client_transport_socket_factory_ptr->createTransportSocket({}), nullptr); |
| 42 | +} |
| 43 | + |
| 44 | +void SslSPIFFECertValidatorIntegrationTest::checkVerifyErrorCouter(uint64_t value) { |
| 45 | + Stats::CounterSharedPtr counter = |
| 46 | + test_server_->counter(listenerStatPrefix("ssl.fail_verify_error")); |
| 47 | + EXPECT_EQ(value, counter->value()); |
| 48 | + counter->reset(); |
| 49 | +} |
| 50 | + |
| 51 | +INSTANTIATE_TEST_SUITE_P( |
| 52 | + IpVersionsClientVersions, SslSPIFFECertValidatorIntegrationTest, |
| 53 | + testing::Combine( |
| 54 | + testing::ValuesIn(TestEnvironment::getIpVersionsForTest()), |
| 55 | + testing::Values(envoy::extensions::transport_sockets::tls::v3::TlsParameters::TLSv1_2, |
| 56 | + envoy::extensions::transport_sockets::tls::v3::TlsParameters::TLSv1_3)), |
| 57 | + SslSPIFFECertValidatorIntegrationTest::ipClientVersionTestParamsToString); |
| 58 | + |
| 59 | +// clientcert.pem's san is "spiffe://lyft.com/frontend-team" so it should be accepted. |
| 60 | +TEST_P(SslSPIFFECertValidatorIntegrationTest, ServerRsaSPIFFEValidatorAccepted) { |
| 61 | + auto typed_conf = new envoy::config::core::v3::TypedExtensionConfig(); |
| 62 | + TestUtility::loadFromYaml(TestEnvironment::substitute(R"EOF( |
| 63 | +name: envoy.tls.cert_validator.spiffe |
| 64 | +typed_config: |
| 65 | + "@type": type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.SPIFFECertValidatorConfig |
| 66 | + trust_domains: |
| 67 | + - name: lyft.com |
| 68 | + trust_bundle: |
| 69 | + filename: "{{ test_rundir }}/test/config/integration/certs/cacert.pem" |
| 70 | + )EOF"), |
| 71 | + *typed_conf); |
| 72 | + |
| 73 | + custom_validator_config_ = typed_conf; |
| 74 | + ConnectionCreationFunction creator = [&]() -> Network::ClientConnectionPtr { |
| 75 | + return makeSslClientConnection({}); |
| 76 | + }; |
| 77 | + testRouterRequestAndResponseWithBody(1024, 512, false, false, &creator); |
| 78 | + checkVerifyErrorCouter(0); |
| 79 | +} |
| 80 | + |
| 81 | +// clientcert.pem's san is "spiffe://lyft.com/frontend-team" so it should be rejected. |
| 82 | +TEST_P(SslSPIFFECertValidatorIntegrationTest, ServerRsaSPIFFEValidatorRejected1) { |
| 83 | + auto typed_conf = new envoy::config::core::v3::TypedExtensionConfig(); |
| 84 | + TestUtility::loadFromYaml(TestEnvironment::substitute(R"EOF( |
| 85 | +name: envoy.tls.cert_validator.spiffe |
| 86 | +typed_config: |
| 87 | + "@type": type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.SPIFFECertValidatorConfig |
| 88 | + trust_domains: |
| 89 | + - name: example.com |
| 90 | + trust_bundle: |
| 91 | + filename: "{{ test_rundir }}/test/config/integration/certs/cacert.pem" |
| 92 | + )EOF"), |
| 93 | + *typed_conf); |
| 94 | + custom_validator_config_ = typed_conf; |
| 95 | + initialize(); |
| 96 | + auto conn = makeSslClientConnection({}); |
| 97 | + if (tls_version_ == envoy::extensions::transport_sockets::tls::v3::TlsParameters::TLSv1_2) { |
| 98 | + auto codec = makeRawHttpConnection(std::move(conn), absl::nullopt); |
| 99 | + EXPECT_FALSE(codec->connected()); |
| 100 | + } else { |
| 101 | + auto codec = makeHttpConnection(std::move(conn)); |
| 102 | + ASSERT_TRUE(codec->waitForDisconnect()); |
| 103 | + codec->close(); |
| 104 | + } |
| 105 | + checkVerifyErrorCouter(1); |
| 106 | +} |
| 107 | + |
| 108 | +// clientcert.pem's san is "spiffe://lyft.com/frontend-team" but the corresponding trust bundle does |
| 109 | +// not match with the client cert. So this should also be rejected. |
| 110 | +TEST_P(SslSPIFFECertValidatorIntegrationTest, ServerRsaSPIFFEValidatorRejected2) { |
| 111 | + auto typed_conf = new envoy::config::core::v3::TypedExtensionConfig(); |
| 112 | + TestUtility::loadFromYaml(TestEnvironment::substitute(R"EOF( |
| 113 | +name: envoy.tls.cert_validator.spiffe |
| 114 | +typed_config: |
| 115 | + "@type": type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.SPIFFECertValidatorConfig |
| 116 | + trust_domains: |
| 117 | + - name: lyft.com |
| 118 | + trust_bundle: |
| 119 | + filename: "{{ test_rundir }}/test/extensions/transport_sockets/tls/test_data/fake_ca_cert.pem" |
| 120 | + - name: example.com |
| 121 | + trust_bundle: |
| 122 | + filename: "{{ test_rundir }}/test/config/integration/certs/cacert.pem" |
| 123 | + )EOF"), |
| 124 | + *typed_conf); |
| 125 | + custom_validator_config_ = typed_conf; |
| 126 | + initialize(); |
| 127 | + auto conn = makeSslClientConnection({}); |
| 128 | + if (tls_version_ == envoy::extensions::transport_sockets::tls::v3::TlsParameters::TLSv1_2) { |
| 129 | + auto codec = makeRawHttpConnection(std::move(conn), absl::nullopt); |
| 130 | + EXPECT_FALSE(codec->connected()); |
| 131 | + } else { |
| 132 | + auto codec = makeHttpConnection(std::move(conn)); |
| 133 | + ASSERT_TRUE(codec->waitForDisconnect()); |
| 134 | + codec->close(); |
| 135 | + } |
| 136 | + checkVerifyErrorCouter(1); |
| 137 | +} |
| 138 | + |
| 139 | +} // namespace Ssl |
| 140 | +} // namespace Envoy |
0 commit comments