1+ import { type Logger } from "../logging/logger";
12import { toSafeHost } from "../util";
23
34import type { Memento, SecretStorage, Disposable } from "vscode";
45
56import type { Deployment } from "../deployment";
67
8+ // Each deployment has its own key to ensure atomic operations (multiple windows
9+ // writing to a shared key could drop data) and to receive proper VS Code events.
710const SESSION_KEY_PREFIX = "coder.session.";
811
912const CURRENT_DEPLOYMENT_KEY = "coder.currentDeployment";
@@ -31,6 +34,7 @@ export class SecretsManager {
3134 constructor(
3235 private readonly secrets: SecretStorage,
3336 private readonly memento: Memento,
37+ private readonly logger: Logger,
3438 ) {}
3539
3640 /**
@@ -50,16 +54,16 @@ export class SecretsManager {
5054 /**
5155 * Gets the current deployment from storage.
5256 */
53- public async getCurrentDeployment(): Promise<Deployment | undefined > {
57+ public async getCurrentDeployment(): Promise<Deployment | null > {
5458 try {
5559 const data = await this.secrets.get(CURRENT_DEPLOYMENT_KEY);
5660 if (!data) {
57- return undefined ;
61+ return null ;
5862 }
59- const parsed = JSON.parse(data) as { deployment: Deployment | null } ;
60- return parsed.deployment ?? undefined ;
63+ const parsed = JSON.parse(data) as CurrentDeploymentState ;
64+ return parsed.deployment;
6165 } catch {
62- return undefined ;
66+ return null ;
6367 }
6468 }
6569
@@ -75,16 +79,14 @@ export class SecretsManager {
7579 return;
7680 }
7781
82+ const deployment = await this.getCurrentDeployment();
7883 try {
79- const data = await this.secrets.get(CURRENT_DEPLOYMENT_KEY);
80- if (data) {
81- const parsed = JSON.parse(data) as {
82- deployment: Deployment | null;
83- };
84- await listener({ deployment: parsed.deployment });
85- }
86- } catch {
87- // Ignore parse errors
84+ await listener({ deployment });
85+ } catch (err) {
86+ this.logger.error(
87+ "Error in onDidChangeCurrentDeployment listener",
88+ err,
89+ );
8890 }
8991 });
9092 }
@@ -102,7 +104,11 @@ export class SecretsManager {
102104 return;
103105 }
104106 const auth = await this.getSessionAuth(label);
105- await listener(auth);
107+ try {
108+ await listener(auth);
109+ } catch (err) {
110+ this.logger.error("Error in onDidChangeSessionAuth listener", err);
111+ }
106112 });
107113 }
108114
@@ -122,16 +128,6 @@ export class SecretsManager {
122128 }
123129 }
124130
125- public async getSessionToken(label: string): Promise<string | undefined> {
126- const auth = await this.getSessionAuth(label);
127- return auth?.token;
128- }
129-
130- public async getUrl(label: string): Promise<string | undefined> {
131- const auth = await this.getSessionAuth(label);
132- return auth?.url;
133- }
134-
135131 public async setSessionAuth(label: string, auth: SessionAuth): Promise<void> {
136132 await this.secrets.store(
137133 `${SESSION_KEY_PREFIX}${label}`,
@@ -210,6 +206,7 @@ export class SecretsManager {
210206
211207 await this.setSessionAuth(label, { url: legacyUrl, token: oldToken });
212208 await this.secrets.delete(LEGACY_SESSION_TOKEN_KEY);
209+ await this.memento.update("url", undefined);
213210
214211 // Also set as current deployment if none exists
215212 const currentDeployment = await this.getCurrentDeployment();
0 commit comments