Skip to content

Commit d3aad49

Browse files
authored
Merge pull request #12 from Keyfactor/logout
feat(cli): Implemented `logout` subcommand that will remove local cli…
2 parents 9de29f4 + 2fd57c0 commit d3aad49

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+186
-50
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,44 @@ kfutil --help
3232
All the variables listed below need to be set in your environment. The `kfutil` command will look for these variables
3333
and use them if they are set. If they are not set, the utility will fail to connect to Keyfactor.
3434

35+
Linux/MacOS:
3536
```bash
3637
export KEYFACTOR_HOSTNAME=<mykeyfactorhost.mydomain.com>
3738
export KEYFACTOR_USERNAME=<myusername> # Do not include domain
3839
export KEYFACTOR_PASSWORD=<mypassword>
3940
export KEYFACTOR_DOMAIN=<mykeyfactordomain>
4041
```
4142

43+
Windows Powershell
44+
```powershell
45+
$env:KEYFACTOR_HOSTNAME="<mykeyfactorhost.mydomain.com>"
46+
$env:KEYFACTOR_USERNAME="<myusername>" # Do not include domain
47+
$env:KEYFACTOR_PASSWORD="<mypassword>"
48+
$env:KEYFACTOR_DOMAIN="<mykeyfactordomain>"
49+
```
50+
51+
## Commands
52+
53+
### Login
54+
For full documentation on the `login` command, see the [login](docs/kfutil_login.md) documentation.
55+
56+
*WARNING* - The `login` command will store your Keyfactor credentials in a file on your local machine. This file is not
57+
encrypted and is not secure. It is recommended that you use the `login` command only on your local machine and not on a
58+
shared machine. Instead of using the `login` command, you can set the environmental variables listed above.
59+
60+
```bash
61+
kfutil login
62+
```
63+
64+
### Logout
65+
For full documentation on the `logout` command, see the [logout](docs/kfutil_logout.md) documentation.
66+
67+
*WARNING* - This will delete the file containing your Keyfactor credentials at `$HOME/.keyfactor/command_config.json`.
68+
69+
```bash
70+
kfutil logout
71+
```
72+
4273
## Commands
4374

4475
### Bulk operations

cmd/login.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,16 @@ const DefaultConfigFileName = "command_config.json"
2424
// loginCmd represents the login command
2525
var loginCmd = &cobra.Command{
2626
Use: "login",
27-
Short: "User interactive login to Keyfactor.",
27+
Short: "User interactive login to Keyfactor. Stores the credentials in the config file '$HOME/.keyfactor/command_config.json'.",
2828
Long: `Will prompt the user for a username and password and then attempt to login to Keyfactor.
2929
You can provide the --config flag to specify a config file to use. If not provided, the default
3030
config file will be used. The default config file is located at $HOME/.keyfactor/command_config.json.
3131
To prevent the prompt for username and password, use the --no-prompt flag. If this flag is provided then
3232
the CLI will default to using the environment variables: KEYFACTOR_HOSTNAME, KEYFACTOR_USERNAME,
3333
KEYFACTOR_PASSWORD and KEYFACTOR_DOMAIN.
34+
35+
WARNING: The username and password will be stored in the config file in plain text at:
36+
'$HOME/.keyfactor/command_config.json.'
3437
`,
3538
Run: func(cmd *cobra.Command, args []string) {
3639
log.SetOutput(io.Discard)
@@ -100,8 +103,11 @@ func authConfigFile(configFile string, noPrompt bool) bool {
100103
fmt.Printf("Enter Keyfactor Command host URL [%s]: \n", envHostName)
101104
_, phErr := fmt.Scanln(&host)
102105
if phErr != nil {
103-
fmt.Println("Error getting hostname: ", phErr)
104-
log.Fatal("[ERROR] getting hostname: ", phErr)
106+
if phErr.Error() != "unexpected newline" {
107+
fmt.Println("Error getting hostname: ", phErr)
108+
log.Println("[ERROR] getting hostname: ", phErr)
109+
}
110+
105111
}
106112
if len(host) == 0 {
107113
host = envHostName
@@ -128,8 +134,10 @@ func authConfigFile(configFile string, noPrompt bool) bool {
128134
fmt.Printf("Enter your Keyfactor Command username [%s]: \n", envUserName)
129135
_, puErr := fmt.Scanln(&username)
130136
if puErr != nil {
131-
fmt.Println("Error getting username: ", puErr)
132-
log.Fatal("[ERROR] getting username: ", puErr)
137+
if puErr.Error() != "unexpected newline" {
138+
fmt.Println("Error getting username: ", puErr)
139+
log.Println("[ERROR] getting username: ", puErr)
140+
}
133141
}
134142
}
135143
if len(username) == 0 {
@@ -189,8 +197,11 @@ func authConfigFile(configFile string, noPrompt bool) bool {
189197
fmt.Printf("Enter your Keyfactor Command AD domain [%s]: \n", envDomain)
190198
_, sdErr := fmt.Scanln(&domain)
191199
if sdErr != nil {
192-
fmt.Println("Error getting domain: ", sdErr)
193-
log.Fatal("[ERROR] getting domain: ", sdErr)
200+
if sdErr.Error() != "unexpected newline" {
201+
fmt.Println("Error getting domain: ", sdErr)
202+
log.Println("[ERROR] getting domain: ", sdErr)
203+
}
204+
194205
}
195206
if len(domain) == 0 {
196207
domain = envDomain

cmd/logout.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Package cmd Copyright 2022 Keyfactor
2+
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
3+
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
4+
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
5+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
6+
// and limitations under the License.
7+
package cmd
8+
9+
import (
10+
"fmt"
11+
"io"
12+
"log"
13+
"os"
14+
15+
"github.com/spf13/cobra"
16+
)
17+
18+
// logoutCmd represents the logout command
19+
var logoutCmd = &cobra.Command{
20+
Use: "logout",
21+
Short: "Removes the credentials file '$HOME/.keyfactor/command_config.json'.",
22+
Long: `Removes the credentials file '$HOME/.keyfactor/command_config.json'.`,
23+
Run: func(cmd *cobra.Command, args []string) {
24+
log.SetOutput(io.Discard)
25+
err := os.Remove(fmt.Sprintf("%s/.keyfactor/%s", os.Getenv("HOME"), DefaultConfigFileName))
26+
if err != nil {
27+
fmt.Println("Error removing config file: ", err)
28+
log.Fatal("[ERROR] removing config file: ", err)
29+
}
30+
fmt.Println("Logged out successfully!")
31+
},
32+
}
33+
34+
func init() {
35+
RootCmd.AddCommand(logoutCmd)
36+
}

docs/kfutil.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ A CLI wrapper around the Keyfactor Platform API.
1616
### SEE ALSO
1717

1818
* [kfutil containers](kfutil_containers.md) - Keyfactor certificate store container API and utilities.
19-
* [kfutil login](kfutil_login.md) - User interactive login to Keyfactor.
19+
* [kfutil login](kfutil_login.md) - User interactive login to Keyfactor. Stores the credentials in the config file '$HOME/.keyfactor/command_config.json'.
20+
* [kfutil logout](kfutil_logout.md) - Removes the credentials file '$HOME/.keyfactor/command_config.json'.
2021
* [kfutil orchs](kfutil_orchs.md) - Keyfactor agents/orchestrators APIs and utilities.
2122
* [kfutil status](kfutil_status.md) - List the status of Keyfactor services.
2223
* [kfutil store-types](kfutil_store-types.md) - Keyfactor certificate store types APIs and utilities.
2324
* [kfutil stores](kfutil_stores.md) - Keyfactor certificate stores APIs and utilities.
2425
* [kfutil version](kfutil_version.md) - Shows version of kfutil
2526

26-
###### Auto generated by spf13/cobra on 29-Nov-2022
27+
###### Auto generated by spf13/cobra on 1-Dec-2022

docs/kfutil_containers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ A collections of APIs and utilities for interacting with Keyfactor certificate s
1818
* [kfutil containers get](kfutil_containers_get.md) - Get certificate store container by ID or name.
1919
* [kfutil containers list](kfutil_containers_list.md) - List certificate store containers.
2020

21-
###### Auto generated by spf13/cobra on 29-Nov-2022
21+
###### Auto generated by spf13/cobra on 1-Dec-2022

docs/kfutil_containers_get.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ kfutil containers get [flags]
2121

2222
* [kfutil containers](kfutil_containers.md) - Keyfactor certificate store container API and utilities.
2323

24-
###### Auto generated by spf13/cobra on 29-Nov-2022
24+
###### Auto generated by spf13/cobra on 1-Dec-2022

docs/kfutil_containers_list.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ kfutil containers list [flags]
2020

2121
* [kfutil containers](kfutil_containers.md) - Keyfactor certificate store container API and utilities.
2222

23-
###### Auto generated by spf13/cobra on 29-Nov-2022
23+
###### Auto generated by spf13/cobra on 1-Dec-2022

docs/kfutil_login.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## kfutil login
22

3-
User interactive login to Keyfactor.
3+
User interactive login to Keyfactor. Stores the credentials in the config file '$HOME/.keyfactor/command_config.json'.
44

55
### Synopsis
66

@@ -11,6 +11,9 @@ To prevent the prompt for username and password, use the --no-prompt flag. If th
1111
the CLI will default to using the environment variables: KEYFACTOR_HOSTNAME, KEYFACTOR_USERNAME,
1212
KEYFACTOR_PASSWORD and KEYFACTOR_DOMAIN.
1313

14+
WARNING: The username and password will be stored in the config file in plain text at:
15+
'$HOME/.keyfactor/command_config.json.'
16+
1417

1518
```
1619
kfutil login [flags]
@@ -28,4 +31,4 @@ kfutil login [flags]
2831

2932
* [kfutil](kfutil.md) - Keyfactor CLI utilities
3033

31-
###### Auto generated by spf13/cobra on 29-Nov-2022
34+
###### Auto generated by spf13/cobra on 1-Dec-2022

docs/kfutil_logout.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## kfutil logout
2+
3+
Removes the credentials file '$HOME/.keyfactor/command_config.json'.
4+
5+
### Synopsis
6+
7+
Removes the credentials file '$HOME/.keyfactor/command_config.json'.
8+
9+
```
10+
kfutil logout [flags]
11+
```
12+
13+
### Options
14+
15+
```
16+
-h, --help help for logout
17+
```
18+
19+
### SEE ALSO
20+
21+
* [kfutil](kfutil.md) - Keyfactor CLI utilities
22+
23+
###### Auto generated by spf13/cobra on 1-Dec-2022

docs/kfutil_orchs.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ A collections of APIs and utilities for interacting with Keyfactor orchestrators
2222
* [kfutil orchs logs](kfutil_orchs_logs.md) - Get orchestrator logs by ID or machine/client name.
2323
* [kfutil orchs reset](kfutil_orchs_reset.md) - Reset orchestrator by ID or machine/client name.
2424

25-
###### Auto generated by spf13/cobra on 29-Nov-2022
25+
###### Auto generated by spf13/cobra on 1-Dec-2022

0 commit comments

Comments
 (0)