Skip to content

Commit

Permalink
Merge branch 'main' into docs/export-http-api-boefje
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanie0x00 authored Jan 21, 2025
2 parents aa68afa + e8eea8e commit 31d939a
Show file tree
Hide file tree
Showing 22 changed files with 458 additions and 399 deletions.
2 changes: 1 addition & 1 deletion octopoes/octopoes/models/ooi/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ class ReportRecipe(OOI):
parent_report_type: str | None = None
report_types: list[str]

cron_expression: str
cron_expression: str | None = None

_natural_key_attrs = ["recipe_id"]
14 changes: 8 additions & 6 deletions rocky/onboarding/templates/step_3c_setup_scan_ooi_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,14 @@ <h3>{% translate "Creating an object" %}</h3>
</div>
</dl>
</div>
<div class="button-container">
<a href="{% url "step_report" organization.code %}?{{ request.GET.urlencode }}"
class="button">{% translate "Start scanning" %}</a>
<a href="{% url "complete_onboarding" organization.code %}"
class="button ghost">{% translate "Skip onboarding" %}</a>
</div>
<form method="post" class="inline">
{% csrf_token %}
<div class="button-container">
<button type="submit">{% translate "Start scanning" %}</button>
<a href="{% url "complete_onboarding" organization.code %}"
class="button ghost">{% translate "Skip onboarding" %}</a>
</div>
</form>
</section>
</main>
{% endblock content %}
74 changes: 49 additions & 25 deletions rocky/onboarding/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@
from katalogus.client import Plugin
from reports.report_types.definitions import ReportPlugins
from reports.report_types.dns_report.report import DNSReport
from reports.views.base import get_selection
from reports.views.generate_report import SaveGenerateReportMixin
from reports.views.base import BaseReportView, get_selection
from tools.models import GROUP_ADMIN, GROUP_CLIENT, GROUP_REDTEAM, Organization, OrganizationMember
from tools.ooi_helpers import get_or_create_ooi
from tools.view_helpers import Breadcrumb

from octopoes.models import OOI
from octopoes.models import OOI, Reference
from octopoes.models.ooi.dns.zone import Hostname
from octopoes.models.ooi.network import Network
from octopoes.models.ooi.web import URL
Expand All @@ -40,8 +39,10 @@
)
from rocky.exceptions import RockyError
from rocky.messaging import clearance_level_warning_dns_report
from rocky.scheduler import scheduler_client
from rocky.views.indemnification_add import IndemnificationAddView
from rocky.views.ooi_view import SingleOOIMixin, SingleOOITreeMixin
from rocky.views.scheduler import SchedulerView

User = get_user_model()

Expand Down Expand Up @@ -310,9 +311,11 @@ def get_context_data(self, **kwargs: Any) -> dict[str, Any]:

class OnboardingSetupScanOOIDetailView(
OrganizationPermissionRequiredMixin,
SingleOOITreeMixin,
IntroductionStepsMixin,
OnboardingBreadcrumbsMixin,
SingleOOIMixin,
BaseReportView,
SchedulerView,
TemplateView,
):
"""
Expand All @@ -322,6 +325,34 @@ class OnboardingSetupScanOOIDetailView(
template_name = "step_3c_setup_scan_ooi_detail.html"
current_step = 3
permission_required = "tools.can_scan_organization"
task_type = "report"

@staticmethod
def is_scheduler_enabled(organization: Organization) -> bool:
scheduler_id = f"report-{organization.code}"
return scheduler_client(organization.code).is_scheduler_ready(scheduler_id)

def post(self, request, *args, **kwargs):
parent_report_name_format, subreport_name_format = self.get_initial_report_names()
parent_report_type = self.get_parent_report_type()
report_recipe = self.create_report_recipe(
parent_report_name_format, subreport_name_format, parent_report_type, None
)
if self.is_scheduler_enabled(self.organization):
self.create_report_schedule(report_recipe, datetime.now(timezone.utc))

return redirect(
reverse("step_report", kwargs={"organization_code": self.organization.code})
+ get_selection(self.request, {"recipe_id": report_recipe.primary_key})
)

def get_ooi_pks(self) -> list[str]:
ooi = self.get_ooi(self.request.GET.get("ooi", ""))
hostname_ooi = [Hostname(name=ooi.web_url.tokenized["netloc"]["name"], network=ooi.network)]
return [hostname_ooi[0].primary_key]

def get_report_type_ids(self) -> list[str]:
return [self.request.GET.get("report_type", "")]

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
Expand All @@ -330,7 +361,7 @@ def get_context_data(self, **kwargs):


class OnboardingReportView(
OrganizationPermissionRequiredMixin, SaveGenerateReportMixin, IntroductionStepsMixin, SingleOOIMixin, TemplateView
OrganizationPermissionRequiredMixin, IntroductionStepsMixin, SingleOOIMixin, BaseReportView, TemplateView
):
"""
10. The user already started the scan and is now waiting till scans are finished to generate the report.
Expand All @@ -341,32 +372,25 @@ class OnboardingReportView(
current_step = 4
permission_required = "tools.can_scan_organization"

def setup(self, request, *args, **kwargs):
super().setup(request, *args, **kwargs)
self.selected_oois = self.get_ooi_pks()
self.selected_report_types = self.get_report_type_ids()

def get_ooi_pks(self) -> list[str]:
ooi = self.get_ooi(self.request.GET.get("ooi", ""))
hostname_ooi = [Hostname(name=ooi.web_url.tokenized["netloc"]["name"], network=ooi.network)]
return [hostname_ooi[0].primary_key]

def get_report_type_ids(self) -> list[str]:
return [self.request.GET.get("report_type", "")]

def post(self, request, *args, **kwargs):
self.set_member_onboarded()

report_ooi = self.save_report([("Onboarding Report", "Onboarding Report")])
recipe_id = request.GET.get("recipe_id", "")

if report_ooi:
return redirect(
reverse("view_report", kwargs={"organization_code": self.organization.code})
+ "?"
+ urlencode({"report_id": report_ooi.reference})
if recipe_id:
reports = self.octopoes_api_connector.query(
"ReportRecipe.<report_recipe[is Report]",
valid_time=datetime.now(timezone.utc),
source=Reference.from_str(recipe_id),
)

return self.get(request, *args, **kwargs)
if reports:
return redirect(
reverse("view_report", kwargs={"organization_code": self.organization.code})
+ "?"
+ urlencode({"report_id": reports[0].reference})
)
return redirect(reverse("scheduled_reports", kwargs={"organization_code": self.organization.code}))

def set_member_onboarded(self):
member = OrganizationMember.objects.get(user=self.request.user, organization=self.organization)
Expand Down
Loading

0 comments on commit 31d939a

Please sign in to comment.