From a3e615652427335c54212ea96a8198b0b4d7fd16 Mon Sep 17 00:00:00 2001 From: Joel Lau Date: Tue, 29 Oct 2024 03:43:27 +0800 Subject: [PATCH 1/3] specify ssh dir Signed-off-by: Joel Lau --- internal/backend/crypto/age/ssh.go | 36 ++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/internal/backend/crypto/age/ssh.go b/internal/backend/crypto/age/ssh.go index 6b0655e0c2..fd9d244661 100644 --- a/internal/backend/crypto/age/ssh.go +++ b/internal/backend/crypto/age/ssh.go @@ -32,14 +32,11 @@ func (a *Age) getSSHIdentities(ctx context.Context) (map[string]age.Identity, er return sshCache, nil } - // notice that this respects the GOPASS_HOMEDIR env variable, and won't - // find a .ssh folder in your home directory if you set GOPASS_HOMEDIR - uhd := appdir.UserHome() - sshDir := filepath.Join(uhd, ".ssh") - if !fsutil.IsDir(sshDir) { - debug.Log("no .ssh directory found at %s. Ignoring SSH identities", sshDir) + sshDir, err := getSSHDir() + if err != nil { + debug.Log("asdf: %s", err) - return nil, fmt.Errorf("no identities found: %w", ErrNoSSHDir) + return nil, fmt.Errorf("asdf: %w", err) } files, err := os.ReadDir(sshDir) @@ -69,6 +66,31 @@ func (a *Age) getSSHIdentities(ctx context.Context) (map[string]age.Identity, er return ids, nil } +func getSSHDir() (string, error) { + preferredPath := os.Getenv("GOPASS_SSHDIR") + if preferredPath != "" { + return preferredPath, nil + } + sshDir := filepath.Join(preferredPath, ".ssh") + if !fsutil.IsDir(sshDir) { + debug.Log("no .ssh directory found at %s. Ignoring SSH identities", sshDir) + + return "", fmt.Errorf("no identities found: %w", ErrNoSSHDir) + } + + // notice that this respects the GOPASS_HOMEDIR env variable, and won't + // find a .ssh folder in your home directory if you set GOPASS_HOMEDIR + uhd := appdir.UserHome() + sshDir = filepath.Join(uhd, ".ssh") + if !fsutil.IsDir(sshDir) { + debug.Log("no .ssh directory found at %s. Ignoring SSH identities", sshDir) + + return "", fmt.Errorf("no identities found: %w", ErrNoSSHDir) + } + + return "", nil +} + // parseSSHIdentity parses a SSH public key file and returns the recipient and the identity. func (a *Age) parseSSHIdentity(ctx context.Context, pubFn string) (string, age.Identity, error) { privFn := strings.TrimSuffix(pubFn, ".pub") From bcc3b44f913c11237429ed4fb24f869f8e025a2e Mon Sep 17 00:00:00 2001 From: Joel Lau Date: Wed, 30 Oct 2024 01:25:00 +0800 Subject: [PATCH 2/3] added documentation, prefer GOPASS_SSHDIR Signed-off-by: Joel Lau --- docs/backends/age.md | 5 +++++ docs/config.md | 1 + internal/backend/crypto/age/ssh.go | 23 ++++++++--------------- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/docs/backends/age.md b/docs/backends/age.md index 181986f973..d1e840a8da 100644 --- a/docs/backends/age.md +++ b/docs/backends/age.md @@ -37,6 +37,11 @@ $ GOPASS_AGE_PASSWORD=mypassword gopass init --crypto age Notice the extra space in front of the command to skip most shell's history. You'll need to set your name and username using `git` directly if you're using it as storage backend (the default one). +You can also specify the ssh directory by setting environment variable +``` +$ GOPASS_SSH_DIR=/Downloads/new_ssh_dir gopass init --crypto age +``` + ## Features * Encryption using `age` library, can be decrypted using the `age` CLI diff --git a/docs/config.md b/docs/config.md index 5488748071..632ecbe8b5 100644 --- a/docs/config.md +++ b/docs/config.md @@ -34,6 +34,7 @@ Some configuration options are only available through setting environment variab | `GOPASS_NO_NOTIFY` | `bool` | Set to any non-empty value to prevent notifications | | `GOPASS_NO_REMINDER` | `bool` | Set to any non-empty value to prevent reminders | | `GOPASS_PW_DEFAULT_LENGTH` | `int` | Set to any integer value larger than zero to define a different default length in the `generate` command. By default the length is 24 characters. | +| `GOPASS_SSH_DIR` | `string` | Set to a filepath that contains ssh keys. Overrides default location. | | `GOPASS_UMASK` | `octal` | Set to any valid umask to mask bits of files created by gopass | | `GOPASS_UNCLIP_CHECKSUM` | `string` | (internal) Used between gopass and it's unclip helper. | | `GOPASS_UNCLIP_NAME` | `string` | (internal) Used between gopass and it's unclip helper. | diff --git a/internal/backend/crypto/age/ssh.go b/internal/backend/crypto/age/ssh.go index fd9d244661..85af37bec7 100644 --- a/internal/backend/crypto/age/ssh.go +++ b/internal/backend/crypto/age/ssh.go @@ -34,9 +34,9 @@ func (a *Age) getSSHIdentities(ctx context.Context) (map[string]age.Identity, er sshDir, err := getSSHDir() if err != nil { - debug.Log("asdf: %s", err) + debug.Log("no .ssh directory found at %s. Ignoring SSH identities", sshDir) - return nil, fmt.Errorf("asdf: %w", err) + return nil, fmt.Errorf("no identities found: %w", err) } files, err := os.ReadDir(sshDir) @@ -67,28 +67,21 @@ func (a *Age) getSSHIdentities(ctx context.Context) (map[string]age.Identity, er } func getSSHDir() (string, error) { - preferredPath := os.Getenv("GOPASS_SSHDIR") - if preferredPath != "" { - return preferredPath, nil - } + preferredPath := os.Getenv("GOPASS_SSH_DIR") sshDir := filepath.Join(preferredPath, ".ssh") - if !fsutil.IsDir(sshDir) { - debug.Log("no .ssh directory found at %s. Ignoring SSH identities", sshDir) - - return "", fmt.Errorf("no identities found: %w", ErrNoSSHDir) + if fsutil.IsDir(sshDir) { + return preferredPath, nil } // notice that this respects the GOPASS_HOMEDIR env variable, and won't // find a .ssh folder in your home directory if you set GOPASS_HOMEDIR uhd := appdir.UserHome() sshDir = filepath.Join(uhd, ".ssh") - if !fsutil.IsDir(sshDir) { - debug.Log("no .ssh directory found at %s. Ignoring SSH identities", sshDir) - - return "", fmt.Errorf("no identities found: %w", ErrNoSSHDir) + if fsutil.IsDir(sshDir) { + return sshDir, nil } - return "", nil + return "", ErrNoSSHDir } // parseSSHIdentity parses a SSH public key file and returns the recipient and the identity. From 37705a55524bd7abb3ff3d1ffa0fb6e17d0048ae Mon Sep 17 00:00:00 2001 From: Joel Lau Date: Tue, 5 Nov 2024 13:50:30 +0800 Subject: [PATCH 3/3] check for empty path Signed-off-by: Joel Lau --- internal/backend/crypto/age/ssh.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/backend/crypto/age/ssh.go b/internal/backend/crypto/age/ssh.go index 85af37bec7..d256ae068b 100644 --- a/internal/backend/crypto/age/ssh.go +++ b/internal/backend/crypto/age/ssh.go @@ -69,7 +69,7 @@ func (a *Age) getSSHIdentities(ctx context.Context) (map[string]age.Identity, er func getSSHDir() (string, error) { preferredPath := os.Getenv("GOPASS_SSH_DIR") sshDir := filepath.Join(preferredPath, ".ssh") - if fsutil.IsDir(sshDir) { + if preferredPath != "" && fsutil.IsDir(sshDir) { return preferredPath, nil }