|
27 | 27 | # Load environment variables from .env file
|
28 | 28 | load_dotenv()
|
29 | 29 |
|
30 |
| -# Resource usage thresholds |
31 |
| -CPU_USAGE_THRESHOLD = float(os.getenv("CPU_USAGE_THRESHOLD", "70.0")) |
32 |
| -MEMORY_USAGE_THRESHOLD = float(os.getenv("MEMORY_USAGE_THRESHOLD", "395.0")) |
33 |
| -DISK_SPACE_THRESHOLD = float(os.getenv("DISK_SPACE_THRESHOLD", "75.0")) |
| 30 | +CERT_EXPIRATION_YEARS = os.getenv("CERT_EXPIRATION_YEARS", "1") |
34 | 31 |
|
35 | 32 | # Verify that required environment variables are set
|
36 |
| -REQUIRED_ENV_VARS = [ |
37 |
| - CPU_USAGE_THRESHOLD, |
38 |
| - MEMORY_USAGE_THRESHOLD, |
39 |
| - DISK_SPACE_THRESHOLD, |
40 |
| -] |
| 33 | +REQUIRED_ENV_VARS = [CERT_EXPIRATION_YEARS] |
41 | 34 |
|
42 | 35 | if not all(REQUIRED_ENV_VARS):
|
43 | 36 | raise EnvironmentError("One or more required environment variables are missing.")
|
@@ -213,7 +206,7 @@ def encrypt_hybrid(self, plain_text: str) -> str:
|
213 | 206 |
|
214 | 207 | # Concatenate with colon as delimiter
|
215 | 208 | encrypted_data = f"{encrypted_aes_key_b64}:{iv_b64}:{ciphertext_b64}"
|
216 |
| - logger.info("Hybrid encryption successful.") |
| 209 | + logger.debug("Hybrid encryption successful.") |
217 | 210 | return encrypted_data
|
218 | 211 |
|
219 | 212 | except Exception as error:
|
@@ -263,12 +256,13 @@ def decrypt_hybrid(self, encrypted_data: str) -> str:
|
263 | 256 | decrypted_text = decryptor.update(ciphertext) + decryptor.finalize()
|
264 | 257 |
|
265 | 258 | decrypted_str = decrypted_text.decode("utf-8")
|
266 |
| - logger.info("Hybrid decryption successful.") |
| 259 | + logger.debug("Hybrid decryption successful.") |
267 | 260 | return decrypted_str
|
268 | 261 |
|
269 | 262 | except Exception as error:
|
270 | 263 | logger.error(f"Hybrid decryption failed: {error}", exc_info=True)
|
271 |
| - raise |
| 264 | + logger.fatal("Can't decrypt encrypted data.") |
| 265 | + sys.exit(1) |
272 | 266 |
|
273 | 267 | def encrypt(self, plain_text: str) -> str:
|
274 | 268 | """
|
@@ -368,7 +362,7 @@ def verify(self) -> bool:
|
368 | 362 | logger.error("The key pair has expired.")
|
369 | 363 | return False
|
370 | 364 |
|
371 |
| - logger.info("Key verification successful.") |
| 365 | + logger.debug("Key verification successful.") |
372 | 366 | return True
|
373 | 367 | except Exception as error:
|
374 | 368 | logger.error(f"Verification failed: {error}", exc_info=True)
|
@@ -439,7 +433,7 @@ def create_keys(self) -> None:
|
439 | 433 |
|
440 | 434 | # Create key pair content as JSON
|
441 | 435 | now = datetime.now()
|
442 |
| - expire = now + timedelta(days=365 * int(os.getenv("CERT_EXPIRATION_YEARS", "1"))) |
| 436 | + expire = now + timedelta(days=365 * int(CERT_EXPIRATION_YEARS)) |
443 | 437 | key_pair_data = {
|
444 | 438 | "public_key_file": self.public_key_file,
|
445 | 439 | "public_fp_sha1": public_fp.sha1,
|
@@ -598,13 +592,25 @@ def fetch_private_key_password() -> str:
|
598 | 592 | )
|
599 | 593 | response.raise_for_status() # Raises HTTPError for bad responses
|
600 | 594 | pk_key_pass = response.json().get("value")
|
601 |
| - if not pk_key_pass: |
602 |
| - logger.error("The key 'value' was not found in the response.") |
603 |
| - sys.exit(1) |
604 | 595 | return pk_key_pass
|
605 |
| - except requests.exceptions.RequestException as e: |
606 |
| - logger.error(f"Error fetching private key password: {e}", exc_info=True) |
607 |
| - sys.exit(1) |
| 596 | + except requests.exceptions.RequestException as e_requests_exception_fetch_password: |
| 597 | + logger.error( |
| 598 | + f"Error fetching private key password from api: {e_requests_exception_fetch_password}", |
| 599 | + exc_info=True, |
| 600 | + ) |
| 601 | + try: |
| 602 | + logger.debug("Trying using KP_PASSWORD value...") |
| 603 | + pk_key_pass = os.getenv("KP_PASSWORD") |
| 604 | + return pk_key_pass |
| 605 | + except KeyError as e_key_error_fetch_password: |
| 606 | + logger.error( |
| 607 | + f"The key was not found in the environment: {e_key_error_fetch_password}", |
| 608 | + exc_info=True, |
| 609 | + ) |
| 610 | + logger.error( |
| 611 | + "STARTING USING DEFAULT PASSWORD WHICH IS NOT RECOMMENDED, CLEAN AND SET THIS ONE TO .env FILE AS KP_PASSWORD..." |
| 612 | + ) |
| 613 | + return "password123456789099ab5e7b9add0dc4e5" |
608 | 614 |
|
609 | 615 |
|
610 | 616 | def send_expiration_alert(expiration_date: datetime) -> None:
|
|
0 commit comments