From d80e716822b9d8d773edee8d5dc71e1a9d3364f0 Mon Sep 17 00:00:00 2001 From: Tom Hubrecht Date: Fri, 24 Feb 2023 16:54:58 +0100 Subject: [PATCH] Add authentication to the ntfy hook --- borgmatic/config/schema.yaml | 10 ++++++++++ borgmatic/hooks/ntfy.py | 11 ++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/borgmatic/config/schema.yaml b/borgmatic/config/schema.yaml index 02bd5d6a..d7acc1c1 100644 --- a/borgmatic/config/schema.yaml +++ b/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: diff --git a/borgmatic/hooks/ntfy.py b/borgmatic/hooks/ntfy.py index c62b5110..fd3b9881 100644 --- a/borgmatic/hooks/ntfy.py +++ b/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: