Skip to content

Commit 6942818

Browse files
authored
Provide a custom CA Bundle to enable handling of self-signed SSL certs, or disable SSL verification entirely (#13)
1 parent df648d5 commit 6942818

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

logging_loki/emitter.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import threading
88
import time
99
from logging.config import ConvertingDict
10-
from typing import Any, Callable, Dict, Optional, Tuple
10+
from typing import Any, Callable, Dict, Optional, Tuple, Union
1111

1212
import requests
1313

@@ -45,7 +45,8 @@ def __init__(self,
4545
as_json: bool = False,
4646
props_to_labels: Optional[list[str]] = None,
4747
level_tag: Optional[str] = const.level_tag,
48-
logger_tag: Optional[str] = const.logger_tag
48+
logger_tag: Optional[str] = const.logger_tag,
49+
verify: Union[bool, str] = True
4950
):
5051
"""
5152
Create new Loki emitter.
@@ -72,6 +73,8 @@ def __init__(self,
7273
self.level_tag: str = level_tag
7374
#: Label name indicating logger name.
7475
self.logger_tag: str = logger_tag
76+
# verify param to be past to requests, can be a bool (to enable/disable SSL verification) or a path to a CA bundle
77+
self.verify = verify
7578

7679
self._session: Optional[requests.Session] = None
7780
self._lock = threading.Lock()
@@ -95,6 +98,7 @@ def session(self) -> requests.Session:
9598
if self._session is None:
9699
self._session = self.session_class()
97100
self._session.auth = self.auth or None
101+
self._session.verify = self.verify
98102
return self._session
99103

100104
def close(self):

logging_loki/handlers.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ def __init__(
7070
as_json: Optional[bool] = False,
7171
props_to_labels: Optional[list[str]] = None,
7272
level_tag: Optional[str] = const.level_tag,
73-
logger_tag: Optional[str] = const.logger_tag
73+
logger_tag: Optional[str] = const.logger_tag,
74+
verify: Union[bool, str] = True
7475
):
7576
"""
7677
Create new Loki logging handler.
@@ -84,10 +85,11 @@ def __init__(
8485
props_to_labels: List of properties that should be converted to loki labels.
8586
level_tag: Label name indicating logging level.
8687
logger_tag: Label name indicating logger name.
88+
verify: Either a boolean, in which case it controls whether we verify the server's TLS certificate, or a string, in which case it must be a path to a CA bundle to use.
8789
8890
"""
8991
super().__init__()
90-
self.emitter = LokiEmitter(url, tags, headers, auth, as_json, props_to_labels, level_tag, logger_tag)
92+
self.emitter = LokiEmitter(url, tags, headers, auth, as_json, props_to_labels, level_tag, logger_tag, verify)
9193

9294
def handleError(self, exc: Exception): # noqa: N802
9395
"""Close emitter and let default handler take actions on error."""

0 commit comments

Comments
 (0)