|
| 1 | +/* |
| 2 | + * Copyright 2018- The Pixie Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * SPDX-License-Identifier: Apache-2.0 |
| 17 | + */ |
| 18 | + |
| 19 | +#include <gmock/gmock.h> |
| 20 | +#include <gtest/gtest.h> |
| 21 | + |
| 22 | +#include <string> |
| 23 | + |
| 24 | +#include "src/common/base/base.h" |
| 25 | +#include "src/common/exec/exec.h" |
| 26 | +#include "src/common/testing/test_environment.h" |
| 27 | +#include "src/shared/types/column_wrapper.h" |
| 28 | +#include "src/shared/types/types.h" |
| 29 | +#include "src/stirling/core/data_table.h" |
| 30 | +#include "src/stirling/source_connectors/socket_tracer/socket_trace_connector.h" |
| 31 | +#include "src/stirling/source_connectors/socket_tracer/testing/container_images.h" |
| 32 | +#include "src/stirling/source_connectors/socket_tracer/testing/protocol_checkers.h" |
| 33 | +#include "src/stirling/source_connectors/socket_tracer/testing/socket_trace_bpf_test_fixture.h" |
| 34 | +#include "src/stirling/source_connectors/socket_tracer/uprobe_symaddrs.h" |
| 35 | +#include "src/stirling/testing/common.h" |
| 36 | + |
| 37 | +namespace px { |
| 38 | +namespace stirling { |
| 39 | + |
| 40 | +namespace mux = protocols::mux; |
| 41 | + |
| 42 | +using ::px::stirling::testing::EqHTTPRecord; |
| 43 | +using ::px::stirling::testing::EqMuxRecord; |
| 44 | +using ::px::stirling::testing::GetTargetRecords; |
| 45 | +using ::px::stirling::testing::SocketTraceBPFTestFixture; |
| 46 | +using ::px::stirling::testing::ToRecordVector; |
| 47 | + |
| 48 | +using ::testing::StrEq; |
| 49 | +using ::testing::UnorderedElementsAre; |
| 50 | + |
| 51 | +class ThriftMuxServerContainerWrapper : public ::px::stirling::testing::ThriftMuxServerContainer {}; |
| 52 | + |
| 53 | +// The Init() function is used to set flags for the entire test. |
| 54 | +// We can't do this in the MuxTraceTest constructor, because it will be too late |
| 55 | +// (SocketTraceBPFTestFixture will already have been constructed). |
| 56 | +bool Init() { |
| 57 | + // Make sure Mux tracing is enabled. |
| 58 | + FLAGS_stirling_enable_mux_tracing = true; |
| 59 | + |
| 60 | + // We turn off CQL and NATS tracing to give some BPF instructions back for Mux. |
| 61 | + // This is required for older kernels with only 4096 BPF instructions. |
| 62 | + FLAGS_stirling_enable_cass_tracing = false; |
| 63 | + FLAGS_stirling_enable_nats_tracing = false; |
| 64 | + |
| 65 | + // Enable the raw fptr fallback for determining ssl lib version. |
| 66 | + FLAGS_openssl_raw_fptrs_enabled = true; |
| 67 | + return true; |
| 68 | +} |
| 69 | + |
| 70 | +bool kInit = Init(); |
| 71 | + |
| 72 | +template <typename TServerContainer> |
| 73 | +class BaseOpenSSLTraceTest : public SocketTraceBPFTestFixture</* TClientSideTracing */ false> { |
| 74 | + protected: |
| 75 | + BaseOpenSSLTraceTest() { |
| 76 | + // Run the nginx HTTPS server. |
| 77 | + // The container runner will make sure it is in the ready state before unblocking. |
| 78 | + // Stirling will run after this unblocks, as part of SocketTraceBPFTestFixture SetUp(). |
| 79 | + StatusOr<std::string> run_result = |
| 80 | + server_.Run(std::chrono::seconds{60}, {}, {"--use-tls", "true"}); |
| 81 | + PL_CHECK_OK(run_result); |
| 82 | + PL_CHECK_OK(this->RunThriftMuxClient()); |
| 83 | + |
| 84 | + // Sleep an additional second, just to be safe. |
| 85 | + sleep(1); |
| 86 | + } |
| 87 | + |
| 88 | + StatusOr<int32_t> RunThriftMuxClient() { |
| 89 | + std::string classpath = |
| 90 | + "@/app/px/src/stirling/source_connectors/socket_tracer/testing/containers/thriftmux/" |
| 91 | + "server_image.classpath"; |
| 92 | + std::string thriftmux_client_output = "StringString"; |
| 93 | + |
| 94 | + // The thriftmux container uses a CA created by |
| 95 | + // src/common/testing/test_utils/cert_generator:cert_generator. |
| 96 | + // This command adds this CA to java's keystore so ssl |
| 97 | + // verification works as expected. |
| 98 | + std::string keytool_cmd = absl::Substitute( |
| 99 | + "docker exec $0 /usr/lib/jvm/java-11-openjdk-amd64/bin/keytool -importcert -keystore " |
| 100 | + "/etc/ssl/certs/java/cacerts -file /etc/ssl/ca.crt -noprompt -storepass changeit", |
| 101 | + server_.container_name()); |
| 102 | + px::Exec(keytool_cmd); |
| 103 | + |
| 104 | + // Runs the Client entrypoint inside the server container |
| 105 | + // capturing the Client process's pid for easier debugging. |
| 106 | + std::string cmd = absl::Substitute( |
| 107 | + "docker exec $0 /usr/bin/java -cp $1 Client --use-tls true & echo $$! && wait", |
| 108 | + server_.container_name(), classpath); |
| 109 | + PL_ASSIGN_OR_RETURN(std::string out, px::Exec(cmd)); |
| 110 | + |
| 111 | + LOG(INFO) << absl::Substitute("thriftmux client command output: '$0'", out); |
| 112 | + |
| 113 | + std::vector<std::string> tokens = absl::StrSplit(out, "\n"); |
| 114 | + |
| 115 | + int32_t client_pid; |
| 116 | + if (!absl::SimpleAtoi(tokens[0], &client_pid)) { |
| 117 | + return error::Internal("Could not extract PID."); |
| 118 | + } |
| 119 | + |
| 120 | + LOG(INFO) << absl::Substitute("Client PID: $0", client_pid); |
| 121 | + |
| 122 | + if (!absl::StrContains(out, thriftmux_client_output)) { |
| 123 | + return error::Internal( |
| 124 | + "Expected output from thriftmux to include '$0', received '$1' instead", |
| 125 | + thriftmux_client_output, out); |
| 126 | + } |
| 127 | + return client_pid; |
| 128 | + } |
| 129 | + |
| 130 | + TServerContainer server_; |
| 131 | +}; |
| 132 | + |
| 133 | +mux::Record RecordWithType(mux::Type req_type) { |
| 134 | + mux::Record r = {}; |
| 135 | + r.req.type = static_cast<int8_t>(req_type); |
| 136 | + |
| 137 | + return r; |
| 138 | +} |
| 139 | + |
| 140 | +using NettyTLSServerImplementations = ::testing::Types<ThriftMuxServerContainerWrapper>; |
| 141 | + |
| 142 | +template <typename T> |
| 143 | +using NettyTLSTraceTest = BaseOpenSSLTraceTest<T>; |
| 144 | + |
| 145 | +TYPED_TEST_SUITE(NettyTLSTraceTest, NettyTLSServerImplementations); |
| 146 | + |
| 147 | +TYPED_TEST(NettyTLSTraceTest, mtls_thriftmux_client) { |
| 148 | + // Uncomment to enable tracing: |
| 149 | + // FLAGS_stirling_conn_trace_pid = this->server_.process_pid(); |
| 150 | + this->StartTransferDataThread(); |
| 151 | + |
| 152 | + ASSERT_OK(this->RunThriftMuxClient()); |
| 153 | + |
| 154 | + this->StopTransferDataThread(); |
| 155 | + |
| 156 | + std::vector<TaggedRecordBatch> tablets = this->ConsumeRecords(SocketTraceConnector::kMuxTableNum); |
| 157 | + ASSERT_NOT_EMPTY_AND_GET_RECORDS(const types::ColumnWrapperRecordBatch& record_batch, tablets); |
| 158 | + std::vector<mux::Record> server_records = |
| 159 | + GetTargetRecords<mux::Record>(record_batch, this->server_.process_pid()); |
| 160 | + |
| 161 | + mux::Record tinitCheck = RecordWithType(mux::Type::kRerrOld); |
| 162 | + mux::Record tinit = RecordWithType(mux::Type::kTinit); |
| 163 | + mux::Record pingRecord = RecordWithType(mux::Type::kTping); |
| 164 | + mux::Record dispatchRecord = RecordWithType(mux::Type::kTdispatch); |
| 165 | + |
| 166 | + EXPECT_THAT(server_records, Contains(EqMuxRecord(tinitCheck))); |
| 167 | + EXPECT_THAT(server_records, Contains(EqMuxRecord(tinit))); |
| 168 | + EXPECT_THAT(server_records, Contains(EqMuxRecord(pingRecord))); |
| 169 | + EXPECT_THAT(server_records, Contains(EqMuxRecord(dispatchRecord))); |
| 170 | +} |
| 171 | + |
| 172 | +} // namespace stirling |
| 173 | +} // namespace px |