Skip to content

Commit

Permalink
(BUGFIX) is_minio_available() now treats http/https correctly
Browse files Browse the repository at this point in the history
Stepped to v2.4.0

Co-authored-by: Patrick van der Leer <[email protected]>
  • Loading branch information
theriverman and patvdleer committed Nov 27, 2020
1 parent 1372e1b commit 25e36a0
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 26 deletions.
45 changes: 25 additions & 20 deletions django_minio_backend/models.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
# Standard python packages
import io
import json
from time import mktime
import mimetypes
import ssl
from datetime import datetime, timedelta
from pathlib import Path
from time import mktime
from typing import Union, List

# noinspection PyPackageRequirements minIO_requirement
import certifi
import minio.definitions
import minio.error
# noinspection PyPackageRequirements minIO_requirement
import urllib3
import mimetypes
# Django packages
from django.core.files import File
from django.core.files.storage import Storage
from django.core.files.uploadedfile import InMemoryUploadedFile
from django.utils.deconstruct import deconstructible
from django.utils.timezone import utc
# Third-party (MinIO) packages
import minio.error
import minio.definitions
from minio import Minio
# Local Packages

from .utils import MinioServerStatus, PrivatePublicMixedError, ConfigurationError, get_setting

__all__ = ['MinioBackend', 'get_iso_date', 'iso_date_prefix', ]
Expand All @@ -42,6 +44,7 @@ class MinioBackend(Storage):
Through self._META_KWARGS, the "metadata", "sse" and "progress" fields can be set
for the underlying put_object() MinIO SDK method
"""

def __init__(self,
bucket_name: str,
*args,
Expand Down Expand Up @@ -232,18 +235,20 @@ def is_minio_available(self) -> MinioServerStatus:
mss.add_message('MINIO_ENDPOINT is not configured in Django settings')
return mss

c = urllib3.HTTPSConnectionPool(self.__MINIO_ENDPOINT, cert_reqs='CERT_NONE', assert_hostname=False)
try:
r = c.request('GET', '/minio/index.html')
return MinioServerStatus(r)
except urllib3.exceptions.MaxRetryError:
mss = MinioServerStatus(None)
mss.add_message(f'Could not open connection to {self.__MINIO_ENDPOINT}/minio/index.html ...')
return mss
except Exception as e:
mss = MinioServerStatus(None)
mss.add_message(repr(e))
return mss
with urllib3.PoolManager(cert_reqs=ssl.CERT_REQUIRED, ca_certs=certifi.where()) as http:
try:
scheme = "https" if self.__MINIO_USE_HTTPS else "http"
r = http.request('GET', f'{scheme}://{self.__MINIO_ENDPOINT}/minio/index.html')
return MinioServerStatus(r)
except urllib3.exceptions.MaxRetryError as e:
mss = MinioServerStatus(None)
mss.add_message(f'Could not open connection to {self.__MINIO_ENDPOINT}/minio/index.html\n'
f'Reason: {e}')
return mss
except Exception as e:
mss = MinioServerStatus(None)
mss.add_message(repr(e))
return mss

@property
def client(self) -> Minio:
Expand Down
12 changes: 8 additions & 4 deletions django_minio_backend/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# noinspection PyPackageRequirements minIO_requirement
import urllib3
from typing import Union, List
from django.conf import settings
Expand Down Expand Up @@ -26,16 +27,19 @@ def __init__(self, request: Union[urllib3.response.HTTPResponse, None]):
self.data = None
self.eval()

self.__OK = 'MinIO is available'
self.___NOK = 'MinIO is NOT available'

def eval(self):
if not self._request:
self._details.append('There was no request provided for MinioServerStatus upon initialisation.')
self.add_message('There was no HTTP request provided for MinioServerStatus upon initialisation.')
return False
self.status = self._request.status
self.data = self._request.data.decode() if self._request.data else 'No data available'
if self.status == 403: # Request was a legal, but the server refuses to respond to it -> it's running fine
self._bool = True
else:
self._details.append('MinIO is not available.')
self._details.append(self.__OK)
self._details.append('Reason: ' + self.data)

def __bool__(self):
Expand All @@ -54,8 +58,8 @@ def details(self):

def __repr__(self):
if self.is_available:
return 'Minio Server Available'
return 'Minio Server Not Available'
return self.__OK
return self.___NOK


class PrivatePublicMixedError(Exception):
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
Django~=3.1.3
minio~=6.0.0
Pillow
urllib3~=1.26.2
setuptools~=50.3.2
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# allow setup.py to be run from any path
os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir)))

CURRENT_GIT_TAG = 'v2.3.1'
CURRENT_GIT_TAG = 'v2.4.0'
year = datetime.datetime.now().year

print(f'setup.py :: Using git tag {CURRENT_GIT_TAG}')
Expand Down

0 comments on commit 25e36a0

Please sign in to comment.