[MM-64663] Fix potential panic in mmctl ldap job show command (#32575)

* Fix potential panic in `mmctl ldap job show` command

* fix examples and docs

---------

Co-authored-by: Christopher Poile <cpoile@gmail.com>
Этот коммит содержится в:
Claudio Costa
2025-07-04 07:48:17 -06:00
коммит произвёл GitHub
родитель 10b6784968
Коммит 4a7aeae861
6 изменённых файлов: 23 добавлений и 8 удалений

Просмотреть файл

@@ -8,6 +8,7 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"github.com/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/mattermost/mattermost/server/public/model" "github.com/mattermost/mattermost/server/public/model"
@@ -25,7 +26,7 @@ func newLDAPSyncCmd() *cobra.Command {
Use: "sync", Use: "sync",
Short: "Synchronize now", Short: "Synchronize now",
Long: "Synchronize all LDAP users and groups now.", Long: "Synchronize all LDAP users and groups now.",
Example: " ldap sync", Example: "mmctl ldap sync",
RunE: withClient(ldapSyncCmdF), RunE: withClient(ldapSyncCmdF),
} }
@@ -47,7 +48,7 @@ var LdapIDMigrate = &cobra.Command{
2. Run the command "mmctl ldap idmigrate objectGUID". 2. Run the command "mmctl ldap idmigrate objectGUID".
3. Update the config within the System Console to the new value "objectGUID". 3. Update the config within the System Console to the new value "objectGUID".
4. Restart the Mattermost server.`, 4. Restart the Mattermost server.`,
Example: " ldap idmigrate objectGUID", Example: "mmctl ldap idmigrate objectGUID",
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
RunE: withClient(ldapIDMigrateCmdF), RunE: withClient(ldapIDMigrateCmdF),
} }
@@ -59,7 +60,7 @@ var LdapJobCmd = &cobra.Command{
var LdapJobListCmd = &cobra.Command{ var LdapJobListCmd = &cobra.Command{
Use: "list", Use: "list",
Example: " ldap job list", Example: "mmctl ldap job list",
Short: "List LDAP sync jobs", Short: "List LDAP sync jobs",
// Alisases cause error in zsh. Supposedly, completion V2 will fix that: https://github.com/spf13/cobra/pull/1146 // Alisases cause error in zsh. Supposedly, completion V2 will fix that: https://github.com/spf13/cobra/pull/1146
// https://mattermost.atlassian.net/browse/MM-57062 // https://mattermost.atlassian.net/browse/MM-57062
@@ -71,8 +72,9 @@ var LdapJobListCmd = &cobra.Command{
var LdapJobShowCmd = &cobra.Command{ var LdapJobShowCmd = &cobra.Command{
Use: "show [ldapJobID]", Use: "show [ldapJobID]",
Example: " import ldap show f3d68qkkm7n8xgsfxwuo498rah", Example: "mmctl ldap show f3d68qkkm7n8xgsfxwuo498rah",
Short: "Show LDAP sync job", Short: "Show LDAP sync job",
Args: cobra.MinimumNArgs(1),
ValidArgsFunction: validateArgsWithClient(ldapJobShowCompletionF), ValidArgsFunction: validateArgsWithClient(ldapJobShowCompletionF),
RunE: withClient(ldapJobShowCmdF), RunE: withClient(ldapJobShowCmdF),
} }
@@ -140,6 +142,10 @@ func ldapJobListCmdF(c client.Client, command *cobra.Command, args []string) err
} }
func ldapJobShowCmdF(c client.Client, command *cobra.Command, args []string) error { func ldapJobShowCmdF(c client.Client, command *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("expected at least one argument (ldapJobID). See help text for details")
}
job, _, err := c.GetJob(context.TODO(), args[0]) job, _, err := c.GetJob(context.TODO(), args[0])
if err != nil { if err != nil {
return fmt.Errorf("failed to get LDAP sync job: %w", err) return fmt.Errorf("failed to get LDAP sync job: %w", err)

Просмотреть файл

@@ -213,6 +213,15 @@ func (s *MmctlE2ETestSuite) TestLdapJobShowCmdF() {
s.Require().Empty(printer.GetErrorLines()) s.Require().Empty(printer.GetErrorLines())
}) })
s.RunForSystemAdminAndLocal("no args", func(c client.Client) {
printer.Clean()
err := ldapJobShowCmdF(c, &cobra.Command{}, []string{})
s.Require().EqualError(err, "expected at least one argument (ldapJobID). See help text for details")
s.Require().Empty(printer.GetLines())
s.Require().Empty(printer.GetErrorLines())
})
s.RunForSystemAdminAndLocal("not found", func(c client.Client) { s.RunForSystemAdminAndLocal("not found", func(c client.Client) {
printer.Clean() printer.Clean()

Просмотреть файл

@@ -25,7 +25,7 @@ Examples
:: ::
ldap idmigrate objectGUID mmctl ldap idmigrate objectGUID
Options Options
~~~~~~~ ~~~~~~~

Просмотреть файл

@@ -20,7 +20,7 @@ Examples
:: ::
ldap job list mmctl ldap job list
Options Options
~~~~~~~ ~~~~~~~

Просмотреть файл

@@ -20,7 +20,7 @@ Examples
:: ::
import ldap show f3d68qkkm7n8xgsfxwuo498rah mmctl ldap show f3d68qkkm7n8xgsfxwuo498rah
Options Options
~~~~~~~ ~~~~~~~

Просмотреть файл

@@ -20,7 +20,7 @@ Examples
:: ::
ldap sync mmctl ldap sync
Options Options
~~~~~~~ ~~~~~~~