Skip to content

Commit e44de62

Browse files
committed
update
Signed-off-by: Hang Yan <[email protected]>
1 parent 18cc669 commit e44de62

File tree

6 files changed

+65
-85
lines changed

6 files changed

+65
-85
lines changed

pkg/antctl/antctl.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ $ antctl get podmulticaststats pod -n namespace`,
753753
},
754754
{
755755
cobraCommand: packetcapture.Command,
756-
supportAgent: true,
756+
supportAgent: false,
757757
supportController: true,
758758
},
759759
{

pkg/antctl/command_list_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func TestGetDebugCommands(t *testing.T) {
7070
{
7171
name: "Antctl running against agent mode",
7272
mode: "agent",
73-
expected: [][]string{{"version"}, {"get", "podmulticaststats"}, {"log-level"}, {"get", "networkpolicy"}, {"get", "appliedtogroup"}, {"get", "addressgroup"}, {"get", "agentinfo"}, {"get", "podinterface"}, {"get", "ovsflows"}, {"trace-packet"}, {"get", "serviceexternalip"}, {"get", "memberlist"}, {"get", "bgppolicy"}, {"get", "bgppeers"}, {"get", "bgproutes"}, {"supportbundle"}, {"traceflow"}, {"packetcapture"}, {"get", "featuregates"}},
73+
expected: [][]string{{"version"}, {"get", "podmulticaststats"}, {"log-level"}, {"get", "networkpolicy"}, {"get", "appliedtogroup"}, {"get", "addressgroup"}, {"get", "agentinfo"}, {"get", "podinterface"}, {"get", "ovsflows"}, {"trace-packet"}, {"get", "serviceexternalip"}, {"get", "memberlist"}, {"get", "bgppolicy"}, {"get", "bgppeers"}, {"get", "bgproutes"}, {"supportbundle"}, {"traceflow"}, {"get", "featuregates"}},
7474
},
7575
{
7676
name: "Antctl running against flow-aggregator mode",

pkg/antctl/raw/packetcapture/command.go

+34-32
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ var (
4848
getCopier = getPodFile
4949
defaultFS = afero.NewOsFs()
5050
)
51+
5152
var option = &struct {
5253
source string
5354
dest string
@@ -59,11 +60,11 @@ var option = &struct {
5960
}{}
6061

6162
var packetCaptureExample = strings.TrimSpace(`
62-
Start capture packets from pod1 to pod2, both Pods are in Namespace default
63+
Start capturing packets from pod1 to pod2, both Pods are in Namespace default
6364
$ antctl packetcaputre -S pod1 -D pod2
64-
Start capture packets from pod1 in Namespace ns1 to a destination IP
65+
Start capturing packets from pod1 in Namespace ns1 to a destination IP
6566
$ antctl packetcapture -S ns1/pod1 -D 192.168.123.123
66-
Start capture UDP packets from pod1 to pod2, with destination port 1234
67+
Start capturing UDP packets from pod1 to pod2, with destination port 1234
6768
$ antctl packetcapture -S pod1 -D pod2 -f udp,udp_dst=1234
6869
Save the packets file to a specified directory
6970
$ antctl packetcapture -S 192.168.123.123 -D pod2 -f tcp,tcp_dst=80 -o /tmp
@@ -114,6 +115,17 @@ func getFlowFields(flow string) (map[string]int, error) {
114115
return fields, nil
115116
}
116117

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+
117129
func getConfigAndClients(cmd *cobra.Command) (*rest.Config, kubernetes.Interface, antrea.Interface, error) {
118130
kubeConfig, err := raw.ResolveKubeconfig(cmd)
119131
if err != nil {
@@ -126,20 +138,20 @@ func getConfigAndClients(cmd *cobra.Command) (*rest.Config, kubernetes.Interface
126138
return kubeConfig, k8sClientset, client, nil
127139
}
128140

129-
func getPodFile(cmd *cobra.Command) (PodFileCopy, error) {
130-
config, client, _, err := getClients(cmd)
131-
if err != nil {
132-
return nil, err
141+
func getPCName(src, dest string) string {
142+
replace := func(s string) string {
143+
return strings.ReplaceAll(s, "/", "-")
133144
}
134-
return &podFile{
135-
restConfig: config,
136-
restInterface: client.CoreV1().RESTClient(),
137-
}, nil
145+
prefix := fmt.Sprintf("%s-%s", replace(src), replace(dest))
146+
if option.nowait {
147+
return prefix
148+
}
149+
return fmt.Sprintf("%s-%s", prefix, rand.String(8))
138150
}
139151

140152
func packetCaptureRunE(cmd *cobra.Command, args []string) error {
141153
option.timeout, _ = cmd.Flags().GetDuration("timeout")
142-
if option.timeout > time.Hour {
154+
if option.timeout > 300*time.Second {
143155
return errors.New("timeout cannot be longer than 1 hour")
144156
}
145157
if option.timeout == 0 {
@@ -157,7 +169,7 @@ func packetCaptureRunE(cmd *cobra.Command, args []string) error {
157169
if err != nil {
158170
return fmt.Errorf("error when constructing a PacketCapture CR: %w", err)
159171
}
160-
createCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
172+
createCtx, cancel := context.WithTimeout(cmd.Context(), 5*time.Second)
161173
defer cancel()
162174

163175
if _, err := antreaClient.CrdV1alpha1().PacketCaptures().Create(createCtx, pc, metav1.CreateOptions{}); err != nil {
@@ -189,7 +201,6 @@ func packetCaptureRunE(cmd *cobra.Command, args []string) error {
189201
}
190202
}
191203
return false, nil
192-
193204
})
194205

195206
if wait.Interrupted(err) {
@@ -204,14 +215,16 @@ func packetCaptureRunE(cmd *cobra.Command, args []string) error {
204215
splits := strings.Split(latestPC.Status.FilePath, ":")
205216
fileName := filepath.Base(splits[1])
206217
copier, _ := getCopier(cmd)
207-
err = copier.CopyFromPod(context.TODO(), env.GetAntreaNamespace(), splits[0], "antrea-agent", splits[1], option.outputDir)
208-
if err == nil {
209-
fmt.Fprintf(cmd.OutOrStdout(), "Captured packets file: %s\n", filepath.Join(option.outputDir, fileName))
218+
if err := copier.CopyFromPod(cmd.Context(), env.GetAntreaNamespace(), splits[0], "antrea-agent", splits[1], option.outputDir); err != nil {
219+
return err
210220
}
211-
return err
221+
fmt.Fprintf(cmd.OutOrStdout(), "Captured packets file: %s\n", filepath.Join(option.outputDir, fileName))
222+
return nil
212223
}
213224

214-
func parseEndpoint(endpoint string) (pod *v1alpha1.PodReference, ip *string) {
225+
func parseEndpoint(endpoint string) (*v1alpha1.PodReference, *string) {
226+
var pod *v1alpha1.PodReference
227+
var ip *string
215228
parsedIP := net.ParseIP(endpoint)
216229
if parsedIP != nil && parsedIP.To4() != nil {
217230
ip = ptr.To(parsedIP.String())
@@ -229,23 +242,12 @@ func parseEndpoint(endpoint string) (pod *v1alpha1.PodReference, ip *string) {
229242
}
230243
}
231244
}
232-
return
233-
}
234-
235-
func getPCName(src, dest string) string {
236-
replace := func(s string) string {
237-
return strings.ReplaceAll(s, "/", "-")
238-
}
239-
prefix := fmt.Sprintf("%s-%s", replace(src), replace(dest))
240-
if option.nowait {
241-
return prefix
242-
}
243-
return fmt.Sprintf("%s-%s", prefix, rand.String(8))
245+
return pod, ip
244246
}
245247

246248
func parseFlow() (*v1alpha1.Packet, error) {
247249
trimFlow := strings.ReplaceAll(option.flow, " ", "")
248-
fields, err := getFlowFields(cleanFlow)
250+
fields, err := getFlowFields(trimFlow)
249251
if err != nil {
250252
return nil, fmt.Errorf("error when parsing the flow: %w", err)
251253
}

pkg/antctl/raw/packetcapture/command_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,14 @@ func TestRun(t *testing.T) {
110110
src: srcPod,
111111
dst: dstPod,
112112
flow: "icmp",
113-
expectErr: "timeout waiting for PacketCapture done",
113+
expectErr: "timeout while waiting for PacketCapture to complete",
114114
},
115115
{
116116
name: "invalid-packetcapture",
117117
src: ipv4,
118118
dst: ipv4,
119119
flow: "icmp",
120-
expectErr: "error when filling up PacketCapture config: one of source and destination must be a Pod",
120+
expectErr: "error when constructing a PacketCapture CR: one of source and destination must be a Pod",
121121
},
122122
}
123123
for _, tt := range tcs {
@@ -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
@@ -159,6 +158,7 @@ func TestRun(t *testing.T) {
159158
Command.SetOutput(buf)
160159
Command.SetOut(buf)
161160
Command.SetErr(buf)
161+
Command.SetContext(context.Background())
162162

163163
err := packetCaptureRunE(Command, nil)
164164
if tt.expectErr == "" {

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)