Skip to content

Commit

Permalink
Merge branch 'fix-auto-archiving' into 'main'
Browse files Browse the repository at this point in the history
Fix auto archiving task

See merge request reportcreator/reportcreator!471
  • Loading branch information
MWedl committed Mar 4, 2024
2 parents 430e759 + c7b828c commit 44019b1
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Prevent cutting off spellcheck error underlines in string fields
* Add more language variants for spellcheck
* Allow duplicating finding templates
* Fix error in periodic task for automatic project archiving


## v2024.16 - 2024-02-22
Expand Down
7 changes: 3 additions & 4 deletions api/src/reportcreator_api/pentests/querysets.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from reportcreator_api.users.models import PentestUser
from reportcreator_api.utils.files import normalize_filename
from reportcreator_api.utils.history import bulk_create_with_history, bulk_delete_with_history, bulk_update_with_history, history_context
from reportcreator_api.utils.models import SubqueryCount
from reportcreator_api.utils.utils import groupby_to_dict, omit_keys, get_key_or_attr, set_key_or_attr
from reportcreator_api.archive.crypto import pgp

Expand Down Expand Up @@ -459,12 +460,10 @@ def filter_projects_can_be_archived(self, projects):
archive_user_count = PentestUser.objects \
.filter(models.Q(is_global_archiver=True) | models.Q(projectmemberinfo__project=models.OuterRef('pk'))) \
.only_active() \
.only_with_public_keys() \
.annotate(user_count=models.Count('id', distinct=True)) \
.values('user_count')
.only_with_public_keys()

return projects \
.annotate(archive_user_count=models.Subquery(archive_user_count)) \
.annotate(archive_user_count=SubqueryCount(archive_user_count)) \
.filter(archive_user_count__gte=settings.ARCHIVING_THRESHOLD)

@transaction.atomic()
Expand Down
6 changes: 3 additions & 3 deletions api/src/reportcreator_api/pentests/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,20 +168,20 @@ def reset_stale_archive_restores(task_info):


async def automatically_archive_projects(task_info):
if not settings.AUTOMATICALLY_ARCHIVE_PROJECTS_AFTER or not license.is_professional():
if not settings.AUTOMATICALLY_ARCHIVE_PROJECTS_AFTER or not await license.ais_professional():
return

projects_to_archive = PentestProject.objects \
.only_archivable() \
.filter(readonly=True) \
.filter(readonly_since__lt=timezone.now() - settings.AUTOMATICALLY_ARCHIVE_PROJECTS_AFTER) \
.only_archivable()

async for p in projects_to_archive:
await sync_to_async(ArchivedProject.objects.create_from_project)(p)


async def automatically_delete_archived_projects(task_info):
if not settings.AUTOMATICALLY_DELETE_ARCHIVED_PROJECTS_AFTER or not license.is_professional():
if not settings.AUTOMATICALLY_DELETE_ARCHIVED_PROJECTS_AFTER or not await license.ais_professional():
return

await ArchivedProject.objects \
Expand Down
1 change: 1 addition & 0 deletions api/src/reportcreator_api/tests/test_periodic_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ def setUp(self):
yield

def test_archived(self):
create_user(public_key=True, is_global_archiver=True)
project_active = create_project(readonly=False, members=[self.user])

with mock_time(after=timedelta(days=40)):
Expand Down
4 changes: 4 additions & 0 deletions api/src/reportcreator_api/utils/license.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ def is_professional():
return check_license().get('type', LicenseType.COMMUNITY) == LicenseType.PROFESSIONAL


async def ais_professional():
return (await acheck_license()).get('type', LicenseType.COMMUNITY) == LicenseType.PROFESSIONAL


def validate_login_allowed(user):
if not is_professional() and not user.is_superuser:
raise LicenseError('Only superusers are allowed to login. A Professional license is required to enable user roles.')
Expand Down
6 changes: 6 additions & 0 deletions api/src/reportcreator_api/utils/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,9 @@ def wrapper(*args, **kwargs):
return
signal_handler(*args, **kwargs)
return wrapper


class SubqueryCount(models.Subquery):
template = "(SELECT count(*) FROM (%(subquery)s) _count)"
output_field = models.PositiveIntegerField()

0 comments on commit 44019b1

Please sign in to comment.