3

I'm trying to catch authentication errors on a Python client for minio (minio package):

from minio import Minio
from minio.error import MinioError, ResponseError

## Data Lake (Minio)
try :
    minioClient = Minio(endpoint= config['databases']['datalake']['hostip']+":"+config['databases']['datalake']['port'],
                access_key= config['databases']['datalake']['accesskey'],
                secret_key= config['databases']['datalake']['secretkey'],
                secure= False)

    app.logger.info("MinIO Server Connected!")

except MinioError as e:
    app.logger.info("Could not connect to MinIO Server")

I can't seem to be able to catch an authentication error when using fake (wrong) creds. It's always a pass... Any ideas on how to catch these sort of issues?

Leitouran
  • 578
  • 3
  • 16

2 Answers2

2

The Minio() only creates an object, but does not connect to a server. Therefore, the object creation works with fake credentials or fake urls and param also, as this object is not used to connect somewhere for now. Your exception handling only tries to catch raised errors that occur from simple python object creation.

To check the connectivity, I try to connect to a non existing bucket. If I get an error message, everything is fine, if there is a timeout, you can catch it and log it. (You could also try to connect to a existing bucket, but this increases complexity in checking whether there is an error in creating/reaching this bucket or the storage)

#create object

client = Minio(
    host,
    access_key=user,
    secret_key=pass,
    more_access_data=...
)

# reach storage
try:
    if not client.bucket_exists("nonexistingbucket"):
        logging.debug("Object storage connected")
except:
    # raise error if storage not reachable
    logging.critical("Object storage not reachable")
Lithilion
  • 1,097
  • 2
  • 11
  • 26
2

As said above:

To check the connectivity, I try to connect to a non existing bucket.

I don't think that this is intuitive, why don't you use list_buckets() instead, for instance:

from urllib3.exceptions import MaxRetryError

self.config = {
    "endpoint": "localhost:9000",
    "access_key": "minioadmin",
    "secret_key": "minioadmin",
    "secure": False,
    "http_client": urllib3.PoolManager(
        num_pools=10,
        )
    }

try:
    self.client = Minio(**self.config)
    self.client.list_buckets()
except MaxRetryError:
    logging.critical("Object storage not reachable")

It's important to inform that if Minio isn't alive, depending on the context of your application, the startup time will take a little longer than usual.
marcelfox
  • 101
  • 6