2015-12-14 21:23:04 +00:00
|
|
|
package token
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
2016-08-24 00:22:45 +00:00
|
|
|
"strings"
|
2015-12-14 21:23:04 +00:00
|
|
|
|
|
|
|
"github.com/mitchellh/go-homedir"
|
|
|
|
)
|
|
|
|
|
2017-09-05 03:50:45 +00:00
|
|
|
var _ TokenHelper = (*InternalTokenHelper)(nil)
|
|
|
|
|
2015-12-14 21:23:04 +00:00
|
|
|
// InternalTokenHelper fulfills the TokenHelper interface when no external
|
|
|
|
// token-helper is configured, and avoids shelling out
|
|
|
|
type InternalTokenHelper struct {
|
|
|
|
tokenPath string
|
|
|
|
}
|
|
|
|
|
|
|
|
// populateTokenPath figures out the token path using homedir to get the user's
|
|
|
|
// home directory
|
|
|
|
func (i *InternalTokenHelper) populateTokenPath() {
|
|
|
|
homePath, err := homedir.Dir()
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("error getting user's home directory: %v", err))
|
|
|
|
}
|
|
|
|
i.tokenPath = homePath + "/.vault-token"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *InternalTokenHelper) Path() string {
|
|
|
|
return i.tokenPath
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get gets the value of the stored token, if any
|
|
|
|
func (i *InternalTokenHelper) Get() (string, error) {
|
|
|
|
i.populateTokenPath()
|
|
|
|
f, err := os.Open(i.tokenPath)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
if _, err := io.Copy(buf, f); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2016-08-24 00:22:45 +00:00
|
|
|
return strings.TrimSpace(buf.String()), nil
|
2015-12-14 21:23:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store stores the value of the token to the file
|
|
|
|
func (i *InternalTokenHelper) Store(input string) error {
|
|
|
|
i.populateTokenPath()
|
|
|
|
f, err := os.OpenFile(i.tokenPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
buf := bytes.NewBufferString(input)
|
|
|
|
if _, err := io.Copy(f, buf); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Erase erases the value of the token
|
|
|
|
func (i *InternalTokenHelper) Erase() error {
|
|
|
|
i.populateTokenPath()
|
|
|
|
if err := os.Remove(i.tokenPath); err != nil && !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|