MCPcopy Index your code
hub / github.com/coder/coder / forwardGPGAgent

Function forwardGPGAgent

cli/ssh_windows.go:36–105  ·  view source on GitHub ↗
(ctx context.Context, stderr io.Writer, sshClient *gossh.Client)

Source from the content-addressed store, hash-verified

34}
35
36func forwardGPGAgent(ctx context.Context, stderr io.Writer, sshClient *gossh.Client) (io.Closer, error) {
37 // Read TCP port and cookie from extra socket file. A gpg-agent socket
38 // file looks like the following:
39 //
40 // 49955
41 // abcdefghijklmnop
42 //
43 // The first line is the TCP port that gpg-agent is listening on, and
44 // the second line is a 16 byte cookie that MUST be sent as the first
45 // bytes of any connection to this port (otherwise the connection is
46 // closed by gpg-agent).
47 localSocket, err := localGPGExtraSocket(ctx)
48 if err != nil {
49 return nil, err
50 }
51 f, err := os.Open(localSocket)
52 if err != nil {
53 return nil, xerrors.Errorf("open gpg-agent-extra socket file %q: %w", localSocket, err)
54 }
55
56 // Scan lines from file to get port and cookie.
57 var (
58 port uint16
59 cookie []byte
60 scanner = bufio.NewScanner(f)
61 )
62 for i := 0; scanner.Scan(); i++ {
63 switch i {
64 case 0:
65 port64, err := strconv.ParseUint(scanner.Text(), 10, 16)
66 if err != nil {
67 return nil, xerrors.Errorf("parse gpg-agent-extra socket file %q: line 1: convert string to integer: %w", localSocket, err)
68 }
69 port = uint16(port64)
70
71 case 1:
72 cookie = scanner.Bytes()
73 if len(cookie) != 16 {
74 return nil, xerrors.Errorf("parse gpg-agent-extra socket file %q: line 2: expected 16 bytes, got %v bytes", localSocket, len(cookie))
75 }
76
77 default:
78 return nil, xerrors.Errorf("parse gpg-agent-extra socket file %q: file contains more than 2 lines", localSocket)
79 }
80 }
81
82 err = scanner.Err()
83 if err != nil {
84 return nil, xerrors.Errorf("parse gpg-agent-extra socket file: %q: %w", localSocket, err)
85 }
86
87 remoteSocket, err := remoteGPGAgentSocket(sshClient)
88 if err != nil {
89 return nil, err
90 }
91
92 localAddr := cookieAddr{
93 Addr: &net.TCPAddr{

Callers

nothing calls this directly

Calls 9

localGPGExtraSocketFunction · 0.85
remoteGPGAgentSocketFunction · 0.85
sshRemoteForwardFunction · 0.85
ErrMethod · 0.80
OpenMethod · 0.45
ErrorfMethod · 0.45
ScanMethod · 0.45
TextMethod · 0.45
BytesMethod · 0.45

Tested by

no test coverage detected