Skip to content

Commit 9060815

Browse files
committed
🐛 fix(core): fixed crypto controller logging levels
1 parent ee4c176 commit 9060815

File tree

3 files changed

+62
-30
lines changed

3 files changed

+62
-30
lines changed

crypto_controller/.env.example

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Password KP Settings
2+
## Password Key Pair (API-Token Mode)
3+
# API_URI="https://tu.dominio.com/private-key" <- Uncomment and comment KP_PASSWORD
4+
# API_TOKEN_SECURITY="api_token" <- Uncomment and comment KP_PASSWORD
5+
# API_TIMEOUT=12 <- Uncomment and comment KP_PASSWORD
6+
### OR
7+
## Pasword Key Pair (Local Mode)
8+
KP_PASSWORD="<28 (Chars)>"
9+
10+
# Certificate Vault Settings
11+
CERT_EXPIRATION_YEARS=6
12+
13+
# Expiration Notifications Settings
14+
SMTP_SERVER=smtp.example.com
15+
SMTP_PORT=587
16+
17+
SMTP_PASSWORD=your_email_password
18+
ALERT_RECIPIENT=[email protected]

crypto_controller/README.md

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,19 @@ CryptoController is a robust Python application designed for secure key manageme
9292
Create a .env file in the project root directory and populate it with the following variables:
9393

9494
```bash
95-
CPU_USAGE_THRESHOLD=70.0
96-
MEMORY_USAGE_THRESHOLD=395.0
97-
DISK_SPACE_THRESHOLD=75.0
98-
EXPIRATION=1
99-
TOKEN_SECURITY=your_secure_token_here
95+
# Password KP Settings
96+
## Password Key Pair (API-Token Mode)
97+
# API_URI="https://tu.dominio.com/private-key" <- Uncomment and comment KP_PASSWORD
98+
# API_TOKEN_SECURITY="api_token" <- Uncomment and comment KP_PASSWORD
99+
# API_TIMEOUT=12 <- Uncomment and comment KP_PASSWORD
100+
### OR
101+
## Password Key Pair (Local Mode)
102+
KP_PASSWORD="<28 (Chars)>"
103+
104+
# Certificate Vault Settings
105+
CERT_EXPIRATION_YEARS=6
106+
107+
# Expiration Notifications Settings
100108
SMTP_SERVER=smtp.example.com
101109
SMTP_PORT=587
102110
@@ -105,11 +113,11 @@ [email protected]
105113
```
106114

107115
- Descriptions:
108-
- CPU_USAGE_THRESHOLD: CPU usage percentage threshold.
109-
- MEMORY_USAGE_THRESHOLD: Memory usage threshold in MB.
110-
- DISK_SPACE_THRESHOLD: Disk space usage percentage threshold.
111-
- EXPIRATION: Number of years before key expiration.
112-
- TOKEN_SECURITY: Token for fetching the private key password securely.
116+
- API_URI: Password API mode base URI.
117+
- API_TOKEN_SECURITY: Password API mode token security.
118+
- API_TIMEOUT: Password API mode timeout.
119+
- KP*PASSWORD: Password plain mode, used it or API* vars.
120+
- CERT_EXPIRATION_YEARS: Number of years before key expiration.
113121
- SMTP_SERVER: SMTP server address for sending emails.
114122
- SMTP_PORT: SMTP server port.
115123
- SMTP_USER: SMTP server username.

crypto_controller/main.py

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,10 @@
2727
# Load environment variables from .env file
2828
load_dotenv()
2929

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")
3431

3532
# 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]
4134

4235
if not all(REQUIRED_ENV_VARS):
4336
raise EnvironmentError("One or more required environment variables are missing.")
@@ -213,7 +206,7 @@ def encrypt_hybrid(self, plain_text: str) -> str:
213206

214207
# Concatenate with colon as delimiter
215208
encrypted_data = f"{encrypted_aes_key_b64}:{iv_b64}:{ciphertext_b64}"
216-
logger.info("Hybrid encryption successful.")
209+
logger.debug("Hybrid encryption successful.")
217210
return encrypted_data
218211

219212
except Exception as error:
@@ -263,12 +256,13 @@ def decrypt_hybrid(self, encrypted_data: str) -> str:
263256
decrypted_text = decryptor.update(ciphertext) + decryptor.finalize()
264257

265258
decrypted_str = decrypted_text.decode("utf-8")
266-
logger.info("Hybrid decryption successful.")
259+
logger.debug("Hybrid decryption successful.")
267260
return decrypted_str
268261

269262
except Exception as error:
270263
logger.error(f"Hybrid decryption failed: {error}", exc_info=True)
271-
raise
264+
logger.fatal("Can't decrypt encrypted data.")
265+
sys.exit(1)
272266

273267
def encrypt(self, plain_text: str) -> str:
274268
"""
@@ -368,7 +362,7 @@ def verify(self) -> bool:
368362
logger.error("The key pair has expired.")
369363
return False
370364

371-
logger.info("Key verification successful.")
365+
logger.debug("Key verification successful.")
372366
return True
373367
except Exception as error:
374368
logger.error(f"Verification failed: {error}", exc_info=True)
@@ -439,7 +433,7 @@ def create_keys(self) -> None:
439433

440434
# Create key pair content as JSON
441435
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))
443437
key_pair_data = {
444438
"public_key_file": self.public_key_file,
445439
"public_fp_sha1": public_fp.sha1,
@@ -598,13 +592,25 @@ def fetch_private_key_password() -> str:
598592
)
599593
response.raise_for_status() # Raises HTTPError for bad responses
600594
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)
604595
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"
608614

609615

610616
def send_expiration_alert(expiration_date: datetime) -> None:

0 commit comments

Comments
 (0)