Informazioni su questo blog

Mozilla Observatory

Il mio blog personale sulla sicurezza

Ciao a tutti, con questo progetto voglio condividere il codice sorgente del mio blog personale. La mia applicazione usa i framework Python Django+Django Rest per la parte backend/API, mentre per il frontend usa NodeJs+Angular. Sentitevi liberi di inviarmi commenti o patch al codice.

Come si esegue il deploy?

1. Clonare il progetto

# git clone https://github.com/axiom4/axiom4.net.git
# cd axiom4.net
# export PROJECT_HOME=${PWD}

2. Creare il venv Python

# python3 -m venv ${PROJECT_HOME}/venv
# source ${PROJECT_HOME}/venv/bin/activate
# cd ${PROJECT_HOME}/axiom4-api
# pip install -r requirements.txt
# cd ${PROJECT_HOME}

3. Installare i moduli node

# cd ${PROJECT_HOME}/axiom4-web
# npm install
# cd ${PROJECT_HOME}

Come si avvia il backend in modalità sviluppo?

# cd ${PROJECT_HOME}/axiom4-api


# ./manage.py makemigrations
No changes detected


# ./manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying auth.0012_alter_user_first_name_max_length... OK
  Applying sessions.0001_initial... OK


# ./manage.py createsuperuser --username admin
Email address: [your mail]
Password: 
Password (again): 
Superuser created successfully. 

# ./manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
August 17, 2023 - 08:03:03
Django version 4.2.4, using settings 'axiom4.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Come si avvia il frontend in modalità sviluppo?

# cd ${PROJECT_HOME}/axiom4-web

# ng serve
✔ Browser application bundle generation complete.

Initial Chunk Files   | Names         |  Raw Size
vendor.js             | vendor        |   2.33 MB | 
polyfills.js          | polyfills     | 333.18 kB | 
styles.css, styles.js | styles        | 230.92 kB | 
main.js               | main          |  48.11 kB | 
runtime.js            | runtime       |   6.52 kB | 

                      | Initial Total |   2.94 MB

Build at: 2023-08-17T08:04:02.902Z - Hash: 5ece58a9d7f1e6e3 - Time: 4966ms

** Angular Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200/ **


✔ Compiled successfully.

Deploy in produzione

Di seguito una configurazione Nginx di esempio:

# Expires map
map $sent_http_content_type $expires {
	default                    off;
	text/html                  epoch; #means no cache, as it is not a static page
	text/css                   max;
	application/javascript     max;
	application/json           max;
	font/woff2                 max;
	application/font-woff2     max;
	woff2                      max;
	application/x-font-woff    max;
	~image/                    30d; #it is only the logo, so maybe I could change it once a month now
}

# dephne supervisor backend
upstream channels-backend {
  server unix:[RUN DIR PATH]/run/axiom4_daphne0.sock;
  server unix:[RUN DIR PATH]/run/axiom4_daphne1.sock;
  server unix:[RUN DIR PATH]/run/axiom4_daphne2.sock;
  server unix:[RUN DIR PATH]/run/axiom4_daphne3.sock;
}

# Default server configuration
#
server {
	listen 80 default_server;
	listen [::]:80 default_server;

	location / {
		# redirect https://
		return 301 https://$host$request_uri;
	}

	location ~ /\.ht {
		deny all;
	}

	location = /robots.txt {
		allow all;
		log_not_found off;
		access_log off;
	}
}


# Virtual Host configuration for SSL
#
server {

	# SSL configuration
	#
	listen 443 ssl default_server http2;
	listen [::]:443 ssl default_server http2;

	ssl_certificate [YOUR CERTIFICATE];
	ssl_certificate_key [YUOR PRIVATE KEY};
	ssl_session_timeout 1d;
	ssl_session_cache shared:MozSSL:10m;  # about 40000 sessions
	ssl_session_tickets off;

	# SSL Protocols and Ciphers
	ssl_protocols TLSv1.2 TLSv1.3;
	ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-CHACHA20-POLY1305;
	ssl_prefer_server_ciphers off;

	# OCSP stapling
	ssl_stapling on;
	ssl_stapling_verify on;

	# verify chain of trust of OCSP response using Root CA and Intermediate certs
	ssl_trusted_certificate [YOUR TRUSTED CERTIFICATES CHAIN];

	# replace with the IP address of your resolver
	resolver 8.8.8.8 8.8.4.4 valid=1h;

	keepalive_timeout 5;
	client_max_body_size 10m;

	server_name [YUOR FQDN];
	access_log [access.log file];
	error_log [error.log file] error;

	root [frontend document root];
	index index.html;

	sub_filter_once off;
	sub_filter 0c651ea88c64bd74a7f91631bdf00476 $request_id;

	add_header 'Access-Control-Allow-Origin' "$server_name" always;
	add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS always, PATCH' always;
	add_header 'Access-Control-Allow-Headers' 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Requested-With' always;
	add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;

	add_header 'Cross-Origin-Embedder-Policy' 'require-corpi; report-to="default"' always;
	add_header 'Cross-Origin-Opener-Policy' 'same-origin-allow-popups; report-to="default"' always;
	add_header 'Cross-Origin-Resource-Policy' 'same-origin' always;

	add_header Strict-Transport-Security 'max-age=31536000; includeSubDomains; preload' always;

	add_header Content-Security-Policy "base-uri https://$server_name; connect-src 'self' https://region1.google-analytics.com; object-src 'none'; script-src 'nonce-$request_id'; script-src-elem https://$server_name https://www.googletagmanager.com/gtag/js 'self' 'unsafe-inline'; img-src https://$server_name 'self' data:; style-src 'self' 'nonce-$request_id';  style-src-elem 'self' https://$server_name 'unsafe-inline'; style-src-attr 'self' https://$server_name 'unsafe-inline' ; frame-src 'self'; frame-ancestors 'self'; font-src 'self'; media-src 'self'; manifest-src 'self' https://$server_name/manifest.webmanifest; form-action 'self'; worker-src https://$server_name/ngsw-worker.js 'self'; default-src 'none';" always;
	add_header X-XSS-Protection "1; mode=block" always;
	add_header X-Frame-Options "SAMEORIGIN" always;
	add_header X-Content-Type-Options "nosniff" always;
	add_header Referrer-Policy "strict-origin" always;
	add_header Permissions-Policy "geolocation=(),midi=(),sync-xhr=(),microphone=(),camera=(),magnetometer=(),gyroscope=(),fullscreen=(self),payment=()" always;

	expires $expires;

	location / {
		index index.html;
		try_files $uri $uri$args $uri$args/ /index.html;
	}

	location ~ ^/static/ {
		root [YOUR API ROOT];
	}

	location ~ ^/media/ {
		root [YOUR API ROOT];
	}

	location ~ ^/api/admin {

		# Protect django admin access
		allow   192.168.1.0/24;
		deny    all;
		proxy_pass http://channels-backend;
		add_header Content-Security-Policy '';

		proxy_http_version 1.1;
		proxy_set_header Upgrade $http_upgrade;
		proxy_set_header Connection "upgrade";
		proxy_set_header SCRIPT_NAME /api;
		proxy_set_header Daphne-Root-Path /api;

		proxy_redirect off;
		proxy_set_header Host $host;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_set_header X-Forwarded-Host $server_name;
	}

	location /api/ {
		proxy_pass http://channels-backend;

		proxy_http_version 1.1;
		proxy_set_header Upgrade $http_upgrade;
		proxy_set_header Connection "upgrade";
		proxy_set_header SCRIPT_NAME /api;
		proxy_set_header Daphne-Root-Path /api;

		proxy_redirect off;
		proxy_set_header Host $host;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_set_header X-Forwarded-Host $server_name;
	}


	# deny access to .htaccess files, if Apache's document root
	# concurs with nginx's one
	#
	location ~ /\.ht {
		deny all;
	}
}

Configurazione di supervisor

[fcgi-program:axiom4]
socket=tcp://127.0.0.1:8000
directory=[YOUR API ROOT]
command=[Pythin VENV BASEDIR]/venv/bin/daphne --root-path [YOUR API ROOT] -u [PATH RUN DIR]/run/axiom4_daphne%(process_num)d.sock --fd 0 --access-log - --proxy-headers axiom4.asgi:application
numprocs=4
process_name=asgi%(process_num)d
autostart=true
autorestart=true
stdout_logfile=[supervisor.log PATH]
redirect_stderr=true
user=[RUN UID]

Test della configurazione di produzione

I seguenti servizi possono essere usati per testare la configurazione di produzione:

Mozilla Observatory
Ssl Labs
Security Headers
Hsts Preload

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *