-
Notifications
You must be signed in to change notification settings - Fork 260
CFE-882: Route external certificate validation #1549
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
thejasn
wants to merge
3
commits into
openshift:master
from
thejasn:feature/route-external-certificate
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,60 @@ | ||
| package route | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| authorizationv1 "k8s.io/api/authorization/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/util/validation/field" | ||
| "k8s.io/apiserver/pkg/endpoints/request" | ||
|
|
||
| routev1 "github.com/openshift/api/route/v1" | ||
| "github.com/openshift/library-go/pkg/authorization/authorizationutil" | ||
| ) | ||
|
|
||
| // SubjectAccessReviewCreator is an interface for performing subject access reviews | ||
| type SubjectAccessReviewCreator interface { | ||
| Create(ctx context.Context, sar *authorizationv1.SubjectAccessReview, opts metav1.CreateOptions) (*authorizationv1.SubjectAccessReview, error) | ||
| } | ||
|
|
||
| // RouteValidationOptions used to tweak how/what fields are validated. These | ||
| // options are propagated by the apiserver. | ||
| type RouteValidationOptions struct { | ||
|
|
||
| // AllowExternalCertificates option is set when RouteExternalCertificate | ||
| AllowExternalCertificates bool | ||
| } | ||
|
|
||
| // CheckRouteCustomHostSAR checks if user has permission to create and update routes/custom-host | ||
| // sub-resource | ||
| func CheckRouteCustomHostSAR(ctx context.Context, fldPath *field.Path, sarc SubjectAccessReviewCreator) field.ErrorList { | ||
|
|
||
| var errs field.ErrorList | ||
| user, ok := request.UserFrom(ctx) | ||
| if !ok { | ||
| return field.ErrorList{field.InternalError(fldPath, fmt.Errorf("unable to verify access"))} | ||
| } | ||
|
|
||
| if err := authorizationutil.Authorize(sarc, user, &authorizationv1.ResourceAttributes{ | ||
| Namespace: request.NamespaceValue(ctx), | ||
| Verb: "create", | ||
| Group: routev1.GroupName, | ||
| Resource: "routes", | ||
| Subresource: "custom-host", | ||
| }); err != nil { | ||
| errs = append(errs, field.Forbidden(fldPath, "user does not have create permission on custom-host")) | ||
| } | ||
|
|
||
| if err := authorizationutil.Authorize(sarc, user, &authorizationv1.ResourceAttributes{ | ||
| Namespace: request.NamespaceValue(ctx), | ||
| Verb: "update", | ||
| Group: routev1.GroupName, | ||
| Resource: "routes", | ||
| Subresource: "custom-host", | ||
| }); err != nil { | ||
| errs = append(errs, field.Forbidden(fldPath, "user does not have update permission on custom-host")) | ||
| } | ||
|
|
||
| return errs | ||
| } | ||
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ import ( | |
| "k8s.io/apiserver/pkg/endpoints/request" | ||
|
|
||
| routev1 "github.com/openshift/api/route/v1" | ||
| "github.com/openshift/library-go/pkg/route" | ||
| ) | ||
|
|
||
| type testAllocator struct { | ||
|
|
@@ -52,6 +53,13 @@ func TestHostWithWildcardPolicies(t *testing.T) { | |
| expected string | ||
| expectedSubdomain string | ||
|
|
||
| // fields for externalCertificate | ||
| validType bool | ||
| secretData map[string]string | ||
| validNS bool | ||
|
Comment on lines
+57
to
+59
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as in |
||
| opts route.RouteValidationOptions | ||
| err error | ||
|
|
||
| errs int | ||
| allow bool | ||
| }{ | ||
|
|
@@ -215,6 +223,62 @@ func TestHostWithWildcardPolicies(t *testing.T) { | |
| allow: false, | ||
| errs: 1, | ||
| }, | ||
| { | ||
| name: "external-certificate-changed-from-certificate", | ||
| host: "host", | ||
| expected: "host", | ||
| oldHost: "host", | ||
| tls: &routev1.TLSConfig{Termination: routev1.TLSTerminationEdge, Certificate: "a"}, | ||
| oldTLS: &routev1.TLSConfig{Termination: routev1.TLSTerminationEdge, ExternalCertificate: &routev1.LocalObjectReference{Name: "b"}}, | ||
| wildcardPolicy: routev1.WildcardPolicyNone, | ||
| allow: false, | ||
| validNS: true, | ||
| errs: 1, | ||
|
|
||
| opts: route.RouteValidationOptions{AllowExternalCertificates: true}, | ||
| }, | ||
| { | ||
| name: "certificate-changed-from-external-certificate", | ||
| host: "host", | ||
| expected: "host", | ||
| oldHost: "host", | ||
| tls: &routev1.TLSConfig{Termination: routev1.TLSTerminationEdge, ExternalCertificate: &routev1.LocalObjectReference{Name: "b"}}, | ||
| oldTLS: &routev1.TLSConfig{Termination: routev1.TLSTerminationEdge, Certificate: "a"}, | ||
| wildcardPolicy: routev1.WildcardPolicyNone, | ||
| allow: false, | ||
| validNS: true, | ||
| errs: 1, | ||
|
|
||
| opts: route.RouteValidationOptions{AllowExternalCertificates: true}, | ||
| }, | ||
| { | ||
| name: "external-certificate-changed", | ||
| host: "host", | ||
| expected: "host", | ||
| oldHost: "host", | ||
| tls: &routev1.TLSConfig{Termination: routev1.TLSTerminationEdge, ExternalCertificate: &routev1.LocalObjectReference{Name: "a"}}, | ||
| oldTLS: &routev1.TLSConfig{Termination: routev1.TLSTerminationEdge, ExternalCertificate: &routev1.LocalObjectReference{Name: "old-b"}}, | ||
| wildcardPolicy: routev1.WildcardPolicyNone, | ||
| allow: false, | ||
| validNS: true, | ||
| errs: 1, | ||
|
|
||
| opts: route.RouteValidationOptions{AllowExternalCertificates: true}, | ||
| }, | ||
| { | ||
| name: "external-certificate-secret-changed", | ||
| host: "host", | ||
| expected: "host", | ||
| oldHost: "host", | ||
| tls: &routev1.TLSConfig{Termination: routev1.TLSTerminationEdge, ExternalCertificate: &routev1.LocalObjectReference{Name: "a"}}, | ||
| oldTLS: &routev1.TLSConfig{Termination: routev1.TLSTerminationEdge, ExternalCertificate: &routev1.LocalObjectReference{Name: "a"}}, | ||
| wildcardPolicy: routev1.WildcardPolicyNone, | ||
| allow: false, | ||
| validNS: true, | ||
| errs: 0, | ||
|
|
||
| opts: route.RouteValidationOptions{AllowExternalCertificates: true}, | ||
| }, | ||
| { | ||
| name: "ca-certificate-unchanged", | ||
| host: "host", | ||
|
|
@@ -368,9 +432,9 @@ func TestHostWithWildcardPolicies(t *testing.T) { | |
| }, | ||
| }, | ||
| } | ||
| errs = ValidateHostUpdate(ctx, route, oldRoute, &testSAR{allow: tc.allow}) | ||
| errs = ValidateHostUpdate(ctx, route, oldRoute, &testSAR{allow: tc.allow}, tc.opts) | ||
| } else { | ||
| errs = AllocateHost(ctx, route, &testSAR{allow: tc.allow}, testAllocator{}) | ||
| errs = AllocateHost(ctx, route, &testSAR{allow: tc.allow}, testAllocator{}, tc.opts) | ||
| } | ||
|
|
||
| if route.Spec.Host != tc.expected { | ||
|
|
||
This file contains hidden or 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,34 @@ | ||
| package hostassignment | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "k8s.io/apimachinery/pkg/util/validation/field" | ||
|
|
||
| routev1 "github.com/openshift/api/route/v1" | ||
| routecommon "github.com/openshift/library-go/pkg/route" | ||
| ) | ||
|
|
||
| // validateHostExternalCertificate checks if the user has permissions to create and update | ||
| // custom-host subresource of routes. This check is required to be done prior to ValidateHostUpdate() | ||
| // since updating hosts while using externalCertificate is contingent on the user having both these | ||
| // permissions. The ValidateHostUpdate() cannot differentiate if the certificate has changed since | ||
| // now the certificates will be present as a secret object, due to this it proceeds with the assumption | ||
| // that the certificate has changed when the route has externalCertificate set. | ||
| func ValidateHostExternalCertificate(ctx context.Context, new, older *routev1.Route, sarc routecommon.SubjectAccessReviewCreator, opts routecommon.RouteValidationOptions) field.ErrorList { | ||
| newTLS := new.Spec.TLS | ||
| oldTLS := older.Spec.TLS | ||
|
|
||
| if !opts.AllowExternalCertificates { | ||
| // return nil since if the feature gate is off | ||
| // ValidateHostUpdate() is sufficient to validate | ||
| // permissions | ||
| return nil | ||
| } | ||
|
|
||
| if (newTLS != nil && newTLS.ExternalCertificate != nil) || (oldTLS != nil && oldTLS.ExternalCertificate != nil) { | ||
| return routecommon.CheckRouteCustomHostSAR(ctx, field.NewPath("spec", "TLS", "externalCertificate"), sarc) | ||
| } | ||
|
|
||
| return nil | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please provide a comment which verbs and sub resources are supposed to be checked?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolved.