This repository has been archived by the owner on Jun 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
Validate fields of OpenstackDataPlaneServiceCert in OpenStackDataPlaneServiceSpec #852
Draft
jpodivin
wants to merge
1
commit into
openstack-k8s-operators:main
Choose a base branch
from
jpodivin:enumtls
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+49
−6
Draft
Changes from all commits
Commits
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 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 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 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 |
---|---|---|
|
@@ -17,6 +17,9 @@ limitations under the License. | |
package v1beta1 | ||
|
||
import ( | ||
"fmt" | ||
|
||
"golang.org/x/exp/slices" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
|
@@ -78,9 +81,15 @@ func (r *OpenStackDataPlaneService) ValidateCreate() (admission.Warnings, error) | |
} | ||
|
||
func (r *OpenStackDataPlaneServiceSpec) ValidateCreate() field.ErrorList { | ||
// TODO(user): fill in your validation logic upon object creation. | ||
var errs field.ErrorList | ||
|
||
return field.ErrorList{} | ||
if r.TLSCerts != nil { | ||
for _, v := range r.TLSCerts { | ||
errs = append(errs, v.ValidateContents()...) | ||
} | ||
} | ||
|
||
return errs | ||
} | ||
|
||
func (r *OpenStackDataPlaneService) ValidateUpdate(original runtime.Object) (admission.Warnings, error) { | ||
|
@@ -99,9 +108,15 @@ func (r *OpenStackDataPlaneService) ValidateUpdate(original runtime.Object) (adm | |
} | ||
|
||
func (r *OpenStackDataPlaneServiceSpec) ValidateUpdate() field.ErrorList { | ||
// TODO(user): fill in your validation logic upon object creation. | ||
var errs field.ErrorList | ||
|
||
return field.ErrorList{} | ||
if r.TLSCerts != nil { | ||
for _, v := range r.TLSCerts { | ||
errs = append(errs, v.ValidateContents()...) | ||
} | ||
} | ||
|
||
return errs | ||
} | ||
|
||
func (r *OpenStackDataPlaneService) ValidateDelete() (admission.Warnings, error) { | ||
|
@@ -125,3 +140,22 @@ func (r *OpenStackDataPlaneServiceSpec) ValidateDelete() field.ErrorList { | |
|
||
return field.ErrorList{} | ||
} | ||
|
||
func (r *OpenstackDataPlaneServiceCert) ValidateContents() field.ErrorList { | ||
|
||
var errs field.ErrorList | ||
// "dnsnames" and "ips" are only allowed usages | ||
allowedContents := []string{ | ||
"dnsnames", | ||
"ips", | ||
} | ||
for _, val := range r.Contents { | ||
|
||
if !slices.Contains(allowedContents, val) { | ||
errs = append(errs, field.Invalid(field.NewPath("spec.tlsCert.Contents"), | ||
r.KeyUsages, | ||
fmt.Sprintf("error validating contents of TLSCert, %s, only valid contents are %v ", val, allowedContents))) | ||
} | ||
} | ||
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. Um -- this validation function is all confused. You are defining the allowed contents for "Contents" and checking that - while putting up an error message associated with KeyUsages. That said - if we changed Contents to be an enum - with the appropriate default - would this whole function be necessary at all? 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. That's because it started is function for keyusages. I wanted to see the test results, before rewriting the comment's, logs etc. |
||
return errs | ||
} |
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
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
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.
Making the default explicit is good. I thought though, that when you mentioned this tightening of the spec would include turning the Contents field into an enum - with "dnsnames" and "ips" as the two possible options. That would result in validation errors in the same way that KeyUsages currently does.
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.
Contents is list, not a string, enum wouldn't work there because you need list a approved values. Hence the
ValidateContents
method.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.
Yes but we can -- and that what I thought you wanted to do -- redefine Contents as a list of enums - just like keyusages is a list of certmgrv1.KeyUsage. We can define a new enum of CertificateContents for example.
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.
That isn't really the point. Kubebuilder
enum
[0] should be used in cases when field must have only one of the values from approved a list. Not when the field is supposed to be a list of approved values.Enum validation just doesn't fit our use case here, and we shouldn't try to change the API just to make it fit.
Putting the validation in a function is, imho, a cleaner approach.
[0] https://book.kubebuilder.io/reference/markers/crd-validation.html
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.
Maybe enum wasn't the right thing here. My point is that we expect contents to be one of a set of approved values. KeyUsages (https://github.com/cert-manager/cert-manager/blob/eb9f8880fdf9a02c2f85e73cc08617c350717378/pkg/api/util/usages.go#L26) is a similar thing, and we can use an equivalent structure to make that explicit - and thereby enforce it in the same way that KeyUsages are enforced.