Skip to content

Commit d76b476

Browse files
author
Wei Fu
authored
Merge pull request #1657 from Ace-Tang/exec_user
bugfix: fix exec record user as container config user
2 parents d34ef8f + 4f346e6 commit d76b476

File tree

6 files changed

+56
-21
lines changed

6 files changed

+56
-21
lines changed

daemon/config/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ type Config struct {
113113
// TODO(Ace-Tang): runtime args is not support, since containerd is not support,
114114
// add a resolution later if it needed.
115115
Runtimes map[string]types.Runtime `json:"add-runtime,omitempty"`
116+
117+
// Namespace is passed to containerd
118+
Namespace string
116119
}
117120

118121
// Validate validates the user input config.

daemon/daemon.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"github.com/alibaba/pouch/pkg/meta"
2020
"github.com/alibaba/pouch/pkg/system"
2121

22-
"github.com/containerd/containerd/namespaces"
2322
systemddaemon "github.com/coreos/go-systemd/daemon"
2423
systemdutil "github.com/coreos/go-systemd/util"
2524
"github.com/gorilla/mux"
@@ -71,10 +70,9 @@ func NewDaemon(cfg *config.Config) *Daemon {
7170
containerdBinaryFile = cfg.ContainerdPath
7271
}
7372

74-
defaultns := namespaces.Default
7573
// the default unix socket path to the containerd socket
7674
if cfg.IsCriEnabled {
77-
defaultns = criconfig.K8sNamespace
75+
cfg.Namespace = criconfig.K8sNamespace
7876
}
7977

8078
containerd, err := ctrd.NewClient(cfg.HomeDir,
@@ -83,7 +81,7 @@ func NewDaemon(cfg *config.Config) *Daemon {
8381
ctrd.WithContainerdBinary(containerdBinaryFile),
8482
ctrd.WithRPCAddr(cfg.ContainerdAddr),
8583
ctrd.WithOOMScoreAdjust(cfg.OOMScoreAdjust),
86-
ctrd.WithDefaultNamespace(defaultns),
84+
ctrd.WithDefaultNamespace(cfg.Namespace),
8785
)
8886
if err != nil {
8987
logrus.Errorf("failed to new containerd's client: %v", err)

daemon/mgr/container.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import (
3434
containerdtypes "github.com/containerd/containerd/api/types"
3535
"github.com/containerd/containerd/errdefs"
3636
"github.com/containerd/containerd/mount"
37-
"github.com/containerd/containerd/namespaces"
3837
"github.com/docker/libnetwork"
3938
"github.com/go-openapi/strfmt"
4039
"github.com/imdario/mergo"
@@ -347,7 +346,8 @@ func (mgr *ContainerManager) Create(ctx context.Context, name string, config *ty
347346
return nil, errors.Wrap(err, "failed to set mount point disk quota")
348347
}
349348

350-
// set container basefs
349+
// set container basefs, basefs is not created in pouchd, it will created
350+
// after create options passed to containerd.
351351
mgr.setBaseFS(ctx, container, id)
352352

353353
// set network settings
@@ -2486,7 +2486,7 @@ func (mgr *ContainerManager) setBaseFS(ctx context.Context, c *Container, id str
24862486

24872487
// io.containerd.runtime.v1.linux as a const used by runc
24882488
c.Lock()
2489-
c.BaseFS = filepath.Join(mgr.Config.HomeDir, "containerd/state", "io.containerd.runtime.v1.linux", namespaces.Default, info.Name, "rootfs")
2489+
c.BaseFS = filepath.Join(mgr.Config.HomeDir, "containerd/state", "io.containerd.runtime.v1.linux", mgr.Config.Namespace, info.Name, "rootfs")
24902490
c.Unlock()
24912491
}
24922492

daemon/mgr/container_exec.go

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/alibaba/pouch/ctrd"
1010
"github.com/alibaba/pouch/pkg/errtypes"
1111
"github.com/alibaba/pouch/pkg/randomid"
12+
"github.com/alibaba/pouch/pkg/user"
1213

1314
"github.com/docker/docker/pkg/stdcopy"
1415
specs "github.com/opencontainers/runtime-spec/specs-go"
@@ -78,29 +79,32 @@ func (mgr *ContainerManager) StartExec(ctx context.Context, execid string, attac
7879
return err
7980
}
8081

81-
c.Lock()
82+
// set exec process user, user decided by exec config
83+
if execConfig.User == "" {
84+
execConfig.User = c.Config.User
85+
}
86+
87+
uid, gid, err := user.Get(c.BaseFS, execConfig.User)
88+
if err != nil {
89+
return err
90+
}
91+
8292
process := &specs.Process{
8393
Args: execConfig.Cmd,
8494
Terminal: execConfig.Tty,
8595
Cwd: "/",
8696
Env: c.Config.Env,
97+
User: specs.User{
98+
UID: uid,
99+
GID: gid,
100+
AdditionalGids: user.GetAdditionalGids(c.HostConfig.GroupAdd),
101+
},
87102
}
88103

89-
if execConfig.User != "" {
90-
c.Config.User = execConfig.User
91-
}
92-
93-
if err = setupUser(ctx, c, &specs.Spec{Process: process}); err != nil {
94-
c.Unlock()
95-
return err
96-
}
97-
98-
// set exec process ulimit
104+
// set exec process ulimit, ulimit not decided by exec config
99105
if err := setupRlimits(ctx, c.HostConfig, &specs.Spec{Process: process}); err != nil {
100-
c.Unlock()
101106
return err
102107
}
103-
c.Unlock()
104108

105109
execConfig.Running = true
106110

main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/alibaba/pouch/storage/quota"
2222
"github.com/alibaba/pouch/version"
2323

24+
"github.com/containerd/containerd/namespaces"
2425
"github.com/docker/docker/pkg/reexec"
2526
"github.com/google/gops/agent"
2627
"github.com/sirupsen/logrus"
@@ -120,7 +121,6 @@ func setupFlags(cmd *cobra.Command) {
120121
flagSet.StringVar(&cfg.Pidfile, "pidfile", "/var/run/pouch.pid", "Save daemon pid")
121122
flagSet.IntVar(&cfg.OOMScoreAdjust, "oom-score-adj", -500, "Set the oom_score_adj for the daemon")
122123
flagSet.Var(optscfg.NewRuntime(&cfg.Runtimes), "add-runtime", "register a OCI runtime to daemon")
123-
124124
}
125125

126126
// runDaemon prepares configs, setups essential details and runs pouchd daemon.
@@ -129,6 +129,9 @@ func runDaemon(cmd *cobra.Command) error {
129129
return fmt.Errorf("failed to load daemon file: %s", err)
130130
}
131131

132+
// set containerd namespace, we use containerd default namespace as pouch namespaces
133+
cfg.Namespace = namespaces.Default
134+
132135
// parse log driver config
133136
logOptMap, err := opts.ParseLogOptions(cfg.DefaultLogConfig.LogDriver, logOpts)
134137
if err != nil {

test/cli_exec_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,30 @@ func (suite *PouchExecSuite) TestExecFail(c *check.C) {
151151
defer DelContainerForceMultyTime(c, name)
152152
c.Assert(res.Stderr(), check.NotNil)
153153
}
154+
155+
// TestExecUser test exec with user.
156+
func (suite *PouchExecSuite) TestExecUser(c *check.C) {
157+
name := "TestExecUser"
158+
res := command.PouchRun("run", "-d", "-u=1001", "--name", name, busyboxImage, "top")
159+
defer DelContainerForceMultyTime(c, name)
160+
res.Assert(c, icmd.Success)
161+
162+
res = command.PouchRun("exec", name, "id", "-u")
163+
res.Assert(c, icmd.Success)
164+
if !strings.Contains(res.Stdout(), "1001") {
165+
c.Fatalf("failed to run a container with expected user: %s, but got %s", "1001", res.Stdout())
166+
}
167+
168+
res = command.PouchRun("exec", "-u=1002", name, "id", "-u")
169+
res.Assert(c, icmd.Success)
170+
if !strings.Contains(res.Stdout(), "1002") {
171+
c.Fatalf("failed to run a container with expected user: %s, but got %s", "1002", res.Stdout())
172+
}
173+
174+
// test user should not changed by exec process
175+
res = command.PouchRun("exec", name, "id", "-u")
176+
res.Assert(c, icmd.Success)
177+
if !strings.Contains(res.Stdout(), "1001") {
178+
c.Fatalf("failed to run a container with expected user: %s, but got %s", "1001", res.Stdout())
179+
}
180+
}

0 commit comments

Comments
 (0)