@@ -16,9 +16,12 @@ package cmd
16
16
17
17
import (
18
18
"fmt"
19
- "log"
19
+ "io"
20
+ stdlog "log"
20
21
"os"
21
22
23
+ "github.com/Keyfactor/keyfactor-auth-client-go/auth_providers"
24
+ "github.com/rs/zerolog/log"
22
25
"github.com/spf13/cobra"
23
26
)
24
27
@@ -27,29 +30,140 @@ var logoutCmd = &cobra.Command{
27
30
Use : "logout" ,
28
31
Short : "Removes the credentials file '$HOME/.keyfactor/command_config.json'." ,
29
32
Long : `Removes the credentials file '$HOME/.keyfactor/command_config.json'.` ,
30
- Run : func (cmd * cobra.Command , args []string ) {
31
- // Global flags
32
- debugFlag , _ := cmd .Flags ().GetBool ("debugFlag" )
33
- //configFile, _ := cmd.Flags().GetString("config")
34
- //noPrompt, _ := cmd.Flags().GetBool("no-prompt")
35
- //profile, _ := cmd.Flags().GetString("profile")
36
-
37
- debugModeEnabled := checkDebug (debugFlag )
38
- log .Println ("Debug mode enabled: " , debugModeEnabled )
39
- err := os .Remove (fmt .Sprintf ("%s/.keyfactor/%s" , os .Getenv ("HOME" ), DefaultConfigFileName ))
33
+ RunE : func (cmd * cobra.Command , args []string ) error {
34
+ log .Info ().Msg ("Running logout command" )
35
+ cmd .SilenceUsage = true
36
+ // expEnabled checks
37
+ isExperimental := false
38
+ debugErr := warnExperimentalFeature (expEnabled , isExperimental )
39
+ if debugErr != nil {
40
+ return debugErr
41
+ }
42
+ stdlog .SetOutput (io .Discard )
43
+ informDebug (debugFlag )
44
+
45
+ logGlobals ()
46
+
47
+ var configFilePath string
48
+ if configFile == "" {
49
+ // check if environment variables for config file is set
50
+ if os .Getenv (auth_providers .EnvKeyfactorConfigFile ) != "" {
51
+ configFilePath = os .Getenv (auth_providers .EnvKeyfactorConfigFile )
52
+ } else {
53
+ userHomeDir , err := os .UserHomeDir ()
54
+ if err != nil {
55
+ userHomeDir , err = os .Getwd ()
56
+ if err != nil {
57
+ userHomeDir = "."
58
+ }
59
+ }
60
+ configFilePath = fmt .Sprintf ("%s/%s" , userHomeDir , auth_providers .DefaultConfigFilePath )
61
+ }
62
+ } else {
63
+ configFilePath = configFile
64
+ }
65
+
66
+ // Remove environment variables
67
+ log .Info ().Msg ("Running logout command for environment variables" )
68
+ envLogout ()
69
+
70
+ log .Info ().
71
+ Str ("configFilePath" , configFilePath ).
72
+ Msg ("Attempting to removing config file" )
73
+ err := os .Remove (configFilePath )
40
74
if err != nil {
41
75
if os .IsNotExist (err ) {
42
- fmt .Println ("Config file does not exist." )
43
- fmt .Println ("Logged out successfully!" )
44
- return
76
+ 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
45
81
}
82
+ log .Error ().
83
+ Err (err ).
84
+ Msg ("unable to remove config file, logout failed" )
46
85
fmt .Println ("Error removing config file: " , err )
47
- //log.Fatal("[ERROR] removing config file: ", err)
86
+ return err
48
87
}
88
+ log .Info ().
89
+ Str ("configFilePath" , configFilePath ).
90
+ Msg ("Config file removed successfully" )
49
91
fmt .Println ("Logged out successfully!" )
92
+ return nil
50
93
},
51
94
}
52
95
96
+ func envLogout () {
97
+ log .Debug ().Msg ("Running logout command for environment variables" )
98
+
99
+ log .Debug ().Msg ("Unsetting base environment variables" )
100
+
101
+ log .Trace ().Str ("EnvKeyfactorHostName" , auth_providers .EnvKeyfactorHostName ).Msg ("Unsetting" )
102
+ os .Unsetenv (auth_providers .EnvKeyfactorHostName )
103
+
104
+ log .Trace ().Str ("EnvKeyfactorPort" , auth_providers .EnvKeyfactorPort ).Msg ("Unsetting" )
105
+ os .Unsetenv (auth_providers .EnvKeyfactorPort )
106
+
107
+ log .Trace ().Str ("EnvKeyfactorAPIPath" , auth_providers .EnvKeyfactorAPIPath ).Msg ("Unsetting" )
108
+ os .Unsetenv (auth_providers .EnvKeyfactorAPIPath )
109
+
110
+ log .Trace ().Str ("EnvAuthCACert" , auth_providers .EnvAuthCACert ).Msg ("Unsetting" )
111
+ os .Unsetenv (auth_providers .EnvAuthCACert )
112
+
113
+ log .Trace ().Str ("EnvKeyfactorSkipVerify" , auth_providers .EnvKeyfactorSkipVerify ).Msg ("Unsetting" )
114
+ os .Unsetenv (auth_providers .EnvKeyfactorSkipVerify )
115
+
116
+ log .Trace ().Str ("EnvKeyfactorClientTimeout" , auth_providers .EnvKeyfactorClientTimeout ).Msg ("Unsetting" )
117
+ os .Unsetenv (auth_providers .EnvKeyfactorClientTimeout )
118
+
119
+ log .Debug ().Msg ("Unsetting kfutil environment variables" )
120
+ log .Trace ().Str ("EnvKeyfactorAuthProfile" , auth_providers .EnvKeyfactorAuthProfile ).Msg ("Unsetting" )
121
+ os .Unsetenv (auth_providers .EnvKeyfactorAuthProfile )
122
+
123
+ log .Trace ().Str ("EnvKeyfactorConfigFile" , auth_providers .EnvKeyfactorConfigFile ).Msg ("Unsetting" )
124
+ os .Unsetenv (auth_providers .EnvKeyfactorConfigFile )
125
+
126
+ log .Debug ().Msg ("Unsetting command basic auth environment variables" )
127
+ log .Trace ().Str ("EnvKeyfactorUsername" , auth_providers .EnvKeyfactorUsername ).Msg ("Unsetting" )
128
+ os .Unsetenv (auth_providers .EnvKeyfactorUsername )
129
+
130
+ log .Trace ().Str ("EnvKeyfactorPassword" , auth_providers .EnvKeyfactorPassword ).Msg ("Unsetting" )
131
+ os .Unsetenv (auth_providers .EnvKeyfactorPassword )
132
+
133
+ log .Trace ().Str ("EnvKeyfactorDomain" , auth_providers .EnvKeyfactorDomain ).Msg ("Unsetting" )
134
+ os .Unsetenv (auth_providers .EnvKeyfactorDomain )
135
+
136
+ log .Debug ().Msg ("Unsetting command oauth2 environment variables" )
137
+ log .Trace ().Str ("EnvKeyfactorClientID" , auth_providers .EnvKeyfactorClientID ).Msg ("Unsetting" )
138
+ os .Unsetenv (auth_providers .EnvKeyfactorClientID )
139
+
140
+ log .Trace ().Str ("EnvKeyfactorClientSecret" , auth_providers .EnvKeyfactorClientSecret ).Msg ("Unsetting" )
141
+ os .Unsetenv (auth_providers .EnvKeyfactorClientSecret )
142
+
143
+ log .Trace ().Str ("EnvKeyfactorAccessToken" , auth_providers .EnvKeyfactorAccessToken ).Msg ("Unsetting" )
144
+ os .Unsetenv (auth_providers .EnvKeyfactorAccessToken )
145
+
146
+ log .Trace ().Str ("EnvKeyfactorAuthTokenURL" , auth_providers .EnvKeyfactorAuthTokenURL ).Msg ("Unsetting" )
147
+ os .Unsetenv (auth_providers .EnvKeyfactorAuthTokenURL )
148
+
149
+ log .Trace ().Str ("EnvKeyfactorAuthScopes" , auth_providers .EnvKeyfactorAuthScopes ).Msg ("Unsetting" )
150
+ os .Unsetenv (auth_providers .EnvKeyfactorAuthScopes )
151
+
152
+ log .Trace ().Str ("EnvKeyfactorAuthAudience" , auth_providers .EnvKeyfactorAuthAudience ).Msg ("Unsetting" )
153
+ os .Unsetenv (auth_providers .EnvKeyfactorAuthAudience )
154
+
155
+ log .Debug ().Msg ("Unsetting command azure environment variables" )
156
+ log .Trace ().Str ("EnvKeyfactorAuthProvider" , auth_providers .EnvKeyfactorAuthProvider ).Msg ("Unsetting" )
157
+ os .Unsetenv (auth_providers .EnvKeyfactorAuthProvider )
158
+
159
+ log .Trace ().Str ("EnvAzureSecretName" , auth_providers .EnvAzureSecretName ).Msg ("Unsetting" )
160
+ os .Unsetenv (auth_providers .EnvAzureSecretName )
161
+
162
+ log .Trace ().Str ("EnvAzureVaultName" , auth_providers .EnvAzureVaultName ).Msg ("Unsetting" )
163
+ os .Unsetenv (auth_providers .EnvAzureVaultName )
164
+
165
+ }
166
+
53
167
func init () {
54
168
RootCmd .AddCommand (logoutCmd )
55
169
}
0 commit comments