77import ssl
88from pathlib import Path
99import xml .etree .ElementTree as ET
10+ import os
1011
1112
1213class MinioClient :
@@ -20,6 +21,7 @@ def __init__(
2021 secure : bool = False ,
2122 ):
2223 retry_count = 0
24+ st = None # Default session token to None if not using STS
2325 # Try STS auth if access or secret key is not defined
2426 while (access_key == None or secret_key == None ) and retry_count < 5 :
2527 print ("Attempting Minio authentication with STS" )
@@ -45,10 +47,10 @@ def __init__(
4547
4648 def handle_sts_auth (self , sts_endpoint , tenant ):
4749 # Mounted in from the service account to include sts.min.io audience
48- SA_TOKEN_FILE = " /minio/token"
50+ SA_TOKEN_FILE = os . getenv ( "MINIO_SA_TOKEN_PATH" , " /minio/token")
4951
5052 # Kube CA cert path added by mounted service account, needed for TLS with Minio STS
51- KUBE_CA_CRT = " /var/run/secrets/kubernetes.io/serviceaccount/ca.crt"
53+ KUBE_CA_CRT = os . getenv ( "STS_CA_CERT_FILE" , " /var/run/secrets/kubernetes.io/serviceaccount/ca.crt")
5254
5355 # Read service account token
5456 sa_token = Path (SA_TOKEN_FILE ).read_text ().strip ()
@@ -66,7 +68,7 @@ def handle_sts_auth(self, sts_endpoint, tenant):
6668
6769 if response .status != 200 :
6870 print (f"STS request failed: { response .status } { response .data .decode ()} " )
69- return None , None
71+ return None , None , None
7072 else :
7173 root = ET .fromstring (response .data )
7274 ns = {"sts" : "https://sts.amazonaws.com/doc/2011-06-15/" }
@@ -78,11 +80,14 @@ def handle_sts_auth(self, sts_endpoint, tenant):
7880 return access_key , secret_key , session_token
7981
8082 def handle_minio_error (self , error : S3Error ):
81- status = 500
8283 if error ._code in ["NoSuchBucket" , "NoSuchKey" ]:
8384 status = 404
85+ elif error ._code in ["AccessDenied" ]:
86+ status = 403
87+ else :
88+ status = 500
8489
85- raise HTTPException (status_code = status , detail = error )
90+ raise HTTPException (status_code = status , detail = error . message )
8691
8792 def create_bucket (self , name , enable_versioning = False ):
8893 try :
0 commit comments