@deinossrl/dgp-agent 1.4.20 → 1.4.22

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 +91 -19
  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.20';
265
+ const AGENT_VERSION = '1.4.22';
207
266
  let AGENT_MODE = 'smart'; // Siempre inteligente
208
267
 
209
268
  // Configuración (prioridad: env vars > archivo config > platform config > defaults)
@@ -1036,7 +1095,9 @@ async function executeDeploy(command) {
1036
1095
  const fs = await import('fs');
1037
1096
  const path = await import('path');
1038
1097
  sshKeyPath = path.join(os.tmpdir(), `dgp_deploy_key_${Date.now()}`);
1039
- fs.writeFileSync(sshKeyPath, sshPrivateKey, { mode: 0o600 });
1098
+ // Normalizar saltos de línea (pueden venir como \n literal del JSON)
1099
+ const normalizedKey = sshPrivateKey.replace(/\\n/g, '\n').replace(/\r\n/g, '\n');
1100
+ fs.writeFileSync(sshKeyPath, normalizedKey, { mode: 0o600 });
1040
1101
  sshOptions = `-e "ssh -i ${sshKeyPath} -o StrictHostKeyChecking=no"`;
1041
1102
  }
1042
1103
 
@@ -1258,10 +1319,32 @@ Analiza este error y sugiere soluciones.`;
1258
1319
  */
1259
1320
  async function executeTestConnection(command) {
1260
1321
  const { server_host, ssh_user, params } = command;
1261
- 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 sshKeySource = 'ninguna';
1326
+
1327
+ // Si viene SSH key en params, guardarla localmente para futuro uso
1328
+ if (sshPrivateKey) {
1329
+ const savedPath = saveSshKeyLocally(sshPrivateKey);
1330
+ if (savedPath) {
1331
+ logSuccess(`SSH Key guardada en ${savedPath}`);
1332
+ sshKeySource = 'plataforma (guardada localmente)';
1333
+ } else {
1334
+ sshKeySource = 'plataforma';
1335
+ }
1336
+ } else if (hasLocalSshKey()) {
1337
+ // Si no viene en params pero hay una guardada, usarla
1338
+ sshPrivateKey = loadLocalSshKey();
1339
+ sshKeySource = `local (${getDgpSshKeyPath()})`;
1340
+ logInfo(`Usando SSH Key guardada: ${getDgpSshKeyPath()}`);
1341
+ }
1262
1342
 
1263
1343
  logCommand(`=== Testing SSH Connection ===`);
1264
1344
  logInfo(`Target: ${ssh_user || 'root'}@${server_host}`);
1345
+ if (sshKeySource !== 'ninguna') {
1346
+ logInfo(`SSH Key: ${sshKeySource}`);
1347
+ }
1265
1348
 
1266
1349
  // Iniciar tracking de logs
1267
1350
  currentCommandId = command.id;
@@ -1304,16 +1387,11 @@ async function executeTestConnection(command) {
1304
1387
  // Step 2: SSH test
1305
1388
  logInfo('Step 2: Testing SSH connection...');
1306
1389
 
1307
- // Si hay SSH key, guardarla en archivo temporal
1390
+ // Usar la SSH key guardada en ~/.ssh/dgp_key (ya se guardó arriba si vino de params)
1308
1391
  let sshKeyPath = null;
1309
- if (sshPrivateKey) {
1310
- const os = await import('os');
1311
- const fs = await import('fs');
1312
- const path = await import('path');
1313
- sshKeyPath = path.join(os.tmpdir(), `dgp_test_key_${Date.now()}`);
1314
- fs.writeFileSync(sshKeyPath, sshPrivateKey, { mode: 0o600 });
1315
- logInfo('SSH Key: Usando clave desde plataforma');
1316
- await addCommandLog('info', 'Usando SSH key configurada en la plataforma');
1392
+ if (sshPrivateKey && hasLocalSshKey()) {
1393
+ sshKeyPath = getDgpSshKeyPath();
1394
+ await addCommandLog('info', `Usando SSH key: ${sshKeyPath}`);
1317
1395
  }
1318
1396
 
1319
1397
  const user = ssh_user || 'root';
@@ -1408,13 +1486,7 @@ async function executeTestConnection(command) {
1408
1486
  }
1409
1487
  }
1410
1488
 
1411
- // Limpiar SSH key temporal
1412
- if (sshKeyPath) {
1413
- try {
1414
- const fs = await import('fs');
1415
- fs.unlinkSync(sshKeyPath);
1416
- } catch (e) { /* ignorar */ }
1417
- }
1489
+ // Nota: No limpiamos la SSH key porque está guardada permanentemente en ~/.ssh/dgp_key
1418
1490
 
1419
1491
  // Determinar estado final
1420
1492
  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.20",
3
+ "version": "1.4.22",
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": {