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

Function uploadGPGKeys

cli/ssh.go:1257–1314  ·  view source on GitHub ↗
(ctx context.Context, sshClient *gossh.Client)

Source from the content-addressed store, hash-verified

1255}
1256
1257func 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 '
1266set -eux
1267agent_socket=$(gpgconf --list-dir agent-socket)
1268echo "$agent_socket"
1269if [ -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
1274fi
1275
1276test ! -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}

Callers 1

sshMethod · 0.85

Calls 3

runRemoteSSHFunction · 0.85
runLocalFunction · 0.85
ErrorfMethod · 0.45

Tested by

no test coverage detected