Skip to content

Commit 8921092

Browse files
authored
refactor(ssh): improve find extended agent (#31)
1. add `Library/Group Containers/2BUA8C4S2C.com.1password/t/agent.sock` path 2. support macos built-in agent fallback Signed-off-by: Black-Hole1 <[email protected]>
1 parent c08f982 commit 8921092

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

pkg/sshagentsock/sshagentsock.go

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os/exec"
1010
"path/filepath"
1111
"strconv"
12+
"strings"
1213

1314
"github.com/oomol-lab/ovm-ssh-agent/pkg/identity"
1415
"github.com/oomol-lab/ovm-ssh-agent/pkg/sshagent"
@@ -17,37 +18,46 @@ import (
1718

1819
var knownAgentPaths = []string{
1920
".1password/agent.sock",
21+
"Library/Group Containers/2BUA8C4S2C.com.1password/t/agent.sock",
2022
}
2123

24+
// FindExtendedAgent finds the extended agent path.
25+
// The find will be done in the following order:
26+
//
27+
// 1. Check if the environment variable SSH_AUTH_SOCK exists (if it is the built-in agent in macOS, it will be used as an alternative).
28+
// 2. Check if any known third-party agent exists at the specified path.
29+
// 3. Get the ssh auth sock of the current system using launchctl.
30+
// 4. If all the above steps fail, use the alternative. Otherwise, return empty
2231
func FindExtendedAgent() (socketPath string, ok bool) {
2332
if p, ok := os.LookupEnv("SSH_AUTH_SOCK"); ok {
24-
return p, true
33+
if strings.Contains(p, "com.apple.launchd.") {
34+
socketPath = p
35+
} else {
36+
return p, true
37+
}
2538
}
2639

27-
home, err := os.UserHomeDir()
28-
if err != nil {
40+
if home, err := os.UserHomeDir(); err != nil {
2941
goto LAUNCHD
30-
}
31-
32-
for _, p := range knownAgentPaths {
33-
p = filepath.Join(home, p)
34-
if _, err := os.Stat(p); err == nil {
35-
return p, true
42+
} else {
43+
for _, p := range knownAgentPaths {
44+
p = filepath.Join(home, p)
45+
if _, err := os.Stat(p); err == nil {
46+
return p, true
47+
}
3648
}
3749
}
3850

3951
LAUNCHD:
4052
output, err := exec.Command("/bin/launchctl", "asuser", strconv.Itoa(os.Getuid()), "launchctl", "getenv", "SSH_AUTH_SOCK").CombinedOutput()
41-
if err != nil {
42-
return "", false
43-
}
44-
45-
out := string(bytes.TrimSpace(output))
46-
if _, err := os.Stat(out); err == nil {
47-
return out, true
53+
if err == nil {
54+
out := string(bytes.TrimSpace(output))
55+
if _, err := os.Stat(out); err == nil {
56+
return out, true
57+
}
4858
}
4959

50-
return "", false
60+
return socketPath, false
5161
}
5262

5363
func Start(sshAuthSocketPath string, log *logger.Context) (*sshagent.SSHAgent, error) {

0 commit comments

Comments
 (0)