-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Bug 1826414: Added validation for GCP Project ID access #3484
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
Bug 1826414: Added validation for GCP Project ID access #3484
Conversation
a91b1ed to
ea3538b
Compare
patrickdillon
left a comment
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.
This is off to a good start. This only adds validation for the TUI wizard. Validation also should be added for the install-config. That can be done by adding to the Validate function here: https://github.com/openshift/installer/blob/master/pkg/asset/installconfig/gcp/validation.go#L15
You should also add a test for a valid and invalid project.
The commit message begins with script: Not sure what that refers to.
| return svc, nil | ||
| } | ||
|
|
||
| //GetListOfProjects gets the list of project names and ids associated with the current user. |
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: there should be a space between // and the first word.
| } | ||
|
|
||
| //GetListOfProjects gets the list of project names and ids associated with the current user. | ||
| func (c *Client) GetListOfProjects(ctx context.Context) ([]string, []string, error) { |
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.
I think it's better for this to return a map or other data structure. I don't think it's good to rely on corresponding indices between two slices--what if you sort one?
pkg/asset/installconfig/gcp/gcp.go
Outdated
| ssn: ssn, | ||
| } | ||
|
|
||
| var listOfCombo []string |
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 better name would be something like projectChoices. Don't begin variable names with types or pseudo types like list; combo is vague.
pkg/asset/installconfig/gcp/gcp.go
Outdated
| Prompt: &survey.Select{ | ||
| Message: "Project", | ||
| Help: "The GCP project to be used for installation.", | ||
| Default: fmt.Sprintf("%s", defaultValue), |
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.
This is strange... Does this just evaluate to defaultValue so why usefmt.Sprintf?
pkg/asset/installconfig/gcp/gcp.go
Outdated
| listOfCombo = append(listOfCombo, listOfNames[i]+" ("+v+")") | ||
| } | ||
|
|
||
| defaultValue := listOfNames[index] + " (" + defaultProject + ")" |
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.
but you should be using fmt.Sprintf here.
pkg/asset/installconfig/gcp/gcp.go
Outdated
| if v == defaultProject { | ||
| index = i | ||
| } | ||
| listOfCombo = append(listOfCombo, listOfNames[i]+" ("+v+")") |
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.
the string formatting here should use fmt.Sprintf. Also I think it's more readable to have the string formatting in its own line and assigned to a variable, rather than as an arg to append.
pkg/asset/installconfig/gcp/gcp.go
Outdated
| Default: fmt.Sprintf("%s", defaultValue), | ||
| Options: listOfCombo, | ||
| }, | ||
| Validate: survey.ComposeValidators(survey.Required, func(ans interface{}) error { |
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.
I think survey already limits choices to Options so I don't think this extra validator is needed.
pkg/asset/installconfig/gcp/gcp.go
Outdated
| selectedProject = strings.SplitN(selectedProject, " (", 2)[1] | ||
| selectedProject = selectedProject[:len(selectedProject)-1] |
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.
Rather than all of this string manipulation, I think it's better when you create options to also create a map with option as key and the id as value.
pkg/asset/installconfig/gcp/gcp.go
Outdated
| Message: "Project", | ||
| Help: "The GCP project to be used for installation.", | ||
| Default: fmt.Sprintf("%s", defaultValue), | ||
| Options: listOfCombo, |
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.
I think it is good for the values passed to Options to be sorted.
18029ac to
83fa604
Compare
|
/bugzilla refresh |
1 similar comment
|
/bugzilla refresh |
|
@rna-afk: No Bugzilla bug is referenced in the title of this pull request. DetailsIn response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
|
/bugzilla refresh |
|
@rna-afk: An error was encountered adding this pull request to the external tracker bugs for bug 1826414 on the Bugzilla server at https://bugzilla.redhat.com:
DetailsIn response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
|
@rna-afk: This pull request references Bugzilla bug 1826414, which is valid. 3 validation(s) were run on this bug
DetailsIn response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
ef7884b to
1d82772
Compare
patrickdillon
left a comment
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.
Left a few more comments. This is in good shape.
Can you update the commit message heading to make it more obvious this relates to GCP?
There are two different conventions generally followed. You can start with something like
pkg/asset/installconfig/gcp: or GCP: but anything that gives more context is fine. Also in general commit messages should be imperative, so Fix error message for Invalid GCP Project ID entry instead of Fixed...
Also we removed the basedomain portion of this card so I was a little thrown initially by the PR title.
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 we change GetListOfProjects -> GetProjects ? Also if you're changing the comment, can you put in the comment that id is key of map and name is value?
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: personally I think just projects is better.
pkg/asset/installconfig/gcp/gcp.go
Outdated
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.
Don't include type in variable name. You could just call this projectIDs, or ids.
pkg/asset/installconfig/gcp/gcp.go
Outdated
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.
I think this line shows that these variable names are confusing.
What about something like:
for id, name := range projects {
option := fmt.Sprintf("%s (%s)", name, id)
ids[option] = id
if option == defaultProject {
defaultValue = option
}
options = append(options, option)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.
error strings like this should start with lowercase letters. Invalid -> invalid. https://github.com/golang/go/wiki/CodeReviewComments#error-strings
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 shorten all of this to:
gcpClient.EXPECT().GetListOfProjects(gomock.Any()).Return({"valid-project" : "valid-project"}, nil).AnyTimes()|
@rna-afk: This pull request references Bugzilla bug 1826414, which is valid. 3 validation(s) were run on this bug
DetailsIn response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
1d82772 to
c534fcb
Compare
|
You should add new test cases in: pkg/asset/installconfig/gcp/validation_test.go There is an existing test case for invalid project in the context of BYO networking. But your code is more general and catches an error with a minimal gcp platform with an invalid project. So I would recommend a similar test case, which only specifies a valid region and invalid project. You could use the editFunctions removeVPC and removeSubnets to make a minimal install config. |
c534fcb to
4e5147c
Compare
pkg/asset/installconfig/gcp/gcp.go
Outdated
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.
| return "", err | |
| return "", errors.Wrap(err, "failed to get projects") |
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.
try to include the path in the expectedErrMsg, makes it much clearer in terms of what we are testing.
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.
so this should be an InternalError, because it didn't have anything to do with the project field technically.
|
/test e2e-gcp |
|
@rna-afk: This pull request references Bugzilla bug 1826414, which is valid. 3 validation(s) were run on this bug
DetailsIn response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
4e5147c to
efd48f6
Compare
|
/approve |
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: abhinavdahiya The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
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.
Abhinav's point in #3484 (comment) was that field.Invalid -> field.InternalError. field.Invalid means the user's input was bad, but in this case there was an error when calling the gcloud API.
You could fix the preexisting code in a separate commit as well.
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.
Abhinav's point in #3484 (comment) was that field.Invalid -> field.InternalError.
I get it now thanks.
You could fix the preexisting code in a separate commit as well.
Got it. Will follow that next time.
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.
You could fix the preexisting code in a separate commit as well.
Got it. Will follow that next time.
No I meant that the earlier code (that I wrote) has the same issue and you could fix it in a separate commit.
Fixes the error message that occurs after the region selection when an invalid project ID is entered. Solved by picking up the list of projects that the user has in their GCP account and offers a select option on the project entry to select the one that they want. Reduces the chance of picking an invalid project ID and avoids the error. CloudResourceManager is used to pick up the list of projects for current user and survey is populated with the values obtained. Generated the gcpclient_generated.go mock file using mockgen and replaced the existing file to add the GetListOfProjects function. Running hack/go-genmock.sh did not work after I changed the interface and ran it.
efd48f6 to
6035d38
Compare
|
/lgtm |
|
/retest Please review the full test history for this PR and help us cut down flakes. |
|
@rna-afk: The following test failed, say
Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here. |
|
/retest Please review the full test history for this PR and help us cut down flakes. |
|
@rna-afk: All pull requests linked via external trackers have merged: openshift/installer#3484. Bugzilla bug 1826414 has been moved to the MODIFIED state. DetailsIn response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
|
Fixes #3044 |
GCP: Fix error message for Invalid Project ID entry
Fixes the error message that occurs after the region selection when an
invalid project ID is entered. Solved by picking up the list of projects
that the user has in their GCP account and offers a select option on the
project entry to select the one that they want. Reduces the chance of
picking an invalid project ID and avoids the error message.
Sample Output: