mirror of https://github.com/k3s-io/k3s
Merge pull request #73895 from WanLinghao/context_util_clean
Clean unused code in pkg/securitycontext/util.gopull/564/head
commit
a3877b1776
|
@ -17,9 +17,6 @@ limitations under the License.
|
||||||
package securitycontext
|
package securitycontext
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"k8s.io/api/core/v1"
|
"k8s.io/api/core/v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -47,26 +44,6 @@ func HasCapabilitiesRequest(container *v1.Container) bool {
|
||||||
return len(container.SecurityContext.Capabilities.Add) > 0 || len(container.SecurityContext.Capabilities.Drop) > 0
|
return len(container.SecurityContext.Capabilities.Add) > 0 || len(container.SecurityContext.Capabilities.Drop) > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
const expectedSELinuxFields = 4
|
|
||||||
|
|
||||||
// ParseSELinuxOptions parses a string containing a full SELinux context
|
|
||||||
// (user, role, type, and level) into an SELinuxOptions object. If the
|
|
||||||
// context is malformed, an error is returned.
|
|
||||||
func ParseSELinuxOptions(context string) (*v1.SELinuxOptions, error) {
|
|
||||||
fields := strings.SplitN(context, ":", expectedSELinuxFields)
|
|
||||||
|
|
||||||
if len(fields) != expectedSELinuxFields {
|
|
||||||
return nil, fmt.Errorf("expected %v fields in selinux; got %v (context: %v)", expectedSELinuxFields, len(fields), context)
|
|
||||||
}
|
|
||||||
|
|
||||||
return &v1.SELinuxOptions{
|
|
||||||
User: fields[0],
|
|
||||||
Role: fields[1],
|
|
||||||
Type: fields[2],
|
|
||||||
Level: fields[3],
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func DetermineEffectiveSecurityContext(pod *v1.Pod, container *v1.Container) *v1.SecurityContext {
|
func DetermineEffectiveSecurityContext(pod *v1.Pod, container *v1.Container) *v1.SecurityContext {
|
||||||
effectiveSc := securityContextFromPodSecurityContext(pod)
|
effectiveSc := securityContextFromPodSecurityContext(pod)
|
||||||
containerSc := container.SecurityContext
|
containerSc := container.SecurityContext
|
||||||
|
|
|
@ -23,68 +23,6 @@ import (
|
||||||
"k8s.io/api/core/v1"
|
"k8s.io/api/core/v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestParseSELinuxOptions(t *testing.T) {
|
|
||||||
cases := []struct {
|
|
||||||
name string
|
|
||||||
input string
|
|
||||||
expected *v1.SELinuxOptions
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "simple",
|
|
||||||
input: "user_t:role_t:type_t:s0",
|
|
||||||
expected: &v1.SELinuxOptions{
|
|
||||||
User: "user_t",
|
|
||||||
Role: "role_t",
|
|
||||||
Type: "type_t",
|
|
||||||
Level: "s0",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "simple + categories",
|
|
||||||
input: "user_t:role_t:type_t:s0:c0",
|
|
||||||
expected: &v1.SELinuxOptions{
|
|
||||||
User: "user_t",
|
|
||||||
Role: "role_t",
|
|
||||||
Type: "type_t",
|
|
||||||
Level: "s0:c0",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "not enough fields",
|
|
||||||
input: "type_t:s0:c0",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tc := range cases {
|
|
||||||
result, err := ParseSELinuxOptions(tc.input)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
if tc.expected == nil {
|
|
||||||
continue
|
|
||||||
} else {
|
|
||||||
t.Errorf("%v: unexpected error: %v", tc.name, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
compareContexts(tc.name, tc.expected, result, t)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func compareContexts(name string, ex, ac *v1.SELinuxOptions, t *testing.T) {
|
|
||||||
if e, a := ex.User, ac.User; e != a {
|
|
||||||
t.Errorf("%v: expected user: %v, got: %v", name, e, a)
|
|
||||||
}
|
|
||||||
if e, a := ex.Role, ac.Role; e != a {
|
|
||||||
t.Errorf("%v: expected role: %v, got: %v", name, e, a)
|
|
||||||
}
|
|
||||||
if e, a := ex.Type, ac.Type; e != a {
|
|
||||||
t.Errorf("%v: expected type: %v, got: %v", name, e, a)
|
|
||||||
}
|
|
||||||
if e, a := ex.Level, ac.Level; e != a {
|
|
||||||
t.Errorf("%v: expected level: %v, got: %v", name, e, a)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAddNoNewPrivileges(t *testing.T) {
|
func TestAddNoNewPrivileges(t *testing.T) {
|
||||||
pfalse := false
|
pfalse := false
|
||||||
ptrue := true
|
ptrue := true
|
||||||
|
|
Loading…
Reference in New Issue