(ctx context.Context, sshClient *gossh.Client)
| 1255 | } |
| 1256 | |
| 1257 | func uploadGPGKeys(ctx context.Context, sshClient *gossh.Client) error { |
| 1258 | // Check if the agent is running in the workspace already. |
| 1259 | // |
| 1260 | // Note: we don't support windows in the workspace for GPG forwarding so |
| 1261 | // using shell commands is fine. |
| 1262 | // |
| 1263 | // Note: we sleep after killing the agent because it doesn't always die |
| 1264 | // immediately. |
| 1265 | agentSocketBytes, err := runRemoteSSH(sshClient, nil, `sh -c ' |
| 1266 | set -eux |
| 1267 | agent_socket=$(gpgconf --list-dir agent-socket) |
| 1268 | echo "$agent_socket" |
| 1269 | if [ -S "$agent_socket" ]; then |
| 1270 | echo "agent socket exists, attempting to kill it" >&2 |
| 1271 | gpgconf --kill gpg-agent |
| 1272 | rm -f "$agent_socket" |
| 1273 | sleep 1 |
| 1274 | fi |
| 1275 | |
| 1276 | test ! -S "$agent_socket" |
| 1277 | '`) |
| 1278 | agentSocket := strings.TrimSpace(string(agentSocketBytes)) |
| 1279 | if err != nil { |
| 1280 | return xerrors.Errorf("check if agent socket is running (check if %q exists): %w", agentSocket, err) |
| 1281 | } |
| 1282 | if agentSocket == "" { |
| 1283 | return xerrors.Errorf("agent socket path is empty, check the output of `gpgconf --list-dir agent-socket`") |
| 1284 | } |
| 1285 | |
| 1286 | // Read the user's public keys and ownertrust from GPG. |
| 1287 | pubKeyExport, err := runLocal(ctx, nil, "gpg", "--armor", "--export") |
| 1288 | if err != nil { |
| 1289 | return xerrors.Errorf("export local public keys from GPG: %w", err) |
| 1290 | } |
| 1291 | ownerTrustExport, err := runLocal(ctx, nil, "gpg", "--export-ownertrust") |
| 1292 | if err != nil { |
| 1293 | return xerrors.Errorf("export local ownertrust from GPG: %w", err) |
| 1294 | } |
| 1295 | |
| 1296 | // Import the public keys and ownertrust into the workspace. |
| 1297 | _, err = runRemoteSSH(sshClient, bytes.NewReader(pubKeyExport), "gpg --import") |
| 1298 | if err != nil { |
| 1299 | return xerrors.Errorf("import public keys into workspace: %w", err) |
| 1300 | } |
| 1301 | _, err = runRemoteSSH(sshClient, bytes.NewReader(ownerTrustExport), "gpg --import-ownertrust") |
| 1302 | if err != nil { |
| 1303 | return xerrors.Errorf("import ownertrust into workspace: %w", err) |
| 1304 | } |
| 1305 | |
| 1306 | // Kill the agent in the workspace if it was started by one of the above |
| 1307 | // commands. |
| 1308 | _, err = runRemoteSSH(sshClient, nil, fmt.Sprintf("gpgconf --kill gpg-agent && rm -f %q", agentSocket)) |
| 1309 | if err != nil { |
| 1310 | return xerrors.Errorf("kill existing agent in workspace: %w", err) |
| 1311 | } |
| 1312 | |
| 1313 | return nil |
| 1314 | } |
no test coverage detected