Consolidate saved environment secret storage
- Store encrypted bearer tokens alongside the saved-environment registry - Preserve secrets when rewriting registry metadata - Update desktop IPC paths and coverage
This commit is contained in:
1 parent
f51a46b46b
commit
b5ac0b3e2d
3 files changed
+122
-42
No files matched your search
@@ -84,12 +84,12 @@ describe("clientPersistence", () => {
|
||||
});
|
||||
|
||||
it("persists encrypted saved environment secrets when encryption is available", () => {
|
||||
const secretsPath = makeTempPath("saved-environment-secrets.json");
|
||||
const registryPath = makeTempPath("saved-environments.json");
|
||||
const secretStorage = makeSecretStorage(true);
|
||||
|
||||
expect(
|
||||
writeSavedEnvironmentSecret({
|
||||
secretsPath,
|
||||
registryPath,
|
||||
environmentId: savedRegistryRecord.environmentId,
|
||||
secret: "bearer-token",
|
||||
secretStorage,
|
||||
@@ -98,7 +98,7 @@ describe("clientPersistence", () => {
|
||||
|
||||
expect(
|
||||
readSavedEnvironmentSecret({
|
||||
secretsPath,
|
||||
registryPath,
|
||||
environmentId: savedRegistryRecord.environmentId,
|
||||
secretStorage,
|
||||
}),
|
||||
@@ -106,11 +106,11 @@ describe("clientPersistence", () => {
|
||||
});
|
||||
|
||||
it("preserves existing secrets when encryption is unavailable", () => {
|
||||
const secretsPath = makeTempPath("saved-environment-secrets.json");
|
||||
const registryPath = makeTempPath("saved-environments.json");
|
||||
const availableSecretStorage = makeSecretStorage(true);
|
||||
|
||||
writeSavedEnvironmentSecret({
|
||||
secretsPath,
|
||||
registryPath,
|
||||
environmentId: savedRegistryRecord.environmentId,
|
||||
secret: "bearer-token",
|
||||
secretStorage: availableSecretStorage,
|
||||
@@ -118,7 +118,7 @@ describe("clientPersistence", () => {
|
||||
|
||||
expect(
|
||||
writeSavedEnvironmentSecret({
|
||||
secretsPath,
|
||||
registryPath,
|
||||
environmentId: savedRegistryRecord.environmentId,
|
||||
secret: "next-token",
|
||||
secretStorage: makeSecretStorage(false),
|
||||
@@ -127,7 +127,7 @@ describe("clientPersistence", () => {
|
||||
|
||||
expect(
|
||||
readSavedEnvironmentSecret({
|
||||
secretsPath,
|
||||
registryPath,
|
||||
environmentId: savedRegistryRecord.environmentId,
|
||||
secretStorage: availableSecretStorage,
|
||||
}),
|
||||
@@ -135,24 +135,24 @@ describe("clientPersistence", () => {
|
||||
});
|
||||
|
||||
it("removes saved environment secrets", () => {
|
||||
const secretsPath = makeTempPath("saved-environment-secrets.json");
|
||||
const registryPath = makeTempPath("saved-environments.json");
|
||||
const secretStorage = makeSecretStorage(true);
|
||||
|
||||
writeSavedEnvironmentSecret({
|
||||
secretsPath,
|
||||
registryPath,
|
||||
environmentId: savedRegistryRecord.environmentId,
|
||||
secret: "bearer-token",
|
||||
secretStorage,
|
||||
});
|
||||
|
||||
removeSavedEnvironmentSecret({
|
||||
secretsPath,
|
||||
registryPath,
|
||||
environmentId: savedRegistryRecord.environmentId,
|
||||
});
|
||||
|
||||
expect(
|
||||
readSavedEnvironmentSecret({
|
||||
secretsPath,
|
||||
registryPath,
|
||||
environmentId: savedRegistryRecord.environmentId,
|
||||
secretStorage,
|
||||
}),
|
||||
@@ -160,12 +160,12 @@ describe("clientPersistence", () => {
|
||||
});
|
||||
|
||||
it("treats malformed secrets documents as empty", () => {
|
||||
const secretsPath = makeTempPath("saved-environment-secrets.json");
|
||||
fs.writeFileSync(secretsPath, "{}\n", "utf8");
|
||||
const registryPath = makeTempPath("saved-environments.json");
|
||||
fs.writeFileSync(registryPath, "{}\n", "utf8");
|
||||
|
||||
expect(
|
||||
readSavedEnvironmentSecret({
|
||||
secretsPath,
|
||||
registryPath,
|
||||
environmentId: savedRegistryRecord.environmentId,
|
||||
secretStorage: makeSecretStorage(true),
|
||||
}),
|
||||
@@ -173,9 +173,32 @@ describe("clientPersistence", () => {
|
||||
|
||||
expect(() =>
|
||||
removeSavedEnvironmentSecret({
|
||||
secretsPath,
|
||||
registryPath,
|
||||
environmentId: savedRegistryRecord.environmentId,
|
||||
}),
|
||||
).not.toThrow();
|
||||
});
|
||||
|
||||
it("preserves encrypted secrets when metadata is rewritten", () => {
|
||||
const registryPath = makeTempPath("saved-environments.json");
|
||||
const secretStorage = makeSecretStorage(true);
|
||||
|
||||
writeSavedEnvironmentSecret({
|
||||
registryPath,
|
||||
environmentId: savedRegistryRecord.environmentId,
|
||||
secret: "bearer-token",
|
||||
secretStorage,
|
||||
});
|
||||
|
||||
writeSavedEnvironmentRegistry(registryPath, [savedRegistryRecord]);
|
||||
|
||||
expect(readSavedEnvironmentRegistry(registryPath)).toEqual([savedRegistryRecord]);
|
||||
expect(
|
||||
readSavedEnvironmentSecret({
|
||||
registryPath,
|
||||
environmentId: savedRegistryRecord.environmentId,
|
||||
secretStorage,
|
||||
}),
|
||||
).toBe("bearer-token");
|
||||
});
|
||||
});
|
||||
@@ -8,12 +8,13 @@ interface ClientSettingsDocument {
|
||||
readonly settings: ClientSettings;
|
||||
}
|
||||
|
||||
interface SavedEnvironmentRegistryDocument {
|
||||
readonly records: readonly PersistedSavedEnvironmentRecord[];
|
||||
interface PersistedSavedEnvironmentStorageRecord extends PersistedSavedEnvironmentRecord {
|
||||
readonly encryptedBearerToken?: string;
|
||||
}
|
||||
|
||||
interface SavedEnvironmentSecretsDocument {
|
||||
readonly byId: Readonly<Record<string, string>>;
|
||||
interface SavedEnvironmentRegistryDocument {
|
||||
readonly records: readonly PersistedSavedEnvironmentStorageRecord[];
|
||||
readonly encryptedBearerTokenById: Readonly<Record<string, string>>;
|
||||
}
|
||||
|
||||
export interface DesktopSecretStorage {
|
||||
@@ -41,9 +42,55 @@ function writeJsonFile(filePath: string, value: unknown): void {
|
||||
FS.renameSync(tempPath, filePath);
|
||||
}
|
||||
|
||||
function readSecretsDocument(filePath: string): SavedEnvironmentSecretsDocument {
|
||||
const parsed = readJsonFile<SavedEnvironmentSecretsDocument>(filePath);
|
||||
return Predicate.isObject(parsed) && Predicate.isObject(parsed.byId) ? parsed : { byId: {} };
|
||||
function isPersistedSavedEnvironmentStorageRecord(
|
||||
value: unknown,
|
||||
): value is PersistedSavedEnvironmentStorageRecord {
|
||||
return (
|
||||
Predicate.isObject(value) &&
|
||||
typeof value.environmentId === "string" &&
|
||||
typeof value.label === "string" &&
|
||||
typeof value.httpBaseUrl === "string" &&
|
||||
typeof value.wsBaseUrl === "string" &&
|
||||
typeof value.createdAt === "string" &&
|
||||
(value.lastConnectedAt === null || typeof value.lastConnectedAt === "string") &&
|
||||
(value.encryptedBearerToken === undefined || typeof value.encryptedBearerToken === "string")
|
||||
);
|
||||
}
|
||||
|
||||
function readSavedEnvironmentRegistryDocument(filePath: string): SavedEnvironmentRegistryDocument {
|
||||
const parsed = readJsonFile<SavedEnvironmentRegistryDocument>(filePath);
|
||||
if (!Predicate.isObject(parsed)) {
|
||||
return { records: [], encryptedBearerTokenById: {} };
|
||||
}
|
||||
|
||||
const records = Array.isArray(parsed.records)
|
||||
? parsed.records.filter(isPersistedSavedEnvironmentStorageRecord)
|
||||
: [];
|
||||
const encryptedBearerTokenById = Predicate.isObject(parsed.encryptedBearerTokenById)
|
||||
? Object.fromEntries(
|
||||
Object.entries(parsed.encryptedBearerTokenById).filter(
|
||||
(entry): entry is [string, string] => typeof entry[1] === "string",
|
||||
),
|
||||
)
|
||||
: {};
|
||||
|
||||
return {
|
||||
records,
|
||||
encryptedBearerTokenById,
|
||||
};
|
||||
}
|
||||
|
||||
function toPersistedSavedEnvironmentRecord(
|
||||
record: PersistedSavedEnvironmentStorageRecord,
|
||||
): PersistedSavedEnvironmentRecord {
|
||||
return {
|
||||
environmentId: record.environmentId,
|
||||
label: record.label,
|
||||
httpBaseUrl: record.httpBaseUrl,
|
||||
wsBaseUrl: record.wsBaseUrl,
|
||||
createdAt: record.createdAt,
|
||||
lastConnectedAt: record.lastConnectedAt,
|
||||
};
|
||||
}
|
||||
|
||||
export function readClientSettings(settingsPath: string): ClientSettings | null {
|
||||
@@ -57,22 +104,30 @@ export function writeClientSettings(settingsPath: string, settings: ClientSettin
|
||||
export function readSavedEnvironmentRegistry(
|
||||
registryPath: string,
|
||||
): readonly PersistedSavedEnvironmentRecord[] {
|
||||
return readJsonFile<SavedEnvironmentRegistryDocument>(registryPath)?.records ?? [];
|
||||
return readSavedEnvironmentRegistryDocument(registryPath).records.map((record) =>
|
||||
toPersistedSavedEnvironmentRecord(record),
|
||||
);
|
||||
}
|
||||
|
||||
export function writeSavedEnvironmentRegistry(
|
||||
registryPath: string,
|
||||
records: readonly PersistedSavedEnvironmentRecord[],
|
||||
): void {
|
||||
writeJsonFile(registryPath, { records } satisfies SavedEnvironmentRegistryDocument);
|
||||
const currentDocument = readSavedEnvironmentRegistryDocument(registryPath);
|
||||
const encryptedBearerTokenById = currentDocument.encryptedBearerTokenById;
|
||||
writeJsonFile(registryPath, {
|
||||
records,
|
||||
encryptedBearerTokenById,
|
||||
} satisfies SavedEnvironmentRegistryDocument);
|
||||
}
|
||||
|
||||
export function readSavedEnvironmentSecret(input: {
|
||||
readonly secretsPath: string;
|
||||
readonly registryPath: string;
|
||||
readonly environmentId: string;
|
||||
readonly secretStorage: DesktopSecretStorage;
|
||||
}): string | null {
|
||||
const encoded = readSecretsDocument(input.secretsPath).byId[input.environmentId];
|
||||
const document = readSavedEnvironmentRegistryDocument(input.registryPath);
|
||||
const encoded = document.encryptedBearerTokenById[input.environmentId];
|
||||
if (!encoded) {
|
||||
return null;
|
||||
}
|
||||
@@ -89,36 +144,39 @@ export function readSavedEnvironmentSecret(input: {
|
||||
}
|
||||
|
||||
export function writeSavedEnvironmentSecret(input: {
|
||||
readonly secretsPath: string;
|
||||
readonly registryPath: string;
|
||||
readonly environmentId: string;
|
||||
readonly secret: string;
|
||||
readonly secretStorage: DesktopSecretStorage;
|
||||
}): boolean {
|
||||
const document = readSecretsDocument(input.secretsPath);
|
||||
const { [input.environmentId]: _previous, ...remaining } = document.byId;
|
||||
const document = readSavedEnvironmentRegistryDocument(input.registryPath);
|
||||
|
||||
if (!input.secretStorage.isEncryptionAvailable()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
writeJsonFile(input.secretsPath, {
|
||||
byId: {
|
||||
...remaining,
|
||||
writeJsonFile(input.registryPath, {
|
||||
records: document.records.map((record) => toPersistedSavedEnvironmentRecord(record)),
|
||||
encryptedBearerTokenById: {
|
||||
...document.encryptedBearerTokenById,
|
||||
[input.environmentId]: input.secretStorage.encryptString(input.secret).toString("base64"),
|
||||
},
|
||||
} satisfies SavedEnvironmentSecretsDocument);
|
||||
} satisfies SavedEnvironmentRegistryDocument);
|
||||
return true;
|
||||
}
|
||||
|
||||
export function removeSavedEnvironmentSecret(input: {
|
||||
readonly secretsPath: string;
|
||||
readonly registryPath: string;
|
||||
readonly environmentId: string;
|
||||
}): void {
|
||||
const document = readSecretsDocument(input.secretsPath);
|
||||
if (!(Predicate.isObject(document.byId) && input.environmentId in document.byId)) {
|
||||
const document = readSavedEnvironmentRegistryDocument(input.registryPath);
|
||||
if (!(input.environmentId in document.encryptedBearerTokenById)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const { [input.environmentId]: _removed, ...remaining } = document.byId;
|
||||
writeJsonFile(input.secretsPath, { byId: remaining } satisfies SavedEnvironmentSecretsDocument);
|
||||
const { [input.environmentId]: _removed, ...remaining } = document.encryptedBearerTokenById;
|
||||
writeJsonFile(input.registryPath, {
|
||||
records: document.records.map((record) => toPersistedSavedEnvironmentRecord(record)),
|
||||
encryptedBearerTokenById: remaining,
|
||||
} satisfies SavedEnvironmentRegistryDocument);
|
||||
}
|
||||
@@ -97,7 +97,6 @@ const STATE_DIR = Path.join(BASE_DIR, "userdata");
|
||||
const DESKTOP_SETTINGS_PATH = Path.join(STATE_DIR, "desktop-settings.json");
|
||||
const CLIENT_SETTINGS_PATH = Path.join(STATE_DIR, "client-settings.json");
|
||||
const SAVED_ENVIRONMENT_REGISTRY_PATH = Path.join(STATE_DIR, "saved-environments.json");
|
||||
const SAVED_ENVIRONMENT_SECRETS_PATH = Path.join(STATE_DIR, "saved-environment-secrets.json");
|
||||
const DESKTOP_SCHEME = "t3";
|
||||
const ROOT_DIR = Path.resolve(__dirname, "../../..");
|
||||
const isDevelopment = Boolean(process.env.VITE_DEV_SERVER_URL);
|
||||
@@ -1411,7 +1410,7 @@ function registerIpcHandlers(): void {
|
||||
}
|
||||
|
||||
return readSavedEnvironmentSecret({
|
||||
secretsPath: SAVED_ENVIRONMENT_SECRETS_PATH,
|
||||
registryPath: SAVED_ENVIRONMENT_REGISTRY_PATH,
|
||||
environmentId: rawEnvironmentId,
|
||||
secretStorage: getDesktopSecretStorage(),
|
||||
});
|
||||
@@ -1430,7 +1429,7 @@ function registerIpcHandlers(): void {
|
||||
}
|
||||
|
||||
return writeSavedEnvironmentSecret({
|
||||
secretsPath: SAVED_ENVIRONMENT_SECRETS_PATH,
|
||||
registryPath: SAVED_ENVIRONMENT_REGISTRY_PATH,
|
||||
environmentId: rawEnvironmentId,
|
||||
secret: rawSecret,
|
||||
secretStorage: getDesktopSecretStorage(),
|
||||
@@ -1447,7 +1446,7 @@ function registerIpcHandlers(): void {
|
||||
}
|
||||
|
||||
removeSavedEnvironmentSecret({
|
||||
secretsPath: SAVED_ENVIRONMENT_SECRETS_PATH,
|
||||
registryPath: SAVED_ENVIRONMENT_REGISTRY_PATH,
|
||||
environmentId: rawEnvironmentId,
|
||||
});
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user