GeezerVison is showing github.com — Update thriftmux test container to support tls via a command line flag by ddelnano · Pull Request #413 · pixie-io/pixie
Update thriftmux test container to support tls via a command line flag
#413
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and
privacy statement . We’ll occasionally send you account related emails.
Already on GitHub?
Sign in
to your account
Uh oh!
There was an error while loading. Please reload this page.
Show file tree
Hide file tree
Changes from all commits
Commits
File filter
Filter by extension
.bazel
(1)
.bzl
(2)
.cc
(1)
.go
(1)
.md
(1)
.scala
(2)
.txt
(1)
All 7 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Hide whitespace
Diff view
Hide whitespace
There are no files selected for viewing
Original file line number
Diff line number
Diff line change
Expand Up
@@ -52,6 +52,11 @@ def _package_manager_setup():
"libsasl2-2",
"libssl1.1",
"libgcc1",
"libcrypt1",
# TODO(ddelnano): byteman is required until dealing with
# netty's libnetty_tcnative.so's random file name is solved for.
# See #407 for more details.
"libbyteman-java",
Comment thread
ddelnano marked this conversation as resolved.
Resolved
Uh oh!
There was an error while loading. Please reload this page.
],
sources = ["@debian_sid//file:Packages.json"],
)
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -31,6 +31,9 @@ def thrift_deps(scala_version):
"com.twitter:scrooge-core_%s:%s" % (scala_minor_version, finagle_version),
"com.twitter:finagle-http_%s:%s" % (scala_minor_version, finagle_version),
"org.apache.thrift:libthrift:0.10.0",
"org.slf4j:slf4j-api:1.7.36",
"ch.qos.logback:logback-core:1.2.10",
"ch.qos.logback:logback-classic:1.2.10",
],
repositories = ["https://repo1.maven.org/maven2"],
)
Original file line number
Diff line number
Diff line change
Expand Up
@@ -24,6 +24,7 @@ import (
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"math/big"
"os"
"time"
Expand Down
Expand Up
@@ -114,9 +115,28 @@ func generateAndWriteCertPair(ca *x509.Certificate, caKey *rsa.PrivateKey, certP
if err != nil {
panic(err)
}
err = pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)})
if err != nil {
panic(err)
keyType := viper.GetString("secret_key_type")
if keyType == "pkcs1" {
err = pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)})
if err != nil {
panic(err)
}
} else if keyType == "pkcs8" {
b, err := x509.MarshalPKCS8PrivateKey(privateKey)
if err != nil {
panic(err)
}
err = pem.Encode(keyOut, &pem.Block{Type: "PRIVATE KEY", Bytes: b})
if err != nil {
panic(err)
}
} else {
panic(fmt.Sprintf("Unsupported private key type: %s"))
}
err = keyOut.Close()
if err != nil {
Expand All
@@ -130,6 +150,7 @@ func main() {
pflag.String("client_key", "", "Path where to write client.key")
pflag.String("server_crt", "", "Path where to write server.crt")
pflag.String("server_key", "", "Path where to write server.key")
pflag.String("secret_key_type", "pkcs1", "Which private key type to use. Possible options include pkcs1, pkcs8")
pflag.Parse()
viper.BindPFlags(pflag.CommandLine)
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -102,10 +102,10 @@ class MuxTraceTest : public SocketTraceBPFTest</* TClientSideTracing */ true> {
LOG(INFO) << absl::StrFormat("Client PID: %d", client_pid);
if (tokens[1] != thriftmux_client_output) {
if (!absl::StrContains(out, thriftmux_client_output) ) {
return error::Internal(
absl::StrFormat("Expected output from thriftmux to be '%s', received '%s' instead",
thriftmux_client_output, tokens[1] ));
absl::StrFormat("Expected output from thriftmux to include '%s', received '%s' instead",
thriftmux_client_output, out ));
}
return client_pid;
}
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -14,13 +14,67 @@
#
# SPDX-License-Identifier: Apache-2.0
load("@package_bundle//file:packages.bzl", "packages")
load("@io_bazel_rules_docker//java:image.bzl", "DEFAULT_JAVA_BASE", "jar_app_layer")
load("@bazel_tools//tools/build_defs/pkg:pkg.bzl", "pkg_tar")
load("@io_bazel_rules_docker//container:container.bzl", "container_image", "container_layer")
load("@io_bazel_rules_docker//scala:image.bzl", "scala_image")
load("@io_bazel_rules_scala//scala:scala.bzl", "scala_binary", "scala_library")
load("@io_bazel_rules_scala//thrift:thrift.bzl", "thrift_library")
load("@io_bazel_rules_scala//twitter_scrooge:twitter_scrooge.bzl", "scrooge_scala_library")
package(default_visibility = ["//src/stirling:__subpackages__"])
genrule(
name = "certs",
outs = [
"ca.crt",
"client.crt",
"client.key",
"server.crt",
"server.key",
],
cmd = """
$(location //src/common/testing/test_utils/cert_generator:cert_generator) \
--ca_crt $(location ca.crt) \
--client_crt $(location client.crt) \
--client_key $(location client.key) \
--server_crt $(location server.crt) \
--server_key $(location server.key) \
--secret_key_type pkcs8
""",
tools = [
"//src/common/testing/test_utils/cert_generator",
],
)
pkg_tar(
name = "ssl_keys",
srcs = [
"ca.crt",
"client.crt",
"client.key",
"server.crt",
"server.key",
],
mode = "0755",
package_dir = "/etc/ssl",
strip_prefix = "/src/stirling/source_connectors/socket_tracer/testing/containers/thriftmux",
)
# TODO(ddelnano): byteman is required until dealing with
# netty's libnetty_tcnative.so's random file name is solved for.
# See #407 for more details.
pkg_tar(
name = "byteman_rule",
srcs = [
"byteman_rule.txt",
],
mode = "0755",
package_dir = "/etc/byteman",
strip_prefix = "/src/stirling/source_connectors/socket_tracer/testing/containers/thriftmux",
)
thrift_library(
name = "thrift_library",
srcs = glob(["**/*.thrift"]),
Expand Down
Expand Up
@@ -54,6 +108,15 @@ scrooge_scala_library(
],
)
scala_library(
name = "logging",
deps = [
"@maven//:ch_qos_logback_logback_classic",
"@maven//:ch_qos_logback_logback_core",
"@maven//:org_slf4j_slf4j_api",
],
)
scala_binary(
name = "server_bin",
srcs = glob(["**/*.scala"]),
Expand All
@@ -72,11 +135,31 @@ scala_binary(
],
)
container_image(
name = "thriftmux_base",
base = DEFAULT_JAVA_BASE,
debs = [
packages["libbyteman-java"],
packages["libcrypt1"],
],
tars = [
":ssl_keys",
":byteman_rule",
],
)
scala_image(
name = "server_image",
srcs = glob(["**/*.scala"]),
base = ":thriftmux_base",
jvm_flags = [
"-javaagent:/usr/share/java/byteman-4.0.11.jar=script:/etc/byteman/byteman_rule.txt,boot:/usr/share/java/byteman-4.0.11.jar",
"-Dorg.jboss.byteman.transform.all",
"-Dio.netty.native.deleteLibAfterLoading=false",
],
main_class = "Server",
deps = [
":logging",
":thriftmux_scrooge",
],
)
Original file line number
Diff line number
Diff line change
Expand Up
@@ -11,7 +11,15 @@ This container can be run as a thriftmux server or client.
$ bazel run src/stirling/source_connectors/socket_tracer/testing/containers/thriftmux:server_image
# Run the client. This requires using 'docker run' directly to override the default entrypoint
$ docker run --entrypoint /usr/bin/java bazel/src/stirling/source_connectors/socket_tracer/testing/containers/thriftmux:server_image -cp @/app/px/src/stirling/source_connectors/socket_tracer/testing/containers/thriftmux/server_image.classpath Client
$ docker exec -it ${container_id} /usr/bin/java -cp @/app/px/src/stirling/source_connectors/socket_tracer/testing/containers/thriftmux/server_image.classpath Client
# Run the server and client with TLS enabled
$ bazel run src/stirling/source_connectors/socket_tracer/testing/containers/thriftmux:server_image -- --use-tls true
# Add TLS certs to java keystore
docker exec -it ${container_id} /usr/lib/jvm/java-11-openjdk-amd64/bin/keytool -importcert -keystore /etc/ssl/certs/java/cacerts -file /etc/ssl/ca.crt -noprompt -storepass changeit
$ docker exec -it ${container_id} /usr/bin/java -cp @/app/px/src/stirling/source_connectors/socket_tracer/testing/containers/thriftmux/server_image.classpath Client --use-tls true
```
See `mux_container_bpf_test.cc` for use case.
Original file line number
Diff line number
Diff line change
@@ -0,0 +1,9 @@
RULE Create predictable dynamic library path for tcnative
CLASS io.netty.util.internal.PlatformDependent
METHOD createTempFile
AT ENTRY
BIND filePath:String = $1;
isTcNativeLib:boolean = filePath.contains("tcnative");
IF isTcNativeLib
DO return new File("/tmp/libnetty_tcnative_linux_x86.so")
ENDRULE
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,39 @@
import com.twitter.util.{Await, Future}
import com.twitter.finagle.thriftmux.thriftscala.TestService
import com.twitter.finagle.ssl._
import com.twitter.finagle.ssl.client.SslClientConfiguration
import com.twitter.finagle.{Thrift, ThriftMux}
object Client extends App {
val stackClient = ThriftMux.client.withLabel("thriftmux_example")
val svcPerEndpoint = stackClient
.methodBuilder("inet!localhost:8080")
.servicePerEndpoint[TestService.ServicePerEndpoint]
val svc = ThriftMux.Client.methodPerEndpoint[TestService.ServicePerEndpoint, TestService.MethodPerEndpoint](svcPerEndpoint)
val r = Await.result(svc.query("String"))
println(r)
import java.io.File;
object Client {
def main(args: Array[String]): Unit = {
var useTls = false
args.grouped(2).toList.collect {
case Array("--use-tls", tls: String) => useTls = tls.toBoolean
}
val keyCredentials = KeyCredentials.CertAndKey(
new File("/etc/ssl/client.crt"),
new File("/etc/ssl/client.key"),
)
val sslConfig = SslClientConfiguration(
None,
keyCredentials,
)
var stackClient = if (useTls) {
ThriftMux.client
.withLabel("thriftmux_example")
.withTransport
.tls(sslConfig)
} else {
ThriftMux.client
.withLabel("thriftmux_example")
}
val svcPerEndpoint = stackClient
.methodBuilder("inet!localhost:8080")
.servicePerEndpoint[TestService.ServicePerEndpoint]
val svc = ThriftMux.Client.methodPerEndpoint[TestService.ServicePerEndpoint, TestService.MethodPerEndpoint](svcPerEndpoint)
val r = Await.result(svc.query("String"))
println(r)
}
}
Original file line number
Diff line number
Diff line change
@@ -1,21 +1,42 @@
import com.twitter.finagle.{Http, Service}
import com.twitter.finagle.http
import com.twitter.finagle.{Thrift, ThriftMux}
import com.twitter.finagle.ssl._
import com.twitter.finagle.ssl.server.SslServerConfiguration
import com.twitter.util.{Await, Future}
import java.net.{InetSocketAddress, InetAddress}
import com.twitter.finagle.thriftmux.thriftscala.TestService
import java.io.File;
object Server {
def main(args: Array[String]): Unit = {
val server = ThriftMux.server
.serveIface(
new InetSocketAddress(InetAddress.getLoopbackAddress, 8080),
new TestService.MethodPerEndpoint {
def query(x: String): Future[String] = Future.value(x + x)
def question(y: String): Future[String] = Future.value(y + y)
def inquiry(z: String): Future[String] = Future.value(z + z)
},
)
var useTls = false
args.grouped(2).toList.collect {
case Array("--use-tls", tls: String) => useTls = tls.toBoolean
}
val keyCredentials = KeyCredentials.CertAndKey(
new File("/etc/ssl/server.crt"),
new File("/etc/ssl/server.key"),
)
val sslConfig = SslServerConfiguration(
keyCredentials,
)
val addr = new InetSocketAddress(InetAddress.getLoopbackAddress, 8080)
val testSvc = new TestService.MethodPerEndpoint {
def query(x: String): Future[String] = Future.value(x + x)
def question(y: String): Future[String] = Future.value(y + y)
def inquiry(z: String): Future[String] = Future.value(z + z)
}
var server = if (useTls) {
ThriftMux.server
.withTransport
.tls(sslConfig)
.serveIface(addr, testSvc)
} else {
ThriftMux.server
.serveIface(addr, testSvc)
}
Await.ready(server)
}
}
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.