Skip to content

Commit 5ac92d9

Browse files
committed
update
Signed-off-by: Hang Yan <[email protected]>
1 parent 3436bfe commit 5ac92d9

File tree

5 files changed

+39
-62
lines changed

5 files changed

+39
-62
lines changed

pkg/antctl/antctl.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ $ antctl get podmulticaststats pod -n namespace`,
754754
{
755755
cobraCommand: packetcapture.Command,
756756
supportAgent: false,
757-
supportController: false,
757+
supportController: true,
758758
},
759759
{
760760
cobraCommand: proxy.Command,

pkg/antctl/raw/packetcapture/command.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,17 @@ func getFlowFields(flow string) (map[string]int, error) {
115115
return fields, nil
116116
}
117117

118+
func getPodFile(cmd *cobra.Command) (PodFileCopy, error) {
119+
config, client, _, err := getClients(cmd)
120+
if err != nil {
121+
return nil, err
122+
}
123+
return &podFile{
124+
restConfig: config,
125+
client: client,
126+
}, nil
127+
}
128+
118129
func getConfigAndClients(cmd *cobra.Command) (*rest.Config, kubernetes.Interface, antrea.Interface, error) {
119130
kubeConfig, err := raw.ResolveKubeconfig(cmd)
120131
if err != nil {
@@ -127,17 +138,6 @@ func getConfigAndClients(cmd *cobra.Command) (*rest.Config, kubernetes.Interface
127138
return kubeConfig, k8sClientset, client, nil
128139
}
129140

130-
func getPodFile(cmd *cobra.Command) (PodFileCopy, error) {
131-
config, client, _, err := getClients(cmd)
132-
if err != nil {
133-
return nil, err
134-
}
135-
return &podFile{
136-
restConfig: config,
137-
restInterface: client.CoreV1().RESTClient(),
138-
}, nil
139-
}
140-
141141
func getPCName(src, dest string) string {
142142
replace := func(s string) string {
143143
return strings.ReplaceAll(s, "/", "-")
@@ -215,7 +215,7 @@ func packetCaptureRunE(cmd *cobra.Command, args []string) error {
215215
splits := strings.Split(latestPC.Status.FilePath, ":")
216216
fileName := filepath.Base(splits[1])
217217
copier, _ := getCopier(cmd)
218-
if err := copier.CopyFromPod(context.TODO(), env.GetAntreaNamespace(), splits[0], "antrea-agent", splits[1], option.outputDir); err == nil {
218+
if err := copier.CopyFromPod(cmd.Context(), env.GetAntreaNamespace(), splits[0], "antrea-agent", splits[1], option.outputDir); err != nil {
219219
return err
220220
}
221221
fmt.Fprintf(cmd.OutOrStdout(), "Captured packets file: %s\n", filepath.Join(option.outputDir, fileName))

pkg/antctl/raw/packetcapture/command_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ func TestRun(t *testing.T) {
150150
getCopier = func(cmd *cobra.Command) (PodFileCopy, error) {
151151
return &testPodFile{}, nil
152152
}
153-
154153
defer func() {
155154
getClients = getConfigAndClients
156155
getCopier = getPodFile

pkg/antctl/raw/packetcapture/cp.go

+12-40
Original file line numberDiff line numberDiff line change
@@ -16,61 +16,33 @@ package packetcapture
1616

1717
import (
1818
"context"
19-
"io"
20-
"os"
21-
_ "unsafe"
19+
"fmt"
20+
"path/filepath"
21+
"strings"
2222

23-
corev1 "k8s.io/api/core/v1"
24-
"k8s.io/client-go/kubernetes/scheme"
23+
"k8s.io/client-go/kubernetes"
2524
"k8s.io/client-go/rest"
26-
"k8s.io/client-go/tools/remotecommand"
2725

26+
"antrea.io/antrea/pkg/antctl/raw/check"
2827
"antrea.io/antrea/pkg/util/compress"
28+
"antrea.io/antrea/pkg/util/env"
2929
)
3030

3131
type PodFileCopy interface {
3232
CopyFromPod(ctx context.Context, namespace, name, containerName, srcPath, dstDir string) error
3333
}
3434

3535
type podFile struct {
36-
restConfig *rest.Config
37-
restInterface rest.Interface
36+
restConfig *rest.Config
37+
client kubernetes.Interface
3838
}
3939

4040
func (p *podFile) CopyFromPod(ctx context.Context, namespace, name, containerName, srcPath, dstDir string) error {
41-
reader, outStream := io.Pipe()
42-
cmdArr := []string{"tar", "cf", "-", srcPath}
43-
req := p.restInterface.
44-
Get().
45-
Namespace(namespace).
46-
Resource("pods").
47-
Name(name).
48-
SubResource("exec").
49-
VersionedParams(&corev1.PodExecOptions{
50-
Container: containerName,
51-
Command: cmdArr,
52-
Stdin: true,
53-
Stdout: true,
54-
Stderr: true,
55-
TTY: false,
56-
}, scheme.ParameterCodec)
57-
58-
exec, err := remotecommand.NewSPDYExecutor(p.restConfig, "POST", req.URL())
41+
dir, fileName := filepath.Split(srcPath)
42+
cmdArr := []string{"/bin/sh", "-c", fmt.Sprintf("cd %s; tar cf - %s", dir, fileName)}
43+
output, _, err := check.ExecInPod(ctx, p.client, p.restConfig, env.GetAntreaNamespace(), name, "antrea-agent", cmdArr)
5944
if err != nil {
6045
return err
6146
}
62-
go func() {
63-
defer outStream.Close()
64-
err = exec.StreamWithContext(ctx, remotecommand.StreamOptions{
65-
Stdin: os.Stdin,
66-
Stdout: outStream,
67-
Stderr: os.Stderr,
68-
Tty: false,
69-
})
70-
if err != nil {
71-
panic(err)
72-
}
73-
}()
74-
err = compress.UnpackReader(defaultFS, reader, dstDir)
75-
return err
47+
return compress.UnpackReader(defaultFS, strings.NewReader(output), false, option.outputDir)
7648
}

pkg/util/compress/compress.go

+14-8
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131
// Sanitize archive file pathing from "G305: Zip Slip vulnerability"
3232
func sanitizeArchivePath(d, t string) (string, error) {
3333
v := filepath.Join(d, t)
34-
if strings.HasPrefix(v, filepath.Clean(d)) {
34+
if strings.HasPrefix(v, filepath.Clean(d)) || (filepath.Clean(d) == "." && v == t) {
3535
return v, nil
3636
}
3737
return "", fmt.Errorf("%s: %s", "content filepath is tainted", t)
@@ -43,15 +43,21 @@ func UnpackDir(fs afero.Fs, fileName string, targetDir string) error {
4343
return err
4444
}
4545
defer file.Close()
46-
return UnpackReader(fs, file, targetDir)
46+
return UnpackReader(fs, file, true, targetDir)
4747
}
4848

49-
func UnpackReader(fs afero.Fs, file io.Reader, targetDir string) error {
50-
reader, err := gzip.NewReader(file)
51-
if err != nil {
52-
return err
49+
func UnpackReader(fs afero.Fs, file io.Reader, useGzip bool, targetDir string) error {
50+
reader := file
51+
var err error
52+
var gzipReader *gzip.Reader
53+
if useGzip {
54+
gzipReader, err = gzip.NewReader(file)
55+
if err != nil {
56+
return err
57+
}
58+
defer gzipReader.Close()
59+
reader = gzipReader
5360
}
54-
defer reader.Close()
5561
tarReader := tar.NewReader(reader)
5662

5763
for true {
@@ -73,10 +79,10 @@ func UnpackReader(fs afero.Fs, file io.Reader, targetDir string) error {
7379
}
7480
case tar.TypeReg:
7581
outFile, err := fs.Create(targetPath)
76-
defer outFile.Close()
7782
if err != nil {
7883
return err
7984
}
85+
defer outFile.Close()
8086
for {
8187
// to resolve G110: Potential DoS vulnerability via decompression bomb
8288
if _, err := io.CopyN(outFile, tarReader, 1024); err != nil {

0 commit comments

Comments
 (0)