Skip to content
This repository was archived by the owner on Mar 9, 2022. It is now read-only.

Commit ed20174

Browse files
committed
Add RunAsGroup support.
Signed-off-by: Lantao Liu <[email protected]>
1 parent f99f0be commit ed20174

File tree

10 files changed

+174
-70
lines changed

10 files changed

+174
-70
lines changed

pkg/server/container_create.go

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package server
1919
import (
2020
"os"
2121
"path/filepath"
22+
"strconv"
2223
"strings"
2324
"time"
2425

@@ -219,11 +220,16 @@ func (c *criService) CreateContainer(ctx context.Context, r *runtime.CreateConta
219220
// Set container username. This could only be done by containerd, because it needs
220221
// access to the container rootfs. Pass user name to containerd, and let it overwrite
221222
// the spec for us.
222-
if uid := securityContext.GetRunAsUser(); uid != nil {
223-
specOpts = append(specOpts, oci.WithUserID(uint32(uid.GetValue())))
223+
userstr, err := generateUserString(
224+
securityContext.GetRunAsUsername(),
225+
securityContext.GetRunAsUser(),
226+
securityContext.GetRunAsGroup(),
227+
)
228+
if err != nil {
229+
return nil, errors.Wrap(err, "failed to generate user string")
224230
}
225-
if username := securityContext.GetRunAsUsername(); username != "" {
226-
specOpts = append(specOpts, oci.WithUsername(username))
231+
if userstr != "" {
232+
specOpts = append(specOpts, oci.WithUser(userstr))
227233
}
228234

229235
apparmorSpecOpts, err := generateApparmorSpecOpts(
@@ -884,3 +890,28 @@ func ensureSharedOrSlave(path string, lookupMount func(string) (mount.Info, erro
884890
}
885891
return errors.Errorf("path %q is mounted on %q but it is not a shared or slave mount", path, mountInfo.Mountpoint)
886892
}
893+
894+
// generateUserString generates valid user string based on OCI Image Spec v1.0.0.
895+
// TODO(random-liu): Add group name support in CRI.
896+
func generateUserString(username string, uid, gid *runtime.Int64Value) (string, error) {
897+
var userstr, groupstr string
898+
if uid != nil {
899+
userstr = strconv.FormatInt(uid.GetValue(), 10)
900+
}
901+
if username != "" {
902+
userstr = username
903+
}
904+
if gid != nil {
905+
groupstr = strconv.FormatInt(gid.GetValue(), 10)
906+
}
907+
if userstr == "" {
908+
if groupstr != "" {
909+
return "", errors.Errorf("user group %q is specified without user", groupstr)
910+
}
911+
return "", nil
912+
}
913+
if groupstr != "" {
914+
userstr = userstr + ":" + groupstr
915+
}
916+
return userstr, nil
917+
}

pkg/server/sandbox_run.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,16 @@ func (c *criService) RunPodSandbox(ctx context.Context, r *runtime.RunPodSandbox
145145
logrus.Debugf("Sandbox container spec: %+v", spec)
146146

147147
var specOpts []oci.SpecOpts
148-
if uid := securityContext.GetRunAsUser(); uid != nil {
149-
specOpts = append(specOpts, oci.WithUserID(uint32(uid.GetValue())))
148+
userstr, err := generateUserString(
149+
"",
150+
securityContext.GetRunAsUser(),
151+
securityContext.GetRunAsGroup(),
152+
)
153+
if err != nil {
154+
return nil, errors.Wrap(err, "failed to generate user string")
155+
}
156+
if userstr != "" {
157+
specOpts = append(specOpts, oci.WithUser(userstr))
150158
}
151159

152160
seccompSpecOpts, err := generateSeccompSpecOpts(

vendor.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ github.com/boltdb/bolt e9cf4fae01b5a8ff89d0ec6b32f0d9c9f79aefdd
44
github.com/BurntSushi/toml a368813c5e648fee92e5f6c30e3944ff9d5e8895
55
github.com/containerd/cgroups fe281dd265766145e943a034aa41086474ea6130
66
github.com/containerd/console cb7008ab3d8359b78c5f464cb7cf160107ad5925
7-
github.com/containerd/containerd v1.1.0-rc.0
7+
github.com/containerd/containerd c0f7fcd910a02cd388c089525d7ea17f9f229a43
88
github.com/containerd/continuity 3e8f2ea4b190484acb976a5b378d373429639a1a
99
github.com/containerd/fifo 3d5202aec260678c48179c56f40e6f38a095738c
1010
github.com/containerd/go-runc bcb223a061a3dd7de1a89c0b402a60f4dd9bd307

vendor/github.com/containerd/containerd/archive/tar_windows.go

Lines changed: 6 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/containerd/containerd/contrib/seccomp/seccomp_default.go

Lines changed: 3 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/containerd/containerd/image.go

Lines changed: 15 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/containerd/containerd/linux/shim/client/client.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/containerd/containerd/oci/spec_opts_unix.go

Lines changed: 96 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/containerd/containerd/sys/socket_unix.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/containerd/containerd/vendor.conf

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)