From 2851b6fc71e538d144873ed556150ae7edd58a32 Mon Sep 17 00:00:00 2001 From: Will Vedder Date: Tue, 29 Nov 2022 15:44:38 -0500 Subject: [PATCH 1/4] Removing config command and replacing single usage in integration tests --- Makefile | 2 +- internal/cli/config.go | 133 ---------------------------------------- internal/cli/root.go | 1 - internal/cli/tenants.go | 5 ++ 4 files changed, 6 insertions(+), 135 deletions(-) delete mode 100644 internal/cli/config.go diff --git a/Makefile b/Makefile index 3d37aa7cb..85c653cce 100644 --- a/Makefile +++ b/Makefile @@ -129,7 +129,7 @@ test-unit: ## Run unit tests test-integration: $(GO_BIN)/commander ## Run integration tests. To run a specific test pass the FILTER var. Usage: `make test-integration FILTER="attack protection"` ${call print, "Running integration tests"} @$(MAKE) install # ensure fresh install prior to running test - auth0 config init && commander test ./test/integration/test-cases.yaml --filter "$(FILTER)"; \ + auth0 tenants add $\AUTH0_CLI_CLIENT_DOMAIN --client-id $\AUTH0_CLI_CLIENT_ID --client-secret $\AUTH0_CLI_CLIENT_SECRET && commander test ./test/integration/test-cases.yaml --filter "$(FILTER)"; \ exit_code=$$?; \ bash ./test/integration/scripts/test-cleanup.sh; \ exit $$exit_code diff --git a/internal/cli/config.go b/internal/cli/config.go deleted file mode 100644 index def0c72e5..000000000 --- a/internal/cli/config.go +++ /dev/null @@ -1,133 +0,0 @@ -package cli - -import ( - "context" - "fmt" - "net/url" - "strings" - - "github.com/spf13/cobra" - "github.com/spf13/viper" - "golang.org/x/oauth2/clientcredentials" - - "github.com/auth0/auth0-cli/internal/auth" -) - -var desiredInputs = `Config init is intended for non-interactive use, -ensure the following env variables are set: - -AUTH0_CLI_CLIENT_DOMAIN -AUTH0_CLI_CLIENT_ID -AUTH0_CLI_CLIENT_SECRET - -Interactive logins should use "auth0 login" instead.` - -type params struct { - filePath string - clientDomain string - clientID string - clientSecret string -} - -func (p params) validate() error { - if p.clientDomain == "" { - return fmt.Errorf("missing client domain:\n%s", desiredInputs) - } - - u, err := url.Parse(p.clientDomain) - if err != nil { - return fmt.Errorf("failed to parse client domain: %s", p.clientDomain) - } - - if u.Scheme != "" { - return fmt.Errorf("client domain cant include a scheme: %s", p.clientDomain) - } - - if p.clientID == "" { - return fmt.Errorf("missing client id:\n%s", desiredInputs) - } - - if p.clientSecret == "" { - return fmt.Errorf("missing client secret:\n%s", desiredInputs) - } - return nil -} - -func configCmd(cli *cli) *cobra.Command { - cmd := &cobra.Command{ - Use: "config", - Short: "Manage auth0-cli config", - Long: "Manage auth0-cli config", - Hidden: true, - } - - cmd.AddCommand(initCmd(cli)) - return cmd -} - -func initCmd(cli *cli) *cobra.Command { - cmd := &cobra.Command{ - Use: "init", - Short: "Configure the CLI from environment variables", - RunE: func(command *cobra.Command, args []string) error { - filePath := viper.GetString("FILEPATH") - clientDomain := viper.GetString("CLIENT_DOMAIN") - clientID := viper.GetString("CLIENT_ID") - clientSecret := viper.GetString("CLIENT_SECRET") - - cli.setPath(filePath) - p := params{filePath, clientDomain, clientID, clientSecret} - if err := p.validate(); err != nil { - return err - } - - u, err := url.Parse("https://" + p.clientDomain) - if err != nil { - return err - } - - c := &clientcredentials.Config{ - ClientID: p.clientID, - ClientSecret: p.clientSecret, - TokenURL: u.String() + "/oauth/token", - EndpointParams: url.Values{ - "client_id": {p.clientID}, - "scope": {strings.Join(auth.RequiredScopesMin(), " ")}, - "audience": {u.String() + "/api/v2/"}, - }, - } - - token, err := c.Token(context.Background()) - if err != nil { - return err - } - - t := Tenant{ - Name: p.clientDomain, - Domain: p.clientDomain, - AccessToken: token.AccessToken, - ExpiresAt: token.Expiry, - Scopes: auth.RequiredScopes(), - } - - if err := cli.addTenant(t); err != nil { - return fmt.Errorf("unexpected error adding tenant to config: %w", err) - } - return nil - }, - } - viper.SetEnvPrefix("AUTH0_CLI") - viper.AutomaticEnv() - - flags := cmd.Flags() - flags.String("filepath", defaultConfigPath(), "Filepath for the auth0 cli config") - _ = viper.BindPFlag("FILEPATH", flags.Lookup("filepath")) - flags.String("client-id", "", "Client ID to set within config") - _ = viper.BindPFlag("CLIENT_ID", flags.Lookup("client-id")) - flags.String("client-secret", "", "Client secret to use to generate token which is set within config") - _ = viper.BindPFlag("CLIENT_SECRET", flags.Lookup("client-secret")) - flags.String("client-domain", "", "Client domain to use to generate token which is set within config") - _ = viper.BindPFlag("CLIENT_DOMAIN", flags.Lookup("client-domain")) - - return cmd -} diff --git a/internal/cli/root.go b/internal/cli/root.go index 57155c498..6907fc9db 100644 --- a/internal/cli/root.go +++ b/internal/cli/root.go @@ -170,7 +170,6 @@ func addSubcommands(rootCmd *cobra.Command, cli *cli) { // relevance or relation with other commands: rootCmd.AddCommand(loginCmd(cli)) rootCmd.AddCommand(logoutCmd(cli)) - rootCmd.AddCommand(configCmd(cli)) rootCmd.AddCommand(tenantsCmd(cli)) rootCmd.AddCommand(appsCmd(cli)) rootCmd.AddCommand(usersCmd(cli)) diff --git a/internal/cli/tenants.go b/internal/cli/tenants.go index f199d62c9..e076eed5b 100644 --- a/internal/cli/tenants.go +++ b/internal/cli/tenants.go @@ -180,6 +180,8 @@ func addTenantCmd(cli *cli) *cobra.Command { inputs.Domain = args[0] } + fmt.Println(inputs) + if err := tenantClientID.Ask(cmd, &inputs.ClientID, nil); err != nil { return err } @@ -203,6 +205,9 @@ func addTenantCmd(cli *cli) *cobra.Command { }, } + tenantClientID.RegisterString(cmd, &inputs.ClientID, "") + tenantClientSecret.RegisterString(cmd, &inputs.ClientSecret, "") + return cmd } From 1063caf71dd78b4d74901cc39eab6a4b36570a4e Mon Sep 17 00:00:00 2001 From: Will Vedder Date: Tue, 29 Nov 2022 15:46:45 -0500 Subject: [PATCH 2/4] Removing erroneous debug --- internal/cli/tenants.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/internal/cli/tenants.go b/internal/cli/tenants.go index e076eed5b..a2f0a5cc2 100644 --- a/internal/cli/tenants.go +++ b/internal/cli/tenants.go @@ -180,8 +180,6 @@ func addTenantCmd(cli *cli) *cobra.Command { inputs.Domain = args[0] } - fmt.Println(inputs) - if err := tenantClientID.Ask(cmd, &inputs.ClientID, nil); err != nil { return err } From a418416a803701698f4e501e0dca00a7017071c4 Mon Sep 17 00:00:00 2001 From: Will Vedder Date: Tue, 29 Nov 2022 16:19:13 -0500 Subject: [PATCH 3/4] Removing unused code --- internal/cli/cli.go | 8 -------- 1 file changed, 8 deletions(-) diff --git a/internal/cli/cli.go b/internal/cli/cli.go index e082f80ad..786fca39f 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -499,14 +499,6 @@ func defaultConfigPath() string { return path.Join(os.Getenv("HOME"), ".config", "auth0", "config.json") } -func (c *cli) setPath(p string) { - if p == "" { - c.path = defaultConfigPath() - return - } - c.path = p -} - func canPrompt(cmd *cobra.Command) bool { noInput, err := cmd.Root().Flags().GetBool("no-input") From 9f83e3d1a63e46ab8d5bfa812256e815c6cc17f1 Mon Sep 17 00:00:00 2001 From: Will Vedder Date: Tue, 29 Nov 2022 16:25:49 -0500 Subject: [PATCH 4/4] Fixing env var usage in makfile --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 85c653cce..e1f267425 100644 --- a/Makefile +++ b/Makefile @@ -129,7 +129,7 @@ test-unit: ## Run unit tests test-integration: $(GO_BIN)/commander ## Run integration tests. To run a specific test pass the FILTER var. Usage: `make test-integration FILTER="attack protection"` ${call print, "Running integration tests"} @$(MAKE) install # ensure fresh install prior to running test - auth0 tenants add $\AUTH0_CLI_CLIENT_DOMAIN --client-id $\AUTH0_CLI_CLIENT_ID --client-secret $\AUTH0_CLI_CLIENT_SECRET && commander test ./test/integration/test-cases.yaml --filter "$(FILTER)"; \ + auth0 tenants add ${AUTH0_CLI_CLIENT_DOMAIN} --client-id ${AUTH0_CLI_CLIENT_ID} --client-secret ${AUTH0_CLI_CLIENT_SECRET} && commander test ./test/integration/test-cases.yaml --filter "$(FILTER)"; \ exit_code=$$?; \ bash ./test/integration/scripts/test-cleanup.sh; \ exit $$exit_code