Remove pcap command and make it an option under tap command

Also copy TAR to worker container.
This commit is contained in:
M. Mert Yildiran
2022-12-28 01:40:47 +03:00
parent d9e53b20c1
commit 7c81369e1a
5 changed files with 36 additions and 22 deletions

View File

@@ -1,18 +0,0 @@
package cmd
import (
"github.com/spf13/cobra"
)
var pcapCmd = &cobra.Command{
Use: "pcap",
Short: "Capture from a PCAP file using your Docker Daemon instead of Kubernetes.",
RunE: func(cmd *cobra.Command, args []string) error {
pcap()
return nil
},
}
func init() {
rootCmd.AddCommand(pcapCmd)
}

View File

@@ -55,6 +55,7 @@ func init() {
tapCmd.Flags().BoolP(configStructs.AllNamespacesLabel, "A", defaultTapConfig.AllNamespaces, "Tap all namespaces.") tapCmd.Flags().BoolP(configStructs.AllNamespacesLabel, "A", defaultTapConfig.AllNamespaces, "Tap all namespaces.")
tapCmd.Flags().String(configStructs.HumanMaxEntriesDBSizeLabel, defaultTapConfig.HumanMaxEntriesDBSize, "Override the default max entries db size.") tapCmd.Flags().String(configStructs.HumanMaxEntriesDBSizeLabel, defaultTapConfig.HumanMaxEntriesDBSize, "Override the default max entries db size.")
tapCmd.Flags().Bool(configStructs.DryRunLabel, defaultTapConfig.DryRun, "Preview of all pods matching the regex, without tapping them.") tapCmd.Flags().Bool(configStructs.DryRunLabel, defaultTapConfig.DryRun, "Preview of all pods matching the regex, without tapping them.")
tapCmd.Flags().StringP(configStructs.PcapLabel, "p", defaultTapConfig.Pcap, "Capture from a PCAP snapshot of Kubeshark (.tar.gz) using your Docker Daemon instead of Kubernetes.")
tapCmd.Flags().Bool(configStructs.ServiceMeshLabel, defaultTapConfig.ServiceMesh, "Capture the encrypted traffic if the cluster is configured with a service mesh and with mTLS.") tapCmd.Flags().Bool(configStructs.ServiceMeshLabel, defaultTapConfig.ServiceMesh, "Capture the encrypted traffic if the cluster is configured with a service mesh and with mTLS.")
tapCmd.Flags().Bool(configStructs.TlsLabel, defaultTapConfig.Tls, "Capture the traffic that's encrypted with OpenSSL or Go crypto/tls libraries.") tapCmd.Flags().Bool(configStructs.TlsLabel, defaultTapConfig.Tls, "Capture the traffic that's encrypted with OpenSSL or Go crypto/tls libraries.")
tapCmd.Flags().Bool(configStructs.DebugLabel, defaultTapConfig.Debug, "Enable the debug mode.") tapCmd.Flags().Bool(configStructs.DebugLabel, defaultTapConfig.Debug, "Enable the debug mode.")

View File

@@ -6,6 +6,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"os"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/container"
@@ -83,7 +84,14 @@ func pullImages(ctx context.Context, cli *client.Client, imageFront string, imag
return nil return nil
} }
func createAndStartContainers(ctx context.Context, cli *client.Client, imageFront string, imageHub string, imageWorker string) ( func createAndStartContainers(
ctx context.Context,
cli *client.Client,
imageFront string,
imageHub string,
imageWorker string,
pcapReader io.Reader,
) (
respFront container.ContainerCreateCreatedBody, respFront container.ContainerCreateCreatedBody,
respHub container.ContainerCreateCreatedBody, respHub container.ContainerCreateCreatedBody,
respWorker container.ContainerCreateCreatedBody, respWorker container.ContainerCreateCreatedBody,
@@ -163,6 +171,10 @@ func createAndStartContainers(ctx context.Context, cli *client.Client, imageFron
return return
} }
if err = cli.CopyToContainer(ctx, respWorker.ID, "/app/import", pcapReader, types.CopyToContainerOptions{}); err != nil {
return
}
var containerWorker types.ContainerJSON var containerWorker types.ContainerJSON
containerWorker, err = cli.ContainerInspect(ctx, respWorker.ID) containerWorker, err = cli.ContainerInspect(ctx, respWorker.ID)
if err != nil { if err != nil {
@@ -210,8 +222,9 @@ func stopAndRemoveContainers(
return return
} }
func pcap() { func pcap(pcapPath string) {
log.Info().Msg("Starting Docker containers...") docker.SetRegistry(config.Config.Tap.DockerRegistry)
docker.SetTag(config.Config.Tap.DockerTag)
ctx := context.Background() ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
@@ -231,7 +244,18 @@ func pcap() {
return return
} }
respFront, respHub, respWorker, workerIPAddr, err := createAndStartContainers(ctx, cli, imageFront, imageHub, imageWorker) pcapFile, err := os.Open(pcapPath)
defer pcapFile.Close()
pcapReader := bufio.NewReader(pcapFile)
respFront, respHub, respWorker, workerIPAddr, err := createAndStartContainers(
ctx,
cli,
imageFront,
imageHub,
imageWorker,
pcapReader,
)
if err != nil { if err != nil {
log.Error().Err(err).Send() log.Error().Err(err).Send()
return return

View File

@@ -42,6 +42,11 @@ func tap() {
state.startTime = time.Now() state.startTime = time.Now()
docker.SetRegistry(config.Config.Tap.DockerRegistry) docker.SetRegistry(config.Config.Tap.DockerRegistry)
docker.SetTag(config.Config.Tap.DockerTag) docker.SetTag(config.Config.Tap.DockerTag)
log.Info().Str("registry", docker.GetRegistry()).Str("tag", docker.GetTag()).Msg("Using Docker:")
if config.Config.Tap.Pcap != "" {
pcap(config.Config.Tap.Pcap)
return
}
connector = connect.NewConnector(kubernetes.GetLocalhostOnPort(config.Config.Tap.Hub.SrcPort), connect.DefaultRetries, connect.DefaultTimeout) connector = connect.NewConnector(kubernetes.GetLocalhostOnPort(config.Config.Tap.Hub.SrcPort), connect.DefaultRetries, connect.DefaultTimeout)

View File

@@ -18,6 +18,7 @@ const (
AllNamespacesLabel = "all-namespaces" AllNamespacesLabel = "all-namespaces"
HumanMaxEntriesDBSizeLabel = "max-entries-db-size" HumanMaxEntriesDBSizeLabel = "max-entries-db-size"
DryRunLabel = "dry-run" DryRunLabel = "dry-run"
PcapLabel = "pcap"
ServiceMeshLabel = "service-mesh" ServiceMeshLabel = "service-mesh"
TlsLabel = "tls" TlsLabel = "tls"
DebugLabel = "debug" DebugLabel = "debug"
@@ -50,6 +51,7 @@ type TapConfig struct {
AllNamespaces bool `yaml:"all-namespaces" default:"false"` AllNamespaces bool `yaml:"all-namespaces" default:"false"`
HumanMaxEntriesDBSize string `yaml:"max-entries-db-size" default:"200MB"` HumanMaxEntriesDBSize string `yaml:"max-entries-db-size" default:"200MB"`
DryRun bool `yaml:"dry-run" default:"false"` DryRun bool `yaml:"dry-run" default:"false"`
Pcap string `yaml:"pcap" default:""`
HubResources models.Resources `yaml:"hub-resources"` HubResources models.Resources `yaml:"hub-resources"`
WorkerResources models.Resources `yaml:"worker-resources"` WorkerResources models.Resources `yaml:"worker-resources"`
ServiceMesh bool `yaml:"service-mesh" default:"true"` ServiceMesh bool `yaml:"service-mesh" default:"true"`