Skip to content

Commit 8001961

Browse files
committed
Refactor gitignore and Makefile, add HTTPS certificate generation and cleanup scripts
- Refactor gitignore to include /certificates directory - Update Makefile to add targets for generating and cleaning HTTPS certificates - Add generate-ssl-cert target to generate self-signed HTTPS certificates - Add clean-ssl-cert target to remove HTTPS certificates - Add show-cert-info target to display information about the HTTPS certificate
1 parent 464076c commit 8001961

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# compiled output
22
/dist
33
/node_modules
4+
/certificates
45
.env
56
docker-compose.yml
67
# Logs

Makefile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ APP_NAME = "sesame-orchestrator"
55
PLATFORM = "linux/amd64"
66
include .env
77

8+
CERT_DIR = ./certificates
9+
COMMON_NAME = localhost
10+
DAYS_VALID = 365
11+
12+
$(shell mkdir -p $(CERT_DIR))
13+
14+
815
.DEFAULT_GOAL := help
916
help:
1017
@printf "\033[33mUsage:\033[0m\n make [target] [arg=\"val\"...]\n\n\033[33mTargets:\033[0m\n"
@@ -126,3 +133,23 @@ ncu: ## Check latest versions of all project dependencies
126133

127134
ncu-upgrade: ## Upgrade all project dependencies to the latest versions
128135
@npx npm-check-updates -u
136+
137+
generate-ssl-cert: ## Générer les certificats HTTPS auto-signés
138+
@echo "Génération des certificats HTTPS auto-signés..."
139+
@openssl req -x509 \
140+
-newkey rsa:4096 \
141+
-keyout $(CERT_DIR)/server.key \
142+
-out $(CERT_DIR)/server.crt \
143+
-days $(DAYS_VALID) \
144+
-nodes \
145+
-subj "/CN=$(COMMON_NAME)"
146+
@chmod 600 $(CERT_DIR)/server.key
147+
@chmod 644 $(CERT_DIR)/server.crt
148+
@echo "Certificats générés avec succès dans $(CERT_DIR)"
149+
150+
clean-ssl-cert: ## Nettoyer les certificats HTTPS
151+
@rm -rf $(CERT_DIR)
152+
@echo "Certificats supprimés"
153+
154+
show-cert-info: ## Afficher les informations du certificat
155+
@openssl x509 -in $(CERT_DIR)/server.crt -text -noout

src/config.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { IAuthModuleOptions } from '@nestjs/passport';
66
import { JwtModuleOptions } from '@nestjs/jwt';
77
import { StorageManagerConfig } from '@the-software-compagny/nestjs_module_factorydrive';
88
import { AmazonWebServicesS3StorageConfig } from '@the-software-compagny/nestjs_module_factorydrive-s3';
9+
import { parse } from 'path';
910

1011
export interface MongoosePlugin {
1112
package: string;
@@ -19,6 +20,11 @@ export interface ConfigInstance {
1920
bodyParser: {
2021
limit: string;
2122
};
23+
https: {
24+
enabled: boolean;
25+
key: string;
26+
cert: string;
27+
}
2228
};
2329
helmet: HelmetOptions;
2430
mongoose: {
@@ -79,6 +85,11 @@ export default (): ConfigInstance => ({
7985
bodyParser: {
8086
limit: '500mb',
8187
},
88+
https: {
89+
enabled: !!parseInt(process.env['SESAME_HTTPS_ENABLED']) || false,
90+
key: process.env['SESAME_HTTPS_PATH_KEY'] || '',
91+
cert: process.env['SESAME_HTTPS_PATH_CERT'] || '',
92+
},
8293
},
8394
helmet: {
8495
contentSecurityPolicy: {

src/main.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { getLogLevel } from './_common/functions/get-log-level';
88
import { AppModule } from './app.module';
99
import configInstance from './config';
1010
import { InternalLogger } from './core/logger/internal.logger';
11+
import { readFileSync } from 'fs';
1112

1213
declare const module: any;
1314
(async (): Promise<void> => {
@@ -17,11 +18,25 @@ declare const module: any;
1718
mongoose: cfg?.mongoose,
1819
});
1920
await logger.initialize();
21+
22+
let httpsOptions = {};
23+
if (cfg.application?.https?.enabled) {
24+
try {
25+
httpsOptions = {
26+
key: readFileSync(cfg.application?.https?.key),
27+
cert: readFileSync(cfg.application?.https?.cert),
28+
};
29+
} catch (error) {
30+
logger.error('Error while reading https key and cert', error);
31+
}
32+
}
33+
2034
const app = await NestFactory.create<NestExpressApplication>(AppModule, {
2135
bodyParser: false,
2236
rawBody: true,
2337
cors: true,
2438
logger,
39+
httpsOptions,
2540
});
2641
app.use((_: any, res: Response, next: () => void) => {
2742
res.removeHeader('x-powered-by');

0 commit comments

Comments
 (0)