Pārlūkot izejas kodu

Merge pull request 'ynh4: fail2ban configuration added' (#34) from logger into main

Reviewed-on: https://codeberg.org/flohmarkt/flohmarkt_ynh/pulls/34
closes: https://codeberg.org/flohmarkt/flohmarkt_ynh/issues/16#issuecomment-1791332
Chris Vogel 1 gadu atpakaļ
vecāks
revīzija
bc8eea5e6d
6 mainītis faili ar 90 papildinājumiem un 9 dzēšanām
  1. 1 1
      conf/systemd.service
  2. 27 0
      logger.json
  3. 3 2
      manifest.toml
  4. 55 0
      run-uvicorn.example
  5. 2 3
      scripts/install
  6. 2 3
      scripts/upgrade

+ 1 - 1
conf/systemd.service

@@ -8,7 +8,7 @@ User=__APP__
 Group=__APP__
 WorkingDirectory=__INSTALL_DIR__/__APP__
 Environment="VENV_DIR=__INSTALL_DIR__/venv/"
-ExecStart=__INSTALL_DIR__/venv/bin/uvicorn --host 127.0.0.1 --port __PORT__ --reload flohmarkt.web:start
+ExecStart=/bin/bash -c "/opt/flohmarkt/venv/bin/uvicorn --host 127.0.0.1 --port 8000 --reload flohmarkt.web:start  2>&1 | /usr/bin/ts '%%Y-%%m-%%d %%H:%%M:%%S'"
 StandardOutput=append:/var/log/__APP__/__APP__.log
 StandardError=inherit
 

+ 27 - 0
logger.json

@@ -0,0 +1,27 @@
+{
+  "version": 1,
+  "disable_existing_loggers": false,
+  "formatters": {
+    "json": {
+      "()": "pythonjsonlogger.jsonlogger.JsonFormatter",
+      "fmt": "%(asctime)s %(name)s %(levelname)s %(message)s"
+    }
+  },
+  "handlers": {
+    "stderr": {
+      "formatter": "json",
+      "class": "logging.StreamHandler",
+      "stream": "ext://sys.stderr"
+    },
+    "stdout": {
+      "formatter": "json",
+      "class": "logging.StreamHandler",
+      "stream": "ext://sys.stdout"
+    }
+  },
+  "loggers": {
+    "uvicorn": {"handlers": ["stderr"], "level": "INFO", "propagate": false},
+    "uvicorn.error": {"level": "INFO"},
+    "uvicorn.access": {"handlers": ["stdout"], "level": "INFO", "propagate": false}
+  }
+}

+ 3 - 2
manifest.toml

@@ -6,7 +6,7 @@ id = "flohmarkt"
 name = "flohmarkt"
 description.en = "A decentral federated small ads platform"
 
-version = "0.0~ynh3"
+version = "0.0~ynh4"
 
 maintainers = ["Chris Vogel"]
 
@@ -156,7 +156,8 @@ ram.runtime = "100M"
 
     [resources.apt]
     # python dependencies shall be installed in a venv using pip.
-    packages = "python3-pip python3-full curl apt-transport-https gnupg"
+	# moreutils is needed for `ts` used in systemd.service
+    packages = "python3-pip python3-full curl apt-transport-https gnupg moreutils"
     # repo for couchdb - doesn't work, yet
     # extras.couchdb.repo = "deb https://apache.jfrog.io/artifactory/couchdb-deb/ __YNH_DEBIAN_VERSION__ main"
     # extras.couchdb.key = "https://couchdb.apache.org/repo/keys.asc"

+ 55 - 0
run-uvicorn.example

@@ -0,0 +1,55 @@
+/opt/flohmarkt/venv/bin/uvicorn --host 127.0.0.1 --port 8000 --reload --log-config logger.json flohmarkt.web:start
+
+Fehler:
+
+Traceback (most recent call last):
+  File "/usr/lib/python3.9/logging/config.py", line 385, in resolve
+    found = self.importer(used)
+ModuleNotFoundError: No module named 'pythonjsonlogger'
+
+The above exception was the direct cause of the following exception:
+
+Traceback (most recent call last):
+  File "/usr/lib/python3.9/logging/config.py", line 543, in configure
+    formatters[name] = self.configure_formatter(
+  File "/usr/lib/python3.9/logging/config.py", line 655, in configure_formatter
+    result = self.configure_custom(config)
+  File "/usr/lib/python3.9/logging/config.py", line 470, in configure_custom
+    c = self.resolve(c)
+  File "/usr/lib/python3.9/logging/config.py", line 398, in resolve
+    raise v
+  File "/usr/lib/python3.9/logging/config.py", line 385, in resolve
+    found = self.importer(used)
+ValueError: Cannot resolve 'pythonjsonlogger.jsonlogger.JsonFormatter': No module named 'pythonjsonlogger'
+
+The above exception was the direct cause of the following exception:
+
+Traceback (most recent call last):
+  File "/opt/flohmarkt/venv/bin/uvicorn", line 8, in <module>
+    sys.exit(main())
+  File "/opt/flohmarkt/venv/lib/python3.9/site-packages/click/core.py", line 1157, in __call__
+    return self.main(*args, **kwargs)
+  File "/opt/flohmarkt/venv/lib/python3.9/site-packages/click/core.py", line 1078, in main
+    rv = self.invoke(ctx)
+  File "/opt/flohmarkt/venv/lib/python3.9/site-packages/click/core.py", line 1434, in invoke
+    return ctx.invoke(self.callback, **ctx.params)
+  File "/opt/flohmarkt/venv/lib/python3.9/site-packages/click/core.py", line 783, in invoke
+    return __callback(*args, **kwargs)
+  File "/opt/flohmarkt/venv/lib/python3.9/site-packages/uvicorn/main.py", line 404, in main
+    run(
+  File "/opt/flohmarkt/venv/lib/python3.9/site-packages/uvicorn/main.py", line 506, in run
+    config = Config(
+  File "/opt/flohmarkt/venv/lib/python3.9/site-packages/uvicorn/config.py", line 299, in __init__
+    self.configure_logging()
+  File "/opt/flohmarkt/venv/lib/python3.9/site-packages/uvicorn/config.py", line 411, in configure_logging
+    logging.config.dictConfig(loaded_config)
+  File "/usr/lib/python3.9/logging/config.py", line 809, in dictConfig
+    dictConfigClass(config).configure()
+  File "/usr/lib/python3.9/logging/config.py", line 546, in configure
+    raise ValueError('Unable to configure '
+ValueError: Unable to configure formatter 'json'
+
+(venv) root@yt:/opt/flohmarkt/flohmarkt# pip install python-json-logger
+Requirement already satisfied: python-json-logger in /usr/lib/python3.9/site-packages (2.0.7)
+
+Ausgabe in json will ich eigentlich auch nicht falls das in der config steht. Der Kram ist aber so dokumentiert, dass ich ihn nicht verstehe...

+ 2 - 3
scripts/install

@@ -122,8 +122,7 @@ chmod 640 "/var/log/$app/$app.log"
 ynh_systemd_action --service_name=$app --action="start"
 
 # SETUP FAIL2BAN
-# @@ todo
-# ynh_script_progression --message="Configuring Fail2Ban..." --weight=3
-# ynh_add_fail2ban_config --logpath="/var/log/$app/$app.log" --failregex="<HOST>:" --max_retry=5
+ynh_script_progression --message="Configuring Fail2Ban..." --weight=3
+ynh_add_fail2ban_config --logpath="/var/log/$app/$app.log" --failregex='INFO: +<HOST>:\d+ - "POST /token HTTP/\d+\.\d+" 403 Forbidden' --max_retry=5
 
 ynh_script_progression --message="Installation of $app completed" --last

+ 2 - 3
scripts/upgrade

@@ -93,9 +93,8 @@ ynh_script_progression --message="Upgrading NGINX web server configuration..." -
 ynh_add_nginx_config
 
 # UPGRADE FAIL2BAN
-# ynh_script_progression --message="Reconfiguring Fail2Ban..." --weight=3
-# Create a dedicated Fail2Ban config
-# ynh_add_fail2ban_config --logpath="/var/log/couchdb/couchdb.log" --failregex="[warning] .*couch_httpd_auth: Authentication failed for user .+ from <HOST>" --max_retry=5
+ynh_script_progression --message="Reconfiguring Fail2Ban..." --weight=3
+ynh_add_fail2ban_config --logpath="/var/log/$app/$app.log" --failregex='INFO: +<HOST>:\d+ - "POST /token HTTP/\d+\.\d+" 403 Forbidden' --max_retry=5
 
 # start flohmarkt
 ynh_script_progression --message="Starting flohmarkt..." --weight=3