Skip to content

Commit d81c7a0

Browse files
authored
remove hardcoded urls and move it to env (#170)
* remove hardcoded urls and move it to env * fix minio bucket ssl issue
1 parent c04e14f commit d81c7a0

File tree

6 files changed

+30
-17
lines changed

6 files changed

+30
-17
lines changed

.env.sample

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,17 @@ POSTGRES_HOST=10021
3434
DATABASE_URL="postgresql://postgresql:yoursupersecret@localhost:10021/templaterdb?schema=public"
3535

3636
# minio
37+
MINIO_URL=
3738
MINIO_USER=root
3839
MINIO_PASSWORD=password
3940

4041
# wkhtmltopdf
4142
WKHTMLTOPDF="/usr/bin/wkhtmltopdf"
4243

4344
# FA
44-
MINIO_FA_APPLICATION_ID=
45-
MINIO_FA_AUTHORIZATION=
45+
FA_APPLICATION_ID=
46+
FA_AUTHORIZATION=
47+
FA_URL=
4648

4749
# Google Client
4850
GC_CLIENT_ID=

src/pdf/plugins/_doc/external.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,12 @@ def upload_file(self):
5151
drive_file_loc = f'pdf/drivefiles/{self.token}.docx'
5252
if self.uploader == "minio":
5353
host = self.user_config["MINIO_HOST"]
54+
isHttps = self.user_config.get("MINIO_IS_HTTPS", False)
5455
access_key = self.user_config["MINIO_ACCESS_KEY"]
5556
secret_key = self.user_config["MINIO_SECRET_KEY"]
5657
bucket_name = self.user_config["MINIO_BUCKET_NAME"]
57-
uploader = MinioUploader(host, access_key, secret_key, bucket_name)
58+
cred_expiry_duration = self.user_config.get("MINIO_CREDENTIAL_EXPIRY_DURATION")
59+
uploader = MinioUploader(host, access_key, secret_key, bucket_name, cred_expiry_duration, isHttps)
5860
error_code, error_msg, final_data = uploader.put(f'{self.token}.docx', f'{self.token}.docx', None)
5961
if error_code is None:
6062
if os.path.exists(drive_file_loc):
@@ -69,4 +71,4 @@ def upload_file(self):
6971
error_code = 805
7072
error_msg = f"Something went wrong: {e}"
7173
finally:
72-
return error_code, error_msg, final_data
74+
return error_code, error_msg, final_data

src/pdf/plugins/_html/external.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ def upload_file(self):
3636
drive_file_loc = f'pdf/drivefiles/{self.token}.html'
3737
if self.uploader == "minio":
3838
host = self.user_config["MINIO_HOST"]
39+
isHttps = self.user_config.get("MINIO_IS_HTTPS", False)
3940
access_key = self.user_config["MINIO_ACCESS_KEY"]
4041
secret_key = self.user_config["MINIO_SECRET_KEY"]
4142
bucket_name = self.user_config["MINIO_BUCKET_NAME"]
42-
uploader = MinioUploader(host, access_key, secret_key, bucket_name)
43+
cred_expiry_duration = self.user_config.get("MINIO_CREDENTIAL_EXPIRY_DURATION")
44+
uploader = MinioUploader(host, access_key, secret_key, bucket_name, cred_expiry_duration, isHttps)
4345
error_code, error_msg, final_data = uploader.put(f'{self.token}.html', f'{self.token}.html', None)
4446
if error_code is None:
4547
if os.path.exists(drive_file_loc):
@@ -54,4 +56,4 @@ def upload_file(self):
5456
error_code = 805
5557
error_msg = f"Something went wrong: {e}"
5658
finally:
57-
return error_code, error_msg, final_data
59+
return error_code, error_msg, final_data

src/pdf/plugins/_pdf/external.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,13 @@ def upload_file(self):
135135
drive_file_loc = f'pdf/drivefiles/{self.token}.pdf'
136136
if self.uploader == "minio":
137137
host = self.user_config["MINIO_HOST"]
138+
isHttps = self.user_config.get("MINIO_IS_HTTPS", False)
138139
access_key = self.user_config["MINIO_ACCESS_KEY"]
139140
secret_key = self.user_config["MINIO_SECRET_KEY"]
140141
bucket_name = self.user_config["MINIO_BUCKET_NAME"]
142+
cred_expiry_duration = self.user_config.get("MINIO_CREDENTIAL_EXPIRY_DURATION")
141143
uploader = MinioUploader(
142-
host, access_key, secret_key, bucket_name)
144+
host, access_key, secret_key, bucket_name, cred_expiry_duration, isHttps)
143145
error_code, error_msg, final_data = uploader.put(
144146
f'{self.token}.pdf', f'{self.token}.pdf', None)
145147
if error_code is None:

src/pdf/plugins/_template/external.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,12 @@ def upload_file(self):
9393
drive_file_loc = f'pdf/drivefiles/{self.token}.pdf'
9494
if self.uploader == "minio":
9595
host = self.user_config["MINIO_HOST"]
96+
isHttps = self.user_config.get("MINIO_IS_HTTPS", False)
9697
access_key = self.user_config["MINIO_ACCESS_KEY"]
9798
secret_key = self.user_config["MINIO_SECRET_KEY"]
9899
bucket_name = self.user_config["MINIO_BUCKET_NAME"]
99-
uploader = MinioUploader(host, access_key, secret_key, bucket_name)
100+
cred_expiry_duration = self.user_config.get("MINIO_CREDENTIAL_EXPIRY_DURATION")
101+
uploader = MinioUploader(host, access_key, secret_key, bucket_name, cred_expiry_duration, isHttps)
100102
error_code, error_msg, final_data = uploader.put(f'{self.token}.pdf', f'{self.token}.pdf', None)
101103
if error_code is None:
102104
if os.path.exists(drive_file_loc):

src/pdf/uploaders/minio.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ def get_fa_token(username, password):
2121
body = json.dumps({
2222
"loginId": f"{username}",
2323
"password": f"{password}",
24-
"applicationId": os.getenv('MINIO_FA_APPLICATION_ID')
24+
"applicationId": os.getenv('FA_APPLICATION_ID')
2525
})
2626
headers = {
2727
'Content-Type': 'application/json',
28-
'Authorization': os.getenv('MINIO_FA_AUTHORIZATION')
28+
'Authorization': os.getenv('FA_AUTHORIZATION')
2929
}
30-
response = requests.post("http://auth.samagra.io:9011/api/login", data=body, headers=headers)
30+
response = requests.post(f"{os.getenv('FA_URL')}/api/login", data=body, headers=headers)
3131
response.raise_for_status()
3232
resp = response.json()
3333
if 'token' in resp:
@@ -41,12 +41,12 @@ def get_fa_token(username, password):
4141
return token
4242

4343

44-
def get_minio_cred(username, password, bucket_name):
44+
def get_minio_cred(username, password, bucket_name, cred_expiry_duration=36000):
4545
access_key = secret_key = session_token = None
4646
try:
4747
token = get_fa_token(username, password)
4848
if token is not None:
49-
minio_url = f"https://cdn.samagra.io/minio/{bucket_name}/?Action=AssumeRoleWithWebIdentity&DurationSeconds=36000&WebIdentityToken={token}&Version=2011-06-15"
49+
minio_url = f"{os.getenv('MINIO_URL')}/{bucket_name}/?Action=AssumeRoleWithWebIdentity&DurationSeconds={cred_expiry_duration}&WebIdentityToken={token}&Version=2011-06-15"
5050
response = requests.post(minio_url)
5151
response.raise_for_status()
5252
resp = response.text
@@ -71,14 +71,17 @@ def get_minio_cred(username, password, bucket_name):
7171

7272
class MinioUploader(implements(Uploader)):
7373

74-
def __init__(self, host, username, password, bucket_name):
74+
def __init__(self, host, username, password, bucket_name, cred_expiry_duration, isHttps=False):
7575
self.host = host
7676
self.bucket_name = bucket_name
7777
# Get the logger specified in the file
7878
self.logger = logging.getLogger()
7979
# Create a client with the MinIO, its access key and secret key.
80-
access_key, secret_key, session_token = get_minio_cred(username, password, bucket_name)
81-
self.client = Minio(host, access_key=access_key, secret_key=secret_key, session_token=session_token)
80+
if cred_expiry_duration:
81+
access_key, secret_key, session_token = get_minio_cred(username, password, bucket_name, cred_expiry_duration)
82+
else:
83+
access_key, secret_key, session_token = get_minio_cred(username, password, bucket_name)
84+
self.client = Minio(host, access_key=access_key, secret_key=secret_key, session_token=session_token, secure=isHttps)
8285

8386
def put(self, file_name, object_name, expires):
8487
error_code = error_msg = None
@@ -139,4 +142,4 @@ def get_object(self, object_name):
139142
pass
140143

141144
def get_public_url(self, file_name):
142-
return f"https://{self.host}/{self.bucket_name}/{file_name}"
145+
return f"{'https' if self.client._base_url.is_https else 'http'}://{self.host}/{self.bucket_name}/{file_name}"

0 commit comments

Comments
 (0)