2025-10-26 15:23:56 +00:00
|
|
|
import { logger } from '../szci.logging.ts';
|
|
|
|
|
import * as plugins from './mod.plugins.ts';
|
2025-12-14 01:42:14 +00:00
|
|
|
|
2018-04-04 22:25:13 +02:00
|
|
|
let sshInstance: plugins.smartssh.SshInstance;
|
2016-06-24 02:54:55 +02:00
|
|
|
|
2025-12-14 01:42:14 +00:00
|
|
|
/**
|
|
|
|
|
* Interface for CLI arguments
|
|
|
|
|
*/
|
|
|
|
|
interface ICliArgs {
|
|
|
|
|
_: string[];
|
|
|
|
|
[key: string]: unknown;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handle SSH CLI commands
|
|
|
|
|
*/
|
|
|
|
|
export const handleCli = async (argvArg: ICliArgs): Promise<void> => {
|
2017-08-27 15:24:17 +02:00
|
|
|
if (argvArg._.length >= 2) {
|
2025-12-14 01:42:14 +00:00
|
|
|
const action = argvArg._[1];
|
2017-08-27 15:24:17 +02:00
|
|
|
switch (action) {
|
|
|
|
|
case 'prepare':
|
2018-04-04 22:25:13 +02:00
|
|
|
await prepare();
|
|
|
|
|
break;
|
2017-08-27 15:24:17 +02:00
|
|
|
default:
|
2018-11-24 15:00:19 +01:00
|
|
|
logger.log('error', `action >>${action}<< not supported`);
|
2025-10-26 15:23:56 +00:00
|
|
|
Deno.exit(1);
|
2017-08-27 15:24:17 +02:00
|
|
|
}
|
2017-08-28 13:25:22 +02:00
|
|
|
} else {
|
2025-12-13 13:27:51 +00:00
|
|
|
logger.log('error', `>>szci ssh ...<< please specify an action!`);
|
2025-10-26 15:23:56 +00:00
|
|
|
Deno.exit(1);
|
2017-08-27 15:24:17 +02:00
|
|
|
}
|
2018-04-04 22:25:13 +02:00
|
|
|
};
|
2017-08-27 15:24:17 +02:00
|
|
|
|
2017-05-18 20:40:09 +00:00
|
|
|
/**
|
2025-12-14 01:42:14 +00:00
|
|
|
* Checks if a string value is defined and not a placeholder
|
2017-05-18 20:40:09 +00:00
|
|
|
*/
|
2025-12-14 01:42:14 +00:00
|
|
|
const isValidValue = (value: string | undefined): boolean => {
|
|
|
|
|
return Boolean(value && value !== 'undefined' && value !== '##');
|
2018-04-04 22:25:13 +02:00
|
|
|
};
|
2017-05-18 20:40:09 +00:00
|
|
|
|
2016-09-04 13:42:22 +02:00
|
|
|
/**
|
2025-12-14 01:42:14 +00:00
|
|
|
* Checks for ENV vars in form of SZCI_SSHKEY_* and deploys any found ones
|
2016-09-04 13:42:22 +02:00
|
|
|
*/
|
2025-12-14 01:42:14 +00:00
|
|
|
export const prepare = async (): Promise<void> => {
|
|
|
|
|
sshInstance = new plugins.smartssh.SshInstance();
|
|
|
|
|
|
|
|
|
|
// Get all env vars and filter for SSH keys
|
|
|
|
|
const envVars = Deno.env.toObject();
|
|
|
|
|
const sshKeyEnvVars = Object.entries(envVars).filter(([key]) =>
|
|
|
|
|
key.startsWith('SZCI_SSHKEY_')
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Process each SSH key env var
|
|
|
|
|
for (const [key, value] of sshKeyEnvVars) {
|
|
|
|
|
logger.log('info', `Processing SSH key from ${key}`);
|
|
|
|
|
addSshKeyFromEnvVar(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Only write to disk if not in test mode
|
|
|
|
|
if (!Deno.env.get('SZCI_TEST')) {
|
|
|
|
|
try {
|
|
|
|
|
sshInstance.writeToDisk();
|
|
|
|
|
logger.log('ok', 'SSH keys written to disk');
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.log('error', `Failed to write SSH keys: ${(error as Error).message}`);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
2017-03-08 14:50:41 +01:00
|
|
|
} else {
|
2018-11-24 15:00:19 +01:00
|
|
|
logger.log('info', 'In test mode, so not storing SSH keys to disk!');
|
2017-06-15 15:46:08 +02:00
|
|
|
}
|
2018-04-04 22:25:13 +02:00
|
|
|
};
|
2016-06-23 22:22:03 +02:00
|
|
|
|
2016-09-04 13:42:22 +02:00
|
|
|
/**
|
2025-12-14 01:42:14 +00:00
|
|
|
* Parses an SSH key env var and adds it to the SSH instance
|
|
|
|
|
* Format: host|privKeyBase64|pubKeyBase64
|
2016-09-04 13:42:22 +02:00
|
|
|
*/
|
2025-12-14 01:42:14 +00:00
|
|
|
const addSshKeyFromEnvVar = (sshkeyEnvVarArg: string): void => {
|
|
|
|
|
const [host, privKeyBase64, pubKeyBase64] = sshkeyEnvVarArg.split('|');
|
2018-11-24 15:00:19 +01:00
|
|
|
const sshKey = new plugins.smartssh.SshKey();
|
2025-12-14 01:42:14 +00:00
|
|
|
|
|
|
|
|
logger.log('info', `Found SSH identity for ${host || 'unknown host'}`);
|
|
|
|
|
|
|
|
|
|
if (isValidValue(host)) {
|
2018-11-24 15:00:19 +01:00
|
|
|
logger.log('info', '---> host defined!');
|
2025-12-14 01:42:14 +00:00
|
|
|
sshKey.host = host;
|
2017-03-08 14:50:41 +01:00
|
|
|
}
|
2025-12-14 01:42:14 +00:00
|
|
|
if (isValidValue(privKeyBase64)) {
|
2018-11-24 15:00:19 +01:00
|
|
|
logger.log('info', '---> privKey defined!');
|
2025-12-14 01:42:14 +00:00
|
|
|
sshKey.privKeyBase64 = privKeyBase64;
|
2017-06-15 15:46:08 +02:00
|
|
|
}
|
2025-12-14 01:42:14 +00:00
|
|
|
if (isValidValue(pubKeyBase64)) {
|
2018-11-24 15:00:19 +01:00
|
|
|
logger.log('info', '---> pubKey defined!');
|
2025-12-14 01:42:14 +00:00
|
|
|
sshKey.pubKeyBase64 = pubKeyBase64;
|
2017-06-15 15:46:08 +02:00
|
|
|
}
|
2016-11-24 23:21:40 +01:00
|
|
|
|
2018-04-04 22:25:13 +02:00
|
|
|
sshInstance.addKey(sshKey);
|
|
|
|
|
};
|