Skip to content

Commit 01a4b51

Browse files
committed
Fix handling of legacy remote configurations
1 parent bee8acd commit 01a4b51

File tree

2 files changed

+47
-15
lines changed

2 files changed

+47
-15
lines changed

src/remote/remote.ts

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export class Remote {
6262
private readonly loginCoordinator: LoginCoordinator;
6363

6464
public constructor(
65-
private readonly serviceContainer: ServiceContainer,
65+
serviceContainer: ServiceContainer,
6666
private readonly commands: Commands,
6767
private readonly extensionContext: vscode.ExtensionContext,
6868
) {
@@ -93,8 +93,8 @@ export class Remote {
9393

9494
const workspaceName = `${parts.username}/${parts.workspace}`;
9595

96-
// Migrate "session_token" file to "session", if needed.
97-
await this.migrateSessionToken(parts.safeHostname);
96+
// Migrate existing legacy file-based auth to secrets storage.
97+
await this.migrateToSecretsStorage(parts.safeHostname);
9898

9999
// Get the URL and token belonging to this host.
100100
const auth = await this.secretsManager.getSessionAuth(parts.safeHostname);
@@ -412,10 +412,10 @@ export class Remote {
412412
// the user for the platform.
413413
let mungedPlatforms = false;
414414
if (
415-
!remotePlatforms[parts.host] ||
416-
remotePlatforms[parts.host] !== agent.operating_system
415+
!remotePlatforms[parts.sshHost] ||
416+
remotePlatforms[parts.sshHost] !== agent.operating_system
417417
) {
418-
remotePlatforms[parts.host] = agent.operating_system;
418+
remotePlatforms[parts.sshHost] = agent.operating_system;
419419
settingsContent = jsonc.applyEdits(
420420
settingsContent,
421421
jsonc.modify(
@@ -476,7 +476,7 @@ export class Remote {
476476
await this.updateSSHConfig(
477477
workspaceClient,
478478
parts.safeHostname,
479-
parts.host,
479+
parts.sshHost,
480480
binaryPath,
481481
logDir,
482482
featureSet,
@@ -488,7 +488,7 @@ export class Remote {
488488

489489
// Monitor SSH process and display network status
490490
const sshMonitor = SshProcessMonitor.start({
491-
sshHost: parts.host,
491+
sshHost: parts.sshHost,
492492
networkInfoPath: this.pathResolver.getNetworkInfoPath(),
493493
proxyLogDir: logDir || undefined,
494494
logger: this.logger,
@@ -551,19 +551,51 @@ export class Remote {
551551
}
552552

553553
/**
554-
* Migrate the session token file from "session_token" to "session", if needed.
554+
* Migrate legacy file-based auth to secrets storage.
555555
*/
556-
private async migrateSessionToken(safeHostname: string) {
556+
private async migrateToSecretsStorage(safeHostname: string) {
557+
await this.migrateSessionTokenFile(safeHostname);
558+
await this.migrateSessionAuthFromFiles(safeHostname);
559+
}
560+
561+
/**
562+
* Migrate the session token file from "session_token" to "session".
563+
*/
564+
private async migrateSessionTokenFile(safeHostname: string) {
557565
const oldTokenPath =
558566
this.pathResolver.getLegacySessionTokenPath(safeHostname);
559567
const newTokenPath = this.pathResolver.getSessionTokenPath(safeHostname);
560568
try {
561569
await fs.rename(oldTokenPath, newTokenPath);
562570
} catch (error) {
563-
if ((error as NodeJS.ErrnoException)?.code === "ENOENT") {
564-
return;
571+
if ((error as NodeJS.ErrnoException)?.code !== "ENOENT") {
572+
throw error;
565573
}
566-
throw error;
574+
}
575+
}
576+
577+
/**
578+
* Migrate URL and session token from files to the mutli-deployment secrets storage.
579+
*/
580+
private async migrateSessionAuthFromFiles(safeHostname: string) {
581+
const existingAuth = await this.secretsManager.getSessionAuth(safeHostname);
582+
if (existingAuth) {
583+
return;
584+
}
585+
586+
const urlPath = this.pathResolver.getUrlPath(safeHostname);
587+
const tokenPath = this.pathResolver.getSessionTokenPath(safeHostname);
588+
const [url, token] = await Promise.allSettled([
589+
fs.readFile(urlPath, "utf8"),
590+
fs.readFile(tokenPath, "utf8"),
591+
]);
592+
593+
if (url.status === "fulfilled" && token.status === "fulfilled") {
594+
this.logger.info("Migrating session auth from files for", safeHostname);
595+
await this.secretsManager.setSessionAuth(safeHostname, {
596+
url: url.value.trim(),
597+
token: token.value.trim(),
598+
});
567599
}
568600
}
569601

src/util.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import url from "node:url";
33

44
export interface AuthorityParts {
55
agent: string | undefined;
6-
host: string;
6+
sshHost: string;
77
safeHostname: string;
88
username: string;
99
workspace: string;
@@ -93,7 +93,7 @@ export function parseRemoteAuthority(authority: string): AuthorityParts | null {
9393

9494
return {
9595
agent: agent,
96-
host: authorityParts[1],
96+
sshHost: authorityParts[1],
9797
safeHostname: parts[0].replace(/^coder-vscode\.?/, ""),
9898
username: parts[1],
9999
workspace: workspace,

0 commit comments

Comments
 (0)