This repository has been archived by the owner on Jun 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/kubernetes-sigs/multi-ten…
…ancy into default-deny-conn
- Loading branch information
Showing
251 changed files
with
6,855 additions
and
3,146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package block_nodeports | ||
|
||
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" | ||
e2edeployment "k8s.io/kubernetes/test/e2e/framework/deployment" | ||
imageutils "k8s.io/kubernetes/test/utils/image" | ||
configutil "sigs.k8s.io/multi-tenancy/benchmarks/e2e/config" | ||
) | ||
|
||
const ( | ||
expectedVal = "Services of type NodePort are not allowed" | ||
) | ||
|
||
func CreateServiceSpec(serviceName string, selector map[string]string) *v1.Service { | ||
Service := &v1.Service{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: serviceName, | ||
}, | ||
Spec: v1.ServiceSpec{ | ||
Selector: selector, | ||
}, | ||
} | ||
Service.Spec.Type = "NodePort" | ||
Service.Spec.Ports = []v1.ServicePort{ | ||
{Port: 80, Name: "http", Protocol: v1.ProtocolTCP}, | ||
} | ||
return Service | ||
} | ||
|
||
var _ = framework.KubeDescribe("[PL1] [PL2] [PL3] Tenants should not be able to create services of type NodePort.", func() { | ||
var config *configutil.BenchmarkConfig | ||
var tenantA configutil.TenantSpec | ||
var user string | ||
var err error | ||
var deploymentName string | ||
var imageName string | ||
var podLabels = map[string]string{"test": "multi"} | ||
var serviceName string | ||
|
||
ginkgo.BeforeEach(func() { | ||
config, err = configutil.ReadConfig(configutil.ConfigPath) | ||
framework.ExpectNoError(err) | ||
|
||
tenantA, err = config.GetValidTenant() | ||
framework.ExpectNoError(err) | ||
|
||
user = configutil.GetContextFromKubeconfig(tenantA.Kubeconfig) | ||
deploymentName = "deployment-" + string(uuid.NewUUID()) | ||
imageName = "image-" + string(uuid.NewUUID()) | ||
serviceName = "image-" + string(uuid.NewUUID()) | ||
}) | ||
|
||
ginkgo.It("Tenants should not be able to create services of type NodePort.", func() { | ||
ginkgo.By(fmt.Sprintf("Tenant %s should not be able to create services of type NodePort.", user)) | ||
|
||
deployment := e2edeployment.NewDeployment(deploymentName, 1, podLabels, imageName, imageutils.GetE2EImage(imageutils.Nginx), "Recreate") | ||
|
||
kclient := configutil.NewKubeClientWithKubeconfig(tenantA.Kubeconfig) | ||
_, err = kclient.AppsV1().Deployments(tenantA.Namespace).Create(deployment) | ||
framework.ExpectNoError(err) | ||
|
||
svc := CreateServiceSpec(serviceName, podLabels) | ||
_, err = kclient.CoreV1().Services(tenantA.Namespace).Create(svc) | ||
|
||
if !strings.Contains(err.Error(), expectedVal) { | ||
framework.Failf("%s must be unable to create service of type NodePort", user) | ||
} | ||
}) | ||
}) |
77 changes: 77 additions & 0 deletions
77
benchmarks/e2e/tests/block_privilege_escalation/block_privilege_escalation.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
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("[PL1] [PL2] [PL3] 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) | ||
} | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# [MTB-PL2-BC-OPS-4] Create Network Policies | ||
|
||
**Profile Applicability:** | ||
|
||
Level 2 | ||
|
||
**Type:** | ||
|
||
Behavioral | ||
|
||
**Category:** | ||
|
||
Self-Service Operations | ||
|
||
**Description:** | ||
|
||
Tenants should be able to perform self-service operations by creating own network policies in their namespaces. | ||
|
||
Tenants | ||
|
||
**Rationale:** | ||
|
||
Enables self-service management of network-policies. | ||
|
||
**Audit:** | ||
|
||
Run the following commands to check for permissions to manage `network-policy` for each verb(get, create, update, patch, delete, and deletecollection) in the tenant namespace: | ||
|
||
kubectl --kubeconfig=tenant-a -n a1 auth can-i <verb> networkpolicy | ||
|
||
Each command must return 'yes' |
56 changes: 56 additions & 0 deletions
56
benchmarks/e2e/tests/create_network_policies/create_network_policies.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package create_network_policies | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"github.com/onsi/ginkgo" | ||
"k8s.io/kubernetes/test/e2e/framework" | ||
configutil "sigs.k8s.io/multi-tenancy/benchmarks/e2e/config" | ||
) | ||
|
||
const ( | ||
expectedVal = "yes" | ||
) | ||
|
||
var _ = framework.KubeDescribe("[PL2] [PL3] Test tenant's network-policy management permissions", func() { | ||
var config *configutil.BenchmarkConfig | ||
var tenantkubeconfig configutil.TenantSpec | ||
var err error | ||
|
||
ginkgo.BeforeEach(func() { | ||
config, err = configutil.ReadConfig(configutil.ConfigPath) | ||
framework.ExpectNoError(err) | ||
}) | ||
|
||
framework.KubeDescribe("Tenant has RBAC privileges for Network-policies", func() { | ||
var user string | ||
var verbs = []string{"get", "list", "create", "update", "patch", "watch", "delete", "deletecollection"} | ||
var namespaceflag = "-n" | ||
|
||
ginkgo.BeforeEach(func() { | ||
tenantkubeconfig, err = config.GetValidTenant() | ||
framework.ExpectNoError(err) | ||
|
||
os.Setenv("KUBECONFIG", tenantkubeconfig.Kubeconfig) | ||
user = configutil.GetContextFromKubeconfig(tenantkubeconfig.Kubeconfig) | ||
}) | ||
|
||
ginkgo.It("Tenant has RBAC privileges for Network-policies", func() { | ||
ginkgo.By(fmt.Sprintf("Tenant %s can modify Network-policies for its namespace", user)) | ||
|
||
for _, verb := range verbs { | ||
_, errNew := framework.LookForString(expectedVal, time.Minute, func() string { | ||
output, err := framework.RunKubectl("auth", "can-i", verb, "networkpolicy", namespaceflag, tenantkubeconfig.Namespace) | ||
if err != nil { | ||
return err.Error() | ||
} | ||
return output | ||
}) | ||
|
||
framework.ExpectNoError(errNew) | ||
} | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.