summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorname <email here>2024-08-14 14:39:18 +0800
committername <email here>2024-08-14 14:39:18 +0800
commitb1f88b682624e85b4b743343dfaaeed113b69413 (patch)
treef5944621027aa5372782287041f07799ba57ed71
pushing configs, no automated setup yet
-rw-r--r--docs84
-rw-r--r--gunicorn1.service14
-rw-r--r--hsm.conf22
-rw-r--r--nginx.conf63
4 files changed, 183 insertions, 0 deletions
diff --git a/docs b/docs
new file mode 100644
index 0000000..68ced06
--- /dev/null
+++ b/docs
@@ -0,0 +1,84 @@
1
2nginx handles mtls, and allows connection to local gunicorn instance. If mTLS fails, connection fails and event is logged
3
4
5 ubuntu
6sudo pip install Flask gunicorn # to install for all users, especially www-data so that the service runs
7 nginx
8install nginx
9sudo systemctl enable --now nginx
10 # cat /var/log/nginx/access.log
11 # /etc/nginx/nginx.conf
12 gunicorn
13gunicorn --bind localhost:5000 app:app # for testing
14sudo nano /etc/systemd/system/gunicorn1.service # as a service
15sudo systemctl enable --now gunicorn1 # as a service
16 python
17app1.py
18sudo mkdir /var/www
19sudo chown -R www-data:www-data /var/www
20sudo cp app1.py /var/www
21
22
23 test
24# private key
25openssl genrsa -out ca.key 2048
26# public certificate
27openssl req -x509 -new -nodes -key ca.key -sha256 -days 1024 -out ca.pem -subj "/C=US/ST=YourState/L=YourCity/O=YourOrg/CN=YourCA"
28
29# server private key
30openssl genrsa -out server.key 2048
31# generate certificate signing request
32openssl req -new -key server.key -out server.csr -subj "/C=US/ST=YourState/L=YourCity/O=YourOrg/CN=localhost"
33# use public and private key of the ca to sign the cert signing request
34openssl x509 -req -in server.csr -CA ca.pem -CAkey ca.key -CAcreateserial -out server.crt -days 365 -sha256
35
36openssl genrsa -out client.key 2048
37openssl req -new -key client.key -out client.csr -subj "/C=US/ST=YourState/L=YourCity/O=YourOrg/CN=client"
38openssl x509 -req -in client.csr -CA ca.pem -CAkey ca.key -CAcreateserial -out client.crt -days 365 -sha256
39
40openssl genrsa -out wrong_client.key 2048
41openssl req -new -x509 -key wrong_client.key -out wrong_client.crt -days 365 -subj "/C=US/ST=State/L=City/O=Organization/CN=incorrectclient"
42
43curl https://127.0.0.1 --cacert ca.pem --cert wrong_client.crt --key wrong_client.key -k
44curl https://127.0.0.1 --cacert ca.pem --cert client.crt --key client.key -k
45
46
47 untested
48sudo cp /path/to/ca.pem /usr/local/share/ca-certificates/your-ca.crt
49sudo update-ca-certificates
50 nginx configuration
51 ocsp server to check that the server is valid
52 crl to check if a client is revoked
53server {
54 listen 443 ssl;
55 server_name yourdomain.com;
56
57 ssl_certificate /path/to/your/server.crt;
58 ssl_certificate_key /path/to/your/server.key;
59
60 # Client certificate verification
61 ssl_client_certificate /path/to/your/ca.pem;
62 ssl_verify_client on;
63
64 # Enable OCSP stapling and strict verification
65 ssl_stapling on;
66 ssl_stapling_verify on;
67 ssl_trusted_certificate /path/to/your/ca.pem;
68
69 # Specify resolver for OCSP stapling
70 resolver 8.8.8.8 8.8.4.4 valid=300s;
71 resolver_timeout 10s;
72
73 # Enforce OCSP response checking strictly
74 ssl_ocsp on;
75 ssl_ocsp_fail closed;
76
77 # Specify CRL file for client certificate revocation checking
78 ssl_crl /etc/nginx/ssl/crl.pem;
79
80 location / {
81 try_files $uri $uri/ =404;
82 }
83}
84
diff --git a/gunicorn1.service b/gunicorn1.service
new file mode 100644
index 0000000..425c453
--- /dev/null
+++ b/gunicorn1.service
@@ -0,0 +1,14 @@
1
2[Unit]
3Description=gunicorn1
4After=network.target
5
6[Service]
7User=www-data
8Group=www-data
9WorkingDirectory=/var/www
10ExecStart=/usr/local/bin/gunicorn --workers 3 --bind 0.0.0.0:5000 app1:app
11
12[Install]
13WantedBy=multi-user.target
14
diff --git a/hsm.conf b/hsm.conf
new file mode 100644
index 0000000..af27cf0
--- /dev/null
+++ b/hsm.conf
@@ -0,0 +1,22 @@
1# PKCS11 engine config
2openssl_conf = openssl_def
3
4[openssl_def]
5engines = engine_section
6
7[req]
8distinguished_name = req_distinguished_name
9
10[req_distinguished_name]
11# empty.
12
13[engine_section]
14pkcs11 = pkcs11_section
15
16[pkcs11_section]
17engine_id = pkcs11
18dynamic_path = /usr/lib/x86_64-linux-gnu/engines-1.1/libpkcs11.so
19MODULE_PATH = /usr/lib/x86_64-linux-gnu/pkcs11/opensc-pkcs11.so #ubuntu
20#MODULE_PATH = /usr/lib64/pkcs11/opensc-pkcs11.so #fedora/rocky
21PIN = 648219
22init = 0
diff --git a/nginx.conf b/nginx.conf
new file mode 100644
index 0000000..fc11627
--- /dev/null
+++ b/nginx.conf
@@ -0,0 +1,63 @@
1
2user nginx;
3worker_processes auto;
4
5error_log /var/log/nginx/error.log notice;
6pid /var/run/nginx.pid;
7
8
9events {
10 worker_connections 1024;
11}
12
13
14http {
15 include /etc/nginx/mime.types;
16 default_type application/octet-stream;
17
18
19 log_format main '$remote_addr - $remote_user [$time_local] "$request" '
20 '$status $body_bytes_sent "$http_referer" '
21 '"$http_user_agent" "$http_x_forwarded_for" '
22 'ssl_protocol:$ssl_protocol ssl_cipher:$ssl_cipher '
23 'ssl_client_verify:$ssl_client_verify '
24 'ssl_client_s_dn:$ssl_client_s_dn';
25 access_log /var/log/nginx/access.log main;
26
27 sendfile on;
28 #tcp_nopush on;
29
30 keepalive_timeout 65;
31
32 #gzip on;
33
34# include /etc/nginx/conf.d/*.conf;
35
36 server {
37 location / {
38 return 301 https://$host$request_uri;
39 #root /data/www;
40 #autoindex on;
41 #autoindex_exact_size off;
42 }
43 }
44 server {
45 listen 443 ssl;
46 server_name localhost;
47
48 ssl_certificate /home/x/auths1/server.crt;
49 ssl_certificate_key /home/x/auths1/server.key;
50 ssl_client_certificate /home/x/auths1/ca.pem;
51 ssl_verify_client on;
52
53 location / {
54 proxy_pass http://localhost:5000;
55 proxy_set_header Host $host;
56 proxy_set_header X-Real-IP $remote_addr;
57 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
58 proxy_set_header X-Forwarded-Proto $scheme;
59
60 }
61 }
62
63}