Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions gprofiler/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
import json
from io import BytesIO
from typing import IO, TYPE_CHECKING, Any, Dict, List, Optional, Tuple, cast
from urllib.parse import quote_plus

import requests
from requests import Session
from requests import RequestException, Session

from gprofiler import __version__
from gprofiler.exceptions import APIError
Expand Down Expand Up @@ -168,9 +169,15 @@ def _send_request(
api_version: str = None,
params: Dict[str, str] = None,
) -> Dict:
return self._request_url(
method, "{}/{}".format(self.get_base_url(api_version), path), data, files, timeout, params
)
try:
return self._request_url(
method, "{}/{}".format(self.get_base_url(api_version), path), data, files, timeout, params
)
except RequestException as e:
# requests exceptions embed the request URL, which contains the token ("key=<token>") - redact it
# so it doesn't leak when the exception is logged.
e.args = (str(e).replace(quote_plus(self._key), "***").replace(self._key, "***"),)
raise e from None

def get(self, path: str, data: Any = None, **kwargs: Any) -> Dict:
return self._send_request("GET", path, data, **kwargs)
Expand Down
18 changes: 16 additions & 2 deletions gprofiler/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import re
import sys
import time
from io import TextIOWrapper
from logging import LogRecord
from typing import Any, Dict, Mapping, Optional
from urllib.parse import urlparse
Expand Down Expand Up @@ -137,6 +138,17 @@ class GProfilerFormatter(_ExtraFormatter, _UTCFormatter):
pass


class _PrivateRotatingFileHandler(logging.handlers.RotatingFileHandler):
def _open(self) -> TextIOWrapper:
# applies to the initial file, an existing file left by an older version, and files created on rotation
stream = super()._open()
Comment thread
mlim19 marked this conversation as resolved.
Resolved
try:
os.chmod(self.baseFilename, 0o600)
Comment thread
mlim19 marked this conversation as resolved.
Resolved
except OSError:
pass # e.g. rootless run with a writable log file owned by another user - keep logging as before
return stream


def initial_root_logger_setup(
stream_level: int,
log_file_path: str,
Expand All @@ -152,8 +164,10 @@ def initial_root_logger_setup(
stream_handler.setFormatter(GProfilerFormatter(LOGS_FORMAT))
logger_adapter.logger.addHandler(stream_handler)

os.makedirs(os.path.dirname(log_file_path), exist_ok=True)
file_handler = logging.handlers.RotatingFileHandler(
# logs may contain sensitive data: a log dir we create is owner-only (an existing dir, e.g. $HOME when running
# rootless with --log-file, is left as is), and log files are owner-only.
os.makedirs(os.path.dirname(log_file_path), mode=0o700, exist_ok=True)
file_handler = _PrivateRotatingFileHandler(
log_file_path,
maxBytes=rotate_max_bytes,
backupCount=rotate_backup_count,
Expand Down
Loading