diff options
| author | name <email here> | 2024-08-14 14:39:18 +0800 |
|---|---|---|
| committer | name <email here> | 2024-08-14 14:39:18 +0800 |
| commit | b1f88b682624e85b4b743343dfaaeed113b69413 (patch) | |
| tree | f5944621027aa5372782287041f07799ba57ed71 | |
pushing configs, no automated setup yet
| -rw-r--r-- | docs | 84 | ||||
| -rw-r--r-- | gunicorn1.service | 14 | ||||
| -rw-r--r-- | hsm.conf | 22 | ||||
| -rw-r--r-- | nginx.conf | 63 |
4 files changed, 183 insertions, 0 deletions
| @@ -0,0 +1,84 @@ | |||
| 1 | |||
| 2 | nginx handles mtls, and allows connection to local gunicorn instance. If mTLS fails, connection fails and event is logged | ||
| 3 | |||
| 4 | |||
| 5 | ubuntu | ||
| 6 | sudo pip install Flask gunicorn # to install for all users, especially www-data so that the service runs | ||
| 7 | nginx | ||
| 8 | install nginx | ||
| 9 | sudo systemctl enable --now nginx | ||
| 10 | # cat /var/log/nginx/access.log | ||
| 11 | # /etc/nginx/nginx.conf | ||
| 12 | gunicorn | ||
| 13 | gunicorn --bind localhost:5000 app:app # for testing | ||
| 14 | sudo nano /etc/systemd/system/gunicorn1.service # as a service | ||
| 15 | sudo systemctl enable --now gunicorn1 # as a service | ||
| 16 | python | ||
| 17 | app1.py | ||
| 18 | sudo mkdir /var/www | ||
| 19 | sudo chown -R www-data:www-data /var/www | ||
| 20 | sudo cp app1.py /var/www | ||
| 21 | |||
| 22 | |||
| 23 | test | ||
| 24 | # private key | ||
| 25 | openssl genrsa -out ca.key 2048 | ||
| 26 | # public certificate | ||
| 27 | openssl 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 | ||
| 30 | openssl genrsa -out server.key 2048 | ||
| 31 | # generate certificate signing request | ||
| 32 | openssl 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 | ||
| 34 | openssl x509 -req -in server.csr -CA ca.pem -CAkey ca.key -CAcreateserial -out server.crt -days 365 -sha256 | ||
| 35 | |||
| 36 | openssl genrsa -out client.key 2048 | ||
| 37 | openssl req -new -key client.key -out client.csr -subj "/C=US/ST=YourState/L=YourCity/O=YourOrg/CN=client" | ||
| 38 | openssl x509 -req -in client.csr -CA ca.pem -CAkey ca.key -CAcreateserial -out client.crt -days 365 -sha256 | ||
| 39 | |||
| 40 | openssl genrsa -out wrong_client.key 2048 | ||
| 41 | openssl 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 | |||
| 43 | curl https://127.0.0.1 --cacert ca.pem --cert wrong_client.crt --key wrong_client.key -k | ||
| 44 | curl https://127.0.0.1 --cacert ca.pem --cert client.crt --key client.key -k | ||
| 45 | |||
| 46 | |||
| 47 | untested | ||
| 48 | sudo cp /path/to/ca.pem /usr/local/share/ca-certificates/your-ca.crt | ||
| 49 | sudo 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 | ||
| 53 | server { | ||
| 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] | ||
| 3 | Description=gunicorn1 | ||
| 4 | After=network.target | ||
| 5 | |||
| 6 | [Service] | ||
| 7 | User=www-data | ||
| 8 | Group=www-data | ||
| 9 | WorkingDirectory=/var/www | ||
| 10 | ExecStart=/usr/local/bin/gunicorn --workers 3 --bind 0.0.0.0:5000 app1:app | ||
| 11 | |||
| 12 | [Install] | ||
| 13 | WantedBy=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 | ||
| 2 | openssl_conf = openssl_def | ||
| 3 | |||
| 4 | [openssl_def] | ||
| 5 | engines = engine_section | ||
| 6 | |||
| 7 | [req] | ||
| 8 | distinguished_name = req_distinguished_name | ||
| 9 | |||
| 10 | [req_distinguished_name] | ||
| 11 | # empty. | ||
| 12 | |||
| 13 | [engine_section] | ||
| 14 | pkcs11 = pkcs11_section | ||
| 15 | |||
| 16 | [pkcs11_section] | ||
| 17 | engine_id = pkcs11 | ||
| 18 | dynamic_path = /usr/lib/x86_64-linux-gnu/engines-1.1/libpkcs11.so | ||
| 19 | MODULE_PATH = /usr/lib/x86_64-linux-gnu/pkcs11/opensc-pkcs11.so #ubuntu | ||
| 20 | #MODULE_PATH = /usr/lib64/pkcs11/opensc-pkcs11.so #fedora/rocky | ||
| 21 | PIN = 648219 | ||
| 22 | init = 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 | |||
| 2 | user nginx; | ||
| 3 | worker_processes auto; | ||
| 4 | |||
| 5 | error_log /var/log/nginx/error.log notice; | ||
| 6 | pid /var/run/nginx.pid; | ||
| 7 | |||
| 8 | |||
| 9 | events { | ||
| 10 | worker_connections 1024; | ||
| 11 | } | ||
| 12 | |||
| 13 | |||
| 14 | http { | ||
| 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 | } | ||
