mirror of
https://github.com/optim-enterprises-bv/vault.git
synced 2025-11-03 03:58:01 +00:00
command/auth: setting tokens works
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/vault/helper/password"
|
||||
)
|
||||
|
||||
// AuthCommand is a Command that handles authentication.
|
||||
@@ -30,6 +34,59 @@ func (c *AuthCommand) Run(args []string) int {
|
||||
return 1
|
||||
}
|
||||
|
||||
tokenHelper, err := c.TokenHelper()
|
||||
if err != nil {
|
||||
c.Ui.Error(fmt.Sprintf(
|
||||
"Error initializing token helper: %s\n\n"+
|
||||
"Please verify that the token helper is available and properly\n"+
|
||||
"configured for your system. Please refer to the documentation\n"+
|
||||
"on token helpers for more information.",
|
||||
err))
|
||||
return 1
|
||||
}
|
||||
|
||||
// token is where the final token will go
|
||||
var token string
|
||||
if method == "" {
|
||||
if len(args) > 0 {
|
||||
token = args[0]
|
||||
|
||||
// TODO(mitchellh): stdin
|
||||
} else {
|
||||
// No arguments given, read the token from user input
|
||||
fmt.Printf("Token (will be hidden): ")
|
||||
token, err = password.Read(os.Stdin)
|
||||
if err != nil {
|
||||
c.Ui.Error(fmt.Sprintf(
|
||||
"Error attempting to ask for token. The raw error message\n"+
|
||||
"is shown below, but the most common reason for this error is\n"+
|
||||
"that you attempted to pipe a value into auth. If you want to\n"+
|
||||
"pipe the token, please pass '-' as the token argument.\n\n"+
|
||||
"Raw error: %s", err))
|
||||
return 1
|
||||
}
|
||||
}
|
||||
|
||||
if token == "" {
|
||||
c.Ui.Error(fmt.Sprintf(
|
||||
"A token must be passed to auth. Please view the help\n" +
|
||||
"for more information."))
|
||||
return 1
|
||||
}
|
||||
} else {
|
||||
// TODO(mitchellh): other auth methods
|
||||
}
|
||||
|
||||
// Store the token!
|
||||
if err := tokenHelper.Store(token); err != nil {
|
||||
c.Ui.Error(fmt.Sprintf(
|
||||
"Error storing token: %s\n\n"+
|
||||
"Authentication was not successful and did not persist.\n"+
|
||||
"Please reauthenticate, or fix the issue above if possible.",
|
||||
err))
|
||||
return 1
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user