Cleanup files and improve file manager.
This commit is contained in:
@@ -1,83 +1,93 @@
|
||||
import { db } from '../database/db/index.js';
|
||||
import { DatabaseEncryption } from './database-encryption.js';
|
||||
import { databaseLogger } from './logger.js';
|
||||
import type { SQLiteTable } from 'drizzle-orm/sqlite-core';
|
||||
import { db } from "../database/db/index.js";
|
||||
import { DatabaseEncryption } from "./database-encryption.js";
|
||||
import { databaseLogger } from "./logger.js";
|
||||
import type { SQLiteTable } from "drizzle-orm/sqlite-core";
|
||||
|
||||
type TableName = 'users' | 'ssh_data' | 'ssh_credentials';
|
||||
type TableName = "users" | "ssh_data" | "ssh_credentials";
|
||||
|
||||
class EncryptedDBOperations {
|
||||
static async insert<T extends Record<string, any>>(
|
||||
table: SQLiteTable<any>,
|
||||
tableName: TableName,
|
||||
data: T
|
||||
data: T,
|
||||
): Promise<T> {
|
||||
try {
|
||||
const encryptedData = DatabaseEncryption.encryptRecord(tableName, data);
|
||||
const result = await db.insert(table).values(encryptedData).returning();
|
||||
|
||||
// Decrypt the returned data to ensure consistency
|
||||
const decryptedResult = DatabaseEncryption.decryptRecord(tableName, result[0]);
|
||||
const decryptedResult = DatabaseEncryption.decryptRecord(
|
||||
tableName,
|
||||
result[0],
|
||||
);
|
||||
|
||||
databaseLogger.debug(`Inserted encrypted record into ${tableName}`, {
|
||||
operation: 'encrypted_insert',
|
||||
table: tableName
|
||||
operation: "encrypted_insert",
|
||||
table: tableName,
|
||||
});
|
||||
|
||||
return decryptedResult as T;
|
||||
} catch (error) {
|
||||
databaseLogger.error(`Failed to insert encrypted record into ${tableName}`, error, {
|
||||
operation: 'encrypted_insert_failed',
|
||||
table: tableName
|
||||
});
|
||||
databaseLogger.error(
|
||||
`Failed to insert encrypted record into ${tableName}`,
|
||||
error,
|
||||
{
|
||||
operation: "encrypted_insert_failed",
|
||||
table: tableName,
|
||||
},
|
||||
);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async select<T extends Record<string, any>>(
|
||||
query: any,
|
||||
tableName: TableName
|
||||
tableName: TableName,
|
||||
): Promise<T[]> {
|
||||
try {
|
||||
const results = await query;
|
||||
const decryptedResults = DatabaseEncryption.decryptRecords(tableName, results);
|
||||
|
||||
databaseLogger.debug(`Selected and decrypted ${decryptedResults.length} records from ${tableName}`, {
|
||||
operation: 'encrypted_select',
|
||||
table: tableName,
|
||||
count: decryptedResults.length
|
||||
});
|
||||
const decryptedResults = DatabaseEncryption.decryptRecords(
|
||||
tableName,
|
||||
results,
|
||||
);
|
||||
|
||||
return decryptedResults;
|
||||
} catch (error) {
|
||||
databaseLogger.error(`Failed to select/decrypt records from ${tableName}`, error, {
|
||||
operation: 'encrypted_select_failed',
|
||||
table: tableName
|
||||
});
|
||||
databaseLogger.error(
|
||||
`Failed to select/decrypt records from ${tableName}`,
|
||||
error,
|
||||
{
|
||||
operation: "encrypted_select_failed",
|
||||
table: tableName,
|
||||
},
|
||||
);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async selectOne<T extends Record<string, any>>(
|
||||
query: any,
|
||||
tableName: TableName
|
||||
tableName: TableName,
|
||||
): Promise<T | undefined> {
|
||||
try {
|
||||
const result = await query;
|
||||
if (!result) return undefined;
|
||||
|
||||
const decryptedResult = DatabaseEncryption.decryptRecord(tableName, result);
|
||||
|
||||
databaseLogger.debug(`Selected and decrypted single record from ${tableName}`, {
|
||||
operation: 'encrypted_select_one',
|
||||
table: tableName
|
||||
});
|
||||
const decryptedResult = DatabaseEncryption.decryptRecord(
|
||||
tableName,
|
||||
result,
|
||||
);
|
||||
|
||||
return decryptedResult;
|
||||
} catch (error) {
|
||||
databaseLogger.error(`Failed to select/decrypt single record from ${tableName}`, error, {
|
||||
operation: 'encrypted_select_one_failed',
|
||||
table: tableName
|
||||
});
|
||||
databaseLogger.error(
|
||||
`Failed to select/decrypt single record from ${tableName}`,
|
||||
error,
|
||||
{
|
||||
operation: "encrypted_select_one_failed",
|
||||
table: tableName,
|
||||
},
|
||||
);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
@@ -86,23 +96,31 @@ class EncryptedDBOperations {
|
||||
table: SQLiteTable<any>,
|
||||
tableName: TableName,
|
||||
where: any,
|
||||
data: Partial<T>
|
||||
data: Partial<T>,
|
||||
): Promise<T[]> {
|
||||
try {
|
||||
const encryptedData = DatabaseEncryption.encryptRecord(tableName, data);
|
||||
const result = await db.update(table).set(encryptedData).where(where).returning();
|
||||
const result = await db
|
||||
.update(table)
|
||||
.set(encryptedData)
|
||||
.where(where)
|
||||
.returning();
|
||||
|
||||
databaseLogger.debug(`Updated encrypted record in ${tableName}`, {
|
||||
operation: 'encrypted_update',
|
||||
table: tableName
|
||||
operation: "encrypted_update",
|
||||
table: tableName,
|
||||
});
|
||||
|
||||
return result as T[];
|
||||
} catch (error) {
|
||||
databaseLogger.error(`Failed to update encrypted record in ${tableName}`, error, {
|
||||
operation: 'encrypted_update_failed',
|
||||
table: tableName
|
||||
});
|
||||
databaseLogger.error(
|
||||
`Failed to update encrypted record in ${tableName}`,
|
||||
error,
|
||||
{
|
||||
operation: "encrypted_update_failed",
|
||||
table: tableName,
|
||||
},
|
||||
);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
@@ -110,21 +128,21 @@ class EncryptedDBOperations {
|
||||
static async delete(
|
||||
table: SQLiteTable<any>,
|
||||
tableName: TableName,
|
||||
where: any
|
||||
where: any,
|
||||
): Promise<any[]> {
|
||||
try {
|
||||
const result = await db.delete(table).where(where).returning();
|
||||
|
||||
databaseLogger.debug(`Deleted record from ${tableName}`, {
|
||||
operation: 'encrypted_delete',
|
||||
table: tableName
|
||||
operation: "encrypted_delete",
|
||||
table: tableName,
|
||||
});
|
||||
|
||||
return result;
|
||||
} catch (error) {
|
||||
databaseLogger.error(`Failed to delete record from ${tableName}`, error, {
|
||||
operation: 'encrypted_delete_failed',
|
||||
table: tableName
|
||||
operation: "encrypted_delete_failed",
|
||||
table: tableName,
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
@@ -135,26 +153,26 @@ class EncryptedDBOperations {
|
||||
|
||||
try {
|
||||
databaseLogger.info(`Starting encryption migration for ${tableName}`, {
|
||||
operation: 'migration_start',
|
||||
table: tableName
|
||||
operation: "migration_start",
|
||||
table: tableName,
|
||||
});
|
||||
|
||||
let table: SQLiteTable<any>;
|
||||
let records: any[];
|
||||
|
||||
switch (tableName) {
|
||||
case 'users':
|
||||
const { users } = await import('../database/db/schema.js');
|
||||
case "users":
|
||||
const { users } = await import("../database/db/schema.js");
|
||||
table = users;
|
||||
records = await db.select().from(users);
|
||||
break;
|
||||
case 'ssh_data':
|
||||
const { sshData } = await import('../database/db/schema.js');
|
||||
case "ssh_data":
|
||||
const { sshData } = await import("../database/db/schema.js");
|
||||
table = sshData;
|
||||
records = await db.select().from(sshData);
|
||||
break;
|
||||
case 'ssh_credentials':
|
||||
const { sshCredentials } = await import('../database/db/schema.js');
|
||||
case "ssh_credentials":
|
||||
const { sshCredentials } = await import("../database/db/schema.js");
|
||||
table = sshCredentials;
|
||||
records = await db.select().from(sshCredentials);
|
||||
break;
|
||||
@@ -164,34 +182,44 @@ class EncryptedDBOperations {
|
||||
|
||||
for (const record of records) {
|
||||
try {
|
||||
const migratedRecord = await DatabaseEncryption.migrateRecord(tableName, record);
|
||||
const migratedRecord = await DatabaseEncryption.migrateRecord(
|
||||
tableName,
|
||||
record,
|
||||
);
|
||||
|
||||
if (JSON.stringify(migratedRecord) !== JSON.stringify(record)) {
|
||||
const { eq } = await import('drizzle-orm');
|
||||
await db.update(table).set(migratedRecord).where(eq((table as any).id, record.id));
|
||||
const { eq } = await import("drizzle-orm");
|
||||
await db
|
||||
.update(table)
|
||||
.set(migratedRecord)
|
||||
.where(eq((table as any).id, record.id));
|
||||
migratedCount++;
|
||||
}
|
||||
} catch (error) {
|
||||
databaseLogger.error(`Failed to migrate record ${record.id} in ${tableName}`, error, {
|
||||
operation: 'migration_record_failed',
|
||||
table: tableName,
|
||||
recordId: record.id
|
||||
});
|
||||
databaseLogger.error(
|
||||
`Failed to migrate record ${record.id} in ${tableName}`,
|
||||
error,
|
||||
{
|
||||
operation: "migration_record_failed",
|
||||
table: tableName,
|
||||
recordId: record.id,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
databaseLogger.success(`Migration completed for ${tableName}`, {
|
||||
operation: 'migration_complete',
|
||||
operation: "migration_complete",
|
||||
table: tableName,
|
||||
migratedCount,
|
||||
totalRecords: records.length
|
||||
totalRecords: records.length,
|
||||
});
|
||||
|
||||
return migratedCount;
|
||||
} catch (error) {
|
||||
databaseLogger.error(`Migration failed for ${tableName}`, error, {
|
||||
operation: 'migration_failed',
|
||||
table: tableName
|
||||
operation: "migration_failed",
|
||||
table: tableName,
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
@@ -202,8 +230,8 @@ class EncryptedDBOperations {
|
||||
const status = DatabaseEncryption.getEncryptionStatus();
|
||||
return status.configValid && status.enabled;
|
||||
} catch (error) {
|
||||
databaseLogger.error('Encryption health check failed', error, {
|
||||
operation: 'health_check_failed'
|
||||
databaseLogger.error("Encryption health check failed", error, {
|
||||
operation: "health_check_failed",
|
||||
});
|
||||
return false;
|
||||
}
|
||||
@@ -211,4 +239,4 @@ class EncryptedDBOperations {
|
||||
}
|
||||
|
||||
export { EncryptedDBOperations };
|
||||
export type { TableName };
|
||||
export type { TableName };
|
||||
|
||||
Reference in New Issue
Block a user