Skip to content

Commit 2fc92c6

Browse files
authored
refactor(ssh): replace the SshConfig class with module functions (#1227)
SshConfig was built for a single operation each time, and its cached text duplicated what each operation could return. No behavior change. - Replace the class with readSshConfig, writeCoderSshConfig and includeCoderSshConfig. Only the Coder file accepts permissions. - Drop the injectable FileSystem; the tests mock node:fs/promises. - Check fs error codes with a hasErrorCode type guard. - Merge or remove tests that the stateless API made redundant, and fix the ManagedPermissions doc. Closes VSC-58
1 parent 0454f42 commit 2fc92c6

5 files changed

Lines changed: 431 additions & 462 deletions

File tree

‎src/remote/remote.ts‎

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,14 @@ import { migrateAuthToSecretsStorage } from "./migration";
6868
import {
6969
getGitForWindowsSshConfigPath,
7070
getSshHomeDir,
71-
SshConfig,
71+
includeCoderSshConfig,
7272
type SshValues,
7373
mergeSshConfigValues,
7474
parseCoderSshOptions,
7575
parseSshConfig,
76+
readSshConfig,
7677
validateDeploymentSshOptions,
78+
writeCoderSshConfig,
7779
} from "./sshConfig";
7880
import { getRemoteSshConfigFile, getRemoteSshPath } from "./sshExtension";
7981
import { applySettingOverrides, buildSshOverrides } from "./sshOverrides";
@@ -913,18 +915,12 @@ export class Remote {
913915
const { hostPrefix, safeHostname, sshHost } = parts;
914916
// -F skips /etc/ssh/ssh_config, so use it only when Remote-SSH does.
915917
const configFile = getRemoteSshConfigFile();
916-
// One file per (host prefix, deployment); the user's config gains one shared include.
917-
const sshConfig = new SshConfig(
918-
configFile ?? path.join(getSshHomeDir(), ".ssh", "config"),
919-
this.logger,
920-
);
921-
await sshConfig.load();
922-
// Never loaded: update() regenerates it without reading the old content.
923-
const coderConfig = new SshConfig(
924-
this.pathResolver.getSshConfigPath(safeHostname, hostEditorId(sshHost)),
925-
this.logger,
926-
undefined,
927-
createManagedPermissions(),
918+
// The user's config gains one shared include.
919+
const userConfigPath =
920+
configFile ?? path.join(getSshHomeDir(), ".ssh", "config");
921+
// The CLI writes its block to the user's config, so read it from there.
922+
const configSshOptions = parseCoderSshOptions(
923+
await readSshConfig(userConfigPath),
928924
);
929925

930926
// Options the user set themselves win the merge below, so they are exempt
@@ -934,8 +930,6 @@ export class Remote {
934930
.getConfiguration("coder")
935931
.get<string[]>("sshConfig", []);
936932
const userConfig = parseSshConfig(userConfigSsh);
937-
// The CLI writes its block to the user's config, so read it from there.
938-
const configSshOptions = parseCoderSshOptions(sshConfig.getRaw());
939933

940934
let deploymentSshConfig = {};
941935
try {
@@ -996,20 +990,32 @@ export class Remote {
996990
SendEnv: SESSION_TYPE_ENV,
997991
};
998992

999-
// Write our file before including it, so the include never dangles.
1000-
await coderConfig.update(sshValues, sshConfigOverrides);
1001-
const includeDir = this.pathResolver.getSshConfigDir();
1002-
await sshConfig.updateInclude(includeDir, safeHostname);
993+
// One file per (host prefix, deployment). Write it before including it,
994+
// so the include never dangles.
995+
const coderConfigText = await writeCoderSshConfig(
996+
{
997+
path: this.pathResolver.getSshConfigPath(
998+
safeHostname,
999+
hostEditorId(sshHost),
1000+
),
1001+
permissions: createManagedPermissions(),
1002+
},
1003+
{ values: sshValues, overrides: sshConfigOverrides },
1004+
this.logger,
1005+
);
1006+
const include = { dir: this.pathResolver.getSshConfigDir(), safeHostname };
1007+
const userConfigText = await includeCoderSshConfig(
1008+
userConfigPath,
1009+
include,
1010+
this.logger,
1011+
);
10031012
// Without -F, Git for Windows ssh reads its own home's config. When that
10041013
// is the profile's config, the include is already there: no write.
10051014
const gitConfigPath = getGitForWindowsSshConfigPath();
10061015
if (!configFile && gitConfigPath) {
10071016
try {
10081017
await raceWithAbort(
1009-
new SshConfig(gitConfigPath, this.logger).updateInclude(
1010-
includeDir,
1011-
safeHostname,
1012-
),
1018+
includeCoderSshConfig(gitConfigPath, include, this.logger),
10131019
AbortSignal.timeout(GIT_SSH_CONFIG_TIMEOUT_MS),
10141020
);
10151021
} catch (error) {
@@ -1028,12 +1034,12 @@ export class Remote {
10281034
);
10291035
} catch (error) {
10301036
if (error instanceof SshProxyCommandMissingError) {
1031-
this.warnCoderSshConfigNotRead(includeDir, error.configFiles);
1037+
this.warnCoderSshConfigNotRead(include.dir, error.configFiles);
10321038
}
10331039
this.logger.warn("ssh -G failed, parsing the SSH config instead", error);
10341040
return computeSshProperties(
10351041
parts.sshHost,
1036-
`${coderConfig.getRaw()}\n${sshConfig.getRaw()}`,
1042+
`${coderConfigText}\n${userConfigText}`,
10371043
);
10381044
}
10391045
}

0 commit comments

Comments
 (0)