2023-01-06 15:15:57 +00:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
2023-07-12 15:36:09 +00:00
|
|
|
"context"
|
2025-01-07 10:49:23 +00:00
|
|
|
"errors"
|
2023-07-12 15:36:09 +00:00
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/libs/auth"
|
2023-07-12 15:36:09 +00:00
|
|
|
"github.com/databricks/cli/libs/cmdio"
|
2023-01-06 15:15:57 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2023-07-27 10:03:08 +00:00
|
|
|
func New() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "auth",
|
|
|
|
Short: "Authentication related commands",
|
2024-04-15 16:43:46 +00:00
|
|
|
Long: `Authentication related commands. For more information regarding how
|
|
|
|
authentication for the Databricks CLI and SDKs work please refer to the documentation
|
|
|
|
linked below.
|
|
|
|
|
2024-04-18 11:55:42 +00:00
|
|
|
AWS: https://docs.databricks.com/dev-tools/auth/index.html
|
|
|
|
Azure: https://learn.microsoft.com/azure/databricks/dev-tools/auth
|
|
|
|
GCP: https://docs.gcp.databricks.com/dev-tools/auth/index.html`,
|
2023-07-27 10:03:08 +00:00
|
|
|
}
|
|
|
|
|
2025-03-09 20:17:56 +00:00
|
|
|
var persistentAuth auth.PersistentAuth
|
|
|
|
cmd.PersistentFlags().StringVar(&persistentAuth.Host, "host", persistentAuth.Host, "Databricks Host")
|
|
|
|
cmd.PersistentFlags().StringVar(&persistentAuth.AccountID, "account-id", persistentAuth.AccountID, "Databricks Account ID")
|
2023-01-06 15:15:57 +00:00
|
|
|
|
2025-03-09 20:17:56 +00:00
|
|
|
hidden := false
|
2023-07-27 10:03:08 +00:00
|
|
|
cmd.AddCommand(newEnvCommand())
|
2025-03-09 20:17:56 +00:00
|
|
|
cmd.AddCommand(newLoginCommand(hidden, &persistentAuth))
|
2023-07-27 10:03:08 +00:00
|
|
|
cmd.AddCommand(newProfilesCommand())
|
2025-03-09 20:17:56 +00:00
|
|
|
cmd.AddCommand(newTokenCommand(&persistentAuth))
|
2024-04-03 08:14:04 +00:00
|
|
|
cmd.AddCommand(newDescribeCommand())
|
2023-07-27 10:03:08 +00:00
|
|
|
return cmd
|
|
|
|
}
|
2023-07-12 15:36:09 +00:00
|
|
|
|
2025-03-09 20:17:56 +00:00
|
|
|
// NewTopLevelLoginCommand creates a new login command for use in a top-level command group.
|
|
|
|
// This is useful for custom CLIs where the 'auth' command group does not exist.
|
|
|
|
func NewTopLevelLoginCommand(hidden bool) *cobra.Command {
|
|
|
|
var persistentAuth auth.PersistentAuth
|
|
|
|
cmd := newLoginCommand(hidden, &persistentAuth)
|
|
|
|
cmd.Flags().StringP("profile", "p", "", "~/.databrickscfg profile")
|
|
|
|
cmd.Flags().StringVar(&persistentAuth.Host, "host", persistentAuth.Host, "Databricks Host")
|
|
|
|
cmd.Flags().StringVar(&persistentAuth.AccountID, "account-id", persistentAuth.AccountID, "Databricks Account ID")
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2023-07-12 15:36:09 +00:00
|
|
|
func promptForHost(ctx context.Context) (string, error) {
|
2024-08-14 13:01:00 +00:00
|
|
|
if !cmdio.IsInTTY(ctx) {
|
2025-01-07 10:49:23 +00:00
|
|
|
return "", errors.New("the command is being run in a non-interactive environment, please specify a host using --host")
|
2023-07-12 15:36:09 +00:00
|
|
|
}
|
2024-08-14 13:01:00 +00:00
|
|
|
|
|
|
|
prompt := cmdio.Prompt(ctx)
|
|
|
|
prompt.Label = "Databricks host (e.g. https://<databricks-instance>.cloud.databricks.com)"
|
|
|
|
return prompt.Run()
|
2023-07-12 15:36:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func promptForAccountID(ctx context.Context) (string, error) {
|
2024-08-14 13:01:00 +00:00
|
|
|
if !cmdio.IsInTTY(ctx) {
|
2025-01-07 10:49:23 +00:00
|
|
|
return "", errors.New("the command is being run in a non-interactive environment, please specify an account ID using --account-id")
|
2024-08-14 13:01:00 +00:00
|
|
|
}
|
|
|
|
|
2023-07-12 15:36:09 +00:00
|
|
|
prompt := cmdio.Prompt(ctx)
|
2024-08-14 13:01:00 +00:00
|
|
|
prompt.Label = "Databricks account ID"
|
2023-07-12 15:36:09 +00:00
|
|
|
prompt.Default = ""
|
|
|
|
prompt.AllowEdit = true
|
2024-08-14 13:01:00 +00:00
|
|
|
return prompt.Run()
|
2023-07-12 15:36:09 +00:00
|
|
|
}
|