diff options
author | Nejc Habjan <nejc.habjan@siemens.com> | 2022-07-30 14:47:01 -0700 |
---|---|---|
committer | John L. Villalovos <john@sodarock.com> | 2022-07-30 14:47:01 -0700 |
commit | dacf8a3269f2545bf442449da60817bd6a23a7df (patch) | |
tree | de31d2638b59e6571cecb24d35c639eb0701a3a0 /gitlab/utils.py | |
parent | 17414f787a70a0d916193ac71bccce0297c4e4e8 (diff) | |
download | gitlab-jlvillal/logging-mask-tokens.tar.gz |
feat(client): mask tokens by default when loggingjlvillal/logging-mask-tokens
Diffstat (limited to 'gitlab/utils.py')
-rw-r--r-- | gitlab/utils.py | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/gitlab/utils.py b/gitlab/utils.py index f3d97f7..7944f4c 100644 --- a/gitlab/utils.py +++ b/gitlab/utils.py @@ -15,6 +15,7 @@ # You should have received a copy of the GNU Lesser General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. +import logging import pathlib import traceback import urllib.parse @@ -31,6 +32,29 @@ class _StdoutStream: print(chunk) +class MaskingFormatter(logging.Formatter): + """A logging formatter that can mask credentials""" + + def __init__( + self, + *args: Any, + masked: Optional[str] = None, + **kwargs: Any, + ) -> None: + super().__init__(*args, **kwargs) + self.masked = masked + + def _filter(self, entry: str) -> str: + if not self.masked: + return entry + + return entry.replace(self.masked, "[MASKED]") + + def format(self, record: logging.LogRecord) -> str: + original = logging.Formatter.format(self, record) + return self._filter(original) + + def response_content( response: requests.Response, streamed: bool, |