(ctx context.Context, stderr io.Writer, sshClient *gossh.Client)
| 34 | } |
| 35 | |
| 36 | func 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{ |
nothing calls this directly
no test coverage detected