-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathauth.go
More file actions
111 lines (95 loc) · 3.66 KB
/
auth.go
File metadata and controls
111 lines (95 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package auth
import (
"context"
"errors"
"fmt"
"github.com/databricks/cli/libs/auth"
"github.com/databricks/cli/libs/cmdio"
"github.com/databricks/cli/libs/databrickscfg/profile"
"github.com/databricks/databricks-sdk-go/config"
"github.com/spf13/cobra"
)
func New() *cobra.Command {
cmd := &cobra.Command{
Use: "auth",
Short: "Authentication related commands",
Long: `Authentication related commands. For more information regarding how
authentication for the Databricks CLI and SDKs work please refer to the documentation
linked below.
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`,
}
var authArguments auth.AuthArguments
cmd.PersistentFlags().StringVar(&authArguments.Host, "host", "", "Databricks Host")
cmd.PersistentFlags().StringVar(&authArguments.AccountID, "account-id", "", "Databricks Account ID")
cmd.PersistentFlags().BoolVar(&authArguments.IsUnifiedHost, "experimental-is-unified-host", false, "Flag to indicate if the host is a unified host")
cmd.PersistentFlags().StringVar(&authArguments.WorkspaceID, "workspace-id", "", "Databricks Workspace ID")
cmd.AddCommand(newEnvCommand())
cmd.AddCommand(newLoginCommand(&authArguments))
cmd.AddCommand(newLogoutCommand())
cmd.AddCommand(newProfilesCommand())
cmd.AddCommand(newTokenCommand(&authArguments))
cmd.AddCommand(newDescribeCommand())
cmd.AddCommand(newSwitchCommand())
return cmd
}
func promptForHost(ctx context.Context) (string, error) {
if !cmdio.IsPromptSupported(ctx) {
return "", errors.New("the command is being run in a non-interactive environment, please specify a host using --host")
}
prompt := cmdio.Prompt(ctx)
prompt.Label = "Databricks host (e.g. https://<databricks-instance>.cloud.databricks.com)"
return prompt.Run()
}
func promptForAccountID(ctx context.Context) (string, error) {
if !cmdio.IsPromptSupported(ctx) {
return "", errors.New("the command is being run in a non-interactive environment, please specify an account ID using --account-id")
}
prompt := cmdio.Prompt(ctx)
prompt.Label = "Databricks account ID"
prompt.Default = ""
prompt.AllowEdit = true
return prompt.Run()
}
// validateProfileHostConflict checks that --profile and --host don't conflict.
// If the profile's host matches the provided host (after canonicalization),
// the flags are considered compatible. If the profile is not found or has no
// host, the check is skipped (let the downstream command handle it).
func validateProfileHostConflict(ctx context.Context, profileName, host string, profiler profile.Profiler) error {
p, err := loadProfileByName(ctx, profileName, profiler)
if err != nil {
return err
}
if p == nil || p.Host == "" {
return nil
}
profileHost := (&config.Config{Host: p.Host}).CanonicalHostName()
flagHost := (&config.Config{Host: host}).CanonicalHostName()
if profileHost != flagHost {
return fmt.Errorf(
"--profile %q has host %q, which conflicts with --host %q. Use --profile only to select a profile",
profileName, p.Host, host,
)
}
return nil
}
// profileHostConflictCheck is a PreRunE function that validates
// --profile and --host don't conflict.
func profileHostConflictCheck(cmd *cobra.Command, args []string) error {
profileFlag := cmd.Flag("profile")
hostFlag := cmd.Flag("host")
// Only validate when both flags are explicitly set by the user.
if profileFlag == nil || hostFlag == nil {
return nil
}
if !profileFlag.Changed || !hostFlag.Changed {
return nil
}
return validateProfileHostConflict(
cmd.Context(),
profileFlag.Value.String(),
hostFlag.Value.String(),
profile.DefaultProfiler,
)
}