Преглед изворни кода

Add authentication to the ntfy hook

Tom Hubrecht пре 2 година
родитељ
комит
d80e716822
2 измењених фајлова са 20 додато и 1 уклоњено
  1. 10 0
      borgmatic/config/schema.yaml
  2. 10 1
      borgmatic/hooks/ntfy.py

+ 10 - 0
borgmatic/config/schema.yaml

@@ -1029,6 +1029,16 @@ properties:
                         description: |
                             The address of your self-hosted ntfy.sh instance.
                         example: https://ntfy.your-domain.com
+                    username:
+                        type: string
+                        description: |
+                            The username used for authentication.
+                        example: testuser
+                    password:
+                        type: string
+                        description: |
+                            The password used for authentication.
+                        example: fakepassword
                     start:
                         type: object
                         properties:

+ 10 - 1
borgmatic/hooks/ntfy.py

@@ -56,10 +56,19 @@ def ping_monitor(hook_config, config_filename, state, monitoring_log_level, dry_
             'X-Tags': state_config.get('tags'),
         }
 
+        username = hook_config.get('username')
+        password = hook_config.get('password')
+
+        auth = (
+            requests.auth.HTTPBasicAuth(username, password)
+            if (username and password) is not None
+            else None
+        )
+
         if not dry_run:
             logging.getLogger('urllib3').setLevel(logging.ERROR)
             try:
-                response = requests.post(f'{base_url}/{topic}', headers=headers)
+                response = requests.post(f'{base_url}/{topic}', headers=headers, auth=auth)
                 if not response.ok:
                     response.raise_for_status()
             except requests.exceptions.RequestException as error: