This repository has been archived by the owner on Apr 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from MarcosCela/store-creds-in-keyring
Store creds in keyring
- Loading branch information
Showing
9 changed files
with
131 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package command | ||
|
||
import ( | ||
"dr/config" | ||
"dr/errcodes" | ||
"fmt" | ||
"github.com/urfave/cli" | ||
"github.com/zalando/go-keyring" | ||
"golang.org/x/crypto/ssh/terminal" | ||
"syscall" | ||
) | ||
|
||
// Login ensures that a password is saved for a given context in the OS keyring | ||
func Login(c *cli.Context) error { | ||
if !c.Args().Present() { | ||
return cli.NewExitError("You must provide a context name to login", errcodes.InsufficientArgs) | ||
} | ||
var contextName = c.Args().First() | ||
if !contextExists(contextName, config.GetCurrentConfiguration(c).Contexts) { | ||
return cli.NewExitError("The context you are trying to login-to does not exist", errcodes.InvalidContext) | ||
} | ||
user, err := getUserForContext(contextName, config.GetCurrentConfiguration(c)) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println("Enter a password:") | ||
bytePassword, err := terminal.ReadPassword(int(syscall.Stdin)) | ||
if err != nil { | ||
return cli.NewExitError("There was an error getting the password from the terminal: "+err.Error(), errcodes.TerminalReadError) | ||
} | ||
|
||
err = keyring.Set(contextName, user, string(bytePassword)) | ||
if err != nil { | ||
return cli.NewExitError("There was an error saving the password to the keyring: "+err.Error(), errcodes.KeyringError) | ||
} | ||
fmt.Println("Correctly set the password for context: <" + contextName + "> and user: <" + user + ">") | ||
return nil | ||
} | ||
|
||
func getUserForContext(contextName string, configuration config.DrConfig) (string, error) { | ||
for _, context := range configuration.Contexts { | ||
if context.Name == contextName { | ||
if context.User == "" { | ||
return "", cli.NewExitError("The username for the context: "+contextName+" is empty!", errcodes.InvalidContext) | ||
} | ||
return context.User, nil | ||
} | ||
} | ||
return "", cli.NewExitError("Could not recover a valid username for the context: <"+contextName+">", errcodes.InvalidContext) | ||
} | ||
|
||
func LoginComplete(c *cli.Context) { | ||
currentConfiguration := config.GetCurrentConfiguration(c) | ||
|
||
if len(currentConfiguration.Contexts) == 0 { | ||
return | ||
} | ||
|
||
contextNames := getContextNames(currentConfiguration) | ||
for _, contextName := range contextNames { | ||
fmt.Println(contextName) | ||
} | ||
} | ||
|
||
func getContextNames(cfg config.DrConfig) []string { | ||
contextNames := make([]string, len(cfg.Contexts)) | ||
for i, context := range cfg.Contexts { | ||
contextNames[i] = context.Name | ||
} | ||
return contextNames | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,4 @@ contexts: | |
- name: default | ||
url: http://localhost:5000 | ||
user: "" | ||
pass: "" | ||
trusted: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters