Compare commits

...

4 commits

Author SHA1 Message Date
7d421ff84d
add tests 2026-04-10 20:19:05 +02:00
f90f936896
fix: format 2026-04-10 20:18:53 +02:00
cebfffbd61
fix: something i have remove when merging 2026-04-10 20:13:37 +02:00
2550cd2509
fix linter 2026-04-10 19:48:19 +02:00
4 changed files with 60 additions and 6 deletions

View file

@ -3105,12 +3105,14 @@ properties:
cert_path:
type: string
description: |
Path to a PEM client certificate file for mutual TLS authentication.
Path to a PEM client certificate file for mutual
TLS authentication.
example: /etc/borgmatic/loki-client.crt
key_path:
type: string
description: |
Path to a PEM private key file for the client certificate.
Path to a PEM private key file for the client
certificate.
example: /etc/borgmatic/loki-client.key
description: |
TLS options for mutual TLS (mTLS) authentication with Loki.

View file

@ -96,7 +96,7 @@ class Loki_log_handler(logging.Handler):
A log handler that sends logs to Loki.
'''
def __init__(self, url, send_logs, dry_run, tls_cert_path=None, tls_key_path=None):
def __init__(self, url, send_logs, log_level, dry_run, tls_cert_path=None, tls_key_path=None):
'''
Given a URL to send logs to, whether all borgmatic logs should be sent (or just explicitly
added messages from this hook), the log level to use (influencing which logs get sent), and
@ -104,7 +104,9 @@ class Loki_log_handler(logging.Handler):
'''
super().__init__()
self.buffer = Loki_log_buffer(url, dry_run, tls_cert_path=tls_cert_path, tls_key_path=tls_key_path)
self.buffer = Loki_log_buffer(
url, dry_run, tls_cert_path=tls_cert_path, tls_key_path=tls_key_path
)
self.send_logs = send_logs
self.setLevel(log_level)
@ -150,11 +152,14 @@ def initialize_monitor(hook_config, config, config_filename, monitoring_log_leve
tls = hook_config.get('tls', {})
if bool(tls.get('cert_path')) != bool(tls.get('key_path')):
raise ValueError('Both cert_path and key_path must be set together for Loki TLS configuration')
raise ValueError(
'Both cert_path and key_path must be set together for Loki TLS configuration'
)
loki = Loki_log_handler(
url,
hook_config.get('send_logs', False),
monitoring_log_level,
dry_run,
tls_cert_path=tls.get('cert_path'),
tls_key_path=tls.get('key_path'),

View file

@ -90,7 +90,7 @@ def test_ping_monitor_sends_log_message():
config_filename = 'test.yaml'
post_called = False
def post(url, data, timeout, headers):
def post(url, data, timeout, headers, **kwargs):
nonlocal post_called
post_called = True

View file

@ -1,5 +1,6 @@
import json
import pytest
from flexmock import flexmock
from borgmatic.hooks.monitoring import loki as module
@ -78,3 +79,49 @@ def test_loki_log_handler_flush_with_empty_buffer_does_not_raise():
handler = module.Loki_log_handler(flexmock(), send_logs=False, log_level=10, dry_run=False)
handler.flush()
def test_loki_log_buffer_init_with_tls_stores_cert_and_key_paths():
buffer = module.Loki_log_buffer(
flexmock(),
dry_run=False,
tls_cert_path='/path/to/cert.crt',
tls_key_path='/path/to/key.key',
)
assert buffer.tls_cert_path == '/path/to/cert.crt'
assert buffer.tls_key_path == '/path/to/key.key'
def test_loki_log_handler_init_with_tls_passes_paths_to_buffer():
handler = module.Loki_log_handler(
flexmock(),
send_logs=False,
log_level=10,
dry_run=False,
tls_cert_path='/path/to/cert.crt',
tls_key_path='/path/to/key.key',
)
assert handler.buffer.tls_cert_path == '/path/to/cert.crt'
assert handler.buffer.tls_key_path == '/path/to/key.key'
def test_initialize_monitor_with_only_cert_path_raises():
hook_config = {
'url': 'http://localhost:3100/loki/api/v1/push',
'tls': {'cert_path': '/path/to/cert.crt'},
}
with pytest.raises(ValueError):
module.initialize_monitor(hook_config, {}, 'test.yaml', 1, False)
def test_initialize_monitor_with_only_key_path_raises():
hook_config = {
'url': 'http://localhost:3100/loki/api/v1/push',
'tls': {'key_path': '/path/to/key.key'},
}
with pytest.raises(ValueError):
module.initialize_monitor(hook_config, {}, 'test.yaml', 1, False)