Skip to content

Commit

Permalink
feat(logout): Add verbose logging for logout sub cli.
Browse files Browse the repository at this point in the history
Signed-off-by: spbsoluble <[email protected]>
  • Loading branch information
spbsoluble committed Dec 10, 2024
1 parent d944f37 commit ee1b63e
Showing 1 changed file with 129 additions and 15 deletions.
144 changes: 129 additions & 15 deletions cmd/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ package cmd

import (
"fmt"
"log"
"io"
stdlog "log"
"os"

"github.com/Keyfactor/keyfactor-auth-client-go/auth_providers"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

Expand All @@ -27,29 +30,140 @@ var logoutCmd = &cobra.Command{
Use: "logout",
Short: "Removes the credentials file '$HOME/.keyfactor/command_config.json'.",
Long: `Removes the credentials file '$HOME/.keyfactor/command_config.json'.`,
Run: func(cmd *cobra.Command, args []string) {
// Global flags
debugFlag, _ := cmd.Flags().GetBool("debugFlag")
//configFile, _ := cmd.Flags().GetString("config")
//noPrompt, _ := cmd.Flags().GetBool("no-prompt")
//profile, _ := cmd.Flags().GetString("profile")

debugModeEnabled := checkDebug(debugFlag)
log.Println("Debug mode enabled: ", debugModeEnabled)
err := os.Remove(fmt.Sprintf("%s/.keyfactor/%s", os.Getenv("HOME"), DefaultConfigFileName))
RunE: func(cmd *cobra.Command, args []string) error {
log.Info().Msg("Running logout command")
cmd.SilenceUsage = true
// expEnabled checks
isExperimental := false
debugErr := warnExperimentalFeature(expEnabled, isExperimental)
if debugErr != nil {
return debugErr
}
stdlog.SetOutput(io.Discard)
informDebug(debugFlag)

logGlobals()

var configFilePath string
if configFile == "" {
// check if environment variables for config file is set
if os.Getenv(auth_providers.EnvKeyfactorConfigFile) != "" {
configFilePath = os.Getenv(auth_providers.EnvKeyfactorConfigFile)
} else {
userHomeDir, err := os.UserHomeDir()
if err != nil {
userHomeDir, err = os.Getwd()
if err != nil {
userHomeDir = "."
}
}
configFilePath = fmt.Sprintf("%s/%s", userHomeDir, auth_providers.DefaultConfigFilePath)
}
} else {
configFilePath = configFile
}

// Remove environment variables
log.Info().Msg("Running logout command for environment variables")
envLogout()

log.Info().
Str("configFilePath", configFilePath).
Msg("Attempting to removing config file")
err := os.Remove(configFilePath)
if err != nil {
if os.IsNotExist(err) {
fmt.Println("Config file does not exist.")
fmt.Println("Logged out successfully!")
return
log.Error().
Err(err).
Msg("Config file does not exist, unable to logout.")
fmt.Println("Config file does not exist, unable to logout.")
return err
}
log.Error().
Err(err).
Msg("unable to remove config file, logout failed")
fmt.Println("Error removing config file: ", err)
//log.Fatal("[ERROR] removing config file: ", err)
return err
}
log.Info().
Str("configFilePath", configFilePath).
Msg("Config file removed successfully")
fmt.Println("Logged out successfully!")
return nil
},
}

func envLogout() {
log.Debug().Msg("Running logout command for environment variables")

log.Debug().Msg("Unsetting base environment variables")

log.Trace().Str("EnvKeyfactorHostName", auth_providers.EnvKeyfactorHostName).Msg("Unsetting")
os.Unsetenv(auth_providers.EnvKeyfactorHostName)

log.Trace().Str("EnvKeyfactorPort", auth_providers.EnvKeyfactorPort).Msg("Unsetting")
os.Unsetenv(auth_providers.EnvKeyfactorPort)

log.Trace().Str("EnvKeyfactorAPIPath", auth_providers.EnvKeyfactorAPIPath).Msg("Unsetting")
os.Unsetenv(auth_providers.EnvKeyfactorAPIPath)

log.Trace().Str("EnvAuthCACert", auth_providers.EnvAuthCACert).Msg("Unsetting")
os.Unsetenv(auth_providers.EnvAuthCACert)

log.Trace().Str("EnvKeyfactorSkipVerify", auth_providers.EnvKeyfactorSkipVerify).Msg("Unsetting")
os.Unsetenv(auth_providers.EnvKeyfactorSkipVerify)

log.Trace().Str("EnvKeyfactorClientTimeout", auth_providers.EnvKeyfactorClientTimeout).Msg("Unsetting")
os.Unsetenv(auth_providers.EnvKeyfactorClientTimeout)

log.Debug().Msg("Unsetting kfutil environment variables")
log.Trace().Str("EnvKeyfactorAuthProfile", auth_providers.EnvKeyfactorAuthProfile).Msg("Unsetting")
os.Unsetenv(auth_providers.EnvKeyfactorAuthProfile)

log.Trace().Str("EnvKeyfactorConfigFile", auth_providers.EnvKeyfactorConfigFile).Msg("Unsetting")
os.Unsetenv(auth_providers.EnvKeyfactorConfigFile)

log.Debug().Msg("Unsetting command basic auth environment variables")
log.Trace().Str("EnvKeyfactorUsername", auth_providers.EnvKeyfactorUsername).Msg("Unsetting")
os.Unsetenv(auth_providers.EnvKeyfactorUsername)

log.Trace().Str("EnvKeyfactorPassword", auth_providers.EnvKeyfactorPassword).Msg("Unsetting")
os.Unsetenv(auth_providers.EnvKeyfactorPassword)

log.Trace().Str("EnvKeyfactorDomain", auth_providers.EnvKeyfactorDomain).Msg("Unsetting")
os.Unsetenv(auth_providers.EnvKeyfactorDomain)

log.Debug().Msg("Unsetting command oauth2 environment variables")
log.Trace().Str("EnvKeyfactorClientID", auth_providers.EnvKeyfactorClientID).Msg("Unsetting")
os.Unsetenv(auth_providers.EnvKeyfactorClientID)

log.Trace().Str("EnvKeyfactorClientSecret", auth_providers.EnvKeyfactorClientSecret).Msg("Unsetting")
os.Unsetenv(auth_providers.EnvKeyfactorClientSecret)

log.Trace().Str("EnvKeyfactorAccessToken", auth_providers.EnvKeyfactorAccessToken).Msg("Unsetting")
os.Unsetenv(auth_providers.EnvKeyfactorAccessToken)

log.Trace().Str("EnvKeyfactorAuthTokenURL", auth_providers.EnvKeyfactorAuthTokenURL).Msg("Unsetting")
os.Unsetenv(auth_providers.EnvKeyfactorAuthTokenURL)

log.Trace().Str("EnvKeyfactorAuthScopes", auth_providers.EnvKeyfactorAuthScopes).Msg("Unsetting")
os.Unsetenv(auth_providers.EnvKeyfactorAuthScopes)

log.Trace().Str("EnvKeyfactorAuthAudience", auth_providers.EnvKeyfactorAuthAudience).Msg("Unsetting")
os.Unsetenv(auth_providers.EnvKeyfactorAuthAudience)

log.Debug().Msg("Unsetting command azure environment variables")
log.Trace().Str("EnvKeyfactorAuthProvider", auth_providers.EnvKeyfactorAuthProvider).Msg("Unsetting")
os.Unsetenv(auth_providers.EnvKeyfactorAuthProvider)

log.Trace().Str("EnvAzureSecretName", auth_providers.EnvAzureSecretName).Msg("Unsetting")
os.Unsetenv(auth_providers.EnvAzureSecretName)

log.Trace().Str("EnvAzureVaultName", auth_providers.EnvAzureVaultName).Msg("Unsetting")
os.Unsetenv(auth_providers.EnvAzureVaultName)

}

func init() {
RootCmd.AddCommand(logoutCmd)
}

0 comments on commit ee1b63e

Please sign in to comment.