|
| 1 | +package apiserver |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + configv1 "github.com/openshift/api/config/v1" |
| 9 | + configlistersv1 "github.com/openshift/client-go/config/listers/config/v1" |
| 10 | + "github.com/openshift/cluster-kube-apiserver-operator/pkg/operator/configobservation" |
| 11 | + "github.com/openshift/library-go/pkg/operator/events" |
| 12 | + |
| 13 | + "k8s.io/apimachinery/pkg/api/equality" |
| 14 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 15 | + "k8s.io/apimachinery/pkg/util/diff" |
| 16 | + "k8s.io/client-go/tools/cache" |
| 17 | + clocktesting "k8s.io/utils/clock/testing" |
| 18 | + "k8s.io/utils/ptr" |
| 19 | +) |
| 20 | + |
| 21 | +func TestObserveAdmissionPlugins(t *testing.T) { |
| 22 | + for _, tt := range []struct { |
| 23 | + name string |
| 24 | + pluginCheckers []pluginCheckerFunc |
| 25 | + existingConfig map[string]any |
| 26 | + |
| 27 | + expectErrors bool |
| 28 | + expectedConfig map[string]any |
| 29 | + }{ |
| 30 | + { |
| 31 | + name: "no plugin checkers available", |
| 32 | + existingConfig: map[string]any{"key": "value"}, |
| 33 | + pluginCheckers: []pluginCheckerFunc{}, |
| 34 | + expectErrors: false, |
| 35 | + expectedConfig: map[string]any{}, |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "observer returns pruned config", |
| 39 | + existingConfig: map[string]any{ |
| 40 | + "key": "value", |
| 41 | + "apiServerArguments": map[string]any{ |
| 42 | + "enable-admission-plugins": []any{"enabled1", "enabled2"}, |
| 43 | + "disable-admission-plugins": []any{"disabled1", "disabled2"}, |
| 44 | + }, |
| 45 | + }, |
| 46 | + pluginCheckers: []pluginCheckerFunc{}, |
| 47 | + expectErrors: false, |
| 48 | + expectedConfig: map[string]any{ |
| 49 | + "apiServerArguments": map[string]any{ |
| 50 | + "enable-admission-plugins": []any{"enabled1", "enabled2"}, |
| 51 | + "disable-admission-plugins": []any{"disabled1", "disabled2"}, |
| 52 | + }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "plugin checker with error", |
| 57 | + existingConfig: map[string]any{"key": "value"}, |
| 58 | + pluginCheckers: []pluginCheckerFunc{ |
| 59 | + func(_ configobservation.Listers) ([]string, []string, error) { |
| 60 | + return nil, nil, fmt.Errorf("plugin checker error") |
| 61 | + }, |
| 62 | + }, |
| 63 | + expectErrors: true, |
| 64 | + expectedConfig: map[string]any{}, |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "plugin checkers must enable and disable plugins", |
| 68 | + existingConfig: map[string]any{"key": "value"}, |
| 69 | + pluginCheckers: []pluginCheckerFunc{ |
| 70 | + func(_ configobservation.Listers) ([]string, []string, error) { |
| 71 | + return []string{"enabled1"}, nil, nil |
| 72 | + }, |
| 73 | + func(_ configobservation.Listers) ([]string, []string, error) { |
| 74 | + return nil, []string{"disabled1"}, nil |
| 75 | + }, |
| 76 | + func(_ configobservation.Listers) ([]string, []string, error) { |
| 77 | + return []string{"enabled2"}, []string{"disabled2"}, nil |
| 78 | + }, |
| 79 | + }, |
| 80 | + expectErrors: false, |
| 81 | + expectedConfig: map[string]any{ |
| 82 | + "apiServerArguments": map[string]any{ |
| 83 | + "enable-admission-plugins": []any{"enabled1", "enabled2"}, |
| 84 | + "disable-admission-plugins": []any{"disabled1", "disabled2"}, |
| 85 | + }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + { |
| 89 | + name: "plugin checkers must overwrite existing enabled and disabled", |
| 90 | + existingConfig: map[string]any{ |
| 91 | + "key": "value", |
| 92 | + "apiServerArguments": map[string]any{ |
| 93 | + "enable-admission-plugins": []any{"another1"}, |
| 94 | + "disable-admission-plugins": []any{"another2"}, |
| 95 | + }, |
| 96 | + }, |
| 97 | + pluginCheckers: []pluginCheckerFunc{ |
| 98 | + func(_ configobservation.Listers) ([]string, []string, error) { |
| 99 | + return []string{"enabled1"}, nil, nil |
| 100 | + }, |
| 101 | + func(_ configobservation.Listers) ([]string, []string, error) { |
| 102 | + return nil, []string{"disabled1"}, nil |
| 103 | + }, |
| 104 | + func(_ configobservation.Listers) ([]string, []string, error) { |
| 105 | + return []string{"enabled2"}, []string{"disabled2"}, nil |
| 106 | + }, |
| 107 | + }, |
| 108 | + expectErrors: false, |
| 109 | + expectedConfig: map[string]any{ |
| 110 | + "apiServerArguments": map[string]any{ |
| 111 | + "enable-admission-plugins": []any{"enabled1", "enabled2"}, |
| 112 | + "disable-admission-plugins": []any{"disabled1", "disabled2"}, |
| 113 | + }, |
| 114 | + }, |
| 115 | + }, |
| 116 | + { |
| 117 | + name: "plugin checkers must return disjoint enabled and disabled plugin slices", |
| 118 | + pluginCheckers: []pluginCheckerFunc{ |
| 119 | + func(_ configobservation.Listers) ([]string, []string, error) { |
| 120 | + return []string{"enabled1", "enabled2"}, nil, nil |
| 121 | + }, |
| 122 | + func(_ configobservation.Listers) ([]string, []string, error) { |
| 123 | + return []string{"enabled3"}, []string{"enabled2"}, nil |
| 124 | + }, |
| 125 | + }, |
| 126 | + expectErrors: true, |
| 127 | + }, |
| 128 | + } { |
| 129 | + t.Run(tt.name, func(t *testing.T) { |
| 130 | + pluginCheckers = tt.pluginCheckers |
| 131 | + |
| 132 | + eventRecorder := events.NewInMemoryRecorder("TestObserveAdmissionPlugins", clocktesting.NewFakePassiveClock(time.Now())) |
| 133 | + listers := configobservation.Listers{} |
| 134 | + gotConfig, gotErrs := ObserveAdmissionPlugins(listers, eventRecorder, tt.existingConfig) |
| 135 | + |
| 136 | + if tt.expectErrors != (len(gotErrs) > 0) { |
| 137 | + t.Errorf("expected errors: %v; got %v", tt.expectErrors, gotErrs) |
| 138 | + } |
| 139 | + |
| 140 | + if !equality.Semantic.DeepEqual(tt.expectedConfig, gotConfig) { |
| 141 | + t.Errorf("unexpected config diff: %s", diff.ObjectReflectDiff(tt.expectedConfig, gotConfig)) |
| 142 | + } |
| 143 | + }) |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +func TestRoleBindingRestrictionPluginChecker(t *testing.T) { |
| 148 | + for _, tt := range []struct { |
| 149 | + name string |
| 150 | + authType *configv1.AuthenticationType |
| 151 | + expectedEnabled []string |
| 152 | + expectedDisabled []string |
| 153 | + expectError bool |
| 154 | + }{ |
| 155 | + { |
| 156 | + name: "authentication cluster not found", |
| 157 | + expectError: true, |
| 158 | + }, |
| 159 | + { |
| 160 | + name: "auth type IntegratedOAuth", |
| 161 | + authType: ptr.To(configv1.AuthenticationTypeIntegratedOAuth), |
| 162 | + expectedEnabled: []string{ |
| 163 | + "authorization.openshift.io/RestrictSubjectBindings", |
| 164 | + "authorization.openshift.io/ValidateRoleBindingRestriction", |
| 165 | + }, |
| 166 | + expectedDisabled: []string{}, |
| 167 | + expectError: false, |
| 168 | + }, |
| 169 | + { |
| 170 | + name: "auth type empty string", |
| 171 | + authType: ptr.To(configv1.AuthenticationType("")), |
| 172 | + expectedEnabled: []string{ |
| 173 | + "authorization.openshift.io/RestrictSubjectBindings", |
| 174 | + "authorization.openshift.io/ValidateRoleBindingRestriction", |
| 175 | + }, |
| 176 | + expectedDisabled: []string{}, |
| 177 | + expectError: false, |
| 178 | + }, |
| 179 | + { |
| 180 | + name: "auth type None", |
| 181 | + authType: ptr.To(configv1.AuthenticationTypeNone), |
| 182 | + expectedEnabled: []string{}, |
| 183 | + expectedDisabled: []string{ |
| 184 | + "authorization.openshift.io/RestrictSubjectBindings", |
| 185 | + "authorization.openshift.io/ValidateRoleBindingRestriction", |
| 186 | + }, |
| 187 | + expectError: false, |
| 188 | + }, |
| 189 | + { |
| 190 | + name: "auth type OIDC", |
| 191 | + authType: ptr.To(configv1.AuthenticationTypeOIDC), |
| 192 | + expectedEnabled: []string{}, |
| 193 | + expectedDisabled: []string{ |
| 194 | + "authorization.openshift.io/RestrictSubjectBindings", |
| 195 | + "authorization.openshift.io/ValidateRoleBindingRestriction", |
| 196 | + }, |
| 197 | + expectError: false, |
| 198 | + }, |
| 199 | + } { |
| 200 | + t.Run(tt.name, func(t *testing.T) { |
| 201 | + indexer := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{}) |
| 202 | + if tt.authType != nil { |
| 203 | + indexer.Add(&configv1.Authentication{ |
| 204 | + ObjectMeta: metav1.ObjectMeta{ |
| 205 | + Name: "cluster", |
| 206 | + }, |
| 207 | + Spec: configv1.AuthenticationSpec{ |
| 208 | + Type: *tt.authType, |
| 209 | + }, |
| 210 | + }) |
| 211 | + } |
| 212 | + |
| 213 | + listers := configobservation.Listers{ |
| 214 | + AuthConfigLister: configlistersv1.NewAuthenticationLister(indexer), |
| 215 | + } |
| 216 | + |
| 217 | + enabled, disabled, err := roleBindingRestrictionPluginChecker(listers) |
| 218 | + if tt.expectError != (err != nil) { |
| 219 | + t.Errorf("expected errors: %v; got %v", tt.expectError, err) |
| 220 | + } |
| 221 | + |
| 222 | + if !equality.Semantic.DeepEqual(tt.expectedEnabled, enabled) { |
| 223 | + t.Errorf("unexpected enabled plugins: %s", diff.ObjectReflectDiff(tt.expectedEnabled, enabled)) |
| 224 | + } |
| 225 | + |
| 226 | + if !equality.Semantic.DeepEqual(tt.expectedDisabled, disabled) { |
| 227 | + t.Errorf("unexpected disabled plugins: %s", diff.ObjectReflectDiff(tt.expectedDisabled, disabled)) |
| 228 | + } |
| 229 | + }) |
| 230 | + } |
| 231 | +} |
0 commit comments