Skip to content

Commit

Permalink
Merge pull request #110 from 99designs/fix-tilde-handling
Browse files Browse the repository at this point in the history
Fix tilde handling
  • Loading branch information
mtibben authored Mar 7, 2022
2 parents ecf5c8e + af41858 commit 932029a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 26 deletions.
4 changes: 2 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Config struct {
// FilePasswordFunc is a required function used to prompt the user for a password
FilePasswordFunc PromptFunc

// FileDir is the directory that keyring files are stored in, ~ is resolved to home dir
// FileDir is the directory that keyring files are stored in, ~/ is resolved to the users' home dir
FileDir string

// KeyCtlScope is the scope of the kernel keyring (either "user", "session", "process" or "thread")
Expand All @@ -44,7 +44,7 @@ type Config struct {
// LibSecretCollectionName is the name collection in secret-service
LibSecretCollectionName string

// PassDir is the pass password-store directory
// PassDir is the pass password-store directory, ~/ is resolved to the users' home dir
PassDir string

// PassCmd is the name of the pass executable
Expand Down
14 changes: 3 additions & 11 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"

jose "github.com/dvsekhvalnov/jose2go"
Expand Down Expand Up @@ -38,16 +37,9 @@ func (k *fileKeyring) resolveDir() (string, error) {
return "", fmt.Errorf("No directory provided for file keyring")
}

dir := k.dir

// expand tilde for home directory
if strings.HasPrefix(dir, "~") {
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
dir = strings.Replace(dir, "~", home, 1)
debugf("Expanded file dir to %s", dir)
dir, err := ExpandTilde(k.dir)
if err != nil {
return "", err
}

stat, err := os.Stat(dir)
Expand Down
30 changes: 17 additions & 13 deletions pass.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,37 @@ import (

func init() {
supportedBackends[PassBackend] = opener(func(cfg Config) (Keyring, error) {
var err error

pass := &passKeyring{
passcmd: cfg.PassCmd,
dir: cfg.PassDir,
prefix: cfg.PassPrefix,
}
if cfg.PassCmd == "" {

if pass.passcmd == "" {
pass.passcmd = "pass"
}
if cfg.PassDir == "" {

if pass.dir == "" {
if passDir, found := os.LookupEnv("PASSWORD_STORE_DIR"); found {
pass.dir = passDir
} else {
pass.dir = filepath.Join(os.Getenv("HOME"), ".password-store")
}
} else if strings.HasPrefix(pass.dir, "~") {
if len(pass.dir) > 1 && pass.dir[1] != '/' {
return nil, fmt.Errorf("Cannot expand path: %s", pass.dir)
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, err
}
pass.dir = filepath.Join(homeDir, ".password-store")
}
home, err := os.UserHomeDir()
if err != nil {
return nil, err
}
pass.dir = filepath.Join(home, pass.dir[1:])
}

pass.dir, err = ExpandTilde(pass.dir)
if err != nil {
return nil, err
}

// fail if the pass program is not available
_, err := exec.LookPath(pass.passcmd)
_, err = exec.LookPath(pass.passcmd)
if err != nil {
return nil, errors.New("The pass program is not available")
}
Expand Down
23 changes: 23 additions & 0 deletions tilde.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package keyring

import (
"os"
"path/filepath"
"strings"
)

// ~/ or ~\ depending on OS
var tildePrefix = string([]rune{'~', filepath.Separator})

// expand tilde for home directory
func ExpandTilde(dir string) (string, error) {
if strings.HasPrefix(dir, tildePrefix) {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", err
}
dir = strings.Replace(dir, "~", homeDir, 1)
debugf("Expanded file dir to %s", dir)
}
return dir, nil
}

0 comments on commit 932029a

Please sign in to comment.