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

Commit b50b5ca

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

File tree

3 files changed

+141
-38
lines changed

3 files changed

+141
-38
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/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.

0 commit comments

Comments
 (0)