mirror of
https://github.com/prowler-cloud/prowler.git
synced 2026-10-23 11:54:20 +00:00
Fix decryption to show key
This commit is contained in:
3 files changed
+21
-8
No files matched your search
@@ -1,8 +1,8 @@
|
||||
import json
|
||||
import re
|
||||
from uuid import UUID, uuid4
|
||||
|
||||
from cryptography.fernet import Fernet
|
||||
import logging
|
||||
from cryptography.fernet import Fernet, InvalidToken
|
||||
from django.conf import settings
|
||||
from django.contrib.auth.models import AbstractBaseUser
|
||||
from django.contrib.postgres.fields import ArrayField
|
||||
@@ -49,6 +49,8 @@ fernet = Fernet(settings.SECRETS_ENCRYPTION_KEY.encode())
|
||||
# Convert Prowler Severity enum to Django TextChoices
|
||||
SeverityChoices = enum_to_choices(Severity)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class StatusChoices(models.TextChoices):
|
||||
"""
|
||||
@@ -1349,9 +1351,14 @@ class LighthouseConfig(RowLevelSecurityProtectedModel):
|
||||
@property
|
||||
def api_key_decoded(self):
|
||||
"""Return the decrypted API key."""
|
||||
if not self.api_key:
|
||||
return None
|
||||
|
||||
try:
|
||||
decrypted_key = fernet.decrypt(self.api_key)
|
||||
api_key_bytes = bytes(self.api_key)
|
||||
decrypted_key = fernet.decrypt(api_key_bytes)
|
||||
return decrypted_key.decode()
|
||||
|
||||
except InvalidToken:
|
||||
logger.warning("Failed to decrypt API key: invalid token.")
|
||||
return None
|
||||
|
||||
@@ -1281,7 +1281,6 @@ class ProviderSecretCreateSerializer(RLSSerializer, BaseWriteProviderSecretSeria
|
||||
return validated_attrs
|
||||
|
||||
|
||||
|
||||
class ProviderSecretUpdateSerializer(BaseWriteProviderSecretSerializer):
|
||||
secret = ProviderSecretField(write_only=True)
|
||||
|
||||
|
||||
@@ -2870,6 +2870,12 @@ class LighthouseConfigViewSet(BaseRLSViewSet):
|
||||
"""
|
||||
instance = self.get_object()
|
||||
|
||||
if not instance.api_key:
|
||||
return Response(
|
||||
{"detail": "API key is missing."},
|
||||
status=status.HTTP_400_BAD_REQUEST,
|
||||
)
|
||||
|
||||
try:
|
||||
decrypted_key = instance.api_key_decoded
|
||||
if decrypted_key is None:
|
||||
@@ -2877,15 +2883,16 @@ class LighthouseConfigViewSet(BaseRLSViewSet):
|
||||
{"detail": "API key is invalid or missing."},
|
||||
status=status.HTTP_400_BAD_REQUEST,
|
||||
)
|
||||
|
||||
return Response(
|
||||
{"api_key": decrypted_key},
|
||||
status=status.HTTP_200_OK,
|
||||
)
|
||||
except Exception:
|
||||
except Exception as e:
|
||||
return Response(
|
||||
{"detail": "API key is invalid or missing."},
|
||||
status=status.HTTP_400_BAD_REQUEST,
|
||||
{
|
||||
"detail": f"An unexpected error occurred while retrieving the API key. {str(e)}"
|
||||
},
|
||||
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
)
|
||||
|
||||
def destroy(self, request, *args, **kwargs):
|
||||
|
||||
Reference in new issue
Block a user