2018-12-21 22:30:19 +00:00
|
|
|
/*
|
|
|
|
Copyright 2018 The Kubernetes Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package settings
|
|
|
|
|
|
|
|
import (
|
2020-03-24 13:44:13 +00:00
|
|
|
"context"
|
2018-12-21 22:45:25 +00:00
|
|
|
"net/http"
|
2022-09-04 23:22:36 +00:00
|
|
|
"strconv"
|
2018-12-21 22:30:19 +00:00
|
|
|
"strings"
|
|
|
|
|
2022-07-31 16:16:28 +00:00
|
|
|
"github.com/onsi/ginkgo/v2"
|
2020-02-19 03:08:56 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-05-08 00:21:13 +00:00
|
|
|
appsv1 "k8s.io/api/apps/v1"
|
2018-12-21 22:30:19 +00:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
2019-12-12 23:12:12 +00:00
|
|
|
policyv1beta1 "k8s.io/api/policy/v1beta1"
|
2018-12-21 22:30:19 +00:00
|
|
|
rbacv1 "k8s.io/api/rbac/v1"
|
|
|
|
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
|
|
|
|
"k8s.io/ingress-nginx/test/e2e/framework"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ingressControllerPSP = "ingress-controller-psp"
|
|
|
|
)
|
|
|
|
|
2020-02-16 18:27:58 +00:00
|
|
|
var _ = framework.IngressNginxDescribe("[Security] Pod Security Policies", func() {
|
2018-12-21 22:30:19 +00:00
|
|
|
f := framework.NewDefaultFramework("pod-security-policies")
|
|
|
|
|
2020-07-01 21:19:51 +00:00
|
|
|
ginkgo.It("should be running with a Pod Security Policy", func() {
|
2022-09-04 23:22:36 +00:00
|
|
|
k8sversion, err := f.KubeClientSet.Discovery().ServerVersion()
|
|
|
|
if err != nil {
|
|
|
|
assert.Nil(ginkgo.GinkgoT(), err, "getting version")
|
|
|
|
}
|
|
|
|
|
|
|
|
numversion, err := strconv.Atoi(k8sversion.Minor)
|
|
|
|
if err != nil {
|
|
|
|
assert.Nil(ginkgo.GinkgoT(), err, "converting version")
|
|
|
|
}
|
|
|
|
|
|
|
|
if numversion > 24 {
|
|
|
|
ginkgo.Skip("PSP not supported in this version")
|
|
|
|
}
|
|
|
|
|
2018-12-21 22:30:19 +00:00
|
|
|
psp := createPodSecurityPolicy()
|
2022-09-04 23:22:36 +00:00
|
|
|
_, err = f.KubeClientSet.PolicyV1beta1().PodSecurityPolicies().Create(context.TODO(), psp, metav1.CreateOptions{})
|
2018-12-21 22:30:19 +00:00
|
|
|
if !k8sErrors.IsAlreadyExists(err) {
|
2020-02-19 03:08:56 +00:00
|
|
|
assert.Nil(ginkgo.GinkgoT(), err, "creating Pod Security Policy")
|
2018-12-21 22:30:19 +00:00
|
|
|
}
|
|
|
|
|
2020-03-24 13:44:13 +00:00
|
|
|
role, err := f.KubeClientSet.RbacV1().Roles(f.Namespace).Get(context.TODO(), "nginx-ingress", metav1.GetOptions{})
|
2020-02-19 03:08:56 +00:00
|
|
|
assert.Nil(ginkgo.GinkgoT(), err, "getting ingress controller cluster role")
|
|
|
|
assert.NotNil(ginkgo.GinkgoT(), role)
|
2018-12-21 22:30:19 +00:00
|
|
|
|
|
|
|
role.Rules = append(role.Rules, rbacv1.PolicyRule{
|
|
|
|
APIGroups: []string{"policy"},
|
|
|
|
Resources: []string{"podsecuritypolicies"},
|
|
|
|
ResourceNames: []string{ingressControllerPSP},
|
|
|
|
Verbs: []string{"use"},
|
|
|
|
})
|
|
|
|
|
2020-03-24 13:44:13 +00:00
|
|
|
_, err = f.KubeClientSet.RbacV1().Roles(f.Namespace).Update(context.TODO(), role, metav1.UpdateOptions{})
|
2020-02-19 03:08:56 +00:00
|
|
|
assert.Nil(ginkgo.GinkgoT(), err, "updating ingress controller cluster role to use a pod security policy")
|
2018-12-21 22:30:19 +00:00
|
|
|
|
|
|
|
// update the deployment just to trigger a rolling update and the use of the security policy
|
2020-09-26 23:27:19 +00:00
|
|
|
err = f.UpdateIngressControllerDeployment(func(deployment *appsv1.Deployment) error {
|
|
|
|
args := deployment.Spec.Template.Spec.Containers[0].Args
|
|
|
|
args = append(args, "--v=2")
|
|
|
|
deployment.Spec.Template.Spec.Containers[0].Args = args
|
|
|
|
_, err := f.KubeClientSet.AppsV1().Deployments(f.Namespace).Update(context.TODO(), deployment, metav1.UpdateOptions{})
|
|
|
|
|
|
|
|
return err
|
|
|
|
})
|
2020-02-19 03:08:56 +00:00
|
|
|
assert.Nil(ginkgo.GinkgoT(), err, "unexpected error updating ingress controller deployment flags")
|
2018-12-21 22:30:19 +00:00
|
|
|
|
2020-07-01 21:19:51 +00:00
|
|
|
f.WaitForNginxListening(80)
|
|
|
|
|
2018-12-21 22:30:19 +00:00
|
|
|
f.NewEchoDeployment()
|
|
|
|
|
|
|
|
f.WaitForNginxConfiguration(
|
|
|
|
func(cfg string) bool {
|
2020-09-17 11:19:32 +00:00
|
|
|
return strings.Contains(cfg, "server_tokens off")
|
2018-12-21 22:30:19 +00:00
|
|
|
})
|
2018-12-21 22:45:25 +00:00
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
f.HTTPTestClient().
|
|
|
|
GET("/").
|
|
|
|
WithHeader("Host", "foo.bar.com").
|
|
|
|
Expect().
|
|
|
|
Status(http.StatusNotFound)
|
2018-12-21 22:30:19 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-12-12 23:12:12 +00:00
|
|
|
func createPodSecurityPolicy() *policyv1beta1.PodSecurityPolicy {
|
2018-12-21 22:30:19 +00:00
|
|
|
trueValue := true
|
2019-12-12 23:12:12 +00:00
|
|
|
return &policyv1beta1.PodSecurityPolicy{
|
2018-12-21 22:30:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: ingressControllerPSP,
|
|
|
|
},
|
2019-12-12 23:12:12 +00:00
|
|
|
Spec: policyv1beta1.PodSecurityPolicySpec{
|
2018-12-21 22:30:19 +00:00
|
|
|
AllowPrivilegeEscalation: &trueValue,
|
|
|
|
RequiredDropCapabilities: []corev1.Capability{"All"},
|
2019-12-12 23:12:12 +00:00
|
|
|
RunAsUser: policyv1beta1.RunAsUserStrategyOptions{
|
2018-12-21 22:30:19 +00:00
|
|
|
Rule: "RunAsAny",
|
|
|
|
},
|
2019-12-12 23:12:12 +00:00
|
|
|
SELinux: policyv1beta1.SELinuxStrategyOptions{
|
2018-12-21 22:30:19 +00:00
|
|
|
Rule: "RunAsAny",
|
|
|
|
},
|
2019-12-12 23:12:12 +00:00
|
|
|
FSGroup: policyv1beta1.FSGroupStrategyOptions{
|
|
|
|
Ranges: []policyv1beta1.IDRange{
|
2018-12-21 22:30:19 +00:00
|
|
|
{
|
|
|
|
Min: 1,
|
|
|
|
Max: 65535,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Rule: "MustRunAs",
|
|
|
|
},
|
2019-12-12 23:12:12 +00:00
|
|
|
SupplementalGroups: policyv1beta1.SupplementalGroupsStrategyOptions{
|
|
|
|
Ranges: []policyv1beta1.IDRange{
|
2018-12-21 22:30:19 +00:00
|
|
|
{
|
|
|
|
Min: 1,
|
|
|
|
Max: 65535,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Rule: "MustRunAs",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|