forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UPSTREAM: <carry>: Add RouteExternalCertificate validation in Route O…
…bjectValidator Co-authored-by: Thejas N <thn@redhat.com> Signed-off-by: Swarup Ghosh <swghosh@redhat.com>
- Loading branch information
Showing
5 changed files
with
188 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
openshift-kube-apiserver/admission/customresourcevalidation/route/validation_opts.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package route | ||
|
||
import ( | ||
"k8s.io/apiserver/pkg/util/feature" | ||
"k8s.io/component-base/featuregate" | ||
|
||
configv1 "github.com/openshift/api/config/v1" | ||
routecommon "github.com/openshift/library-go/pkg/route" | ||
) | ||
|
||
type RouteValidationOptionGetter interface { | ||
GetValidationOptions() routecommon.RouteValidationOptions | ||
} | ||
|
||
type RouteValidationOpts struct { | ||
opts routecommon.RouteValidationOptions | ||
} | ||
|
||
var _ RouteValidationOptionGetter = &RouteValidationOpts{} | ||
|
||
func NewRouteValidationOpts() *RouteValidationOpts { | ||
return &RouteValidationOpts{} | ||
} | ||
|
||
func (o *RouteValidationOpts) GetValidationOptions() routecommon.RouteValidationOptions { | ||
return o.opts | ||
} | ||
|
||
func (o *RouteValidationOpts) Initialize() *RouteValidationOpts { | ||
o.opts = routecommon.RouteValidationOptions{ | ||
AllowExternalCertificates: feature.DefaultMutableFeatureGate.Enabled(featuregate.Feature(configv1.FeatureGateRouteExternalCertificate)), | ||
} | ||
|
||
return o | ||
} |
95 changes: 95 additions & 0 deletions
95
openshift-kube-apiserver/admission/customresourcevalidation/route/validation_wrapper.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package route | ||
|
||
import ( | ||
"fmt" | ||
|
||
routev1 "github.com/openshift/api/route/v1" | ||
"github.com/openshift/library-go/pkg/apiserver/admission/admissionrestconfig" | ||
authorizationv1client "k8s.io/client-go/kubernetes/typed/authorization/v1" | ||
corev1client "k8s.io/client-go/kubernetes/typed/core/v1" | ||
"k8s.io/klog/v2" | ||
|
||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
utilruntime "k8s.io/apimachinery/pkg/util/runtime" | ||
"k8s.io/apiserver/pkg/admission" | ||
"k8s.io/client-go/rest" | ||
"k8s.io/kubernetes/openshift-kube-apiserver/admission/customresourcevalidation" | ||
) | ||
|
||
type validateCustomResourceWithClient struct { | ||
admission.ValidationInterface | ||
|
||
secretsGetter corev1client.SecretsGetter | ||
sarGetter authorizationv1client.SubjectAccessReviewsGetter | ||
routeValidationOptsGetter RouteValidationOptionGetter | ||
} | ||
|
||
func NewValidateRoute() (admission.Interface, error) { | ||
ret := &validateCustomResourceWithClient{} | ||
|
||
delegate, err := customresourcevalidation.NewValidator( | ||
map[schema.GroupResource]bool{ | ||
routev1.GroupVersion.WithResource("routes").GroupResource(): true, | ||
}, | ||
map[schema.GroupVersionKind]customresourcevalidation.ObjectValidator{ | ||
routev1.GroupVersion.WithKind("Route"): routeV1{ | ||
secretsGetter: ret.getSecretsGetter(), | ||
sarGetter: ret.getSubjectAccessReviewsGetter(), | ||
routeValidationOptsGetter: ret.getRouteValidationOptions(), | ||
}, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
ret.ValidationInterface = delegate | ||
|
||
klog.V(2).Infof("%s constructed Route validation client", PluginName) | ||
return ret, nil | ||
} | ||
|
||
var _ admissionrestconfig.WantsRESTClientConfig = &validateCustomResourceWithClient{} | ||
|
||
func (a *validateCustomResourceWithClient) getSecretsGetter() corev1client.SecretsGetter { | ||
return a.secretsGetter | ||
} | ||
|
||
func (a *validateCustomResourceWithClient) getSubjectAccessReviewsGetter() authorizationv1client.SubjectAccessReviewsGetter { | ||
return a.sarGetter | ||
} | ||
|
||
func (a *validateCustomResourceWithClient) getRouteValidationOptions() RouteValidationOptionGetter { | ||
return a.routeValidationOptsGetter | ||
} | ||
|
||
func (a *validateCustomResourceWithClient) SetRESTClientConfig(restClientConfig rest.Config) { | ||
var err error | ||
|
||
a.secretsGetter, err = corev1client.NewForConfig(&restClientConfig) | ||
if err != nil { | ||
utilruntime.HandleError(err) | ||
return | ||
} | ||
|
||
a.sarGetter, err = authorizationv1client.NewForConfig(&restClientConfig) | ||
if err != nil { | ||
utilruntime.HandleError(err) | ||
return | ||
} | ||
|
||
a.routeValidationOptsGetter = NewRouteValidationOpts().Initialize() | ||
} | ||
|
||
func (a *validateCustomResourceWithClient) ValidateInitialization() error { | ||
if a.secretsGetter == nil { | ||
return fmt.Errorf("%s needs a secretsGetter", PluginName) | ||
} | ||
if a.sarGetter == nil { | ||
return fmt.Errorf("%s needs a subjectAccessReviewsGetter", PluginName) | ||
} | ||
if a.routeValidationOptsGetter == nil { | ||
return fmt.Errorf("%s needs a routeValidationOptsGetter", PluginName) | ||
} | ||
|
||
klog.V(2).Infof("%s validator initialization successful", PluginName) | ||
return nil | ||
} |
41 changes: 41 additions & 0 deletions
41
openshift-kube-apiserver/admission/customresourcevalidation/route/validation_wrapper_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package route | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"k8s.io/client-go/kubernetes/fake" | ||
) | ||
|
||
// setter is only available in unit-tests | ||
func (a *validateCustomResourceWithClient) setupWithFakeClient() { | ||
c := fake.NewSimpleClientset() | ||
a.secretsGetter = c.CoreV1() | ||
a.sarGetter = c.AuthorizationV1() | ||
a.routeValidationOptsGetter = NewRouteValidationOpts().Initialize() | ||
} | ||
|
||
// TestRouteValidateInitialization will check if validator client is | ||
// conformant with admission.InitializationValidator interface | ||
func TestRouteValidateInitialization(t *testing.T) { | ||
admitter, err := NewValidateRoute() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
routeAdmitter, ok := admitter.(*validateCustomResourceWithClient) | ||
if !ok { | ||
t.Fatal(fmt.Errorf( | ||
"could not type cast returned value of NewValidateRoute() into type validateCustomResourceWithClient, " + | ||
"perhaps you changed the type in the implementation but not in the tests!")) | ||
} | ||
|
||
// unit test specific logic as a replacement for routeAdmitter.SetRESTClientConfig(...) | ||
routeAdmitter.setupWithFakeClient() | ||
|
||
// admission.InitializationValidator -> ValidateInitialization() | ||
err = routeAdmitter.ValidateInitialization() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters