mirror of
https://github.com/Portabase/portabase.git
synced 2026-07-14 11:16:13 +02:00
fix: working on AEA-GCM encryption.
This commit is contained in:
@@ -15,3 +15,19 @@ export function getPublicServerKeyContent() {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get Master server key
|
||||||
|
*/
|
||||||
|
export function getMasterServerKeyContent() {
|
||||||
|
try {
|
||||||
|
return fs.readFileSync("private/keys/master_key.bin");
|
||||||
|
} catch (error: any) {
|
||||||
|
console.error("Error :", error);
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
message: `An error occurred while getting master server key`,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,16 +1,16 @@
|
|||||||
"use server"
|
"use server"
|
||||||
import {getPublicServerKeyContent} from "@/features/keys/keys.action";
|
import {getMasterServerKeyContent} from "@/features/keys/keys.action";
|
||||||
|
|
||||||
export async function generateEdgeKey(serverUrl: string, agentId: string): Promise<string> {
|
export async function generateEdgeKey(serverUrl: string, agentId: string): Promise<string> {
|
||||||
const publicKey = getPublicServerKeyContent()
|
const masterKey = getMasterServerKeyContent()
|
||||||
|
console.log("Master server key: ", masterKey)
|
||||||
const edgeKeyData = {
|
const edgeKeyData = {
|
||||||
serverUrl,
|
serverUrl,
|
||||||
agentId,
|
agentId,
|
||||||
publicKey
|
masterKeyB64: masterKey.toString('base64')
|
||||||
};
|
};
|
||||||
const edgeKeyJson = JSON.stringify(edgeKeyData);
|
const edgeKeyJson = JSON.stringify(edgeKeyData);
|
||||||
const edgeKeyBuffer = Buffer.from(edgeKeyJson, 'utf-8');
|
return Buffer.from(edgeKeyJson, 'utf-8').toString('base64');
|
||||||
return edgeKeyBuffer.toString('base64').replace(/=+$/, '').replace(/\+/g, '-').replace(/\//g, '_');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function decodeEdgeKey(edgeKey: string): object {
|
function decodeEdgeKey(edgeKey: string): object {
|
||||||
|
|||||||
+2
-1
@@ -3,13 +3,14 @@ import {db, makeMigration} from "@/db";
|
|||||||
import {eq} from "drizzle-orm";
|
import {eq} from "drizzle-orm";
|
||||||
import * as drizzleDb from "@/db";
|
import * as drizzleDb from "@/db";
|
||||||
import {retentionJob} from "@/lib/tasks";
|
import {retentionJob} from "@/lib/tasks";
|
||||||
import {generateRSAKeys} from "@/utils/rsa-keys";
|
import {generateRSAKeys, getOrCreateMasterKey} from "@/utils/rsa-keys";
|
||||||
import {StorageProviderKind} from "@/features/storages/types";
|
import {StorageProviderKind} from "@/features/storages/types";
|
||||||
|
|
||||||
|
|
||||||
export async function init() {
|
export async function init() {
|
||||||
consoleAscii();
|
consoleAscii();
|
||||||
console.log("====Init Functions====");
|
console.log("====Init Functions====");
|
||||||
|
await getOrCreateMasterKey();
|
||||||
await generateRSAKeys();
|
await generateRSAKeys();
|
||||||
await makeMigration();
|
await makeMigration();
|
||||||
await createDefaultOrganization();
|
await createDefaultOrganization();
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import {promises as fs} from 'fs';
|
|||||||
import path from 'path';
|
import path from 'path';
|
||||||
import {generateKeyPair} from 'crypto';
|
import {generateKeyPair} from 'crypto';
|
||||||
import {promisify} from 'util';
|
import {promisify} from 'util';
|
||||||
|
import {randomBytes} from 'crypto';
|
||||||
|
|
||||||
const generateKeyPairAsync = promisify(generateKeyPair);
|
const generateKeyPairAsync = promisify(generateKeyPair);
|
||||||
|
|
||||||
@@ -37,3 +38,33 @@ export async function generateRSAKeys(dir = path.join(process.cwd(), 'private/ke
|
|||||||
|
|
||||||
return {privateKeyPath, publicKeyPath};
|
return {privateKeyPath, publicKeyPath};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate a 256-bit AES master key for AES-256-GCM.
|
||||||
|
* - Skips generation if the file already exists.
|
||||||
|
* - File mode 0o600 for private key.
|
||||||
|
* @param {string} [filePath] Path to store the key
|
||||||
|
* @returns {Promise<Buffer>} The master key
|
||||||
|
*/
|
||||||
|
export async function getOrCreateMasterKey(filePath = path.join(process.cwd(), 'private/keys', 'master_key.bin')) {
|
||||||
|
|
||||||
|
await fs.mkdir(path.dirname(filePath), {recursive: true});
|
||||||
|
|
||||||
|
try {
|
||||||
|
const existing = await fs.readFile(filePath);
|
||||||
|
console.log('Master key already exists. Skipping generation.');
|
||||||
|
return existing;
|
||||||
|
} catch {
|
||||||
|
// File does not exist, generate
|
||||||
|
}
|
||||||
|
|
||||||
|
const key = randomBytes(32); // 256-bit key
|
||||||
|
|
||||||
|
console.log(key)
|
||||||
|
|
||||||
|
await fs.writeFile(filePath, key, {mode: 0o600});
|
||||||
|
console.log(`Master key generated at ${filePath}`);
|
||||||
|
|
||||||
|
return key;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user