Skip to content

Commit b333d23

Browse files
committed
Allow osde2e to modify SCCs
osde2e runs a conformance test which needs to be able to modify SCCs https://github.com/openshift/kubernetes/blob/e994e5dbd69833088514616351b0aa997e4ed79d/openshift-hack/e2e/namespace.go#L64-L71 Signed-off-by: Michael Shen <[email protected]>
1 parent b1752fa commit b333d23

File tree

2 files changed

+18
-23
lines changed

2 files changed

+18
-23
lines changed

pkg/webhooks/scc/scc.go

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,22 @@ package scc
33
import (
44
"fmt"
55
"net/http"
6-
"os"
6+
"regexp"
77
"slices"
88

99
securityv1 "github.com/openshift/api/security/v1"
1010
"github.com/openshift/managed-cluster-validating-webhooks/pkg/webhooks/utils"
1111
admissionv1 "k8s.io/api/admission/v1"
1212
admissionregv1 "k8s.io/api/admissionregistration/v1"
13-
corev1 "k8s.io/api/core/v1"
1413
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1514
"k8s.io/apimachinery/pkg/runtime"
1615
logf "sigs.k8s.io/controller-runtime/pkg/log"
1716
admissionctl "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
1817
)
1918

2019
const (
21-
WebhookName string = "scc-validation"
22-
docString string = `Managed OpenShift Customers may not modify the following default SCCs: %s`
20+
WebhookName = "scc-validation"
21+
docString = `Managed OpenShift Customers may not modify the following default SCCs: %s`
2322
)
2423

2524
var (
@@ -42,8 +41,8 @@ var (
4241
"system:serviceaccount:openshift-cluster-version:default",
4342
"system:admin",
4443
}
45-
allowedGroups = []string{}
46-
defaultSCCs = []string{
44+
allowedGroupsRe = regexp.MustCompile("^system:serviceaccounts:osde2e-(h-)?[a-z0-9]{5}")
45+
defaultSCCs = []string{
4746
"anyuid",
4847
"hostaccess",
4948
"hostmount-anyuid",
@@ -59,25 +58,13 @@ var (
5958
)
6059

6160
type SCCWebHook struct {
62-
s runtime.Scheme
61+
scheme *runtime.Scheme
6362
}
6463

6564
// NewWebhook creates the new webhook
6665
func NewWebhook() *SCCWebHook {
67-
scheme := runtime.NewScheme()
68-
err := admissionv1.AddToScheme(scheme)
69-
if err != nil {
70-
log.Error(err, "Fail adding admissionsv1 scheme to SCCWebHook")
71-
os.Exit(1)
72-
}
73-
err = corev1.AddToScheme(scheme)
74-
if err != nil {
75-
log.Error(err, "Fail adding corev1 scheme to SCCWebHook")
76-
os.Exit(1)
77-
}
78-
7966
return &SCCWebHook{
80-
s: *scheme,
67+
scheme: runtime.NewScheme(),
8168
}
8269
}
8370

@@ -117,7 +104,7 @@ func (s *SCCWebHook) authorized(request admissionctl.Request) admissionctl.Respo
117104

118105
// renderSCC render the SCC object from the requests
119106
func (s *SCCWebHook) renderSCC(request admissionctl.Request) (*securityv1.SecurityContextConstraints, error) {
120-
decoder, err := admissionctl.NewDecoder(&s.s)
107+
decoder, err := admissionctl.NewDecoder(s.scheme)
121108
if err != nil {
122109
return nil, err
123110
}
@@ -139,8 +126,8 @@ func isAllowedUserGroup(request admissionctl.Request) bool {
139126
return true
140127
}
141128

142-
for _, group := range allowedGroups {
143-
if slices.Contains(request.UserInfo.Groups, group) {
129+
for _, group := range request.UserInfo.Groups {
130+
if allowedGroupsRe.Match([]byte(group)) {
144131
return true
145132
}
146133
}

pkg/webhooks/scc/scc_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,14 @@ func TestUserPositive(t *testing.T) {
167167
userGroups: []string{"system:authenticated", "system:authenticated:oauth"},
168168
shouldBeAllowed: true,
169169
},
170+
{
171+
targetSCC: "privileged",
172+
testID: "osde2e-serviceaccounts-are-allowed",
173+
username: "system:serviceaccount:osde2e-abcde:osde2e-runner",
174+
operation: admissionv1.Update,
175+
userGroups: []string{"system:authenticated", "system:serviceaccounts:osde2e-abcde"},
176+
shouldBeAllowed: true,
177+
},
170178
}
171179
runSCCTests(t, tests)
172180
}

0 commit comments

Comments
 (0)