Skip to content

Commit 9ff52f3

Browse files
committed
separate plugin configuration function and add tests
Signed-off-by: Bryce Palmer <[email protected]>
1 parent 2750c09 commit 9ff52f3

File tree

2 files changed

+86
-12
lines changed

2 files changed

+86
-12
lines changed

openshift-kube-apiserver/admission/authorization/restrictusers/restrictusers.go

+13-12
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,21 @@ import (
3030
const RestrictSubjectBindingsPluginName = "authorization.openshift.io/RestrictSubjectBindings"
3131

3232
func Register(plugins *admission.Plugins) {
33-
plugins.Register(RestrictSubjectBindingsPluginName,
34-
func(config io.Reader) (admission.Interface, error) {
35-
cfg, err := readConfig(config)
36-
if err != nil {
37-
return nil, err
38-
}
33+
plugins.Register(RestrictSubjectBindingsPluginName, pluginForConfig)
34+
}
3935

40-
if cfg != nil && cfg.OpenShiftOAuthDesiredState == v1alpha1.OpenShiftOAuthStateNotDesired {
41-
klog.Infof("Admission plugin %q configured to expect the OpenShift oauth-apiserver as not being available. This is effectively the same as disabling the plugin, so it will be disabled.", RestrictSubjectBindingsPluginName)
42-
return nil, nil
43-
}
36+
func pluginForConfig(config io.Reader) (admission.Interface, error) {
37+
cfg, err := readConfig(config)
38+
if err != nil {
39+
return nil, err
40+
}
41+
42+
if cfg != nil && cfg.OpenShiftOAuthDesiredState == v1alpha1.OpenShiftOAuthStateNotDesired {
43+
klog.Infof("Admission plugin %q configured to expect the OpenShift oauth-apiserver as not being available. This is effectively the same as disabling the plugin, so it will be disabled.", RestrictSubjectBindingsPluginName)
44+
return nil, nil
45+
}
4446

45-
return NewRestrictUsersAdmission()
46-
})
47+
return NewRestrictUsersAdmission()
4748
}
4849

4950
func readConfig(reader io.Reader) (*v1alpha1.RestrictSubjectBindingsAdmissionConfig, error) {

openshift-kube-apiserver/admission/authorization/restrictusers/restrictusers_test.go

+73
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package restrictusers
33
import (
44
"context"
55
"fmt"
6+
"io"
67
"strings"
78
"testing"
89

910
corev1 "k8s.io/api/core/v1"
11+
"k8s.io/apimachinery/pkg/api/equality"
1012
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1113
"k8s.io/apimachinery/pkg/runtime"
1214
"k8s.io/apimachinery/pkg/runtime/schema"
@@ -402,3 +404,74 @@ func TestAdmission(t *testing.T) {
402404
}
403405
}
404406
}
407+
408+
func TestPluginForConfig(t *testing.T) {
409+
testcases := []struct {
410+
name string
411+
config string
412+
expectedErr string
413+
expectedPlugin admission.Interface
414+
}{
415+
{
416+
name: "no config, no err, expect plugin",
417+
expectedPlugin: func() admission.Interface {
418+
plugin, _ := NewRestrictUsersAdmission()
419+
return plugin
420+
}(),
421+
},
422+
{
423+
name: "config sets openshiftOAuthDesiredState to NotDesired, no err, nil plugin",
424+
config: `apiVersion: authorization.openshift.io/v1alpha1
425+
kind: RestrictSubjectBindingsAdmissionConfig
426+
openshiftOAuthDesiredState: NotDesired
427+
`,
428+
expectedPlugin: nil,
429+
},
430+
{
431+
name: "config sets openshiftOAuthDesiredState to Desired, no err, expect plugin",
432+
config: `apiVersion: authorization.openshift.io/v1alpha1
433+
kind: RestrictSubjectBindingsAdmissionConfig
434+
openshiftOAuthDesiredState: Desired
435+
`,
436+
expectedPlugin: func() admission.Interface {
437+
plugin, _ := NewRestrictUsersAdmission()
438+
return plugin
439+
}(),
440+
},
441+
{
442+
name: "config sets openshiftOAuthDesiredState to invalid value, err, nil plugin",
443+
config: `apiVersion: authorization.openshift.io/v1alpha1
444+
kind: RestrictSubjectBindingsAdmissionConfig
445+
openshiftOAuthDesiredState: FooBar
446+
`,
447+
expectedPlugin: nil,
448+
expectedErr: "config is invalid, openshiftOAuthDesiredState must be one of Desired,NotDesired",
449+
},
450+
}
451+
452+
for _, tc := range testcases {
453+
t.Run(tc.name, func(t *testing.T) {
454+
var reader io.Reader
455+
if len(tc.config) > 0 {
456+
reader = strings.NewReader(tc.config)
457+
}
458+
459+
plugin, err := pluginForConfig(reader)
460+
switch {
461+
case len(tc.expectedErr) == 0 && err == nil:
462+
case len(tc.expectedErr) == 0 && err != nil:
463+
t.Errorf("%s: unexpected error: %v", tc.name, err)
464+
case len(tc.expectedErr) != 0 && err == nil:
465+
t.Errorf("%s: missing error: %v", tc.name, tc.expectedErr)
466+
case len(tc.expectedErr) != 0 && err != nil &&
467+
!strings.Contains(err.Error(), tc.expectedErr):
468+
t.Errorf("%s: missing error: expected %v, got %v",
469+
tc.name, tc.expectedErr, err)
470+
}
471+
472+
if !equality.Semantic.DeepEqual(tc.expectedPlugin, plugin) {
473+
t.Errorf("plugin does not match. expected %v, got %v", tc.expectedPlugin, plugin)
474+
}
475+
})
476+
}
477+
}

0 commit comments

Comments
 (0)