Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoiding CGO landmine #81

Merged
merged 1 commit into from
Nov 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions vault/token_strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package vault

import (
"fmt"
"github.com/blang/vfs"
"io/ioutil"
"log"
"net/url"
"os"
"os/user"
"path"

"github.com/blang/vfs"
)

// TokenAuthStrategy - a pass-through strategy for situations where we already
Expand Down Expand Up @@ -50,12 +50,20 @@ func (a *TokenAuthStrategy) Revokable() bool {
return false
}

func getTokenFromFile(fs vfs.Filesystem) string {
u, err := user.Current()
if err != nil {
log.Fatal(err)
func homeDir() string {
if home := os.Getenv("HOME"); home != "" {
return home
}
f, err := fs.OpenFile(path.Join(u.HomeDir, ".vault-token"), os.O_RDONLY, 0)
if home := os.Getenv("USERPROFILE"); home != "" {
return home
}
log.Fatal(`Neither HOME nor USERPROFILE environment variables are set!
I can't figure out where the current user's home directory is!`)
return ""
}

func getTokenFromFile(fs vfs.Filesystem) string {
f, err := fs.OpenFile(path.Join(homeDir(), ".vault-token"), os.O_RDONLY, 0)
if err != nil {
return ""
}
Expand Down
7 changes: 2 additions & 5 deletions vault/token_strategy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package vault

import (
"os"
"os/user"
"path"
"testing"

Expand All @@ -23,13 +22,11 @@ func TestNewTokenAuthStrategy_FromEnvVar(t *testing.T) {

func TestNewTokenAuthStrategy_FromFileGivenNoEnvVar(t *testing.T) {
token := "deadbeef"
u, err := user.Current()
assert.NoError(t, err)

fs := memfs.Create()
err = vfs.MkdirAll(fs, u.HomeDir, 0777)
err := vfs.MkdirAll(fs, homeDir(), 0777)
assert.NoError(t, err)
f, err := vfs.Create(fs, path.Join(u.HomeDir, ".vault-token"))
f, err := vfs.Create(fs, path.Join(homeDir(), ".vault-token"))
assert.NoError(t, err)
f.Write([]byte(token))

Expand Down