Skip to content

Commit c5f7f77

Browse files
committed
add webhook for serviceaccount deletion
1 parent 408009d commit c5f7f77

File tree

20 files changed

+486
-29
lines changed

20 files changed

+486
-29
lines changed

build/selectorsyncset.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,36 @@ objects:
505505
scope: Cluster
506506
sideEffects: None
507507
timeoutSeconds: 2
508+
- apiVersion: admissionregistration.k8s.io/v1
509+
kind: ValidatingWebhookConfiguration
510+
metadata:
511+
annotations:
512+
service.beta.openshift.io/inject-cabundle: "true"
513+
creationTimestamp: null
514+
name: sre-serviceaccount-validation
515+
webhooks:
516+
- admissionReviewVersions:
517+
- v1
518+
clientConfig:
519+
service:
520+
name: validation-webhook
521+
namespace: openshift-validation-webhook
522+
path: /serviceaccount-validation
523+
failurePolicy: Ignore
524+
matchPolicy: Equivalent
525+
name: serviceaccount-validation.managed.openshift.io
526+
rules:
527+
- apiGroups:
528+
- ""
529+
apiVersions:
530+
- v1
531+
operations:
532+
- DELETE
533+
resources:
534+
- serviceaccounts
535+
scope: Namespaced
536+
sideEffects: None
537+
timeoutSeconds: 2
508538
- apiVersion: admissionregistration.k8s.io/v1
509539
kind: ValidatingWebhookConfiguration
510540
metadata:

config/package/resources.yaml.gotmpl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,36 @@ webhooks:
377377
---
378378
apiVersion: admissionregistration.k8s.io/v1
379379
kind: ValidatingWebhookConfiguration
380+
metadata:
381+
annotations:
382+
package-operator.run/phase: webhooks
383+
service.beta.openshift.io/inject-cabundle: "false"
384+
creationTimestamp: null
385+
name: sre-serviceaccount-validation
386+
webhooks:
387+
- admissionReviewVersions:
388+
- v1
389+
clientConfig:
390+
caBundle: '{{.config.serviceca | b64enc }}'
391+
url: https://validation-webhook.{{.package.metadata.namespace}}.svc.cluster.local/serviceaccount-validation
392+
failurePolicy: Ignore
393+
matchPolicy: Equivalent
394+
name: serviceaccount-validation.managed.openshift.io
395+
rules:
396+
- apiGroups:
397+
- ""
398+
apiVersions:
399+
- v1
400+
operations:
401+
- DELETE
402+
resources:
403+
- serviceaccounts
404+
scope: Namespaced
405+
sideEffects: None
406+
timeoutSeconds: 2
407+
---
408+
apiVersion: admissionregistration.k8s.io/v1
409+
kind: ValidatingWebhookConfiguration
380410
metadata:
381411
annotations:
382412
package-operator.run/phase: webhooks

docs/webhooks-short.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
},
3030
{
3131
"webhookName": "regular-user-validation",
32-
"documentString": "Managed OpenShift customers may not manage any objects in the following APIgroups [machine.openshift.io admissionregistration.k8s.io addons.managed.openshift.io ocmagent.managed.openshift.io upgrade.managed.openshift.io machineconfiguration.openshift.io network.openshift.io managed.openshift.io splunkforwarder.managed.openshift.io autoscaling.openshift.io config.openshift.io operator.openshift.io cloudcredential.openshift.io cloudingress.managed.openshift.io], nor may Managed OpenShift customers alter the APIServer, KubeAPIServer, OpenShiftAPIServer, ClusterVersion, Proxy or SubjectPermission objects."
32+
"documentString": "Managed OpenShift customers may not manage any objects in the following APIgroups [cloudcredential.openshift.io machine.openshift.io admissionregistration.k8s.io operator.openshift.io splunkforwarder.managed.openshift.io upgrade.managed.openshift.io machineconfiguration.openshift.io managed.openshift.io ocmagent.managed.openshift.io network.openshift.io config.openshift.io addons.managed.openshift.io cloudingress.managed.openshift.io autoscaling.openshift.io], nor may Managed OpenShift customers alter the APIServer, KubeAPIServer, OpenShiftAPIServer, ClusterVersion, Proxy or SubjectPermission objects."
3333
},
3434
{
3535
"webhookName": "regular-user-validation-osd",
@@ -39,6 +39,10 @@
3939
"webhookName": "scc-validation",
4040
"documentString": "Managed OpenShift Customers may not modify the following default SCCs: [anyuid hostaccess hostmount-anyuid hostnetwork hostnetwork-v2 node-exporter nonroot nonroot-v2 privileged restricted restricted-v2]"
4141
},
42+
{
43+
"webhookName": "serviceaccount-validation",
44+
"documentString": "Managed OpenShift Customers may not delete the service accounts under the managed namespaces。"
45+
},
4246
{
4347
"webhookName": "techpreviewnoupgrade-validation",
4448
"documentString": "Managed OpenShift Customers may not use TechPreviewNoUpgrade FeatureGate that could prevent any future ability to do a y-stream upgrade to their clusters."

docs/webhooks.json

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@
318318
"scope": "*"
319319
}
320320
],
321-
"documentString": "Managed OpenShift customers may not manage any objects in the following APIgroups [addons.managed.openshift.io cloudingress.managed.openshift.io upgrade.managed.openshift.io autoscaling.openshift.io config.openshift.io operator.openshift.io machine.openshift.io managed.openshift.io ocmagent.managed.openshift.io machineconfiguration.openshift.io network.openshift.io cloudcredential.openshift.io admissionregistration.k8s.io splunkforwarder.managed.openshift.io], nor may Managed OpenShift customers alter the APIServer, KubeAPIServer, OpenShiftAPIServer, ClusterVersion, Proxy or SubjectPermission objects."
321+
"documentString": "Managed OpenShift customers may not manage any objects in the following APIgroups [addons.managed.openshift.io ocmagent.managed.openshift.io operator.openshift.io network.openshift.io admissionregistration.k8s.io cloudingress.managed.openshift.io splunkforwarder.managed.openshift.io upgrade.managed.openshift.io config.openshift.io cloudcredential.openshift.io machine.openshift.io managed.openshift.io autoscaling.openshift.io machineconfiguration.openshift.io], nor may Managed OpenShift customers alter the APIServer, KubeAPIServer, OpenShiftAPIServer, ClusterVersion, Proxy or SubjectPermission objects."
322322
},
323323
{
324324
"webhookName": "regular-user-validation-osd",
@@ -364,6 +364,27 @@
364364
],
365365
"documentString": "Managed OpenShift Customers may not modify the following default SCCs: [anyuid hostaccess hostmount-anyuid hostnetwork hostnetwork-v2 node-exporter nonroot nonroot-v2 privileged restricted restricted-v2]"
366366
},
367+
{
368+
"webhookName": "serviceaccount-validation",
369+
"rules": [
370+
{
371+
"operations": [
372+
"DELETE"
373+
],
374+
"apiGroups": [
375+
""
376+
],
377+
"apiVersions": [
378+
"v1"
379+
],
380+
"resources": [
381+
"serviceaccounts"
382+
],
383+
"scope": "Namespaced"
384+
}
385+
],
386+
"documentString": "Managed OpenShift Customers may not delete the service accounts under the managed namespaces。"
387+
},
367388
{
368389
"webhookName": "techpreviewnoupgrade-validation",
369390
"rules": [

pkg/testutils/testutils.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ import (
66
"net/http"
77
"net/http/httptest"
88

9-
responsehelper "github.com/openshift/managed-cluster-validating-webhooks/pkg/helpers"
10-
"github.com/openshift/managed-cluster-validating-webhooks/pkg/webhooks/utils"
119
admissionv1 "k8s.io/api/admission/v1"
1210
authenticationv1 "k8s.io/api/authentication/v1"
1311
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1412
"k8s.io/apimachinery/pkg/runtime"
1513
"k8s.io/apimachinery/pkg/types"
1614
admissionctl "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
15+
16+
responsehelper "github.com/openshift/managed-cluster-validating-webhooks/pkg/helpers"
17+
"github.com/openshift/managed-cluster-validating-webhooks/pkg/webhooks/utils"
1718
)
1819

1920
// Webhook interface
@@ -54,7 +55,7 @@ func CanCanNot(b bool) string {
5455
func CreateFakeRequestJSON(uid string,
5556
gvk metav1.GroupVersionKind, gvr metav1.GroupVersionResource,
5657
operation admissionv1.Operation,
57-
username string, userGroups []string,
58+
username string, userGroups []string, namespace string,
5859
obj, oldObject *runtime.RawExtension) ([]byte, error) {
5960

6061
req := admissionv1.AdmissionReview{
@@ -64,6 +65,7 @@ func CreateFakeRequestJSON(uid string,
6465
RequestKind: &gvk,
6566
Resource: gvr,
6667
Operation: operation,
68+
Namespace: namespace,
6769
UserInfo: authenticationv1.UserInfo{
6870
Username: username,
6971
Groups: userGroups,
@@ -96,9 +98,9 @@ func CreateFakeRequestJSON(uid string,
9698
func CreateHTTPRequest(uri, uid string,
9799
gvk metav1.GroupVersionKind, gvr metav1.GroupVersionResource,
98100
operation admissionv1.Operation,
99-
username string, userGroups []string,
101+
username string, userGroups []string, namespace string,
100102
obj, oldObject *runtime.RawExtension) (*http.Request, error) {
101-
req, err := CreateFakeRequestJSON(uid, gvk, gvr, operation, username, userGroups, obj, oldObject)
103+
req, err := CreateFakeRequestJSON(uid, gvk, gvr, operation, username, userGroups, namespace, obj, oldObject)
102104
if err != nil {
103105
return nil, err
104106
}

pkg/webhooks/add_serviceaccount.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package webhooks
2+
3+
import (
4+
"github.com/openshift/managed-cluster-validating-webhooks/pkg/webhooks/serviceaccount"
5+
)
6+
7+
func init() {
8+
Register(serviceaccount.WebhookName, func() Webhook { return serviceaccount.NewWebhook() })
9+
}

pkg/webhooks/clusterlogging/clusterlogging_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import (
44
"fmt"
55
"testing"
66

7-
"github.com/openshift/managed-cluster-validating-webhooks/pkg/testutils"
8-
"github.com/openshift/managed-cluster-validating-webhooks/pkg/webhooks/clusterlogging"
97
admissionv1 "k8s.io/api/admission/v1"
108
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
119
"k8s.io/apimachinery/pkg/runtime"
10+
11+
"github.com/openshift/managed-cluster-validating-webhooks/pkg/testutils"
12+
"github.com/openshift/managed-cluster-validating-webhooks/pkg/webhooks/clusterlogging"
1213
)
1314

1415
type clusterloggingTestSuite struct {
@@ -128,7 +129,7 @@ func runTests(t *testing.T, tests []clusterloggingTestSuite) {
128129
hook := clusterlogging.NewWebhook()
129130
httprequest, err := testutils.CreateHTTPRequest(hook.GetURI(),
130131
test.testID,
131-
metav1.GroupVersionKind{}, metav1.GroupVersionResource{}, test.operation, test.username, test.userGroups, obj, test.oldObject)
132+
metav1.GroupVersionKind{}, metav1.GroupVersionResource{}, test.operation, test.username, test.userGroups, "", obj, test.oldObject)
132133
if err != nil {
133134
t.Fatalf("Expected no error, got %s", err.Error())
134135
}

pkg/webhooks/clusterrolebinding/clusterrolebinding_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import (
55
"fmt"
66
"testing"
77

8-
"github.com/openshift/managed-cluster-validating-webhooks/pkg/testutils"
98
admissionv1 "k8s.io/api/admission/v1"
109
rbacv1 "k8s.io/api/rbac/v1"
1110
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1211

12+
"github.com/openshift/managed-cluster-validating-webhooks/pkg/testutils"
13+
1314
"k8s.io/apimachinery/pkg/runtime"
1415
)
1516

@@ -74,7 +75,7 @@ func runClusterRoleBindingTests(t *testing.T, tests []ClusterRoleBindingTestSuit
7475

7576
hook := NewWebhook()
7677
httprequest, err := testutils.CreateHTTPRequest(hook.GetURI(),
77-
test.testID, gvk, gvr, test.operation, test.username, test.userGroups, &obj, &oldObj)
78+
test.testID, gvk, gvr, test.operation, test.username, test.userGroups, "", &obj, &oldObj)
7879
if err != nil {
7980
t.Fatalf("Expected no error, got %s", err.Error())
8081
}

pkg/webhooks/hiveownership/hiveownership_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func runTests(t *testing.T, tests []hiveOwnershipTestSuites) {
6767
hook := NewWebhook()
6868
httprequest, err := testutils.CreateHTTPRequest(hook.GetURI(),
6969
test.testID,
70-
gvk, gvr, test.operation, test.username, test.userGroups, obj, test.oldObject)
70+
gvk, gvr, test.operation, test.username, test.userGroups, "", obj, test.oldObject)
7171
if err != nil {
7272
t.Fatalf("Expected no error, got %s", err.Error())
7373
}

pkg/webhooks/imagecontentpolicies/imagecontentpolicies_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ import (
77

88
configv1 "github.com/openshift/api/config/v1"
99
operatorv1alpha1 "github.com/openshift/api/operator/v1alpha1"
10-
"github.com/openshift/managed-cluster-validating-webhooks/pkg/testutils"
1110
admissionv1 "k8s.io/api/admission/v1"
1211
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1312
"k8s.io/apimachinery/pkg/runtime"
1413
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
14+
15+
"github.com/openshift/managed-cluster-validating-webhooks/pkg/testutils"
1516
)
1617

1718
func Test_authorizeImageDigestMirrorSet(t *testing.T) {
@@ -476,7 +477,7 @@ func TestImageContentPolicy(t *testing.T) {
476477
for _, test := range tests {
477478
t.Run(test.name, func(t *testing.T) {
478479
hook := NewWebhook()
479-
req, err := testutils.CreateHTTPRequest(hook.GetURI(), test.name, test.gvk, test.gvr, test.op, "", []string{}, test.obj, test.oldObj)
480+
req, err := testutils.CreateHTTPRequest(hook.GetURI(), test.name, test.gvk, test.gvr, test.op, "", []string{}, "", test.obj, test.oldObj)
480481
if err != nil {
481482
t.Errorf("failed to create test HTTP request: %v", err)
482483
}

0 commit comments

Comments
 (0)