Skip to content

Fix client command detection when flags precede subcommand#506

Merged
bestbeforetoday merged 1 commit into
hyperledger:mainfrom
antcybersec:fix-client-flags-before-subcommand-465
Jun 7, 2026
Merged

Fix client command detection when flags precede subcommand#506
bestbeforetoday merged 1 commit into
hyperledger:mainfrom
antcybersec:fix-client-flags-before-subcommand-465

Conversation

@antcybersec

Copy link
Copy Markdown
Contributor

Summary

  • Resolve the active subcommand with cobra's Find instead of assuming args[1] is always the command name
  • Fixes incorrect enrollment validation when global flags (e.g. --loglevel warning) appear before enroll, gencsr, getcacert, or getcainfo
  • Add unit tests for command resolution and the reported enroll failure mode

Problem

fabric-ca-client --loglevel warning enroll -u ... was treated as if the subcommand was warning, so ConfigInit incorrectly required prior enrollment and failed with:

Enrollment information does not exist. Please execute enroll command first.

Test plan

  • go test ./cmd/fabric-ca-client/command/ -run 'TestResolveCommandName|TestEnrollWithGlobalFlagsBeforeSubcommand'
  • go test ./cmd/fabric-ca-client/command/

Fixes #465

@antcybersec antcybersec requested a review from a team as a code owner June 5, 2026 01:07

@bestbeforetoday bestbeforetoday left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you very much for this contribution. Just a concern with one of the unit tests (see inline comments); otherwise looks good to me.

Comment on lines +65 to +73
defYaml := util.GetDefaultConfigFile("fabric-ca-client")
os.Remove(defYaml)
defer os.Remove(defYaml)

err := RunMain([]string{cmdName, "--loglevel", "warning", "enroll", "-d"})
if assert.Error(t, err) {
assert.NotContains(t, err.Error(), "Enrollment information does not exist",
"enroll with flags before subcommand should not require prior enrollment")
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this configuration should be guaranteed to fail so it should require an error, not just assert if an error occurs. The NotContians assertion on the error message also seems error-prone since the error messages could be changed in the future, which would make this test pass regardless of actual behaviour.

I think this really needs to change to a positive test:

Suggested change
defYaml := util.GetDefaultConfigFile("fabric-ca-client")
os.Remove(defYaml)
defer os.Remove(defYaml)
err := RunMain([]string{cmdName, "--loglevel", "warning", "enroll", "-d"})
if assert.Error(t, err) {
assert.NotContains(t, err.Error(), "Enrollment information does not exist",
"enroll with flags before subcommand should not require prior enrollment")
}
adminHome := filepath.Join(tdDir, "enrolladminhome")
// Remove admin home directory if it exists
err := os.RemoveAll(adminHome)
require.NoErrorf(t, err, "Failed to remove directory %s: %s", adminHome, err)
// Remove admin home directory that this test is going to create before
// exiting the test case
defer os.RemoveAll(adminHome)
srv := setupEnrollTest(t)
// Cleanup before exiting the test case
defer stopAndCleanupServer(t, srv)
err = RunMain([]string{cmdName, "--loglevel", "warning", "enroll", "-d", "-u", enrollURL, "-H", adminHome})
require.NoError(t, err, "enroll should succeed with global flags before subcommand")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point — updated to a positive test that enrolls against a running CA server with global flags before the subcommand. Thanks for the suggestion.

…er#465)

Use cobra's Find to resolve the top-level subcommand instead of
assuming args[1] is always the command name. This fixes incorrect
enrollment validation when global flags such as --loglevel appear
before enroll, gencsr, getcacert, or getcainfo.

Signed-off-by: antcybersec <anant1234466@gmail.com>
@antcybersec antcybersec force-pushed the fix-client-flags-before-subcommand-465 branch from 22a8da8 to 47f15ac Compare June 7, 2026 20:16
@bestbeforetoday bestbeforetoday merged commit 6396d32 into hyperledger:main Jun 7, 2026
6 checks passed
@bestbeforetoday bestbeforetoday changed the title Fix client command detection when flags precede subcommand (#465) Fix client command detection when flags precede subcommand Jun 7, 2026
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.

fabric-ca-client: validation issues when flags are positioned before the subcommand

2 participants