diff --git a/Makefile b/Makefile index 3d37aa7cb..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 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/cli.go b/internal/cli/cli.go index 0ed688418..116688ecb 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -501,14 +501,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") 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 10c7c013c..120bd1207 100644 --- a/internal/cli/root.go +++ b/internal/cli/root.go @@ -167,7 +167,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 b685d53d7..7d178905c 100644 --- a/internal/cli/tenants.go +++ b/internal/cli/tenants.go @@ -225,6 +225,9 @@ func addTenantCmd(cli *cli) *cobra.Command { }, } + tenantClientID.RegisterString(cmd, &inputs.ClientID, "") + tenantClientSecret.RegisterString(cmd, &inputs.ClientSecret, "") + return cmd }