Skip to content
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

Fixing bug when looking up plugins on path with slash in argument #1415

Merged

Conversation

psschwei
Copy link
Contributor

@psschwei psschwei commented Aug 4, 2021

Description

Per discussion in PR 1412, found a bug where we fail to look up in path when there's slash in arguments. (This bug was also causing the service import test to fail).

Changes

  • fixing bug when looking up plugins on path with slash in argument

(changelog entry to follow)

/lint

@google-cla google-cla bot added the cla: yes Indicates the PR's author has signed the CLA. label Aug 4, 2021
Copy link
Contributor

@knative-prow-robot knative-prow-robot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@psschwei: 0 warnings.

In response to this:

Description

Per discussion in PR 1412, found a bug where we fail to look up in path when there's slash in arguments. (This bug was also causing the service import test to fail).

Changes

  • fixing bug when looking up plugins on path with slash in argument

(changelog entry to follow)

/lint

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.

@knative-prow-robot knative-prow-robot added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label Aug 4, 2021
@codecov
Copy link

codecov bot commented Aug 4, 2021

Codecov Report

Merging #1415 (5d1e08d) into main (9d1bfc6) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #1415   +/-   ##
=======================================
  Coverage   78.51%   78.51%           
=======================================
  Files         160      160           
  Lines        8283     8285    +2     
=======================================
+ Hits         6503     6505    +2     
  Misses       1097     1097           
  Partials      683      683           
Impacted Files Coverage Δ
pkg/kn/plugin/manager.go 84.14% <100.00%> (+0.19%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9d1bfc6...5d1e08d. Read the comment docs.

@dsimansk
Copy link
Contributor

dsimansk commented Aug 4, 2021

@psschwei thansk!
Could you please try to add unit test that would verify plugin with slash in arguments won't have that slash part in name maybe? If it's too cumbersome to reach that meaningful coverage, I'd happy to go with it.

@rhuss as a lesson here, maybe it isn't a good idea to have file to path in args. I recall some review feedback when working on kn service import. :)

@psschwei
Copy link
Contributor Author

psschwei commented Aug 4, 2021

As I'm writing a test, I have a question about what the assumptions are for plugin arguments.

Take this example:

kn plugin test /with/slash argument

According to the logic in this PR, that would "find" a plugin called kn-plugin-test-argument (i.e. it would simply drop the argument containing a slash). Is that the behavior we'd expect? Or do we expect that kn would drop ALL terms after and including one with a slash (i.e. our plugin would be kn-plugin-test and /with/slash and argument are parameters for the plugin)?

If it's the latter, then I think we'd need to update the logic some to parse the initial parts slice before processing.

@dsimansk
Copy link
Contributor

dsimansk commented Aug 4, 2021

Or do we expect that kn would drop ALL terms after and including one with a slash (i.e. our plugin would be kn-plugin-test and /with/slash and argument are parameters for the plugin)?

Yes, I believe this is already true with current implementation.

I hope we may safely assume that there won't plugin sub-command with slashes in name.

Handling of plugin name and arguments is done by the outer for-loop that's moving through params array and reducing it.
Until the plugin can recognized/found, the plugin object is returned. In addition, original args are preserved for the execution time. Therefore dropping arg with slashes in look-up function shouldn't result in missing arg in execution.

I believe it should go like this.

kn plugin test arg0 /arg1/slash arg2
/arg1/slash => always dropped due to slashes breaking on path lookup
kn plugin test arg0 arg2 => plugin not found
kn plugin test arg0  => plugin not found
kn plugin test  => found 

//the rest of original array [arg0, /arg1/slash/, arg2] are arguments to the plugin cmd "kn plugin test"

@knative-prow-robot knative-prow-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Aug 4, 2021
@psschwei
Copy link
Contributor Author

psschwei commented Aug 4, 2021

Got it -- I think I was confusing the plugin lookup and actually executing the plugin :)

@dsimansk
Copy link
Contributor

dsimansk commented Aug 5, 2021

Thanks for the fix & the test coverage @psschwei!

@rhuss @maximilien can you please take a look?

/lgtm

@knative-prow-robot knative-prow-robot added the lgtm Indicates that a PR is ready to be merged. label Aug 5, 2021
@@ -401,6 +401,10 @@ func findMostSpecificPluginInPath(dir string, parts []string, lookupInPath bool)
var nameParts []string
var commandParts []string
for _, p := range parts[0:i] {
// skip arguments with slashes
if strings.Contains(p, "/") || strings.Contains(p, "\\") {
Copy link
Contributor

@rhuss rhuss Aug 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that you should just skip it, but completely stop the loop when the first part appears that can not be used as a plugin part, otherwise, you could call plugins accidentally, e.g.

kn myplugin not/possible bla

would also call an existing plugin kn-myplugin-bla, but this is not following the plugin contract.

Also, you should ensure that you only filter characters that are not possible on the current OS filesystem. E.g. on UNIX you can have a backslash as part of a file name, so a plugin like kn-myplugin-ext\ra is possible on UNIX (but not on Windows). So you should filter on the concrete file separator.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, you should ensure that you only filter characters that are not possible on the current's OS filesystem. E.g. on UNIX you can have a backslash as part of a file name, so a plugin like kn-myplugin-ext\ra is possible on UNIX (but not on Windows). So you should filter on the concrete file separator.

+1 that's a great point.

@knative-prow-robot knative-prow-robot removed the lgtm Indicates that a PR is ready to be merged. label Aug 5, 2021
Copy link
Contributor

@maximilien maximilien left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/ok-to-test

@knative-prow-robot knative-prow-robot added the ok-to-test Indicates a non-member PR verified by an org member that is safe to test. label Aug 5, 2021
fmt.Println(tmpPathDir)

createTestPluginInDirectory(t, "kn-path-test", tmpPathDir)
pluginCommands := []string{"path", "test", "/withslash"}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test should also use a os.PathSeparator otherwise it will fail on Windows.

Also I would suggest to use a loop and multiple tests, e.g.

path test /withslash
path test /withslash extra arg
path test ext\ra  (using something like `os.PathSeparator == '/' ? "\\" : "/"`)

@psschwei psschwei force-pushed the fix-lookup-path-with-slash branch from bb380a8 to 5d1e08d Compare August 6, 2021 15:30
@knative-metrics-robot
Copy link

The following is the coverage report on the affected files.
Say /test pull-knative-client-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/kn/plugin/manager.go 89.8% 89.9% 0.1

Copy link
Contributor

@rhuss rhuss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/approve
/lgtm

thanks !

@knative-prow-robot knative-prow-robot added the lgtm Indicates that a PR is ready to be merged. label Aug 6, 2021
@knative-prow-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: psschwei, rhuss

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@knative-prow-robot knative-prow-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Aug 6, 2021
@knative-prow-robot knative-prow-robot merged commit 60c740f into knative:main Aug 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cla: yes Indicates the PR's author has signed the CLA. lgtm Indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants