Skip to content

Commit f2afc79

Browse files
committed
feat(logout): Add logout support for profile and config-file flags.
feat(logout): Add `are you sure` prompts to `logout`. Signed-off-by: spbsoluble <1661003+spbsoluble@users.noreply.github.com>
1 parent 282f69a commit f2afc79

File tree

3 files changed

+162
-23
lines changed

3 files changed

+162
-23
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
### CLI
66

77
- `auth`: Added support for authenticating to Keyfactor Command using a oAuth2 client credentials or access token.
8+
- `logout`: Added support for logging out of specific `profile` and `config-file`.
9+
- `logout`: Added `yes|no` prompt for logout actions, which can be skipped by using the `--no-prompt` flag.
810

911
### Store Types
1012

cmd/login.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,17 @@ func getDomainFromUsername(username string) string {
327327
return ""
328328
}
329329

330+
func promptForInteractiveYesNo(parameterName string) bool {
331+
var input string
332+
fmt.Printf("%s [y/n]: \n", parameterName)
333+
_, err := fmt.Scanln(&input)
334+
_ = handleInteractiveError(err, parameterName)
335+
if strings.ToLower(input) == "y" || strings.ToLower(input) == "yes" {
336+
return true
337+
}
338+
return false
339+
}
340+
330341
func promptForInteractiveParameter(parameterName string, defaultValue string) string {
331342
var input string
332343
fmt.Printf("Enter %s [%s]: \n", parameterName, defaultValue)
@@ -559,7 +570,7 @@ func authInteractive(
559570
}
560571

561572
func prepHomeDir() (string, error) {
562-
log.Debug().Msg("prepHomeDir() called")
573+
log.Debug().Msg(fmt.Sprintf("%s prepHomeDir()", DebugFuncEnter))
563574
// Set up home directory config
564575
userHomeDir, hErr := os.UserHomeDir()
565576

@@ -573,17 +584,21 @@ func prepHomeDir() (string, error) {
573584
} else {
574585
userHomeDir = fmt.Sprintf("%s/.keyfactor", userHomeDir)
575586
}
576-
//log.Println("[DEBUG] Configuration directory: ", userHomeDir)
587+
577588
log.Debug().Str("userHomeDir", userHomeDir).Msg("Configuration directory")
578589
_, err := os.Stat(userHomeDir)
579590

580591
if os.IsNotExist(err) {
581592
errDir := os.MkdirAll(userHomeDir, 0700)
582593
if errDir != nil {
583-
fmt.Println("Unable to create login config file. ", errDir)
584-
log.Printf("[ERROR] creating directory: %s", errDir)
594+
outputError(fmt.Errorf("error creating home directory: %v", errDir), false, outputFormat)
595+
log.Error().Err(errDir)
596+
log.Debug().Msg(fmt.Sprintf("%s prepHomeDir() returning", DebugFuncExit))
585597
return userHomeDir, errDir
586598
}
587599
}
600+
log.Debug().
601+
Str("userHomeDir", userHomeDir).
602+
Msg(fmt.Sprintf("%s prepHomeDir() returning", DebugFuncExit))
588603
return userHomeDir, hErr
589604
}

cmd/logout.go

Lines changed: 141 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -63,37 +63,95 @@ var logoutCmd = &cobra.Command{
6363
configFilePath = configFile
6464
}
6565

66-
// Remove environment variables
6766
log.Info().Msg("Running logout command for environment variables")
6867
envLogout()
6968

70-
log.Info().
71-
Str("configFilePath", configFilePath).
72-
Msg("Attempting to removing config file")
73-
err := os.Remove(configFilePath)
74-
if err != nil {
75-
if os.IsNotExist(err) {
69+
if profile != "" {
70+
pErr := logoutProfile(profile, configFilePath)
71+
if pErr != nil {
7672
log.Error().
77-
Err(err).
78-
Msg("config file does not exist, unable to logout")
79-
fmt.Println("Config file does not exist, unable to logout.")
80-
return err
73+
Err(pErr).
74+
Str("profile", profile).
75+
Msg("unable to logout profile")
76+
return pErr
8177
}
78+
fmt.Printf(
79+
"Logged out successfully, removed profile '%s' from config file '%s'.",
80+
profile,
81+
configFilePath,
82+
)
83+
return nil
84+
}
85+
86+
logoutFileErr := logoutFile(configFilePath)
87+
if logoutFileErr != nil {
8288
log.Error().
83-
Err(err).
84-
Msg("unable to remove config file, logout failed")
85-
fmt.Println("Error removing config file: ", err)
86-
return err
89+
Err(logoutFileErr).
90+
Str("configFilePath", configFilePath).
91+
Msg("unable to logout")
92+
return logoutFileErr
8793
}
88-
log.Info().
89-
Str("configFilePath", configFilePath).
90-
Msg("Config file removed successfully")
91-
fmt.Println("Logged out successfully!")
9294
return nil
9395
},
9496
}
9597

98+
// logoutFile removes the config file
99+
func logoutFile(f string) error {
100+
log.Info().
101+
Str("configFilePath", f).
102+
Msg("Running logout command for config file")
103+
104+
var performLogout bool
105+
if !noPrompt {
106+
performLogout = promptForInteractiveYesNo(
107+
fmt.Sprintf(
108+
"Are you sure you want to remove the config file '%s'?",
109+
f,
110+
),
111+
)
112+
if !performLogout {
113+
log.Info().Msg("Logout file cancelled")
114+
fmt.Println(fmt.Sprintf("Logout file '%s' cancelled.", f))
115+
return nil
116+
}
117+
}
118+
119+
log.Debug().
120+
Str("configFilePath", f).
121+
Msg("Removing config file")
122+
err := os.Remove(f)
123+
124+
if err != nil {
125+
if os.IsNotExist(err) {
126+
log.Error().
127+
Err(err).
128+
Msg("config file does not exist, unable to logout")
129+
return err
130+
}
131+
log.Error().
132+
Err(err).
133+
Msg("unable to remove config file, logout failed")
134+
return err
135+
}
136+
log.Info().
137+
Str("configFilePath", f).
138+
Msg("Config file removed successfully")
139+
fmt.Println(fmt.Sprintf("Logged out successfully, removed config file '%s'", f))
140+
return nil
141+
}
142+
143+
// envLogout unsets environment variables
96144
func envLogout() {
145+
146+
if !noPrompt {
147+
performLogout := promptForInteractiveYesNo("Are you sure you want to unset environment variables?")
148+
if !performLogout {
149+
log.Info().Msg("Logout environment variables cancelled")
150+
fmt.Println("Logout environment variables cancelled.")
151+
return
152+
}
153+
}
154+
97155
log.Debug().Msg("Running logout command for environment variables")
98156

99157
log.Debug().Msg("Unsetting base environment variables")
@@ -164,6 +222,70 @@ func envLogout() {
164222

165223
}
166224

225+
// logoutProfile removes the profile from the config file
226+
func logoutProfile(p string, f string) error {
227+
log.Info().
228+
Str("profile", p).
229+
Str("configFilePath", f).
230+
Msg("Running logout command for profile")
231+
232+
var performLogout bool
233+
if !noPrompt {
234+
performLogout = promptForInteractiveYesNo(
235+
fmt.Sprintf(
236+
"Are you sure you want to remove profile '%s' from '%s?", p, f,
237+
),
238+
)
239+
if !performLogout {
240+
log.Info().Msg("Logout profile cancelled")
241+
fmt.Println(fmt.Sprintf("Logout profile '%s' in '%s' cancelled.", p, f))
242+
return nil
243+
}
244+
}
245+
246+
log.Debug().
247+
Str("configFilePath", f).
248+
Msg("Reading config file")
249+
config, err := auth_providers.ReadConfigFromJSON(f)
250+
if err != nil {
251+
log.Error().
252+
Err(err).
253+
Str("configFilePath", f).
254+
Str("profile", p).
255+
Msg("unable to read config file, logout failed")
256+
return err
257+
} else if config == nil {
258+
log.Error().
259+
Str("configFilePath", f).
260+
Str("profile", p).
261+
Msg("config file is empty, unable to logout")
262+
return fmt.Errorf("config file is empty, unable to logout profile '%s'", p)
263+
}
264+
265+
// check if profile exists
266+
if _, ok := config.Servers[p]; !ok {
267+
log.Error().
268+
Str("profile", p).
269+
Msg("profile does not exist, unable to logout")
270+
return fmt.Errorf("profile '%s' does not exist, unable to logout", p)
271+
}
272+
delete(config.Servers, p)
273+
wErr := auth_providers.WriteConfigToJSON(f, config)
274+
if wErr != nil {
275+
log.Error().
276+
Err(wErr).
277+
Str("configFilePath", f).
278+
Str("profile", p).
279+
Msg("unable to write config file, logout failed")
280+
return wErr
281+
}
282+
log.Info().
283+
Str("configFilePath", f).
284+
Str("profile", p).
285+
Msg("Profile removed successfully")
286+
return nil
287+
}
288+
167289
func init() {
168290
RootCmd.AddCommand(logoutCmd)
169291
}

0 commit comments

Comments
 (0)