Merge pull request #250 from DavidWittman/233-auth-stdin
Read from stdin with auth command
This commit is contained in:
commit
98c60f2a18
|
@ -1,7 +1,9 @@
|
|||
package command
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
|
@ -25,6 +27,9 @@ type AuthCommand struct {
|
|||
Meta
|
||||
|
||||
Handlers map[string]AuthHandler
|
||||
|
||||
// The fields below can be overwritten for tests
|
||||
testStdin io.Reader
|
||||
}
|
||||
|
||||
func (c *AuthCommand) Run(args []string) int {
|
||||
|
@ -63,6 +68,23 @@ func (c *AuthCommand) Run(args []string) int {
|
|||
|
||||
// token is where the final token will go
|
||||
handler := c.Handlers[method]
|
||||
|
||||
// Read token from stdin if first arg is exactly "-"
|
||||
var stdin io.Reader = os.Stdin
|
||||
if c.testStdin != nil {
|
||||
stdin = c.testStdin
|
||||
}
|
||||
|
||||
if len(args) > 0 && args[0] == "-" {
|
||||
stdinR := bufio.NewReader(stdin)
|
||||
args[0], err = stdinR.ReadString('\n')
|
||||
if err != nil && err != io.EOF {
|
||||
c.Ui.Error(fmt.Sprintf("Error reading from stdin: %s", err))
|
||||
return 1
|
||||
}
|
||||
args[0] = strings.TrimSpace(args[0])
|
||||
}
|
||||
|
||||
if method == "" {
|
||||
token := ""
|
||||
if len(args) > 0 {
|
||||
|
|
|
@ -2,6 +2,7 @@ package command
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
@ -82,6 +83,36 @@ func TestAuth_token(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestAuth_stdin(t *testing.T) {
|
||||
core, _, token := vault.TestCoreUnsealed(t)
|
||||
ln, addr := http.TestServer(t, core)
|
||||
defer ln.Close()
|
||||
|
||||
testAuthInit(t)
|
||||
|
||||
stdinR, stdinW := io.Pipe()
|
||||
ui := new(cli.MockUi)
|
||||
c := &AuthCommand{
|
||||
Meta: Meta{
|
||||
Ui: ui,
|
||||
},
|
||||
testStdin: stdinR,
|
||||
}
|
||||
|
||||
go func() {
|
||||
stdinW.Write([]byte(token))
|
||||
stdinW.Close()
|
||||
}()
|
||||
|
||||
args := []string{
|
||||
"-address", addr,
|
||||
"-",
|
||||
}
|
||||
if code := c.Run(args); code != 0 {
|
||||
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuth_badToken(t *testing.T) {
|
||||
core, _, _ := vault.TestCoreUnsealed(t)
|
||||
ln, addr := http.TestServer(t, core)
|
||||
|
|
Loading…
Reference in New Issue