autocreate folder keys

This commit is contained in:
Sofía Aritz 2024-06-30 19:03:38 +02:00
parent f2f80dd2f7
commit c73774dc03
Signed by: sofia
GPG key ID: 90B5116E3542B28F
3 changed files with 27 additions and 9 deletions

View file

@ -1,2 +1,4 @@
ASSET_API_ASSETS_FOLDER = "./.assets/"
ASSET_API_IDENTITY_API_ENDPOINT = "http://localhost:3000/"
ASSET_API_PRIVATE_KEY_PATH = "./.keys/m2m.pem"
ASSET_API_PUBLIC_KEY_PATH = "./.keys/m2m.pub"

View file

@ -18,7 +18,9 @@ import "dotenv/config";
const REQUIRED_VARS = [
"ASSET_API_ASSETS_FOLDER",
"ASSET_API_IDENTITY_API_ENDPOINT"
"ASSET_API_IDENTITY_API_ENDPOINT",
"ASSET_API_PRIVATE_KEY_PATH",
"ASSET_API_PUBLIC_KEY_PATH",
];
REQUIRED_VARS.forEach((element) => {
@ -36,3 +38,5 @@ export const LISTEN_PORT = Number(process.env["ASSET_API_LISTEN_PORT"]) || 3001;
export const ASSETS_FOLDER = process.env["ASSET_API_ASSETS_FOLDER"];
export const IDENTITY_API_ENDPOINT = process.env["ASSET_API_IDENTITY_API_ENDPOINT"];
export const M2M_ALGORITHM = process.env["ASSET_API_M2M_ALGORITHM"] || "RSA-SHA512";
export const PRIVATE_KEY_PATH = process.env["ASSET_API_PRIVATE_KEY_PATH"]
export const PUBLIC_KEY_PATH = process.env["ASSET_API_PUBLIC_KEY_PATH"]

View file

@ -15,7 +15,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import { readFile } from "node:fs/promises";
import { createWriteStream, readFileSync, writeFileSync } from "node:fs";
import { createWriteStream, mkdirSync, readFileSync, writeFileSync, existsSync } from "node:fs";
import { createSign, generateKeyPairSync, randomUUID } from "node:crypto";
import Fastify from "fastify";
import multipart from "@fastify/multipart";
@ -24,7 +24,7 @@ import mime from "mime";
import { promisify } from "node:util";
import { pipeline } from "node:stream";
import cors from "@fastify/cors";
import { M2M_ALGORITHM, ASSETS_FOLDER, ASSET_API_LANDING_MESSAGE, IDENTITY_API_ENDPOINT } from "./consts.js";
import { M2M_ALGORITHM, ASSETS_FOLDER, ASSET_API_LANDING_MESSAGE, IDENTITY_API_ENDPOINT, PRIVATE_KEY_PATH, PUBLIC_KEY_PATH } from "./consts.js";
const { private: M2M_PRIVATE_KEY, public: M2M_PUBLIC_KEY } = loadM2MKeys();
if (M2M_PRIVATE_KEY == null || M2M_PUBLIC_KEY == null) {
@ -131,11 +131,11 @@ app.listen({ port: 3001 });
function loadM2MKeys() {
try {
return {
private: readFileSync("./.keys/m2m.pem").toString("ascii"),
public: readFileSync("./.keys/m2m.pub").toString("ascii"),
private: readFileSync(PRIVATE_KEY_PATH).toString("ascii"),
public: readFileSync(PUBLIC_KEY_PATH).toString("ascii"),
};
} catch {
console.warn("Generating M2M key pair!");
console.warn("M2M key pair not found. Generating M2M key pair!");
let { publicKey, privateKey } = generateKeyPairSync("rsa", {
modulusLength: 4096,
@ -149,8 +149,20 @@ function loadM2MKeys() {
},
});
writeFileSync("./.keys/m2m.pem", privateKey);
writeFileSync("./.keys/m2m.pub", publicKey);
let privateDir = join(PRIVATE_KEY_PATH, "..");
if (!existsSync(privateDir)) {
console.warn("The private key folder does not exist. It will be created.")
mkdirSync(privateDir, { recursive: true });
}
let publicDir = join(PUBLIC_KEY_PATH, "..");
if (!existsSync(publicDir)) {
console.warn("The public key folder does not exist. It will be created.")
mkdirSync(publicDir, { recursive: true });
}
writeFileSync(PRIVATE_KEY_PATH, privateKey);
writeFileSync(PUBLIC_KEY_PATH, publicKey);
return loadM2MKeys();
}