Skip to content

Commit a6936c3

Browse files
Ubuntubkueng
Ubuntu
authored andcommitted
Add let's encrypt for cron renew SSL certificate, modified default_ssl.conf for challenging let's encrypt, added support nginx to use basic authentication, tested with AWS docker environment. Fix run_pylint.sh and move pylintrc to app. Fix CI/CD. Moved config_default to app for usage w/o docker.
1 parent b91d87c commit a6936c3

14 files changed

+68
-45
lines changed

README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,13 @@ the root directory (Eg `tornado_handlers.py`). Then to make sure the same module
140140
is only loaded once, we use `import xy` instead of `import plot_app.xy`.
141141
It's useful to look at `print('\n'.join(sys.modules.keys()))` to check this.
142142

143-
# Description
143+
# Docker usage
144144

145145
This section explain about how to work with docker.
146146

147-
# Arguments
147+
## Arguments
148+
149+
Edit the `.env` file according to your setup:
148150

149151
- PORT - The number of port, what listen service in docker, default 5006
150152
- USE_PROXY - The set his, if you use reverse proxy (Nginx, ...)
@@ -188,6 +190,8 @@ Remember to Change NGINX_CONF to use default_ssl.conf and add the EMAIL for prod
188190

189191
### Production
190192
```bash
193+
htpasswd -c ./nginx/.htpasswd username
194+
# here to create a .htpasswd for nginx basic authentication
191195
chmod u+x init-letsencrypt.sh
192196
./init-letsencrypt.sh
193197
```

config_default.ini app/config_default.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ domain_name = review.px4.io
55
http_protocol = http
66

77
# path for everything that will be stored on disk (including the database)
8-
storage_path = data
8+
storage_path = ../data
99

1010
# DB file name (if empty, $storage_path/logs.sqlite is used)
1111
db_filename =

app/plot_app/plotted_tables.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ def get_changed_parameters(ulog, plot_width):
481481
is_default = abs(float(system_default) - float(param_value)) < 0.00001
482482
if 'decimal' in default_param:
483483
param_value = round(param_value, int(default_param['decimal']))
484-
airframe_default = round(float(airframe_default), int(default_param['decimal']))
484+
airframe_default = round(float(airframe_default), int(default_param['decimal'])) #pylint: disable=line-too-long
485485
else:
486486
is_default = int(system_default) == int(param_value)
487487
if not is_default:

pylintrc app/pylintrc

File renamed without changes.

app/serve.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313
from bokeh.server.server import Server
1414
from bokeh.application.handlers import DirectoryHandler
1515

16-
from tornado.web import StaticFileHandler
16+
1717

1818
# this is needed for the following imports
1919
sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'plot_app'))
20+
from tornado.web import StaticFileHandler
2021
from tornado.web import RedirectHandler
2122
from tornado_handlers.download import DownloadHandler
2223
from tornado_handlers.upload import UploadHandler
@@ -27,8 +28,8 @@
2728
from tornado_handlers.radio_controller import RadioControllerHandler
2829
from tornado_handlers.error_labels import UpdateErrorLabelHandler
2930

30-
from helper import set_log_id_is_filename, print_cache_info
31-
from config import debug_print_timing, get_overview_img_filepath
31+
from helper import set_log_id_is_filename, print_cache_info #pylint: disable=C0411
32+
from config import debug_print_timing, get_overview_img_filepath #pylint: disable=C0411
3233

3334
#pylint: disable=invalid-name
3435

docker-compose.dev.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ services:
99
- 5006:5006
1010
volumes:
1111
- ./app:/opt/service/
12-
- ./data:/opt/service/data
13-
- ${PWD}/config_default.ini:/opt/service/config_default.ini:ro # Absolute for volume a file.
12+
- ./data:/opt/data
13+
- ${PWD}/app/config_default.ini:/opt/service/config_default.ini:ro # Absolute for volume a file.
1414
restart: always

docker-compose.prod.yml

+18-15
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,45 @@ services:
55
build:
66
context: ./app
77
dockerfile: Dockerfile
8-
env_file: .env_prod
8+
env_file: .env
99
volumes:
10-
- ./data:/opt/service/data
10+
- ./data:/opt/data
1111
# Absolute for volume a file.
12-
- ${PWD}/config_default.ini:/opt/service/config_default.ini:ro
13-
- ${PWD}/config_user.ini:/opt/service/config_user.ini:ro
14-
restart: always
15-
certbot:
16-
image: certbot/certbot
17-
volumes:
18-
- ./data/certbot/conf:/etc/letsencrypt
19-
- ./data/certbot/www:/var/www/certbot
20-
- ./logs/letsencrypt:/var/log/letsencrypt
21-
- /etc/localtime:/etc/localtime:ro # for synchronize with host timezone
22-
entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'"
12+
- ${PWD}/app/config_default.ini:/opt/service/config_default.ini:ro
13+
- ${PWD}/app/config_user.ini:/opt/service/config_user.ini:ro
2314
restart: always
2415
nginx:
2516
build:
2617
context: ./nginx
2718
dockerfile: Dockerfile
2819
args:
2920
- NGINX_CONF=${NGINX_CONF}
30-
env_file: .env_prod
21+
env_file: .env
3122
ports:
3223
- 80:80
3324
- 443:443
3425
volumes:
3526
- ./data/certbot/conf:/etc/letsencrypt
3627
- ./data/certbot/www:/var/www/certbot
3728
- ./logs/nginx:/var/log/nginx/
29+
- ${PWD}/nginx/.htpasswd:/etc/nginx/.htpasswd # for nginx basic authentication
3830
- /etc/localtime:/etc/localtime:ro # for synchronize with host timezone
3931
command: "/bin/sh -c 'while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx -g \"daemon off;\"'"
4032
links:
4133
- flight_review_app
4234
depends_on:
4335
- flight_review_app
4436
restart: always
45-
37+
certbot:
38+
build:
39+
context: ./letsencrypt
40+
dockerfile: Dockerfile
41+
volumes:
42+
- ./data/certbot/conf:/etc/letsencrypt
43+
- ./data/certbot/www:/var/www/certbot
44+
- ./logs/letsencrypt:/var/log/letsencrypt
45+
- /etc/localtime:/etc/localtime:ro # for synchronize with host timezone
46+
entrypoint: "/bin/sh -c 'trap exit TERM; crond -f'"
47+
# tty: true
48+
restart: always
4649

docker-compose.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ services:
77
dockerfile: Dockerfile
88
env_file: .env
99
volumes:
10-
- ./data:/opt/service/data
11-
- ${PWD}/config_default.ini:/opt/service/config_default.ini:ro # Absolute for volume a file.
10+
- ./data:/opt/data
11+
- ${PWD}/app/config_default.ini:/opt/service/config_default.ini:ro # Absolute for volume a file.
1212
restart: always
1313
nginx:
1414
build:

init-letsencrypt.sh

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ if ! [ -x "$(command -v docker-compose -f docker-compose.prod.yml)" ]; then
55
exit 1
66
fi
77

8-
export $(cat .env_prod | grep -v ^\# | xargs); # get variable from .env file
8+
. .env # get variable from .env file
99

10-
domain=(${DOMAIN} www.${DOMAIN})
10+
domain=${DOMAIN} # www.${DOMAIN})
1111
rsa_key_size=4096
1212
cert_path=${CERT_PATH}
1313
email=${EMAIL} # Adding a valid address is strongly recommended
@@ -21,6 +21,7 @@ if [ -d "$cert_path" ]; then
2121
fi
2222

2323

24+
# Download TLS parameters
2425
if [ ! -e "$cert_path/conf/options-ssl-nginx.conf" ] || [ ! -e "$cert_path/conf/ssl-dhparams.pem" ]; then
2526
echo "### Downloading recommended TLS parameters ..."
2627
mkdir -p "$cert_path/conf"
@@ -80,3 +81,5 @@ echo
8081

8182
echo "### Reloading nginx ..."
8283
docker-compose -f docker-compose.prod.yml exec nginx nginx -s reload
84+
docker-compose -f docker-compose.prod.yml build certbot
85+
docker-compose -f docker-compose.prod.yml up -d

letsencrypt/Dockerfile

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
FROM certbot/certbot
2+
ADD letsencrypt-renew /var/spool/cron/crontabs/root

letsencrypt/letsencrypt-renew

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# renew every week
2+
0 12 * * */1 certbot renew

nginx/Dockerfile

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
FROM nginx
22
ARG NGINX_CONF
3+
RUN echo ${NGINX_CONF}
34
COPY ./${NGINX_CONF} /etc/nginx/conf.d/default.conf

nginx/default_ssl.conf

+19-13
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,40 @@ upstream flight_review_app {
55

66
server {
77
listen 80;
8-
server_name example.org;
8+
server_name review.px4.io;
9+
location ^~ /.well-known/acme-challenge/ {
10+
root /var/www/certbot;
11+
allow all;
12+
}
913
location / {
10-
return 301 https://$host$request_uri;
11-
}
14+
return 307 https://$host$request_uri;
15+
}
1216
}
1317

1418
server {
15-
listen 443 ssl;
16-
server_name example.org;
19+
listen 443 ssl http2;
20+
server_name review.px4.io;
1721
access_log /var/log/nginx/access.log;
1822
error_log /var/log/nginx/error.log;
19-
ssl_certificate /etc/letsencrypt/live/example.org/fullchain.pem;
20-
ssl_certificate_key /etc/letsencrypt/live/example.org/privkey.pem;
23+
ssl_certificate /etc/letsencrypt/live/review.px4.io/fullchain.pem;
24+
ssl_certificate_key /etc/letsencrypt/live/review.px4.io/privkey.pem;
2125
include /etc/letsencrypt/options-ssl-nginx.conf;
2226
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
27+
proxy_connect_timeout 180s;
28+
proxy_read_timeout 180s;
29+
proxy_send_timeout 180s;
30+
charset utf-8;
2331

2432
client_max_body_size 100M;
2533
location / {
34+
proxy_request_buffering off;
2635
proxy_pass http://flight_review_app;
2736
proxy_set_header Upgrade $http_upgrade;
2837
proxy_set_header Connection "upgrade";
2938
proxy_http_version 1.1;
3039
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
31-
proxy_set_header Host $host;#:$server_port;
32-
proxy_buffering off;
40+
proxy_set_header Host $host; #:$server_port;
41+
auth_basic "Restricted"; # message to show when authentication error
42+
auth_basic_user_file /etc/nginx/.htpasswd; # .htpasswd path
3343
}
34-
35-
location /.well-known/acme-challenge/ {
36-
root /var/www/certbot;
37-
}
3844
}

run_pylint.sh

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ pylint_exec=$(which pylint 2>/dev/null)
77

88
set -e
99

10-
export PYTHONPATH=app/plot_app
11-
python $pylint_exec app/tornado_handlers/*.py app/serve.py \
12-
app/plot_app/*.py app/download_logs.py
13-
10+
pushd app
11+
export PYTHONPATH=plot_app
12+
python $pylint_exec tornado_handlers/*.py serve.py \
13+
plot_app/*.py download_logs.py
14+
popd
1415
exit 0

0 commit comments

Comments
 (0)