Skip to content
This repository has been archived by the owner on Jun 26, 2023. It is now read-only.

Commit

Permalink
#401 [MTB] Added test for privilege escalation
Browse files Browse the repository at this point in the history
  • Loading branch information
phoenixking25 committed May 10, 2020
1 parent 87d1844 commit 9d13ad9
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package block_privilege_escalation

import (
"fmt"
"strings"

"github.com/onsi/ginkgo"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/uuid"
"k8s.io/kubernetes/test/e2e/framework"
imageutils "k8s.io/kubernetes/test/utils/image"
configutil "sigs.k8s.io/multi-tenancy/benchmarks/e2e/config"
)

const (
expectedVal = "Allowing privilege escalation for containers is not allowed"
)


func MakeSecPod(Namespace string, AllowPrivilegeEscalation bool) (*v1.Pod) {
podName := "security-context-" + string(uuid.NewUUID())
podSpec := &v1.Pod{
TypeMeta: metav1.TypeMeta{
Kind: "Pod",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: podName,
Namespace: Namespace,
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "write-pod",
Image: imageutils.GetE2EImage(imageutils.BusyBox),
Command: []string{"/bin/sh"},
Args: []string{"-c", ""},
SecurityContext: &v1.SecurityContext{
AllowPrivilegeEscalation: &AllowPrivilegeEscalation,
},
},
},
RestartPolicy: v1.RestartPolicyOnFailure,
},
}
return podSpec
}

var _ = framework.KubeDescribe("Processes in tenant containers should not be allowed to gain additional priviliges", func() {
var config *configutil.BenchmarkConfig
var tenantA configutil.TenantSpec
var user string
var err error

ginkgo.BeforeEach(func() {
config, err = configutil.ReadConfig(configutil.ConfigPath)
framework.ExpectNoError(err)

tenantA, err = config.GetValidTenant()
framework.ExpectNoError(err)

user = configutil.GetContextFromKubeconfig(tenantA.Kubeconfig)
})

ginkgo.It("Validate tenants can not create pods/container with allowedprivilege set to true", func() {
ginkgo.By(fmt.Sprintf("tenant %s cannot create pod/container with with allowedprivilege set to true", user))

kclient := configutil.NewKubeClientWithKubeconfig(tenantA.Kubeconfig)

pod := MakeSecPod(tenantA.Namespace, true)
_, err = kclient.CoreV1().Pods(tenantA.Namespace).Create(pod)

if !strings.Contains(err.Error(), expectedVal) {
framework.Failf("%s must be unable to create pod/container that sets allowedprivileged to true", user)
}
})
})
1 change: 1 addition & 0 deletions benchmarks/e2e/tests/e2e.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
_ "sigs.k8s.io/multi-tenancy/benchmarks/e2e/tests/block_multitenant_resources"
_ "sigs.k8s.io/multi-tenancy/benchmarks/e2e/tests/block_ns_quotas"
_ "sigs.k8s.io/multi-tenancy/benchmarks/e2e/tests/block_other_tenant_resources"
_ "sigs.k8s.io/multi-tenancy/benchmarks/e2e/tests/block_privilege_escalation"
_ "sigs.k8s.io/multi-tenancy/benchmarks/e2e/tests/block_privileged_containers"
_ "sigs.k8s.io/multi-tenancy/benchmarks/e2e/tests/configure_ns_quotas"
_ "sigs.k8s.io/multi-tenancy/benchmarks/e2e/tests/create_role_bindings"
Expand Down
3 changes: 3 additions & 0 deletions benchmarks/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ require (
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4 // indirect
github.com/prometheus/common v0.7.0 // indirect
github.com/prometheus/procfs v0.0.5 // indirect
<<<<<<< HEAD
github.com/realshuting/multi-tenancy-benchmarks v0.0.0-20191028041724-a27250830445 // indirect
=======
>>>>>>> #401 [MTB] Added test for privilege escalation
github.com/spf13/pflag v1.0.5 // indirect
go.uber.org/zap v1.12.0 // indirect
golang.org/x/crypto v0.0.0-20191029031824-8986dd9e96cf // indirect
Expand Down
Loading

0 comments on commit 9d13ad9

Please sign in to comment.