Skip to content

Commit

Permalink
minioadmin: fix http trace properly (#1438)
Browse files Browse the repository at this point in the history
Signed-off-by: Bala.FA <[email protected]>
  • Loading branch information
balamurugana authored Aug 22, 2024
1 parent f673f09 commit b7da8a8
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions minio/minioadmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import os
from datetime import timedelta
from enum import Enum, unique
from typing import TextIO, Tuple, cast
from typing import Any, TextIO, Tuple, cast
from urllib.parse import urlunsplit

import certifi
Expand Down Expand Up @@ -96,6 +96,14 @@ class _COMMAND(Enum):
SERVICE_ACCOUNT_DELETE = "delete-service-account"


def _safe_str(value: Any) -> str:
"""Convert to string safely"""
try:
return value.decode() if isinstance(value, bytes) else str(value)
except UnicodeDecodeError:
return value.hex()


class MinioAdmin:
"""Client to perform MinIO administration operations."""

Expand Down Expand Up @@ -202,9 +210,7 @@ def _url_open(
self._trace_stream.write("\n")
if body is not None:
self._trace_stream.write("\n")
self._trace_stream.write(
body.decode() if isinstance(body, bytes) else str(body),
)
self._trace_stream.write(_safe_str(body))
self._trace_stream.write("\n")
self._trace_stream.write("\n")

Expand All @@ -230,15 +236,19 @@ def _url_open(
headers_to_strings(response.headers),
)
self._trace_stream.write("\n")
self._trace_stream.write("\n")
self._trace_stream.write(response.data.decode())
self._trace_stream.write("\n")
if preload_content:
self._trace_stream.write("\n")
self._trace_stream.write(_safe_str(response.data))
self._trace_stream.write("\n")
self._trace_stream.write("----------END-HTTP----------\n")

if response.status in [200, 204, 206]:
return response

raise MinioAdminException(str(response.status), response.data.decode())
raise MinioAdminException(
str(response.status),
_safe_str(response.data),
)

def set_app_info(self, app_name: str, app_version: str):
"""
Expand Down

0 comments on commit b7da8a8

Please sign in to comment.