2015-03-30 16:21:59 +00:00
|
|
|
package disk
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/mitchellh/go-homedir"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DefaultPath is the default path where the Vault token is stored.
|
|
|
|
const DefaultPath = "~/.vault-token"
|
|
|
|
|
2015-03-30 18:07:31 +00:00
|
|
|
type Command struct {
|
|
|
|
Path string
|
|
|
|
}
|
2015-03-30 16:21:59 +00:00
|
|
|
|
|
|
|
func (c *Command) Run(args []string) int {
|
|
|
|
var path string
|
2015-03-30 18:07:31 +00:00
|
|
|
pathDefault := DefaultPath
|
|
|
|
if c.Path != "" {
|
|
|
|
pathDefault = c.Path
|
|
|
|
}
|
|
|
|
|
2015-03-30 16:21:59 +00:00
|
|
|
f := flag.NewFlagSet("token-disk", flag.ContinueOnError)
|
2015-03-30 18:07:31 +00:00
|
|
|
f.StringVar(&path, "path", pathDefault, "")
|
2015-03-30 16:21:59 +00:00
|
|
|
f.Usage = func() { fmt.Fprintf(os.Stderr, c.Help()+"\n") }
|
|
|
|
if err := f.Parse(args); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "\n%s\n", err)
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
path, err := homedir.Expand(path)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Error expanding directory: %s\n", err)
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2015-03-31 06:21:17 +00:00
|
|
|
args = f.Args()
|
2015-03-30 16:21:59 +00:00
|
|
|
switch args[0] {
|
|
|
|
case "get":
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "%s\n", err)
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
if _, err := io.Copy(os.Stdout, f); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "%s\n", err)
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
case "store":
|
|
|
|
f, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "%s\n", err)
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
if _, err := io.Copy(f, os.Stdin); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "%s\n", err)
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
case "erase":
|
|
|
|
if err := os.Remove(path); err != nil && !os.IsNotExist(err) {
|
|
|
|
fmt.Fprintf(os.Stderr, "%s\n", err)
|
|
|
|
return 1
|
|
|
|
}
|
2015-03-31 06:21:17 +00:00
|
|
|
default:
|
|
|
|
fmt.Fprintf(os.Stderr, "Error: unknown subcommand: %s\n", args[0])
|
|
|
|
return 1
|
2015-03-30 16:21:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Command) Synopsis() string {
|
|
|
|
return "Stores Vault tokens on disk"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Command) Help() string {
|
|
|
|
helpText := `
|
|
|
|
Usage: vault token-disk [options] [operation]
|
|
|
|
|
|
|
|
Vault token helper (see vault config "token_helper") that writes
|
|
|
|
authenticated tokens to disk unencrypted.
|
|
|
|
|
|
|
|
Options:
|
|
|
|
|
|
|
|
-path=path Path to store the token.
|
|
|
|
|
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|