-
Notifications
You must be signed in to change notification settings - Fork 225
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ package preflight | |
import ( | ||
"context" | ||
"fmt" | ||
"regexp" | ||
|
||
corev1beta1 "github.com/GoogleCloudPlatform/k8s-config-connector/operator/pkg/apis/core/v1beta1" | ||
"github.com/GoogleCloudPlatform/k8s-config-connector/operator/pkg/k8s" | ||
|
@@ -52,5 +53,21 @@ func (c *ConfigConnectorContextChecker) Preflight(_ context.Context, o declarati | |
return fmt.Errorf("spec.billingProject must be set if spec.requestProjectPolicy is set to %v", k8s.BillingProjectPolicy) | ||
} | ||
|
||
if err := validateGSAFormat(ccc.Spec.GoogleServiceAccount); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func validateGSAFormat(gsa string) error { | ||
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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more.
I think I got the regex from a back-and-forth discussion between me and AI...
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.
|
||
emailRegex := regexp.MustCompile(validGSAPattern) | ||
if !emailRegex.MatchString(gsa) { | ||
return fmt.Errorf("invalid GoogleServiceAccount format for %q", gsa) | ||
} | ||
return nil | ||
} |
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.