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

feat(cli): Add Plugin Support to the Argo CD CLI #20074

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

nitishfy
Copy link
Member

@nitishfy nitishfy commented Sep 24, 2024

Ref: #19624
Checklist:

  • Either (a) I've created an enhancement proposal and discussed it with the community, (b) this is a bug fix, or (c) this does not need to be in the release notes.
  • The title of the PR states what changed and the related issues number (used for the release note).
  • The title of the PR conforms to the Toolchain Guide
  • I've included "Closes [ISSUE #]" or "Fixes [ISSUE #]" in the description to automatically close the associated issue.
  • I've updated both the CLI and UI to expose my feature, or I plan to submit a second PR with them.
  • Does this PR require documentation updates?
  • I've updated documentation as required by this PR.
  • I have signed off all my commits as required by DCO
  • I have written unit and/or e2e tests for my change. PRs without these are unlikely to be merged.
  • My build is green (troubleshooting builds).
  • My new feature complies with the feature status guidelines.
  • I have added a brief description of why this PR is necessary and/or what this PR solves.
  • Optional. My organization is added to USERS.md.
  • Optional. For bug fixes, I've indicated what older releases this fix should be cherry-picked into (this may or may not happen depending on risk/complexity).

Copy link

bunnyshell bot commented Sep 24, 2024

🔴 Preview Environment stopped on Bunnyshell

See: Environment Details | Pipeline Logs

Available commands (reply to this comment):

  • 🔵 /bns:start to start the environment
  • 🚀 /bns:deploy to redeploy the environment
  • /bns:delete to remove the environment

Copy link

bunnyshell bot commented Sep 24, 2024

✅ Preview Environment created on Bunnyshell but will not be auto-deployed

See: Environment Details

Available commands (reply to this comment):

  • 🚀 /bns:deploy to deploy the environment

Copy link

codecov bot commented Sep 24, 2024

Codecov Report

Attention: Patch coverage is 80.76923% with 25 lines in your changes missing coverage. Please review.

Please upload report for BASE (master@e66068c). Learn more about missing BASE report.
Report is 14 commits behind head on master.

Files with missing lines Patch % Lines
cmd/argocd/commands/plugin.go 85.71% 12 Missing and 3 partials ⚠️
cmd/main.go 60.00% 9 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff            @@
##             master   #20074   +/-   ##
=========================================
  Coverage          ?   59.07%           
=========================================
  Files             ?      338           
  Lines             ?    57159           
  Branches          ?        0           
=========================================
  Hits              ?    33768           
  Misses            ?    20579           
  Partials          ?     2812           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@leoluz leoluz left a comment

Choose a reason for hiding this comment

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

Please consider adding a new documentation page explaining how to write plugins and how to install and consume them.

cmd/argocd/commands/root.go Outdated Show resolved Hide resolved
cmd/argocd/commands/root.go Outdated Show resolved Hide resolved
cmd/argocd/commands/root.go Outdated Show resolved Hide resolved
@leoluz leoluz self-assigned this Sep 24, 2024
@nitishfy nitishfy force-pushed the nitish/argocd-plugin-support branch 2 times, most recently from 92967cd to 4a260dd Compare October 1, 2024 11:40
Copy link
Member Author

@nitishfy nitishfy left a comment

Choose a reason for hiding this comment

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

plugin_test.go is kept in separate package because keeping it in the util will create circular dependency.

Update: Resolved

@nitishfy nitishfy force-pushed the nitish/argocd-plugin-support branch 2 times, most recently from 0e8f338 to c314e43 Compare October 8, 2024 11:35
@nitishfy nitishfy changed the title WIP: Add Plugin Support to the Argo CD CLI feat: Add Plugin Support to the Argo CD CLI Oct 8, 2024
@nitishfy nitishfy requested a review from leoluz October 8, 2024 11:57
Copy link
Collaborator

@leoluz leoluz left a comment

Choose a reason for hiding this comment

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

@nitishfy Great progress. I think this PR needs to be reviewed by someone from the security sig as I see some potential risks in the code. I added comments to the security issues that I identified at first but a more security driven review would be important in this case.
cc @jannfis @crenshaw-dev

}
if filepath.Base(name) == name {
lp, err := exec.LookPath(name)
if lp != "" && !shouldSkipOnLookPathErr(err) {
Copy link
Collaborator

@leoluz leoluz Oct 8, 2024

Choose a reason for hiding this comment

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

This may lead to security implications. For example: if someone has their PATH configured as PATH=.:/usr/bin, the first . makes the current directory available in the PATH which enables different types of exploits. Go 1.19+ will return ErrDot to notify us that the return value from LookPath() is a relative path. We shouldn't ignore this error. Instead, we should fail and return an error stating that the executable wasn't found. Also, we must state in the docs that the plugin executable must be in the path and should not be provided as relative path.

cmd/argocd/commands/plugin_test.go Outdated Show resolved Hide resolved
cmd/argocd/commands/root.go Outdated Show resolved Hide resolved
cmd/main.go Outdated Show resolved Hide resolved
cmd/argocd/commands/root.go Outdated Show resolved Hide resolved
cmd/main.go Outdated Show resolved Hide resolved
@blakepettersson blakepettersson changed the title feat: Add Plugin Support to the Argo CD CLI feat(cli): Add Plugin Support to the Argo CD CLI Oct 10, 2024
@nitishfy nitishfy force-pushed the nitish/argocd-plugin-support branch from 652887c to fea85cd Compare October 11, 2024 09:46
Copy link
Member Author

@nitishfy nitishfy left a comment

Choose a reason for hiding this comment

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

We are not using the cmd.Find() anymore.

@nitishfy nitishfy requested a review from leoluz October 11, 2024 11:58
Copy link
Collaborator

@leoluz leoluz left a comment

Choose a reason for hiding this comment

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

Please check my comments

cmd/main.go Outdated Show resolved Hide resolved
cmd/main.go Outdated Show resolved Hide resolved
cmd/main.go Outdated Show resolved Hide resolved
cmd/main.go Outdated Show resolved Hide resolved
cmd/main.go Outdated Show resolved Hide resolved
cmd/main.go Outdated Show resolved Hide resolved
cmd/main.go Outdated Show resolved Hide resolved
cmd/main.go Outdated Show resolved Hide resolved
Copy link
Member Author

@nitishfy nitishfy left a comment

Choose a reason for hiding this comment

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

PTAL.

@nitishfy
Copy link
Member Author

Also, I think I'll have to restructure the entire tests for plugin_test.go once the change is accepted.

Copy link
Collaborator

@leoluz leoluz left a comment

Choose a reason for hiding this comment

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

Great progress. Tks @nitishfy!
There are a few more open items to be addressed in this PR before we merge.
Please take a look.

cmd/argocd/commands/root.go Outdated Show resolved Hide resolved
cmd/main.go Outdated Show resolved Hide resolved
go.mod Outdated
@@ -83,6 +83,7 @@ require (
go.opentelemetry.io/otel v1.30.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.30.0
go.opentelemetry.io/otel/sdk v1.30.0
go.uber.org/automaxprocs v1.6.0
Copy link
Member

Choose a reason for hiding this comment

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

I think you should probably revert the changes in go.mod in order for there not to be any conflicts with master.

Copy link
Member Author

Choose a reason for hiding this comment

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

this will lead to dependency issues, resulting in failure of CI checks.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@nitishfy You probably need to run go mod tidy and push the changes.

Copy link
Member

Choose a reason for hiding this comment

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

Can you run

git checkout origin/master -- go.mod

?

Copy link
Member

Choose a reason for hiding this comment

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

I think this needs to be done again once you sync your hook with argocd/master and then pulling in the latest changes from your hook on the master branch.

Copy link
Member Author

@nitishfy nitishfy left a comment

Choose a reason for hiding this comment

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

Few suggestions needed and once done, I'll start with improving the comments and add the docs. Thanks!

Copy link
Member

@blakepettersson blakepettersson left a comment

Choose a reason for hiding this comment

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

Added a few pointers which I think would be good to do before proceeding further.

cmd/main.go Outdated Show resolved Hide resolved
cmd/argocd/commands/root.go Outdated Show resolved Hide resolved
cmd/argocd/commands/plugin.go Outdated Show resolved Hide resolved
cmd/argocd/commands/plugin.go Outdated Show resolved Hide resolved
@nitishfy
Copy link
Member Author

@leoluz I'm personally in the opinion of searching for a plugin, starting with the longest prefix. Why would you want to follow another approach?

@nitishfy nitishfy force-pushed the nitish/argocd-plugin-support branch 2 times, most recently from 319184f to fde718d Compare December 26, 2024 13:19
Copy link
Member

@blakepettersson blakepettersson left a comment

Choose a reason for hiding this comment

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

I think there's a lot of room to simplify this further - there's also a bunch of test code existing which does not exercise a lot of the actual code (as well as there being a lot of test code which seems redundant?).

By abstracting away exec.LookPath and cmd.Run tests can be written which exercises the actual implementation of DefaultPluginHandler (just create custom functions for lookPath and run in your tests)

cmd/argocd/commands/plugin.go Show resolved Hide resolved
cmd/argocd/commands/plugin.go Show resolved Hide resolved
cmd/argocd/commands/plugin.go Outdated Show resolved Hide resolved
cmd/argocd/commands/plugin.go Outdated Show resolved Hide resolved
cmd/argocd/commands/plugin.go Show resolved Hide resolved
cmd/main.go Outdated Show resolved Hide resolved
cmd/main.go Outdated Show resolved Hide resolved
cmd/main.go Outdated Show resolved Hide resolved
go.mod Outdated
@@ -83,6 +83,7 @@ require (
go.opentelemetry.io/otel v1.30.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.30.0
go.opentelemetry.io/otel/sdk v1.30.0
go.uber.org/automaxprocs v1.6.0
Copy link
Member

Choose a reason for hiding this comment

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

Can you run

git checkout origin/master -- go.mod

?

cmd/argocd/commands/root.go Outdated Show resolved Hide resolved
@nitishfy nitishfy force-pushed the nitish/argocd-plugin-support branch from f0fe0f0 to a1e9407 Compare December 29, 2024 10:58
Copy link
Member Author

@nitishfy nitishfy left a comment

Choose a reason for hiding this comment

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

cmd/argocd/commands/plugin.go Outdated Show resolved Hide resolved
cmd/argocd/commands/root.go Outdated Show resolved Hide resolved
cmd/argocd/commands/plugin.go Outdated Show resolved Hide resolved
cmd/argocd/commands/root.go Outdated Show resolved Hide resolved
cmd/argocd/commands/plugin_test.go Outdated Show resolved Hide resolved
@nitishfy nitishfy force-pushed the nitish/argocd-plugin-support branch from 6139175 to 7ccd4a7 Compare January 3, 2025 09:07
@nitishfy
Copy link
Member Author

nitishfy commented Jan 3, 2025

@leoluz @blakepettersson I'd need your feedback on this PR. Having said that, there are a few things I'd like to mention:

  1. We need to decide if we would like to follow the longest executable search performed or not.
  2. I can see a lot of code optimization and few testcases can be added. I'm thinking if it would be okay to add them as some separate PR.
  3. I'll start working on writing the docs as soon as I get confirmation about if we'd like to have longest executable search operation in code or not.

@nitishfy nitishfy force-pushed the nitish/argocd-plugin-support branch from d8723ca to d0ba8ce Compare January 3, 2025 09:15
@blakepettersson
Copy link
Member

I can see a lot of code optimization and few testcases can be added. I'm thinking if it would be okay to add them as some separate PR.

What kind of code optimizations and test cases can be done? Why can't they be done in this PR?

cmd/main.go Outdated Show resolved Hide resolved
go.mod Outdated
@@ -83,6 +83,7 @@ require (
go.opentelemetry.io/otel v1.30.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.30.0
go.opentelemetry.io/otel/sdk v1.30.0
go.uber.org/automaxprocs v1.6.0
Copy link
Member

Choose a reason for hiding this comment

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

I think this needs to be done again once you sync your hook with argocd/master and then pulling in the latest changes from your hook on the master branch.

cmd/argocd/commands/plugin.go Outdated Show resolved Hide resolved
Signed-off-by: nitishfy <[email protected]>
Signed-off-by: nitishfy <[email protected]>
Signed-off-by: nitishfy <[email protected]>
Signed-off-by: nitishfy <[email protected]>
@nitishfy nitishfy force-pushed the nitish/argocd-plugin-support branch from fbe994f to 0cd9b74 Compare January 6, 2025 10:59
Signed-off-by: nitishfy <[email protected]>
cmd/argocd/commands/plugin.go Outdated Show resolved Hide resolved
cmd/argocd/commands/plugin.go Outdated Show resolved Hide resolved
cmd/argocd/commands/plugin.go Outdated Show resolved Hide resolved
cmd/argocd/commands/plugin_test.go Outdated Show resolved Hide resolved
Signed-off-by: nitishfy <[email protected]>
// TODO: verify how errors should be handled
path, err := h.lookPath(pluginName)
if err != nil || len(path) == 0 {
continue
Copy link
Collaborator

Choose a reason for hiding this comment

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

shouldn't this error be logged so users know why their plugins are not being registered?

Copy link
Member Author

Choose a reason for hiding this comment

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

we are not logging the error here because we try to search for another plugin name based on the longest prefix match. For eg. If a plugin argocd demo plugin is not found, the err will be non-nil and we then search for a plugin named argocd demo. This is getting done by ranging over the valid prefixes.

// Unexpected errors (e.g., permission issues) are logged for debugging but do not stop the search.
// This doesn't care about the plugin execution errors since those errors are handled separately by
// the execute function. Only those binaries are considered as argocd plugins if they start with
// argocd and have executable permisssions. If they don't have executable permissions inspite of being
Copy link
Collaborator

Choose a reason for hiding this comment

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

If they don't have executable permissions

I don't see the executable validation logic as part of this function. Please just document the function specific behaviour and move this doc to where the validation is actually implemented.

func (h *DefaultPluginHandler) lookForPlugin(filename string) (string, bool) {
for _, prefix := range h.ValidPrefixes {
pluginName := fmt.Sprintf("%s-%s", prefix, filename) // Combine prefix and filename
// TODO: verify how errors should be handled
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should define how this error should be handled as part of this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants