This commit is contained in:
2025-12-14 01:42:14 +00:00
parent 9ad5222b95
commit 5d18e53e30
10 changed files with 310 additions and 9863 deletions

View File

@@ -1,10 +1,22 @@
import { logger } from '../szci.logging.ts';
import * as plugins from './mod.plugins.ts';
let sshInstance: plugins.smartssh.SshInstance;
export let handleCli = async (argvArg: any) => {
/**
* Interface for CLI arguments
*/
interface ICliArgs {
_: string[];
[key: string]: unknown;
}
/**
* Handle SSH CLI commands
*/
export const handleCli = async (argvArg: ICliArgs): Promise<void> => {
if (argvArg._.length >= 2) {
const action: string = argvArg._[1];
const action = argvArg._[1];
switch (action) {
case 'prepare':
await prepare();
@@ -20,45 +32,66 @@ export let handleCli = async (argvArg: any) => {
};
/**
* checks if not undefined
* Checks if a string value is defined and not a placeholder
*/
const notUndefined = (stringArg: string) => {
return stringArg && stringArg !== 'undefined' && stringArg !== '##';
const isValidValue = (value: string | undefined): boolean => {
return Boolean(value && value !== 'undefined' && value !== '##');
};
/**
* checks for ENV vars in form of SZCI_SSHKEY_* and deploys any found ones
* Checks for ENV vars in form of SZCI_SSHKEY_* and deploys any found ones
*/
export let prepare = async () => {
sshInstance = new plugins.smartssh.SshInstance(); // init ssh instance
plugins.smartobject.forEachMinimatch(Deno.env.toObject(), 'SZCI_SSHKEY_*', evaluateSshEnv);
if (!Deno.env.get("SZCI_TEST")) {
sshInstance.writeToDisk();
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;
}
} else {
logger.log('info', 'In test mode, so not storing SSH keys to disk!');
}
};
/**
* gets called for each found SSH ENV Var and deploys it
* Parses an SSH key env var and adds it to the SSH instance
* Format: host|privKeyBase64|pubKeyBase64
*/
const evaluateSshEnv = async (sshkeyEnvVarArg: string) => {
const sshEnvArray = sshkeyEnvVarArg.split('|');
const addSshKeyFromEnvVar = (sshkeyEnvVarArg: string): void => {
const [host, privKeyBase64, pubKeyBase64] = sshkeyEnvVarArg.split('|');
const sshKey = new plugins.smartssh.SshKey();
logger.log('info', 'Found SSH identity for ' + sshEnvArray[1]);
if (notUndefined(sshEnvArray[0])) {
logger.log('info', `Found SSH identity for ${host || 'unknown host'}`);
if (isValidValue(host)) {
logger.log('info', '---> host defined!');
sshKey.host = sshEnvArray[0];
sshKey.host = host;
}
if (notUndefined(sshEnvArray[1])) {
if (isValidValue(privKeyBase64)) {
logger.log('info', '---> privKey defined!');
sshKey.privKeyBase64 = sshEnvArray[1];
sshKey.privKeyBase64 = privKeyBase64;
}
if (notUndefined(sshEnvArray[2])) {
if (isValidValue(pubKeyBase64)) {
logger.log('info', '---> pubKey defined!');
sshKey.pubKeyBase64 = sshEnvArray[2];
sshKey.pubKeyBase64 = pubKeyBase64;
}
sshInstance.addKey(sshKey);
return;
};