Skip to content

Commit

Permalink
Renaming services module + misc cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
galvana committed Jan 12, 2025
1 parent f4b53ad commit d1948a8
Show file tree
Hide file tree
Showing 21 changed files with 112 additions and 127 deletions.
8 changes: 4 additions & 4 deletions src/fides/api/api/deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
from fastapi import Depends
from sqlalchemy.orm import Session

from fides.api.common_exceptions import FunctionalityNotConfigured
from fides.api.common_exceptions import RedisNotConfigured
from fides.api.db.session import get_db_engine, get_db_session
from fides.api.util.cache import get_cache as get_redis_connection
from fides.config import CONFIG, FidesConfig
from fides.config import get_config as get_app_config
from fides.config.config_proxy import ConfigProxy
from fides.services.messaging.messaging_service import MessagingService
from fides.services.privacy_request.privacy_request_service import PrivacyRequestService
from fides.service.messaging.messaging_service import MessagingService
from fides.service.privacy_request.privacy_request_service import PrivacyRequestService

_engine = None

Expand Down Expand Up @@ -64,7 +64,7 @@ def get_config_proxy(db: Session = Depends(get_db)) -> ConfigProxy:
def get_cache() -> Generator:
"""Return a connection to our Redis cache"""
if not CONFIG.redis.enabled:
raise FunctionalityNotConfigured(
raise RedisNotConfigured(
"Application redis cache required, but it is currently disabled! Please update your application configuration to enable integration with a Redis cache."
)
yield get_redis_connection()
Expand Down
8 changes: 4 additions & 4 deletions src/fides/api/api/v1/endpoints/consent_request_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

from fides.api.api.deps import get_config_proxy, get_db, get_messaging_service
from fides.api.common_exceptions import (
FunctionalityNotConfigured,
RedisNotConfigured,
IdentityVerificationException,
)
from fides.api.db.seed import DEFAULT_CONSENT_POLICY
Expand Down Expand Up @@ -66,8 +66,8 @@
)
from fides.config import CONFIG
from fides.config.config_proxy import ConfigProxy
from fides.services.messaging.messaging_service import MessagingService
from fides.services.privacy_request.privacy_request_service import PrivacyRequestService
from fides.service.messaging.messaging_service import MessagingService
from fides.service.privacy_request.privacy_request_service import PrivacyRequestService

router = APIRouter(tags=["Consent"], prefix=V1_URL_PREFIX)

Expand Down Expand Up @@ -183,7 +183,7 @@ def create_consent_request(
) -> ConsentRequestResponse:
"""Creates a verification code for the user to verify access to manage consent preferences."""
if not CONFIG.redis.enabled:
raise FunctionalityNotConfigured(
raise RedisNotConfigured(
"Application Redis cache required, but it is currently disabled! Please update your application configuration to enable integration with a Redis cache."
)
# TODO: (PROD-2142)- pass in property id here
Expand Down
4 changes: 2 additions & 2 deletions src/fides/api/api/v1/endpoints/drp_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@
from fides.common.api.v1 import urn_registry as urls
from fides.config import CONFIG
from fides.config.config_proxy import ConfigProxy
from fides.services.messaging.messaging_service import (
from fides.service.messaging.messaging_service import (
check_and_dispatch_error_notifications,
)
from fides.services.privacy_request.privacy_request_service import queue_privacy_request
from fides.service.privacy_request.privacy_request_service import queue_privacy_request

router = APIRouter(tags=["DRP"], prefix=urls.V1_URL_PREFIX)

Expand Down
20 changes: 6 additions & 14 deletions src/fides/api/api/v1/endpoints/privacy_request_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
Union,
)

from fides.service.messaging.messaging_service import MessagingService
import sqlalchemy
from fastapi import Body, Depends, HTTPException, Security
from fastapi.encoders import jsonable_encoder
Expand Down Expand Up @@ -166,9 +167,8 @@
)
from fides.config import CONFIG
from fides.config.config_proxy import ConfigProxy
from fides.services.privacy_request.privacy_request_service import (
from fides.service.privacy_request.privacy_request_service import (
PrivacyRequestService,
_send_privacy_request_receipt_message_to_user,
_trigger_pre_approval_webhooks,
queue_privacy_request,
)
Expand Down Expand Up @@ -1218,6 +1218,7 @@ def verify_identification_code(
*,
db: Session = Depends(deps.get_db),
config_proxy: ConfigProxy = Depends(deps.get_config_proxy),
messaging_service: MessagingService = Depends(deps.get_messaging_service),
provided_code: VerificationCode,
) -> PrivacyRequestResponse:
"""Verify the supplied identity verification code.
Expand All @@ -1236,18 +1237,9 @@ def verify_identification_code(
policy: Optional[Policy] = Policy.get(
db=db, object_id=privacy_request.policy_id
)
if message_send_enabled(
db,
privacy_request.property_id,
MessagingActionType.PRIVACY_REQUEST_RECEIPT,
config_proxy.notifications.send_request_receipt_notification,
):
_send_privacy_request_receipt_message_to_user(
policy,
privacy_request.get_persisted_identity(),
config_proxy.notifications.notification_service_type,
privacy_request.property_id,
)
messaging_service.send_privacy_request_receipt(
policy, privacy_request.get_persisted_identity(), privacy_request
)

except IdentityVerificationException as exc:
raise HTTPException(status_code=HTTP_400_BAD_REQUEST, detail=exc.message)
Expand Down
10 changes: 5 additions & 5 deletions src/fides/api/api/v1/exception_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
from fastapi.responses import JSONResponse
from starlette.status import HTTP_500_INTERNAL_SERVER_ERROR

from fides.api.common_exceptions import FunctionalityNotConfigured
from fides.api.common_exceptions import RedisNotConfigured


class ExceptionHandlers:
@staticmethod
def functionality_not_configured_handler(
request: Request, exc: FunctionalityNotConfigured
def redis_not_configured_handler(
request: Request, exc: RedisNotConfigured
) -> JSONResponse:
return JSONResponse(
status_code=HTTP_500_INTERNAL_SERVER_ERROR, content={"message": str(exc)}
Expand All @@ -19,5 +19,5 @@ def functionality_not_configured_handler(
@classmethod
def get_handlers(
cls,
) -> List[Callable[[Request, FunctionalityNotConfigured], JSONResponse]]:
return [ExceptionHandlers.functionality_not_configured_handler]
) -> List[Callable[[Request, RedisNotConfigured], JSONResponse]]:
return [ExceptionHandlers.redis_not_configured_handler]
4 changes: 2 additions & 2 deletions src/fides/api/app_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from fides.api.api.v1.endpoints.generic_overrides import GENERIC_OVERRIDES_ROUTER
from fides.api.api.v1.endpoints.health import HEALTH_ROUTER
from fides.api.api.v1.exception_handlers import ExceptionHandlers
from fides.api.common_exceptions import FunctionalityNotConfigured, RedisConnectionError
from fides.api.common_exceptions import RedisNotConfigured, RedisConnectionError
from fides.api.db.database import configure_db
from fides.api.db.seed import create_or_update_parent_user
from fides.api.models.application_config import ApplicationConfig
Expand Down Expand Up @@ -81,7 +81,7 @@ def create_fides_app(
fastapi_app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler) # type: ignore
for handler in ExceptionHandlers.get_handlers():
# Starlette bug causing this to fail mypy
fastapi_app.add_exception_handler(FunctionalityNotConfigured, handler) # type: ignore
fastapi_app.add_exception_handler(RedisNotConfigured, handler) # type: ignore
fastapi_app.add_middleware(SlowAPIMiddleware)
fastapi_app.add_middleware(
GZipMiddleware, minimum_size=1000, compresslevel=5
Expand Down
4 changes: 2 additions & 2 deletions src/fides/api/common_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@ class NoSuchStrategyException(ValueError):
"""Exception for when a masking strategy does not exist"""


class FunctionalityNotConfigured(Exception):
"""Custom exception for when invoked functionality is unavailable due to configuration."""
class RedisNotConfigured(Exception):
"""Redis cache is unavailable due to configuration"""


class InvalidSaaSRequestOverrideException(ValueError):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from fides.api.tasks import DatabaseTask, celery_app
from fides.api.tasks.scheduled.scheduler import scheduler
from fides.config import get_config
from fides.services.privacy_request.privacy_request_service import queue_privacy_request
from fides.service.privacy_request.privacy_request_service import queue_privacy_request

CONFIG = get_config()
BATCH_EMAIL_SEND = "batch_email_send"
Expand Down
2 changes: 1 addition & 1 deletion src/fides/api/task/execute_request_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def queue_downstream_tasks(
# Only queue privacy request from the next step if we haven't reached the terminator before.
# Multiple pathways could mark the same node as complete, so we may have already reached the
# terminator node through a quicker path.
from fides.services.privacy_request.privacy_request_service import ( # pylint: disable=cyclic-import
from fides.service.privacy_request.privacy_request_service import ( # pylint: disable=cyclic-import
queue_privacy_request,
)

Expand Down
2 changes: 1 addition & 1 deletion src/fides/api/util/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def get_cache(should_log: Optional[bool] = False) -> FidesopsRedis:
"""Return a singleton connection to our Redis cache"""

if not CONFIG.redis.enabled:
raise common_exceptions.FunctionalityNotConfigured(
raise common_exceptions.RedisNotConfigured(
"Application Redis cache required, but it is currently disabled! Please update your application configuration to enable integration with a Redis cache."
)

Expand Down
2 changes: 1 addition & 1 deletion src/fides/api/util/connection_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
)
from fides.api.util.logger import Pii
from fides.common.api.v1.urn_registry import CONNECTION_TYPES, SAAS_CONFIG
from fides.services.privacy_request.privacy_request_service import queue_privacy_request
from fides.service.privacy_request.privacy_request_service import queue_privacy_request

# pylint: disable=too-many-nested-blocks,too-many-branches,too-many-statements

Expand Down
2 changes: 1 addition & 1 deletion src/fides/api/util/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,5 @@ def get_full_exception_name(exception: Exception) -> str:
return module + "." + exception.__class__.__name__


class FunctionalityNotConfigured(Exception):
class RedisNotConfigured(Exception):
"""Custom exception for when invoked functionality is unavailable due to configuration."""
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,43 @@ def send_verification_code_to_user(
)

return verification_code


def send_privacy_request_receipt_message_to_user(
policy: Optional[Policy],
to_identity: Optional[Identity],
service_type: Optional[str],
property_id: Optional[str],
) -> None:
"""Helper function to send request receipt message to the user"""
if not to_identity:
logger.error(
IdentityNotFoundException(
"Identity was not found, so request receipt message could not be sent."
)
)
return
if not policy:
logger.error(
PolicyNotFoundException(
"Policy was not found, so request receipt message could not be sent."
)
)
return
request_types: Set[str] = set()
for action_type in ActionType:
if policy.get_rules_for_action(action_type=ActionType(action_type)):
request_types.add(action_type)

dispatch_message_task.apply_async(
queue=MESSAGING_QUEUE_NAME,
kwargs={
"message_meta": FidesopsMessage(
action_type=MessagingActionType.PRIVACY_REQUEST_RECEIPT,
body_params=RequestReceiptBodyParams(request_types=request_types),
).model_dump(mode="json"),
"service_type": service_type,
"to_identity": to_identity.labeled_dict(),
"property_id": property_id,
},
)
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
from datetime import datetime
from typing import Any, Dict, List, Optional, Set
from typing import Any, Dict, List, Optional

from loguru import logger
from sqlalchemy.orm import Session

from fides.api.common_exceptions import (
FunctionalityNotConfigured,
IdentityNotFoundException,
RedisNotConfigured,
MessageDispatchException,
PolicyNotFoundException,
)
from fides.api.models.audit_log import AuditLog, AuditLogAction
from fides.api.models.policy import Policy
Expand All @@ -25,33 +23,28 @@
from fides.api.models.property import Property
from fides.api.schemas.api import BulkUpdateFailed
from fides.api.schemas.messaging.messaging import (
FidesopsMessage,
MessagingActionType,
RequestReceiptBodyParams,
)
from fides.api.schemas.policy import ActionType
from fides.api.schemas.privacy_request import (
BulkPostPrivacyRequests,
BulkReviewResponse,
PrivacyRequestCreate,
PrivacyRequestResponse,
)
from fides.api.schemas.redis_cache import Identity
from fides.api.service.messaging.message_dispatch_service import (
dispatch_message_task,
message_send_enabled,
)
from fides.api.service.privacy_request.request_service import (
build_required_privacy_request_kwargs,
cache_data,
)
from fides.api.tasks import MESSAGING_QUEUE_NAME
from fides.api.util.cache import cache_task_tracking_key
from fides.api.util.logger_context_utils import LoggerContextKeys, log_context
from fides.config.config_proxy import ConfigProxy
from fides.services.messaging.messaging_service import (
from fides.service.messaging.messaging_service import (
MessagingService,
check_and_dispatch_error_notifications,
send_privacy_request_receipt_message_to_user,
send_verification_code_to_user,
)

Expand Down Expand Up @@ -204,7 +197,7 @@ def create_privacy_request(

return privacy_request

except FunctionalityNotConfigured as exc:
except RedisNotConfigured as exc:
logger.error(f"{exc.__class__.__name__}: {str(exc)}")
raise exc
except MessageDispatchException as exc:
Expand Down Expand Up @@ -545,7 +538,7 @@ def _handle_notifications_and_processing(
MessagingActionType.PRIVACY_REQUEST_RECEIPT,
config_proxy.notifications.send_request_receipt_notification,
):
_send_privacy_request_receipt_message_to_user(
send_privacy_request_receipt_message_to_user(
policy,
privacy_request_data.identity,
config_proxy.notifications.notification_service_type,
Expand All @@ -567,46 +560,6 @@ def _handle_notifications_and_processing(
queue_privacy_request(privacy_request.id)


def _send_privacy_request_receipt_message_to_user(
policy: Optional[Policy],
to_identity: Optional[Identity],
service_type: Optional[str],
property_id: Optional[str],
) -> None:
"""Helper function to send request receipt message to the user"""
if not to_identity:
logger.error(
IdentityNotFoundException(
"Identity was not found, so request receipt message could not be sent."
)
)
return
if not policy:
logger.error(
PolicyNotFoundException(
"Policy was not found, so request receipt message could not be sent."
)
)
return
request_types: Set[str] = set()
for action_type in ActionType:
if policy.get_rules_for_action(action_type=ActionType(action_type)):
request_types.add(action_type)

dispatch_message_task.apply_async(
queue=MESSAGING_QUEUE_NAME,
kwargs={
"message_meta": FidesopsMessage(
action_type=MessagingActionType.PRIVACY_REQUEST_RECEIPT,
body_params=RequestReceiptBodyParams(request_types=request_types),
).model_dump(mode="json"),
"service_type": service_type,
"to_identity": to_identity.labeled_dict(),
"property_id": property_id,
},
)


def _trigger_pre_approval_webhooks(
db: Session, privacy_request: PrivacyRequest
) -> None:
Expand Down
4 changes: 2 additions & 2 deletions tests/ops/api/test_deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import fides.api.api.deps
from fides.api.api.deps import get_api_session, get_cache
from fides.api.common_exceptions import FunctionalityNotConfigured
from fides.api.common_exceptions import RedisNotConfigured
from fides.config import CONFIG


Expand All @@ -32,7 +32,7 @@ def mock_config_changed_db_engine_settings():

@pytest.mark.usefixtures("mock_config")
def test_get_cache_not_enabled():
with pytest.raises(FunctionalityNotConfigured):
with pytest.raises(RedisNotConfigured):
next(get_cache())


Expand Down
Loading

0 comments on commit d1948a8

Please sign in to comment.