iop
Version:
Ship Docker Anywhere
137 lines • 5.57 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSSHCredentials = getSSHCredentials;
const fs = __importStar(require("fs"));
const os = __importStar(require("os"));
/**
* Get SSH credentials for connecting to a server.
* This function handles credentials consistently across all commands.
*/
async function getSSHCredentials(serverHostname, config, secrets, verbose = false) {
const sshUser = config.ssh?.username || "root"; // Default to root, though setup warns against it
const sshPort = config.ssh?.port || 22;
const sshOptions = {
username: sshUser,
host: serverHostname,
port: sshPort,
verbose: verbose,
};
// Check for server-specific key path in secrets
const serverSpecificKeyEnvVar = `SSH_KEY_${serverHostname
.replace(/\./g, "_")
.toUpperCase()}`;
const serverSpecificKeyPath = secrets[serverSpecificKeyEnvVar];
if (serverSpecificKeyPath) {
if (verbose) {
console.log(`[${serverHostname}] Attempting SSH with server-specific key from secrets (${serverSpecificKeyEnvVar}): ${serverSpecificKeyPath}`);
}
sshOptions.identity = serverSpecificKeyPath;
return sshOptions;
}
// Check for key_file in config
const configKeyFile = config.ssh?.key_file;
if (configKeyFile) {
// Expand ~ to home directory
const expandedPath = configKeyFile.replace(/^~/, os.homedir());
if (fs.existsSync(expandedPath)) {
if (verbose) {
console.log(`[${serverHostname}] Attempting SSH with key file from config: ${expandedPath}`);
}
sshOptions.identity = expandedPath;
return sshOptions;
}
else if (verbose) {
console.log(`[${serverHostname}] Config key_file ${expandedPath} does not exist, skipping...`);
}
}
// Check for server-specific password in secrets
const serverSpecificPasswordEnvVar = `SSH_PASSWORD_${serverHostname
.replace(/\./g, "_")
.toUpperCase()}`;
const serverSpecificPassword = secrets[serverSpecificPasswordEnvVar];
if (serverSpecificPassword) {
if (verbose) {
console.log(`[${serverHostname}] Attempting SSH with server-specific password from secrets (${serverSpecificPasswordEnvVar}).`);
}
sshOptions.password = serverSpecificPassword;
return sshOptions;
}
// Check for default password in secrets
const defaultPassword = secrets.DEFAULT_SSH_PASSWORD;
if (defaultPassword) {
if (verbose) {
console.log(`[${serverHostname}] Attempting SSH with default password from secrets (DEFAULT_SSH_PASSWORD).`);
}
sshOptions.password = defaultPassword;
return sshOptions;
}
// Try to find common SSH key files in the user's home directory
const homeDir = os.homedir();
if (verbose) {
console.log(`[${serverHostname}] Home directory resolved as: ${homeDir}`);
}
// Check for common SSH key files
try {
const keyPaths = [
`${homeDir}/.ssh/id_rsa`,
`${homeDir}/.ssh/id_ed25519`,
`${homeDir}/.ssh/id_ecdsa`,
`${homeDir}/.ssh/id_dsa`,
];
for (const keyPath of keyPaths) {
if (fs.existsSync(keyPath)) {
sshOptions.identity = keyPath;
if (verbose) {
console.log(`[${serverHostname}] Explicitly using SSH key at: ${keyPath}`);
}
break; // Stop after finding the first existing key
}
}
if (!sshOptions.identity && verbose) {
console.log(`[${serverHostname}] No SSH keys found at standard locations: ${keyPaths.join(", ")}`);
}
}
catch (error) {
if (verbose) {
console.error(`[${serverHostname}] Error checking for default SSH keys:`, error);
}
}
if (verbose) {
console.log(`[${serverHostname}] No specific SSH key or password found in iop secrets. Attempting agent-based authentication or found key file.`);
}
return sshOptions;
}
//# sourceMappingURL=utils.js.map