2024-08-25 21:53:44 +00:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
|
|
|
|
2024-09-01 16:22:18 +00:00
|
|
|
"github.com/databricks/cli/libs/auth"
|
2024-08-25 21:53:44 +00:00
|
|
|
"github.com/databricks/cli/libs/cmdio"
|
|
|
|
"github.com/databricks/cli/libs/databrickscfg"
|
|
|
|
"github.com/databricks/cli/libs/databrickscfg/profile"
|
|
|
|
"github.com/databricks/databricks-sdk-go/config"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2024-09-01 16:22:18 +00:00
|
|
|
type LogoutSession struct {
|
|
|
|
Profile string
|
|
|
|
File config.File
|
|
|
|
PersistentAuth *auth.PersistentAuth
|
2024-08-25 21:53:44 +00:00
|
|
|
}
|
|
|
|
|
2024-09-01 16:22:18 +00:00
|
|
|
func (l *LogoutSession) load(ctx context.Context, profileName string, persistentAuth *auth.PersistentAuth) error {
|
2024-08-25 21:53:44 +00:00
|
|
|
l.Profile = profileName
|
2024-09-01 16:22:18 +00:00
|
|
|
l.PersistentAuth = persistentAuth
|
2024-08-25 21:53:44 +00:00
|
|
|
iniFile, err := profile.DefaultProfiler.Get(ctx)
|
|
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
|
|
return err
|
|
|
|
} else if err != nil {
|
|
|
|
return fmt.Errorf("cannot parse config file: %w", err)
|
|
|
|
}
|
|
|
|
l.File = *iniFile
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-01 16:22:18 +00:00
|
|
|
func (l *LogoutSession) getConfigSetionMap() (map[string]string, error) {
|
2024-08-25 21:53:44 +00:00
|
|
|
section, err := l.File.GetSection(l.Profile)
|
|
|
|
if err != nil {
|
|
|
|
return map[string]string{}, fmt.Errorf("profile does not exist in config file: %w", err)
|
|
|
|
}
|
|
|
|
return section.KeysHash(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// clear token from ~/.databricks/token-cache.json
|
2024-09-01 16:22:18 +00:00
|
|
|
func (l *LogoutSession) clearTokenCache(ctx context.Context) error {
|
|
|
|
return l.PersistentAuth.ClearToken(ctx)
|
2024-08-25 21:53:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Overrewrite profile to .databrickscfg without fields marked as sensitive
|
|
|
|
// Other attributes are preserved.
|
2024-09-01 16:22:18 +00:00
|
|
|
func (l *LogoutSession) clearConfigFile(ctx context.Context, sectionMap map[string]string) error {
|
2024-08-25 21:53:44 +00:00
|
|
|
return databrickscfg.SaveToProfile(ctx, &config.Config{
|
|
|
|
ConfigFile: l.File.Path(),
|
|
|
|
Profile: l.Profile,
|
|
|
|
Host: sectionMap["host"],
|
|
|
|
ClusterID: sectionMap["cluster_id"],
|
|
|
|
WarehouseID: sectionMap["warehouse_id"],
|
|
|
|
ServerlessComputeID: sectionMap["serverless_compute_id"],
|
|
|
|
AccountID: sectionMap["account_id"],
|
|
|
|
Username: sectionMap["username"],
|
|
|
|
GoogleServiceAccount: sectionMap["google_service_account"],
|
|
|
|
AzureResourceID: sectionMap["azure_workspace_resource_id"],
|
|
|
|
AzureClientID: sectionMap["azure_client_id"],
|
|
|
|
AzureTenantID: sectionMap["azure_tenant_id"],
|
|
|
|
AzureEnvironment: sectionMap["azure_environment"],
|
|
|
|
AzureLoginAppID: sectionMap["azure_login_app_id"],
|
|
|
|
ClientID: sectionMap["client_id"],
|
|
|
|
AuthType: sectionMap["auth_type"],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-09-01 16:22:18 +00:00
|
|
|
func newLogoutCommand(persistentAuth *auth.PersistentAuth) *cobra.Command {
|
2024-08-25 21:53:44 +00:00
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "logout [PROFILE]",
|
|
|
|
Short: "Logout from specified profile",
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.RunE = func(cmd *cobra.Command, args []string) error {
|
|
|
|
ctx := cmd.Context()
|
2024-09-01 16:22:18 +00:00
|
|
|
profileName := cmd.Flag("profile").Value.String()
|
|
|
|
// If the user has not specified a profile name, prompt for one.
|
|
|
|
if profileName == "" {
|
|
|
|
var err error
|
|
|
|
profileName, err = promptForProfile(ctx, persistentAuth.ProfileName())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-08-25 21:53:44 +00:00
|
|
|
}
|
2024-09-01 16:22:18 +00:00
|
|
|
// Set the host and account-id based on the provided arguments and flags.
|
|
|
|
err := setHostAndAccountId(ctx, profileName, persistentAuth, args)
|
2024-08-25 21:53:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-09-01 16:22:18 +00:00
|
|
|
defer persistentAuth.Close()
|
|
|
|
LogoutSession := &LogoutSession{}
|
|
|
|
LogoutSession.load(ctx, profileName, persistentAuth)
|
|
|
|
configSectionMap, err := LogoutSession.getConfigSetionMap()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := LogoutSession.clearTokenCache(ctx); err != nil {
|
2024-08-25 21:53:44 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-09-01 16:22:18 +00:00
|
|
|
if err := LogoutSession.clearConfigFile(ctx, configSectionMap); err != nil {
|
2024-08-25 21:53:44 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
cmdio.LogString(ctx, fmt.Sprintf("Profile %s was successfully logged out", profileName))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return cmd
|
|
|
|
}
|