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

Fix tilde handling #110

Merged
merged 1 commit into from Mar 7, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions config.go
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
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
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
@@ -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
}