From d42a859679ac61ddc6828d956e41bbf17525d686 Mon Sep 17 00:00:00 2001 From: Mauricio Siu <47042324+Siumauricio@users.noreply.github.com> Date: Sun, 9 Mar 2025 21:27:45 -0600 Subject: [PATCH] feat(templates): add JWT generation and expand template variable processing - Implement generateJwt function for creating JWT tokens - Add support for 'jwt' and 'jwt:length' template variables - Introduce new base64 and password generation shortcuts - Enhance template variable processing with additional utility functions --- .../templates/config.template.test.ts | 3 ++- packages/server/src/templates/index.ts | 4 ++++ packages/server/src/templates/processors.ts | 19 +++++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/apps/dokploy/__test__/templates/config.template.test.ts b/apps/dokploy/__test__/templates/config.template.test.ts index 2dfab7a4..50ef7627 100644 --- a/apps/dokploy/__test__/templates/config.template.test.ts +++ b/apps/dokploy/__test__/templates/config.template.test.ts @@ -349,7 +349,7 @@ describe("processTemplate", () => { { serviceName: "plausible", port: 8000, - host: "${randomDomain}", + host: "${hash}", }, ], env: { @@ -367,6 +367,7 @@ describe("processTemplate", () => { }; const result = processTemplate(template, mockSchema); + console.log(result); expect(result.envs).toHaveLength(3); expect(result.domains).toHaveLength(1); expect(result.mounts).toHaveLength(1); diff --git a/packages/server/src/templates/index.ts b/packages/server/src/templates/index.ts index f6027239..14ff0ac0 100644 --- a/packages/server/src/templates/index.ts +++ b/packages/server/src/templates/index.ts @@ -63,6 +63,10 @@ export function generateBase64(length: number): string { .substring(0, length); } +export function generateJwt(length = 256): string { + return randomBytes(length).toString("hex"); +} + /** * Reads a template's docker-compose.yml file * First tries to fetch from GitHub, falls back to local cache if fetch fails diff --git a/packages/server/src/templates/processors.ts b/packages/server/src/templates/processors.ts index 505ccc33..5764b220 100644 --- a/packages/server/src/templates/processors.ts +++ b/packages/server/src/templates/processors.ts @@ -2,6 +2,7 @@ import type { Schema } from "./index"; import { generateBase64, generateHash, + generateJwt, generatePassword, generateRandomDomain, } from "./index"; @@ -72,6 +73,10 @@ function processValue( if (varName === "randomDomain") { return generateRandomDomain(schema); } + + if (varName === "base64") { + return generateBase64(32); + } if (varName.startsWith("base64:")) { const length = Number.parseInt(varName.split(":")[1], 10) || 32; return generateBase64(length); @@ -80,6 +85,11 @@ function processValue( const length = Number.parseInt(varName.split(":")[1], 10) || 16; return generatePassword(length); } + + if (varName === "password") { + return generatePassword(16); + } + if (varName.startsWith("hash:")) { const length = Number.parseInt(varName.split(":")[1], 10) || 8; return generateHash(length); @@ -96,6 +106,15 @@ function processValue( return Math.floor(Math.random() * 65535).toString(); } + if (varName === "jwt") { + return generateJwt(); + } + + if (varName.startsWith("jwt:")) { + const length = Number.parseInt(varName.split(":")[1], 10) || 256; + return generateJwt(length); + } + // If not a utility function, try to get from variables return variables[varName] || match; });