forked from Devatoria/go-nsenter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnsenter.go
167 lines (141 loc) · 4.61 KB
/
nsenter.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package nsenter
import (
"bytes"
"context"
"fmt"
"os/exec"
"strconv"
)
// Config is the nsenter configuration used to generate
// nsenter command
type Config struct {
AllNamespaces bool // Enter all namespaces of the target process
Cgroup bool // Enter cgroup namespace
CgroupFile string // Cgroup namespace location, default to /proc/PID/ns/cgroup
FollowContext bool // Set SELinux security context
GID int // GID to use to execute given program
IPC bool // Enter IPC namespace
IPCFile string // IPC namespace location, default to /proc/PID/ns/ipc
Mount bool // Enter mount namespace
MountFile string // Mount namespace location, default to /proc/PID/ns/mnt
Net bool // Enter network namespace
NetFile string // Network namespace location, default to /proc/PID/ns/net
NoFork bool // Do not fork before executing the specified program
PID bool // Enter PID namespace
PIDFile string // PID namespace location, default to /proc/PID/ns/pid
PreserveCredentials bool // Preserve current UID/GID when entering namespaces
RootDirectory string // Set the root directory, default to target process root directory
Target int // Target PID (required)
UID int // UID to use to execute given program
User bool // Enter user namespace
UserFile string // User namespace location, default to /proc/PID/ns/user
UTS bool // Enter UTS namespace
UTSFile string // UTS namespace location, default to /proc/PID/ns/uts
WorkingDirectory string // Set the working directory, default to target process working directory
}
// Execute executs the givne command with a default background context
func (c *Config) Execute(program string, args ...string) (string, string, error) {
return c.ExecuteContext(context.Background(), program, args...)
}
// ExecuteContext the given program using the given nsenter configuration and given context
// and return stdout/stderr or an error if command has failed
func (c *Config) ExecuteContext(ctx context.Context, program string, args ...string) (string, string, error) {
cmd, err := c.buildCommand(ctx)
if err != nil {
return "", "", fmt.Errorf("Error while building command: %v", err)
}
// Prepare command
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
cmd.Args = append(cmd.Args, program)
cmd.Args = append(cmd.Args, args...)
err = cmd.Run()
if err != nil {
return stdout.String(), stderr.String(), fmt.Errorf("Error while executing command: %v", err)
}
return stdout.String(), stderr.String(), nil
}
func (c *Config) buildCommand(ctx context.Context) (*exec.Cmd, error) {
if c.Target == 0 {
return nil, fmt.Errorf("Target must be specified")
}
var args []string
args = append(args, "--target", strconv.Itoa(c.Target))
if c.AllNamespaces {
args = append(args, "--all")
}
if c.Cgroup {
if c.CgroupFile != "" {
args = append(args, fmt.Sprintf("--cgroup=%s", c.CgroupFile))
} else {
args = append(args, "--cgroup")
}
}
if c.FollowContext {
args = append(args, "--follow-context")
}
if c.GID != 0 {
args = append(args, "--setgid", strconv.Itoa(c.GID))
}
if c.IPC {
if c.IPCFile != "" {
args = append(args, fmt.Sprintf("--ip=%s", c.IPCFile))
} else {
args = append(args, "--ipc")
}
}
if c.Mount {
if c.MountFile != "" {
args = append(args, fmt.Sprintf("--mount=%s", c.MountFile))
} else {
args = append(args, "--mount")
}
}
if c.Net {
if c.NetFile != "" {
args = append(args, fmt.Sprintf("--net=%s", c.NetFile))
} else {
args = append(args, "--net")
}
}
if c.NoFork {
args = append(args, "--no-fork")
}
if c.PID {
if c.PIDFile != "" {
args = append(args, fmt.Sprintf("--pid=%s", c.PIDFile))
} else {
args = append(args, "--pid")
}
}
if c.PreserveCredentials {
args = append(args, "--preserve-credentials")
}
if c.RootDirectory != "" {
args = append(args, "--root", c.RootDirectory)
}
if c.UID != 0 {
args = append(args, "--setuid", strconv.Itoa(c.UID))
}
if c.User {
if c.UserFile != "" {
args = append(args, fmt.Sprintf("--user=%s", c.UserFile))
} else {
args = append(args, "--user")
}
}
if c.UTS {
if c.UTSFile != "" {
args = append(args, fmt.Sprintf("--uts=%s", c.UTSFile))
} else {
args = append(args, "--uts")
}
}
if c.WorkingDirectory != "" {
args = append(args, "--wd", c.WorkingDirectory)
}
cmd := exec.CommandContext(ctx, "nsenter", args...)
return cmd, nil
}