@deinossrl/dgp-agent 1.4.21 → 1.4.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.mjs +113 -26
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -202,8 +202,67 @@ async function fetchPlatformConfig() {
202
202
  // Variable para config de la plataforma (se carga async)
203
203
  let platformConfig = null;
204
204
 
205
+ /**
206
+ * Obtiene la ruta del archivo de SSH key del agente
207
+ */
208
+ function getDgpSshKeyPath() {
209
+ const os = require('os');
210
+ const path = require('path');
211
+ return path.join(os.homedir(), '.ssh', 'dgp_key');
212
+ }
213
+
214
+ /**
215
+ * Guarda una SSH key en ~/.ssh/dgp_key
216
+ */
217
+ function saveSshKeyLocally(sshPrivateKey) {
218
+ try {
219
+ const fs = require('fs');
220
+ const os = require('os');
221
+ const path = require('path');
222
+
223
+ const sshDir = path.join(os.homedir(), '.ssh');
224
+ if (!fs.existsSync(sshDir)) {
225
+ fs.mkdirSync(sshDir, { recursive: true, mode: 0o700 });
226
+ }
227
+
228
+ const keyPath = getDgpSshKeyPath();
229
+ // Normalizar saltos de línea
230
+ const normalizedKey = sshPrivateKey.replace(/\\n/g, '\n').replace(/\r\n/g, '\n');
231
+ fs.writeFileSync(keyPath, normalizedKey, { mode: 0o600 });
232
+
233
+ return keyPath;
234
+ } catch (e) {
235
+ console.error('Error guardando SSH key:', e.message);
236
+ return null;
237
+ }
238
+ }
239
+
240
+ /**
241
+ * Carga la SSH key guardada localmente (si existe)
242
+ */
243
+ function loadLocalSshKey() {
244
+ try {
245
+ const fs = require('fs');
246
+ const keyPath = getDgpSshKeyPath();
247
+ if (fs.existsSync(keyPath)) {
248
+ return fs.readFileSync(keyPath, 'utf8');
249
+ }
250
+ return null;
251
+ } catch (e) {
252
+ return null;
253
+ }
254
+ }
255
+
256
+ /**
257
+ * Verifica si hay una SSH key guardada localmente
258
+ */
259
+ function hasLocalSshKey() {
260
+ const fs = require('fs');
261
+ return fs.existsSync(getDgpSshKeyPath());
262
+ }
263
+
205
264
  // Versión del agente
206
- const AGENT_VERSION = '1.4.21';
265
+ const AGENT_VERSION = '1.4.23';
207
266
  let AGENT_MODE = 'smart'; // Siempre inteligente
208
267
 
209
268
  // Configuración (prioridad: env vars > archivo config > platform config > defaults)
@@ -1260,10 +1319,34 @@ Analiza este error y sugiere soluciones.`;
1260
1319
  */
1261
1320
  async function executeTestConnection(command) {
1262
1321
  const { server_host, ssh_user, params } = command;
1263
- const sshPrivateKey = params?.ssh_private_key || null;
1322
+
1323
+ // SSH Key: prioridad params > guardada localmente
1324
+ let sshPrivateKey = params?.ssh_private_key || null;
1325
+ let sshPassword = params?.ssh_password || null;
1326
+ let authMethod = 'ninguno';
1327
+
1328
+ // Si viene SSH key en params, guardarla localmente para futuro uso
1329
+ if (sshPrivateKey) {
1330
+ const savedPath = saveSshKeyLocally(sshPrivateKey);
1331
+ if (savedPath) {
1332
+ logSuccess(`SSH Key guardada en ${savedPath}`);
1333
+ authMethod = 'key (guardada localmente)';
1334
+ } else {
1335
+ authMethod = 'key (plataforma)';
1336
+ }
1337
+ } else if (hasLocalSshKey()) {
1338
+ // Si no viene en params pero hay una guardada, usarla
1339
+ sshPrivateKey = loadLocalSshKey();
1340
+ authMethod = `key (${getDgpSshKeyPath()})`;
1341
+ logInfo(`Usando SSH Key guardada: ${getDgpSshKeyPath()}`);
1342
+ } else if (sshPassword) {
1343
+ authMethod = 'password';
1344
+ logInfo('Usando autenticación por password');
1345
+ }
1264
1346
 
1265
1347
  logCommand(`=== Testing SSH Connection ===`);
1266
1348
  logInfo(`Target: ${ssh_user || 'root'}@${server_host}`);
1349
+ logInfo(`Auth: ${authMethod}`);
1267
1350
 
1268
1351
  // Iniciar tracking de logs
1269
1352
  currentCommandId = command.id;
@@ -1306,26 +1389,36 @@ async function executeTestConnection(command) {
1306
1389
  // Step 2: SSH test
1307
1390
  logInfo('Step 2: Testing SSH connection...');
1308
1391
 
1309
- // Si hay SSH key, guardarla en archivo temporal
1392
+ const user = ssh_user || 'root';
1393
+ let sshCmd = '';
1394
+
1395
+ // Usar la SSH key guardada en ~/.ssh/dgp_key (ya se guardó arriba si vino de params)
1310
1396
  let sshKeyPath = null;
1311
- if (sshPrivateKey) {
1312
- const os = await import('os');
1313
- const fs = await import('fs');
1314
- const path = await import('path');
1315
- sshKeyPath = path.join(os.tmpdir(), `dgp_test_key_${Date.now()}`);
1316
- // Normalizar saltos de línea (pueden venir como \n literal del JSON)
1317
- const normalizedKey = sshPrivateKey.replace(/\\n/g, '\n').replace(/\r\n/g, '\n');
1318
- fs.writeFileSync(sshKeyPath, normalizedKey, { mode: 0o600 });
1319
- logInfo('SSH Key: Usando clave desde plataforma');
1320
- await addCommandLog('info', 'Usando SSH key configurada en la plataforma');
1397
+ if (sshPrivateKey && hasLocalSshKey()) {
1398
+ sshKeyPath = getDgpSshKeyPath();
1399
+ await addCommandLog('info', `Usando SSH key: ${sshKeyPath}`);
1400
+ const sshOpts = `-i ${sshKeyPath} -o StrictHostKeyChecking=no -o ConnectTimeout=10 -o BatchMode=yes`;
1401
+ sshCmd = `ssh ${sshOpts} ${user}@${server_host} "echo 'SSH_OK' && hostname && whoami"`;
1402
+ } else if (sshPassword) {
1403
+ // Usar sshpass para autenticación por password
1404
+ await addCommandLog('info', 'Usando autenticación por password (sshpass)');
1405
+ const isWindows = process.platform === 'win32';
1406
+ if (isWindows) {
1407
+ // En Windows usar plink (PuTTY) o mostrar advertencia
1408
+ await addCommandLog('warning', 'SSH con password en Windows requiere plink (PuTTY)');
1409
+ // Intentar con plink si está disponible
1410
+ sshCmd = `echo y | plink -ssh -pw "${sshPassword}" ${user}@${server_host} "echo SSH_OK && hostname && whoami"`;
1411
+ } else {
1412
+ // En Linux/Mac usar sshpass
1413
+ sshCmd = `sshpass -p "${sshPassword}" ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 ${user}@${server_host} "echo 'SSH_OK' && hostname && whoami"`;
1414
+ }
1415
+ } else {
1416
+ // Sin key ni password, intentar con key del sistema
1417
+ await addCommandLog('info', 'Intentando conexión con keys del sistema (~/.ssh/)');
1418
+ const sshOpts = `-o StrictHostKeyChecking=no -o ConnectTimeout=10 -o BatchMode=yes`;
1419
+ sshCmd = `ssh ${sshOpts} ${user}@${server_host} "echo 'SSH_OK' && hostname && whoami"`;
1321
1420
  }
1322
1421
 
1323
- const user = ssh_user || 'root';
1324
- const sshOpts = sshKeyPath
1325
- ? `-i ${sshKeyPath} -o StrictHostKeyChecking=no -o ConnectTimeout=10 -o BatchMode=yes`
1326
- : `-o StrictHostKeyChecking=no -o ConnectTimeout=10 -o BatchMode=yes`;
1327
-
1328
- const sshCmd = `ssh ${sshOpts} ${user}@${server_host} "echo 'SSH_OK' && hostname && whoami"`;
1329
1422
  await addCommandLog('command', `ssh ${user}@${server_host} "hostname && whoami"`);
1330
1423
 
1331
1424
  try {
@@ -1412,13 +1505,7 @@ async function executeTestConnection(command) {
1412
1505
  }
1413
1506
  }
1414
1507
 
1415
- // Limpiar SSH key temporal
1416
- if (sshKeyPath) {
1417
- try {
1418
- const fs = await import('fs');
1419
- fs.unlinkSync(sshKeyPath);
1420
- } catch (e) { /* ignorar */ }
1421
- }
1508
+ // Nota: No limpiamos la SSH key porque está guardada permanentemente en ~/.ssh/dgp_key
1422
1509
 
1423
1510
  // Determinar estado final
1424
1511
  const overallSuccess = results.ssh.success;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deinossrl/dgp-agent",
3
- "version": "1.4.21",
3
+ "version": "1.4.23",
4
4
  "description": "Agente local para Despliegue-GPT - Reporta el estado del repositorio Git a la plataforma TenMinute IA",
5
5
  "main": "index.mjs",
6
6
  "bin": {