forked from kata-containers/kata-containers
-
Notifications
You must be signed in to change notification settings - Fork 29
/
kata-iptables.go
122 lines (106 loc) · 2.84 KB
/
kata-iptables.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright (c) 2022 Apple Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
package main
import (
"fmt"
"os"
containerdshim "github.com/kata-containers/kata-containers/src/runtime/pkg/containerd-shim-v2"
"github.com/kata-containers/kata-containers/src/runtime/pkg/katautils"
"github.com/kata-containers/kata-containers/src/runtime/pkg/utils/shimclient"
"github.com/urfave/cli"
)
var (
sandboxID string
isIPv6 bool
)
var iptablesSubCmds = []cli.Command{
getIPTablesCommand,
setIPTablesCommand,
}
var kataIPTablesCommand = cli.Command{
Name: "iptables",
Usage: "get or set iptables within the Kata Containers guest",
Subcommands: iptablesSubCmds,
Action: func(context *cli.Context) {
cli.ShowSubcommandHelp(context)
},
}
var getIPTablesCommand = cli.Command{
Name: "get",
Usage: "get iptables from the Kata Containers guest",
Flags: []cli.Flag{
cli.StringFlag{
Name: "sandbox-id",
Usage: "the target sandbox for getting the iptables",
Required: true,
Destination: &sandboxID,
},
cli.BoolFlag{
Name: "v6",
Usage: "indicate we're requesting ipv6 iptables",
Destination: &isIPv6,
},
},
Action: func(c *cli.Context) error {
// verify sandbox exists:
if err := katautils.VerifyContainerID(sandboxID); err != nil {
return err
}
url := containerdshim.IPTablesUrl
if isIPv6 {
url = containerdshim.IP6TablesUrl
}
body, err := shimclient.DoGet(sandboxID, defaultTimeout, url)
if err != nil {
return err
}
fmt.Println(string(body))
return nil
},
}
var setIPTablesCommand = cli.Command{
Name: "set",
Usage: "set iptables in a specifc Kata Containers guest based on file",
Flags: []cli.Flag{
cli.StringFlag{
Name: "sandbox-id",
Usage: "the target sandbox for setting the iptables",
Required: true,
Destination: &sandboxID,
},
cli.BoolFlag{
Name: "v6",
Usage: "indicate we're requesting ipv6 iptables",
Destination: &isIPv6,
},
},
Action: func(c *cli.Context) error {
iptablesFile := c.Args().Get(0)
// verify sandbox exists:
if err := katautils.VerifyContainerID(sandboxID); err != nil {
return err
}
// verify iptables were provided:
if iptablesFile == "" {
return fmt.Errorf("iptables file not provided")
}
if !katautils.FileExists(iptablesFile) {
return fmt.Errorf("iptables file does not exist: %s", iptablesFile)
}
// Read file into buffer, and make request to the appropriate shim
buf, err := os.ReadFile(iptablesFile)
if err != nil {
return err
}
url := containerdshim.IPTablesUrl
if isIPv6 {
url = containerdshim.IP6TablesUrl
}
if err = shimclient.DoPut(sandboxID, defaultTimeout, url, "application/octet-stream", buf); err != nil {
return fmt.Errorf("Error observed when making iptables-set request(%s): %s", iptablesFile, err)
}
return nil
},
}