Skip to content

Commit 5c33240

Browse files
committed
feat: add reset certs functionality to CLI
- Introduced a new CLI action to reset certificates, prompting the user for confirmation before deletion. - Updated the generateMasterCerts utility to use ECDSA with P-256 for key generation, enhancing security. - Extended the certificate validity period from 3 to 10 years for improved longevity.
1 parent ef27f7e commit 5c33240

File tree

2 files changed

+58
-11
lines changed

2 files changed

+58
-11
lines changed

src/bin/cli/cli.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const prisma = new PrismaClient({
1313

1414
const enum CLI_ACTIONS {
1515
EXIT = 'exit',
16+
RESET_CERTS = 'reset-certs',
1617
RESET_SUPERADMIN = 'reset-superadmin',
1718
}
1819

@@ -58,6 +59,46 @@ async function resetSuperadmin() {
5859
process.exit(1);
5960
}
6061
}
62+
63+
async function resetCerts() {
64+
const answer = await consola.prompt(
65+
'Are you sure you want to delete the certs? You will need to add new certs to all nodes again.',
66+
{
67+
type: 'confirm',
68+
required: true,
69+
},
70+
);
71+
72+
if (!answer) {
73+
consola.error('❌ Aborted.');
74+
process.exit(1);
75+
}
76+
77+
consola.start('🔄 Deleting certs...');
78+
79+
const keygen = await prisma.keygen.findFirst();
80+
81+
if (!keygen) {
82+
consola.error('❌ Certs not found.');
83+
process.exit(1);
84+
}
85+
86+
try {
87+
await prisma.keygen.delete({
88+
where: {
89+
uuid: keygen.uuid,
90+
},
91+
});
92+
consola.success(`✅ Certs deleted successfully.`);
93+
consola.warn(
94+
`Restart Remnawave to apply changes by running "docker compose down && docker compose up -d".`,
95+
);
96+
} catch (error) {
97+
consola.error('❌ Failed to reset certs:', error);
98+
process.exit(1);
99+
}
100+
}
101+
61102
async function main() {
62103
consola.box('Remnawave Rescue CLI v0.1');
63104

@@ -78,6 +119,11 @@ async function main() {
78119
label: 'Reset superadmin',
79120
hint: 'Fully reset superadmin',
80121
},
122+
{
123+
value: CLI_ACTIONS.RESET_CERTS,
124+
label: 'Reset certs',
125+
hint: 'Fully reset certs',
126+
},
81127
{
82128
value: CLI_ACTIONS.EXIT,
83129
label: 'Exit',
@@ -90,6 +136,9 @@ async function main() {
90136
case CLI_ACTIONS.RESET_SUPERADMIN:
91137
await resetSuperadmin();
92138
break;
139+
case CLI_ACTIONS.RESET_CERTS:
140+
await resetCerts();
141+
break;
93142
case CLI_ACTIONS.EXIT:
94143
consola.info('👋 Exiting...');
95144
process.exit(0);

src/common/utils/certs/generate-certs.util.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@ export async function generateMasterCerts() {
2121

2222
// === CA (Certificate Authority) ===
2323
const caAlgorithm = {
24-
name: 'RSASSA-PKCS1-v1_5',
24+
name: 'ECDSA',
25+
namedCurve: 'P-256',
2526
hash: { name: 'SHA-256' },
26-
publicExponent: new Uint8Array([1, 0, 1]),
27-
modulusLength: 2048,
2827
};
2928

3029
const caKeys = await crypto.subtle.generateKey(caAlgorithm, true, ['sign', 'verify']);
@@ -54,10 +53,9 @@ export async function generateMasterCerts() {
5453

5554
// === Client (Master) ===
5655
const clientAlgorithm = {
57-
name: 'RSASSA-PKCS1-v1_5',
56+
name: 'ECDSA',
57+
namedCurve: 'P-256',
5858
hash: { name: 'SHA-256' },
59-
publicExponent: new Uint8Array([1, 0, 1]),
60-
modulusLength: 2048,
6159
};
6260

6361
const clientKeys = await crypto.subtle.generateKey(clientAlgorithm, true, ['sign', 'verify']);
@@ -66,7 +64,7 @@ export async function generateMasterCerts() {
6664
serialNumber: '02',
6765
subject: `CN=${genRandomString()}`,
6866
notBefore: new Date(),
69-
notAfter: new Date(new Date().setFullYear(new Date().getFullYear() + 3)),
67+
notAfter: new Date(new Date().setFullYear(new Date().getFullYear() + 10)),
7068
issuer: caCert.subjectName,
7169
publicKey: clientKeys.publicKey,
7270
signingKey: caKeys.privateKey,
@@ -110,7 +108,8 @@ export async function generateNodeCert(
110108
'pkcs8',
111109
pemToArrayBuffer(caKeyPem),
112110
{
113-
name: 'RSASSA-PKCS1-v1_5',
111+
name: 'ECDSA',
112+
namedCurve: 'P-256',
114113
hash: { name: 'SHA-256' },
115114
},
116115
false,
@@ -119,9 +118,8 @@ export async function generateNodeCert(
119118

120119
const nodeKeys = await crypto.subtle.generateKey(
121120
{
122-
name: 'RSASSA-PKCS1-v1_5',
123-
modulusLength: 2048,
124-
publicExponent: new Uint8Array([1, 0, 1]),
121+
name: 'ECDSA',
122+
namedCurve: 'P-256',
125123
hash: { name: 'SHA-256' },
126124
},
127125
true,

0 commit comments

Comments
 (0)