Skip to content

Commit

Permalink
Add domains endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
aloftus23 committed Sep 18, 2024
1 parent 21c21d0 commit 5b9d6c9
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 15 deletions.
54 changes: 44 additions & 10 deletions backend/src/xfd_django/xfd_api/schemas.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Pydantic models used by FastAPI."""
from collections import UserDict
# Standard Python Libraries
from datetime import date, datetime
from decimal import Decimal
Expand All @@ -7,8 +8,10 @@
from typing import Any, Dict, List, Optional, Tuple
from uuid import UUID

from matplotlib.text import OffsetFrom
# Third-Party Libraries
from pydantic import BaseModel, EmailStr, Field, UUID4
from pygments.lexers.configs import UnixConfigLexer

"""
Developer Note: If there comes an instance as in class Cidrs where there are
Expand Down Expand Up @@ -50,20 +53,51 @@ class Domain(BaseModel):
id: UUID
createdAt: datetime
updatedAt: datetime
syncedAt: datetime
ip: str
syncedAt: Optional[datetime]
ip: Optional[str]
fromRootDomain: Optional[str]
subdomainSource: Optional[str]
ipOnly: bool
reverseName: Optional[str]
name: Optional[str]
reverseName: str
name: str
screenshot: Optional[str]
country: Optional[str]
asn: Optional[str]
cloudHosted: bool
ssl: Optional[Any]
censysCertificatesResults: Optional[dict]
trustymailResults: Optional[dict]
discoveredById: Optional[Any]
organizationId: Optional[Any]

ssl: Optional[Dict]
censysCertificatesResults: Dict
trustymailResults: Dict
discoveredById_id: Optional[UUID4]
organizationId_id: Optional[UUID4]

class Config:
"""Domain base schema schema config."""

orm_mode = True
validate_assignment = True


class Organization(BaseModel):
id: UUID
createdAt: datetime
updatedAt: datetime
acronym: Optional[str]
name: str
rootDomains: str
ipBlocks: str
isPassive: bool
pendingDomains: str
country: Optional[str]
state: Optional[str]
regionId: Optional[str]
stateFips: Optional[int]
stateName: Optional[str]
county: Optional[str]
countyFips: Optional[int]
type: Optional[str]

class Config:
"""Organization base schema schema config."""

orm_mode = True
validate_assignment = True
37 changes: 36 additions & 1 deletion backend/src/xfd_django/xfd_api/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import redis
import django
from django.db.models import Count
from xfd_api.models import Service
from xfd_api.models import Service, Domain
from django.conf import settings # Import settings after setting DJANGO_SETTINGS_MODULE

# Set the Django settings module environment variable
Expand Down Expand Up @@ -46,6 +46,41 @@ def populate_Statscache(event, context):
except Exception as e:
return {"status": "error",
"message": "An unexpected error occurred while populating the cache."}

def populate_Portscache(event, context):
try:
# Connect to Redis Elasticache
redis_client = redis.StrictRedis(host=settings.ELASTICACHE_ENDPOINT,
port=6379, db=0)

# Fetch data from Django models
ports = (
Service.objects
.filter(
domain__isnull=False) # Equivalent to INNER JOIN on the domain
.values('port') # Select 'port' as id
.annotate(value=Count('port')) # Count the occurrences of each port
.order_by('-value') # Order by the count value in descending order
)

# Populate Elasticache with data
for item in ports:
the_id = str(item.id) # Convert UUID to string
redis_client.set(the_id, item.value) # Store the 'value' in Redis

return {"status": "success", "message": "Cache populated successfully."}

except redis.RedisError as redis_error:
return {"status": "error",
"message": "Failed to populate cache due to Redis error."}

except django.db.DatabaseError as db_error:
return {"status": "error",
"message": "Failed to populate cache due to database error."}

except Exception as e:
return {"status": "error",
"message": "An unexpected error occurred while populating the cache."}

##Use the following for elasticache testing.
# def retrieve_Statscache():
Expand Down
17 changes: 13 additions & 4 deletions backend/src/xfd_django/xfd_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
status,
)
from fastapi_limiter import FastAPILimiter
from pydantic import BaseModel, UUID4, Field, ConfigDict
from uuid import UUID
from redis import asyncio as aioredis
from . import schemas
from typing import Any, List, Optional, Union
Expand Down Expand Up @@ -250,7 +252,7 @@ async def get_Stats(domain_id: str, redis_client=Depends(get_redis_client)):
@api_router.get(
"/domain/{domain_id}",
# dependencies=[Depends(get_current_active_user)],
response_model=List[schemas.Domain],
response_model=schemas.Domain,
tags=["Get domain by id"],
)
async def get_domain_by_id(domain_id: str):
Expand All @@ -260,8 +262,15 @@ async def get_domain_by_id(domain_id: str):
object: a single Domain object.
"""
try:
domains = list(Domain.objects.filter(id=domain_id))
# Fetch the Domain object
# domain = Domain.objects.filter('organizationId').get(
# id=domain_id)
domain = Domain.objects.get(id=domain_id)

return domains

return domain # Return the mapped Pydantic model instance

except Domain.DoesNotExist:
raise HTTPException(status_code=404, detail="Domain not found.")
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
raise HTTPException(status_code=500, detail=str(e))

0 comments on commit 5b9d6c9

Please sign in to comment.