(accountId, entryId, identityId, command)
| 6 | const controlPlane = require("../lib/controlPlane/ControlPlaneServer"); |
| 7 | |
| 8 | const execCommand = async (accountId, entryId, identityId, command) => { |
| 9 | const entry = await Entry.findByPk(entryId); |
| 10 | if (!entry) { |
| 11 | return { code: 404, message: "Entry not found" }; |
| 12 | } |
| 13 | |
| 14 | const accessResult = await validateEntryAccess(accountId, entry); |
| 15 | if (!accessResult.valid) { |
| 16 | return { code: 403, message: "Access denied" }; |
| 17 | } |
| 18 | |
| 19 | if (entry.config?.protocol !== "ssh") { |
| 20 | return { code: 400, message: "Command execution is only supported for SSH entries" }; |
| 21 | } |
| 22 | |
| 23 | if (!controlPlane.hasEngine()) { |
| 24 | return { code: 503, message: "No engine connected" }; |
| 25 | } |
| 26 | |
| 27 | const result = await resolveIdentity(entry, identityId, null, accountId); |
| 28 | const identity = result?.identity !== undefined ? result.identity : result; |
| 29 | |
| 30 | if (result.accessDenied) { |
| 31 | return { code: 403, message: "You don't have access to this identity" }; |
| 32 | } |
| 33 | |
| 34 | if (!identity || !identity.id) { |
| 35 | return { code: 400, message: "No identity available for this entry" }; |
| 36 | } |
| 37 | |
| 38 | const credentials = await getIdentityCredentials(identity.id); |
| 39 | const params = buildSSHParams(identity, credentials); |
| 40 | const host = entry.config?.ip; |
| 41 | const port = entry.config?.port || 22; |
| 42 | |
| 43 | if (!host) { |
| 44 | return { code: 400, message: "Missing host configuration" }; |
| 45 | } |
| 46 | |
| 47 | const jumpHosts = await resolveJumpHosts(entry); |
| 48 | const execResult = await controlPlane.execCommand(host, port, params, command, jumpHosts); |
| 49 | |
| 50 | return { |
| 51 | success: execResult.success, |
| 52 | stdout: execResult.stdout || "", |
| 53 | stderr: execResult.stderr || "", |
| 54 | exitCode: execResult.exitCode ?? (execResult.success ? 0 : 1), |
| 55 | errorMessage: execResult.errorMessage || null, |
| 56 | }; |
| 57 | }; |
| 58 | |
| 59 | module.exports = { execCommand }; |
no test coverage detected