GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Implement changes to get finagle tls tracing working. · k8sstormcenter/pixie@c785ca5

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src/stirling/source_connectors/socket_tracer
    • BUILD.bazel
    • mux_trace_bpf_test.cc
    • netty_tls_trace_bpf_test.cc
    • socket_trace_connector.cc
    • uprobe_manager.cc
    • uprobe_symaddrs.cc
    • uprobe_symaddrs.h
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/stirling/source_connectors/socket_tracer/BUILD.bazel‎

Copy file name to clipboardExpand all lines: src/stirling/source_connectors/socket_tracer/BUILD.bazel
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,25 @@ pl_cc_test(
447447
],
448448
)
449449

450+
pl_cc_test(
451+
name = "netty_tls_trace_bpf_test",
452+
timeout = "long",
453+
srcs = ["netty_tls_trace_bpf_test.cc"],
454+
flaky = True,
455+
shard_count = 2,
456+
tags = [
457+
"no_asan",
458+
"requires_bpf",
459+
],
460+
deps = [
461+
":cc_library",
462+
"//src/common/testing/test_utils:cc_library",
463+
"//src/stirling/source_connectors/socket_tracer/testing:cc_library",
464+
"//src/stirling/source_connectors/socket_tracer/testing:container_images",
465+
"//src/stirling/testing:cc_library",
466+
],
467+
)
468+
450469
pl_cc_test(
451470
name = "dyn_lib_trace_bpf_test",
452471
timeout = "moderate",
Collapse file

‎src/stirling/source_connectors/socket_tracer/mux_trace_bpf_test.cc‎

Copy file name to clipboardExpand all lines: src/stirling/source_connectors/socket_tracer/mux_trace_bpf_test.cc
-9Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,6 @@ mux::Record RecordWithType(mux::Type req_type) {
140140
//-----------------------------------------------------------------------------
141141

142142
TEST_F(MuxTraceTest, Capture) {
143-
// If kernel version is older than 5.2, we turn off some protocol tracers due to instruction
144-
// limits.
145-
constexpr uint32_t kLinux5p2VersionCode = 328192;
146-
auto kernel_version = utils::GetKernelVersion();
147-
if (!kernel_version.ok() || kernel_version.ConsumeValueOrDie().code() < kLinux5p2VersionCode) {
148-
VLOG(1) << "Mux Tracing is turned off for kernel version older than 5.2.";
149-
return;
150-
}
151-
152143
StartTransferDataThread();
153144

154145
ASSERT_OK(RunThriftMuxClient());
Collapse file

‎src/stirling/source_connectors/socket_tracer/netty_tls_trace_bpf_test.cc‎

Copy file name to clipboard
+173Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
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
Collapse file

‎src/stirling/source_connectors/socket_tracer/socket_trace_connector.cc‎

Copy file name to clipboardExpand all lines: src/stirling/source_connectors/socket_tracer/socket_trace_connector.cc
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,7 @@ DEFINE_int32(stirling_enable_nats_tracing, px::stirling::TraceMode::On,
9595
"If true, stirling will trace and process NATS messages.");
9696
DEFINE_int32(stirling_enable_kafka_tracing, px::stirling::TraceMode::On,
9797
"If true, stirling will trace and process Kafka messages.");
98-
DEFINE_int32(stirling_enable_mux_tracing,
99-
gflags::Uint32FromEnv("PL_STIRLING_TRACER_ENABLE_MUX", px::stirling::TraceMode::Off),
98+
DEFINE_int32(stirling_enable_mux_tracing, px::stirling::TraceMode::OnForNewerKernel,
10099
"If true, stirling will trace and process Mux messages.");
101100
DEFINE_int32(stirling_enable_amqp_tracing, px::stirling::TraceMode::On,
102101
"If true, stirling will trace and process AMQP messages.");
Collapse file

‎src/stirling/source_connectors/socket_tracer/uprobe_manager.cc‎

Copy file name to clipboardExpand all lines: src/stirling/source_connectors/socket_tracer/uprobe_manager.cc
+64-34Lines changed: 64 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <algorithm>
2929
#include <filesystem>
3030
#include <map>
31+
#include <tuple>
3132

3233
#include "src/common/base/base.h"
3334
#include "src/common/base/utils.h"
@@ -282,53 +283,82 @@ StatusOr<std::vector<std::filesystem::path>> FindHostPathForPIDLibs(
282283
HostPathForPIDPathSearchType::kSearchTypeEndsWith);
283284
}
284285

286+
// SSLLibMatcher allows customizing the search of shared object files
287+
// that need to be traced with the SSL_write and SSL_read uprobes.
288+
// In dynamically linked cases, it's likely that there are two
289+
// shared libraries (libssl and libcrypto). In constrast, statically
290+
// linked cases are contained within the same binary.
291+
struct SSLLibMatcher {
292+
std::string_view libssl;
293+
std::string_view libcrypto;
294+
HostPathForPIDPathSearchType search_type;
295+
};
296+
297+
static constexpr const auto kLibSSLMatchers = MakeArray<SSLLibMatcher>({
298+
SSLLibMatcher{
299+
.libssl = "libssl.so.1.1",
300+
.libcrypto = "libcrypto.so.1.1",
301+
.search_type = HostPathForPIDPathSearchType::kSearchTypeEndsWith,
302+
},
303+
SSLLibMatcher{
304+
.libssl = kLibNettyTcnativePrefix,
305+
.libcrypto = kLibNettyTcnativePrefix,
306+
.search_type = HostPathForPIDPathSearchType::kSearchTypeContains,
307+
},
308+
});
309+
285310
// Return error if something unexpected occurs.
286311
// Return 0 if nothing unexpected, but there is nothing to deploy (e.g. no OpenSSL detected).
287312
StatusOr<int> UProbeManager::AttachOpenSSLUProbesOnDynamicLib(uint32_t pid) {
288-
constexpr std::string_view kLibSSL = "libssl.so.1.1";
289-
constexpr std::string_view kLibCrypto = "libcrypto.so.1.1";
290-
const std::vector<std::string_view> lib_names = {kLibSSL, kLibCrypto};
291-
292313
const system::Config& sysconfig = system::Config::GetInstance();
293314

294-
// Find paths to libssl.so and libcrypto.so for the pid, if they are in use (i.e. mapped).
295-
PL_ASSIGN_OR_RETURN(const std::vector<std::filesystem::path> container_lib_paths,
296-
FindHostPathForPIDLibs(lib_names, pid, proc_parser_.get(), &fp_resolver_));
315+
for (auto ssl_library_match : kLibSSLMatchers) {
316+
const auto libssl = ssl_library_match.libssl;
317+
const auto libcrypto = ssl_library_match.libcrypto;
297318

298-
std::filesystem::path container_libssl = container_lib_paths[0];
299-
std::filesystem::path container_libcrypto = container_lib_paths[1];
319+
const std::vector<std::string_view> lib_names = {libssl, libcrypto};
320+
const auto search_type = ssl_library_match.search_type;
300321

301-
if (container_libssl.empty() || container_libcrypto.empty()) {
302-
// Looks like this process doesn't have dynamic OpenSSL library installed, because it did not
303-
// map both of libssl.so.x.x & libcrypto.so.x.x.
304-
// Return "0" to indicate zero probes were attached. This is not an error.
305-
return 0;
306-
}
322+
// Find paths to libssl.so and libcrypto.so for the pid, if they are in use (i.e. mapped).
323+
PL_ASSIGN_OR_RETURN(
324+
const std::vector<std::filesystem::path> container_lib_paths,
325+
FindHostPathForPIDLibs(lib_names, pid, proc_parser_.get(), &fp_resolver_, search_type));
307326

308-
// Convert to host path, in case we're running inside a container ourselves.
309-
container_libssl = sysconfig.ToHostPath(container_libssl);
310-
container_libcrypto = sysconfig.ToHostPath(container_libcrypto);
327+
std::filesystem::path container_libssl = container_lib_paths[0];
328+
std::filesystem::path container_libcrypto = container_lib_paths[1];
311329

312-
if (!fs::Exists(container_libssl)) {
313-
return error::Internal("libssl not found [path = $0]", container_libssl.string());
314-
}
315-
if (!fs::Exists(container_libcrypto)) {
316-
return error::Internal("libcrypto not found [path = $0]", container_libcrypto.string());
317-
}
330+
if ((container_libssl.empty() || container_libcrypto.empty())) {
331+
// Looks like this process doesn't have dynamic OpenSSL library installed, because it did not
332+
// map both of libssl.so.x.x & libcrypto.so.x.x or another compatible library.
333+
// Move on to the next possible SSL library. This is not an error.
334+
continue;
335+
}
336+
337+
// Convert to host path, in case we're running inside a container ourselves.
338+
container_libssl = sysconfig.ToHostPath(container_libssl);
339+
container_libcrypto = sysconfig.ToHostPath(container_libcrypto);
318340

319-
auto fptr_manager = std::make_unique<obj_tools::RawFptrManager>(container_libcrypto);
341+
if (!fs::Exists(container_libssl)) {
342+
return error::Internal("libssl not found [path = $0]", container_libssl.string());
343+
}
344+
if (!fs::Exists(container_libcrypto)) {
345+
return error::Internal("libcrypto not found [path = $0]", container_libcrypto.string());
346+
}
320347

321-
PL_RETURN_IF_ERROR(UpdateOpenSSLSymAddrs(fptr_manager.get(), container_libcrypto, pid));
348+
auto fptr_manager = std::make_unique<obj_tools::RawFptrManager>(container_libcrypto);
322349

323-
// Only try probing .so files that we haven't already set probes on.
324-
auto result = openssl_probed_binaries_.insert(container_libssl);
325-
if (!result.second) {
326-
return 0;
327-
}
350+
PL_RETURN_IF_ERROR(UpdateOpenSSLSymAddrs(fptr_manager.get(), container_libcrypto, pid));
328351

329-
for (auto spec : kOpenSSLUProbes) {
330-
spec.binary_path = container_libssl.string();
331-
PL_RETURN_IF_ERROR(LogAndAttachUProbe(spec));
352+
// Only try probing .so files that we haven't already set probes on.
353+
auto result = openssl_probed_binaries_.insert(container_libssl);
354+
if (!result.second) {
355+
return 0;
356+
}
357+
358+
for (auto spec : kOpenSSLUProbes) {
359+
spec.binary_path = container_libssl.string();
360+
PL_RETURN_IF_ERROR(LogAndAttachUProbe(spec));
361+
}
332362
}
333363
return kOpenSSLUProbes.size();
334364
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.