Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function parseSshConfig(sshConfigFileContents) {
// Previously we were using a library that parsed ssh config files into arrays.
// Mimic that behavior here so the rest of the code doesn't need to change.
return objectToArray(sshConfig.parse(sshConfigFileContents));
}
const loadFromLocal = () => {
const managedConfig = readManagedLocalConfig()
const config = sshConfig.parse(managedConfig)
// Return an empty array in case the local file is empty.
if (!0 in config) return []
// Extract items.
const items = Object.keys(config)
.filter((key) => {
const index = Number(key)
return Number.isInteger(index)
})
.map((key) => config[key])
.map((item) => convertToAppFormat(item))
return items
}
it('should stringify with config5 file', async () => {
const config5 = await readFile(path.resolve(__dirname, 'fixtures/ssh-config/config5'), { encoding: 'utf8' });
const conf = SSHConfig.parse(config5);
ensureHostAndKeyPath(conf, { host: 'bar' }, '/id_rsa');
expect(SSHConfig.stringify(conf)).toEqual(config5);
});
export function resolverFromConfig(text: string): ConfigResolver {
const config = parseConfig(text);
return h => config.compute(h.Host);
}
ensureSection(conf: SSHConfig.SSHConfig, host: string, newline: boolean): SSHConfig.ConfigDirective {
const section = conf.find({ Host: host });
if (!section) {
conf.push(SSHConfig.parse(`${newline ? '\n' : ''}Host ${host}\n`)[0]);
}
return conf.find({ Host: host });
}
async applyConfig(text: string, keyPath: string): Promise {
const c = await this.config.load();
const conf = SSHConfig.parse(text);
const host = c.git.host;
const section = this.ensureSection(conf, host, Boolean(text));
this.ensureSectionLine(section, 'IdentityFile', keyPath);
if (typeof c.git.port === 'number') {
this.ensureSectionLine(section, 'Port', String(c.git.port));
}
return SSHConfig.stringify(conf);
}
export async function loadFromPath(p: string): Promise {
const s = await fileToString(p);
return SSHConfig.parse(s);
}
function ensureHostSection(conf: SSHConfig.SSHConfig, host: string): SSHConfig.ConfigHostDirective {
let section = findHostSection(conf, host);
if (!section) {
conf.push(SSHConfig.parse(`\nHost ${host}\n`)[0]);
section = findHostSection(conf, host);
}
if (!section) {
throw new Error(`Could not find/insert section for host: ${host}`);
}
return section;
}
fs.readFile(process.env.HOME + '/.ssh/config', function (err, data) {
if (err) {
throw err;
}
sshConfig = sshConfig.parse(data.toString());
var output = {
Profiles: [],
};
for (var i = 0, len = sshConfig.length; i < len; i++) {
var section = sshConfig[i];
var host = section.Host;
var comment = null;
var commentIndex = section.Host.indexOf('#');
if (commentIndex !== -1) {
host = section.Host.substr(0, commentIndex);
comment = section.Host.substr(commentIndex + 1).trim();
function ensureSectionLine(section: SSHConfig.ConfigHostDirective, key: string, value: string): void {
const found = section.config.some(line => {
if (isDirective(line)) {
if (line.param === key) {
line.value = value;
return true;
}
}
return false;
});
if (!found) {
section.config = section.config.concat(SSHConfig.parse(`${key} ${value}\n`));
}
}