Skip to content

Commit

Permalink
Add scans endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
aloftus23 committed Sep 16, 2024
1 parent db0f1ba commit 03351c9
Show file tree
Hide file tree
Showing 8 changed files with 723 additions and 171 deletions.
2 changes: 2 additions & 0 deletions backend/src/api/scans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,8 @@ export const update = wrapHandler(async (event) => {
* - Scans
*/
export const create = wrapHandler(async (event) => {
console.log(event);
console.log(event.body);
if (!isGlobalWriteAdmin(event)) return Unauthorized;
await connectToDatabase();
const body = await validateBody(NewScan, event.body);
Expand Down
4 changes: 2 additions & 2 deletions backend/src/xfd_django/xfd_api/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ def get_user_by_api_key(api_key: str):
hashed_key = sha256(api_key.encode()).hexdigest()
try:
api_key_instance = ApiKey.objects.get(hashedKey=hashed_key)
api_key_instance.lastused = timezone.now()
api_key_instance.lastUsed = timezone.now()
api_key_instance.save(update_fields=["lastUsed"])
return api_key_instance.userid
return api_key_instance.userId
except ApiKey.DoesNotExist:
print("API Key not found")
return None
Expand Down
88 changes: 41 additions & 47 deletions backend/src/xfd_django/xfd_api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
# Feel free to rename the models, but don't rename db_table values or field names.
# Third-Party Libraries
from django.db import models
from django.contrib.postgres.fields import ArrayField, JSONField
import uuid


class ApiKey(models.Model):
Expand Down Expand Up @@ -245,19 +247,15 @@ class Meta:
class Organization(models.Model):
"""The Organization model."""

id = models.UUIDField(primary_key=True)
createdAt = models.DateTimeField(db_column="createdAt")
updatedAt = models.DateTimeField(db_column="updatedAt")
acronym = models.CharField(unique=True, blank=True, null=True)
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
createdAt = models.DateTimeField(db_column="createdAt", auto_now_add=True)
updatedAt = models.DateTimeField(db_column="updatedAt", auto_now=True)
acronym = models.CharField(unique=True, blank=True, null=True, max_length=255)
name = models.CharField()
rootDomains = models.TextField(
db_column="rootDomains"
) # This field type is a guess.
ipBlocks = models.TextField(db_column="ipBlocks") # This field type is a guess.
rootDomains = ArrayField(models.CharField(max_length=255), db_column="rootDomains")
ipBlocks = ArrayField(models.CharField(max_length=255), db_column="ipBlocks")
isPassive = models.BooleanField(db_column="isPassive")
pendingDomains = models.TextField(
db_column="pendingDomains"
) # This field type is a guess.
pendingDomains = models.TextField(db_column="pendingDomains", default=list)
country = models.CharField(blank=True, null=True)
state = models.CharField(blank=True, null=True)
regionId = models.CharField(db_column="regionId", blank=True, null=True)
Expand All @@ -272,6 +270,9 @@ class Organization(models.Model):
createdById = models.ForeignKey(
"User", models.DO_NOTHING, db_column="createdById", blank=True, null=True
)
# Relationships with other models (Scan, OrganizationTag)
granularScans = models.ManyToManyField('Scan', related_name="organizations", through='ScanOrganizationsOrganization')
tags = models.ManyToManyField('OrganizationTag', related_name="organizations", through='ScanTagsOrganizationTag')

class Meta:
"""The meta class for Organization."""
Expand All @@ -283,9 +284,9 @@ class Meta:
class OrganizationTag(models.Model):
"""The OrganizationTag model."""

id = models.UUIDField(primary_key=True)
createdAt = models.DateTimeField(db_column="createdAt")
updatedAt = models.DateTimeField(db_column="updatedAt")
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
createdAt = models.DateTimeField(db_column="createdAt", auto_now_add=True)
updatedAt = models.DateTimeField(db_column="updatedAt", auto_now=True)
name = models.CharField(unique=True)

class Meta:
Expand All @@ -298,14 +299,15 @@ class Meta:
class OrganizationTagOrganizationsOrganization(models.Model):
"""The OrganizationTagOrganizationsOrganization model."""

organizationTagId = models.OneToOneField(
organizationTagId = models.ForeignKey(
OrganizationTag,
models.DO_NOTHING,
db_column="organizationTagId",
primary_key=True,
) # The composite primary key (organizationTagId, organizationId) found, that is not supported. The first column is selected.
on_delete=models.CASCADE,
db_column="organizationTagId"
)
organizationId = models.ForeignKey(
Organization, models.DO_NOTHING, db_column="organizationId"
Organization,
on_delete=models.CASCADE,
db_column="organizationId"
)

class Meta:
Expand All @@ -314,6 +316,7 @@ class Meta:
managed = False
db_table = "organization_tag_organizations_organization"
unique_together = (("organizationTagId", "organizationId"),)
auto_created = True


class QueryResultCache(models.Model):
Expand Down Expand Up @@ -479,22 +482,24 @@ class Meta:
class Scan(models.Model):
"""The Scan model."""

id = models.UUIDField(primary_key=True)
createdAt = models.DateTimeField(db_column="createdAt")
updatedAt = models.DateTimeField(db_column="updatedAt")
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
createdAt = models.DateTimeField(db_column="createdAt", auto_now_add=True)
updatedAt = models.DateTimeField(db_column="updatedAt", auto_now=True)
name = models.CharField()
arguments = models.JSONField()
arguments = models.TextField() # JSON in the database but fails: the JSON object must be str, bytes or bytearray, not dict
frequency = models.IntegerField()
lastRun = models.DateTimeField(db_column="lastRun", blank=True, null=True)
isGranular = models.BooleanField(db_column="isGranular")
isGranular = models.BooleanField(db_column="isGranular", default=False)
isUserModifiable = models.BooleanField(
db_column="isUserModifiable", blank=True, null=True
db_column="isUserModifiable", blank=True, null=True, default=False
)
isSingleScan = models.BooleanField(db_column="isSingleScan")
manualRunPending = models.BooleanField(db_column="manualRunPending")
isSingleScan = models.BooleanField(db_column="isSingleScan", default=False)
manualRunPending = models.BooleanField(db_column="manualRunPending", default=False)
createdBy = models.ForeignKey(
"User", models.DO_NOTHING, db_column="createdById", blank=True, null=True
)
tags = models.ManyToManyField('OrganizationTag', through='ScanTagsOrganizationTag', related_name='scans')
organizations = models.ManyToManyField('Organization', through='ScanOrganizationsOrganization', related_name='scans')

class Meta:
"""The Meta class for Scan."""
Expand All @@ -506,37 +511,26 @@ class Meta:
class ScanOrganizationsOrganization(models.Model):
"""The ScanOrganizationsOrganization model."""

scanId = models.OneToOneField(
Scan, models.DO_NOTHING, db_column="scanId", primary_key=True
) # The composite primary key (scanId, organizationId) found, that is not supported. The first column is selected.
organizationId = models.ForeignKey(
Organization, models.DO_NOTHING, db_column="organizationId"
)
scanId = models.ForeignKey('Scan', on_delete=models.CASCADE, db_column="scanId", primary_key=True)
organizationId = models.ForeignKey('Organization', on_delete=models.CASCADE, db_column="organizationId", primary_key=True)

class Meta:
"""The Meta class for ScanOrganizationsOrganization."""

managed = False
db_table = "scan_organizations_organization"
unique_together = (("scanId", "organizationId"),)
# Do not create an id column automatically, treat both columns as composite primary keys
auto_created = True


class ScanTagsOrganizationTag(models.Model):
"""The ScanTagsOrganizationTag model."""
"""Intermediary model for the Many-to-Many relationship between Scan and OrganizationTag."""

scanId = models.OneToOneField(
Scan, models.DO_NOTHING, db_column="scanId", primary_key=True
) # The composite primary key (scanId, organizationTagId) found, that is not supported. The first column is selected.
organizationTagId = models.ForeignKey(
OrganizationTag, models.DO_NOTHING, db_column="organizationTagId"
)
scanId = models.ForeignKey('Scan', on_delete=models.CASCADE, db_column="scanId", primary_key=True)
organizationTagId = models.ForeignKey('OrganizationTag', on_delete=models.CASCADE, db_column="organizationTagId", primary_key=True)

class Meta:
"""The Meta class for ScanTagsOrganizationTag."""

managed = False
db_table = "scan_tags_organization_tag"
unique_together = (("scanId", "organizationTagId"),)
auto_created = True


class ScanTask(models.Model):
Expand Down
Loading

0 comments on commit 03351c9

Please sign in to comment.