Merge pull request #1200 from jterry75/image_user

Assign ImageSpec User if SecurityContext is not set
This commit is contained in:
Lantao Liu 2019-08-07 13:50:08 -07:00 committed by GitHub
commit 95bd02d28f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 97 additions and 3 deletions

View File

@ -227,11 +227,16 @@ func (c *criService) CreateContainer(ctx context.Context, r *runtime.CreateConta
userstr, err := generateUserString(
securityContext.GetRunAsUsername(),
securityContext.GetRunAsUser(),
securityContext.GetRunAsGroup(),
)
securityContext.GetRunAsGroup())
if err != nil {
return nil, errors.Wrap(err, "failed to generate user string")
}
if userstr == "" {
// Lastly, since no user override was passed via CRI try to set via OCI
// Image
userstr = image.ImageSpec.Config.User
}
if userstr != "" {
specOpts = append(specOpts, oci.WithUser(userstr))
}
@ -589,7 +594,20 @@ func generateApparmorSpecOpts(apparmorProf string, privileged, apparmorEnabled b
}
}
// generateUserString generates valid user string based on OCI Image Spec v1.0.0.
// generateUserString generates valid user string based on OCI Image Spec
// v1.0.0.
//
// CRI defines that the following combinations are valid:
//
// (none) -> ""
// username -> username
// username, uid -> username
// username, uid, gid -> username:gid
// username, gid -> username:gid
// uid -> uid
// uid, gid -> uid:gid
// gid -> error
//
// TODO(random-liu): Add group name support in CRI.
func generateUserString(username string, uid, gid *runtime.Int64Value) (string, error) {
var userstr, groupstr string

View File

@ -1231,3 +1231,74 @@ func TestDisableCgroup(t *testing.T) {
t.Log("cgroup path should be empty")
assert.Empty(t, spec.Linux.CgroupsPath)
}
func TestGenerateUserString(t *testing.T) {
type testcase struct {
// the name of the test case
name string
u string
uid, gid *runtime.Int64Value
result string
expectedError bool
}
testcases := []testcase{
{
name: "Empty",
result: "",
},
{
name: "Username Only",
u: "testuser",
result: "testuser",
},
{
name: "Username, UID",
u: "testuser",
uid: &runtime.Int64Value{Value: 1},
result: "testuser",
},
{
name: "Username, UID, GID",
u: "testuser",
uid: &runtime.Int64Value{Value: 1},
gid: &runtime.Int64Value{Value: 10},
result: "testuser:10",
},
{
name: "Username, GID",
u: "testuser",
gid: &runtime.Int64Value{Value: 10},
result: "testuser:10",
},
{
name: "UID only",
uid: &runtime.Int64Value{Value: 1},
result: "1",
},
{
name: "UID, GID",
uid: &runtime.Int64Value{Value: 1},
gid: &runtime.Int64Value{Value: 10},
result: "1:10",
},
{
name: "GID only",
gid: &runtime.Int64Value{Value: 10},
result: "",
expectedError: true,
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
r, err := generateUserString(tc.u, tc.uid, tc.gid)
if tc.expectedError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
assert.Equal(t, tc.result, r)
})
}
}

View File

@ -169,6 +169,11 @@ func (c *criService) RunPodSandbox(ctx context.Context, r *runtime.RunPodSandbox
if err != nil {
return nil, errors.Wrap(err, "failed to generate user string")
}
if userstr == "" {
// Lastly, since no user override was passed via CRI try to set via OCI
// Image
userstr = image.ImageSpec.Config.User
}
if userstr != "" {
specOpts = append(specOpts, oci.WithUser(userstr))
}