-
Notifications
You must be signed in to change notification settings - Fork 211
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
Validate format of GoogleServiceAccount in CCC #2287
base: master
Are you sure you want to change the base?
Conversation
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here.
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
} | ||
|
||
func validateGSAFormat(gsa string) error { | ||
if gsa == "" { // GoogleServiceAccount is a required field. We do not need to fail here. |
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.
Nit: do you mean not a required field? Or do you mean that it's required at the schema level, so this is unreachable?
Either way, I agree that you should just return nil for empty values!
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.
Yeah this code should be unreachable. It is a required field, so empty string will error early during CR creation.
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.
A readability rule of thumb is to always consider the function for its own and always handle the edge cases for itself, it would be easier to maintenance and less error prone if the other parts changed. i.e. somehow we decide to disable the required field check in the CRD level.
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.
If we fail here, that effectively means the GoogleServiceAccount is a required field, even if we mark it as optional in the CRD level. So I think we should not fail here.
if gsa == "" { // GoogleServiceAccount is a required field. We do not need to fail here. | ||
return nil | ||
} | ||
validGSAPattern := `^[A-Za-z0-9._%+\-]+@[a-z0-9.\-]+\.gserviceaccount.com$` |
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.
Where did you get this regex from? I found https://cloud.google.com/iam/docs/reference/rest/v1/projects.serviceAccounts/create which suggested a more restrictive regex for the first bit.
projectID matches description here https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects (which is good)
I'm actually more concerned about us rejecting valid gcpServiceAccount values though, so as long as our regex is too permissive, that's OK IMO!
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.
Where did you get this regex from
I think I got the regex from a back-and-forth discussion between me and AI...
I found https://cloud.google.com/iam/docs/reference/rest/v1/projects.serviceAccounts/create which suggested a more restrictive regex for the first bit.
Good find! We could certainly make the first bit more restrictive to match Cloud IAM, but I feel leaving it less restrictive has more benefits.
- If Cloud IAM update the regex in the future, our validation will likely still work.
- IMO the primary goal is to reject really wrong formats, like if user forgot to replace "${GSA}" with the actual email address, it should get rejected here. (today we don't reject it, and annotate KSA with any string user write to this field)
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.
LGTM, one nit on the comment and some background on the regex
Create a CCC object with invalid GSA.
CCC Status:
KCC operator logs: