Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/iam/command_rbac_role_binding_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (c *roleBindingCommand) newListCommand() *cobra.Command {
}

cmd.Flags().String("resource", "", `Resource type and identifier using "Prefix:ID" format. If specified with "--role" and no principals, list all principals and role bindings.`)
cmd.Flags().Bool("inclusive", false, "List role bindings for specified scopes and nested scopes. Otherwise, list role bindings for the specified scopes. If scopes are unspecified, list only organization-scoped role bindings.")
cmd.Flags().Bool("inclusive", false, "List role bindings for specified scopes and nested scopes. Otherwise, list role bindings for the specified scopes. If scopes are unspecified, list role bindings across all scopes. Only applies to Confluent Cloud.")
pcmd.AddOutputFlag(cmd)

return cmd
Expand Down
45 changes: 31 additions & 14 deletions internal/logout/command.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package logout

import (
"context"
"fmt"

"github.com/spf13/cobra"
Expand All @@ -12,11 +13,12 @@ import (
"github.com/confluentinc/cli/v4/pkg/ccloudv2"
pcmd "github.com/confluentinc/cli/v4/pkg/cmd"
"github.com/confluentinc/cli/v4/pkg/config"
"github.com/confluentinc/cli/v4/pkg/log"
"github.com/confluentinc/cli/v4/pkg/output"
)

type command struct {
*pcmd.AuthenticatedCLICommand
*pcmd.CLICommand
cfg *config.Config
authTokenHandler pauth.AuthTokenHandler
}
Expand All @@ -28,16 +30,18 @@ func New(cfg *config.Config, prerunner pcmd.PreRunner, authTokenHandler pauth.Au
}

context := "Confluent Cloud or Confluent Platform"
c := &command{
AuthenticatedCLICommand: pcmd.NewAuthenticatedCLICommand(cmd, prerunner),
cfg: cfg,
authTokenHandler: authTokenHandler,
}
if cfg.IsCloudLogin() {
context = "Confluent Cloud"
} else if cfg.IsOnPremLogin() {
context = "Confluent Platform"
c.AuthenticatedCLICommand = pcmd.NewAuthenticatedWithMDSCLICommand(cmd, prerunner)
}

c := &command{
// Anonymous (not Authenticated): logout must not require being logged in, and must not
// trigger an auto-login via env-var credentials only to immediately log back out.
CLICommand: pcmd.NewAnonymousCLICommand(cmd, prerunner),
cfg: cfg,
authTokenHandler: authTokenHandler,
}

cmd.Short = fmt.Sprintf("Log out of %s.", context)
Expand All @@ -49,11 +53,14 @@ func New(cfg *config.Config, prerunner pcmd.PreRunner, authTokenHandler pauth.Au

func (c *command) logout(_ *cobra.Command, _ []string) error {
ctx := c.Config.Context()
if ctx != nil {
if ccloudv2.IsCCloudURL(ctx.Platform.Server, c.cfg.IsTest) {
if _, err := c.revokeCCloudRefreshToken(ctx); err != nil {
return err
}
if ctx == nil {
// Already logged out: do nothing.
return nil
}

if ccloudv2.IsCCloudURL(ctx.Platform.Server, c.cfg.IsTest) {
if _, err := c.revokeCCloudRefreshToken(ctx); err != nil {
return err
}
}

Expand All @@ -71,10 +78,20 @@ func (c *command) revokeCCloudRefreshToken(ctx *config.Context) (*ccloudv1.Authe
return nil, err
}

var userAgent string
if c.Version != nil {
userAgent = c.Version.UserAgent
}
client := ccloudv1.NewClientWithJWT(context.Background(), contextState.AuthToken, &ccloudv1.Params{
BaseURL: ctx.GetPlatformServer(),
Logger: log.CliLogger,
UserAgent: userAgent,
})

req := &ccloudv1.AuthenticateRequest{IdToken: contextState.AuthToken}
if sso.IsOkta(ctx.Platform.Server) {
return c.Client.Auth.OktaLogout(req)
return client.Auth.OktaLogout(req)
} else {
return c.Client.Auth.Logout(req)
return client.Auth.Logout(req)
}
}
18 changes: 17 additions & 1 deletion pkg/linter/command_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,11 @@
}

// RequireValidExamples checks that a command's examples have the right flags.
func RequireValidExamples() CommandRule {

Check failure on line 220 in pkg/linter/command_rules.go

View check run for this annotation

SonarQube-Confluent / SonarQube Code Analysis

Refactor this method to reduce its Cognitive Complexity from 23 to the 15 allowed.

[S3776] Cognitive Complexity of functions should not be too high See more on https://sonarqube.confluent.io/project/issues?id=cli&pullRequest=3423&issues=e173f9fe-9a2e-4da6-8aac-e028a302e7c4&open=e173f9fe-9a2e-4da6-8aac-e028a302e7c4
return func(cmd *cobra.Command) error {
requiredFlags := getRequiredFlags(cmd.Flags())
allFlags := getAllFlags(cmd.Flags())
boolFlags := getBoolFlags(cmd.Flags())

errs := new(multierror.Error)

Expand All @@ -238,7 +239,12 @@
}

for _, match := range regexp.MustCompile(`--[a-z\-]+=`).FindAllString(example, -1) {
errs = multierror.Append(errs, fmt.Errorf("%s: flag `%s` must not use \"=\" in example %d", cmd.CommandPath(), strings.TrimSuffix(match, "="), i+1))
flag := strings.TrimSuffix(match, "=")
// Boolean flags legitimately need "=" to set the non-default value, e.g. --flag=false.
if slices.Contains(boolFlags, flag) {
continue
}
errs = multierror.Append(errs, fmt.Errorf("%s: flag `%s` must not use \"=\" in example %d", cmd.CommandPath(), flag, i+1))
}
}

Expand Down Expand Up @@ -274,6 +280,16 @@
return all
}

func getBoolFlags(flags *pflag.FlagSet) []string {
var boolFlags []string
flags.VisitAll(func(flag *pflag.Flag) {
if flag.Value.Type() == "bool" {
boolFlags = append(boolFlags, "--"+flag.Name)
}
})
return boolFlags
}

func getValueByName(obj any, name string) string {
return reflect.Indirect(reflect.ValueOf(obj)).FieldByName(name).String()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Flags:
--ksql-cluster string ksqlDB cluster name, which specifies the ksqlDB cluster scope.
--flink-region string Flink region for the role binding, formatted as "cloud.region".
--resource string Resource type and identifier using "Prefix:ID" format. If specified with "--role" and no principals, list all principals and role bindings.
--inclusive List role bindings for specified scopes and nested scopes. Otherwise, list role bindings for the specified scopes. If scopes are unspecified, list only organization-scoped role bindings.
--inclusive List role bindings for specified scopes and nested scopes. Otherwise, list role bindings for the specified scopes. If scopes are unspecified, list role bindings across all scopes. Only applies to Confluent Cloud.
-o, --output string Specify the output format as "human", "json", or "yaml". (default "human")

Global Flags:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Flags:
--context string CLI context name.
--cluster-name string Cluster name, which specifies the cluster scope.
--resource string Resource type and identifier using "Prefix:ID" format. If specified with "--role" and no principals, list all principals and role bindings.
--inclusive List role bindings for specified scopes and nested scopes. Otherwise, list role bindings for the specified scopes. If scopes are unspecified, list only organization-scoped role bindings.
--inclusive List role bindings for specified scopes and nested scopes. Otherwise, list role bindings for the specified scopes. If scopes are unspecified, list role bindings across all scopes. Only applies to Confluent Cloud.
-o, --output string Specify the output format as "human", "json", or "yaml". (default "human")

Global Flags:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Flags:
--context string CLI context name.
--cluster-name string Cluster name, which specifies the cluster scope.
--resource string Resource type and identifier using "Prefix:ID" format. If specified with "--role" and no principals, list all principals and role bindings.
--inclusive List role bindings for specified scopes and nested scopes. Otherwise, list role bindings for the specified scopes. If scopes are unspecified, list only organization-scoped role bindings.
--inclusive List role bindings for specified scopes and nested scopes. Otherwise, list role bindings for the specified scopes. If scopes are unspecified, list role bindings across all scopes. Only applies to Confluent Cloud.
-o, --output string Specify the output format as "human", "json", or "yaml". (default "human")

Global Flags:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Flags:
--ksql-cluster string ksqlDB cluster name, which specifies the ksqlDB cluster scope.
--flink-region string Flink region for the role binding, formatted as "cloud.region".
--resource string Resource type and identifier using "Prefix:ID" format. If specified with "--role" and no principals, list all principals and role bindings.
--inclusive List role bindings for specified scopes and nested scopes. Otherwise, list role bindings for the specified scopes. If scopes are unspecified, list only organization-scoped role bindings.
--inclusive List role bindings for specified scopes and nested scopes. Otherwise, list role bindings for the specified scopes. If scopes are unspecified, list role bindings across all scopes. Only applies to Confluent Cloud.
-o, --output string Specify the output format as "human", "json", or "yaml". (default "human")

Global Flags:
Expand Down