Skip to content

Commit 0124a13

Browse files
Merge pull request #249 from BenediktMKuehne/sbom_api
quick_Sbom_api
2 parents 268c1c4 + 47115d6 commit 0124a13

File tree

7 files changed

+294
-362
lines changed

7 files changed

+294
-362
lines changed

Pipfile.lock

Lines changed: 258 additions & 355 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

VERSION.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.2-051390d9
1+
0.3

embark/dashboard/urls.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@
2626
path('dashboard/report/addlabel/<uuid:analysis_id>', views.add_label, name='embark-dashboard-add-label'),
2727
path('dashboard/report/rmlabel/<uuid:analysis_id><str:label_name>', views.rm_label, name='embark-dashboard-remove-label'),
2828
path('dashboard/report/sbom/<uuid:analysis_id>', views.get_sbom_analysis, name='embark-dashboard-sbom'),
29-
path('dashboard/report/sbom/<uuid:sbom_id>', views.get_sbom, name='embark-get-sbom')
29+
path('dashboard/report/sbom/<uuid:sbom_id>', views.get_sbom, name='embark-get-sbom'),
30+
path('api/sbom/<uuid:analysis_id>', views.api_sbom_analysis, name='api-sbom-analysis')
3031
]

embark/dashboard/views.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from dashboard.models import Result, SoftwareBillOfMaterial
2626
from dashboard.forms import LabelSelectForm, StopAnalysisForm
2727
from porter.views import make_zip
28+
from users.decorators import require_api_key
2829

2930

3031
logger = logging.getLogger(__name__)
@@ -437,3 +438,28 @@ def get_sbom_analysis(request, analysis_id):
437438
response['Content-Disposition'] = 'inline; filename=' + str(analysis_id) + '_sbom.json'
438439
messages.success(request, 'Analysis: ' + str(analysis_id) + ' successfully exported sbom')
439440
return response
441+
442+
443+
@require_api_key
444+
@require_http_methods(["GET"])
445+
def api_sbom_analysis(request, analysis_id):
446+
"""
447+
exports sbom as raw json
448+
"""
449+
logger.info("export sbom with analysis id: %s", analysis_id)
450+
response = JsonResponse({"ERROR": "SBOM for this analysis-id does not exist"})
451+
try:
452+
analysis = FirmwareAnalysis.objects.get(id=analysis_id)
453+
result = Result.objects.get(firmware_analysis=analysis)
454+
sbom = result.sbom
455+
except Result.DoesNotExist:
456+
response = JsonResponse({"ERROR": "Result for this analysis-id does not exist"})
457+
# check if user auth
458+
if not user_is_auth(request.user, analysis.user):
459+
response = JsonResponse({"ERROR": "You are not authorized!"})
460+
if sbom is not None:
461+
with open(sbom.file, "r", encoding='UTF-8') as sbom_file:
462+
response = JsonResponse(json.load(sbom_file))
463+
logger.info("export sbom with analysis id: %s", analysis_id)
464+
response['Content-Disposition'] = 'inline; filename=' + str(analysis_id) + '_sbom.json'
465+
return response

embark/templates/user/login.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
</div>
1111
<div class="d-flex justify-content-center">
1212
<!-- TODO put actual version here-->
13-
<h2><span class="badge bg-primary">New</span> Version 0.2</h2>
13+
<h2><span class="badge bg-primary">New</span> Version 0.3</h2>
1414
</div>
1515
<div class="d-flex justify-content-center">
16-
<h3><span class="badge bg-secondary">New</span> Track you SBOM</h3>
16+
<h3><span class="badge bg-secondary">New</span> Troopers25-edition </h3>
17+
<h3><span class="badge bg-secondary">New</span> API </h3>
1718
</div>
1819
<div class="login">
1920
<form action="{% url 'embark-login' %}" class="login-form" method="post" novalidate>
@@ -35,4 +36,4 @@ <h2 class="title">Sign in</h2>
3536
</div>
3637

3738
</div>
38-
{% endblock maincontent %}
39+
{% endblock maincontent %}

embark/users/decorators.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def _wrapped_view(*args, **kwargs):
2020
try:
2121
user = User.objects.get(api_key=api_key)
2222
request.api_user = user
23+
request.user = user # For compatibility with Django's request.user
2324
except User.DoesNotExist:
2425
return JsonResponse({'error': 'Invalid API key'}, status=401)
2526

supervisor.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ exec 2>&1
2222
while :; do
2323
if ! ip a show embark_backend | grep -q "172.22.0.1" ; then
2424
systemctl restart docker
25-
echo "$(date +"%D %T")""retstarted docker"
25+
echo "$(date +"%D %T")"" restarted docker"
2626
fi
27-
sleep 1m
27+
sleep 2s
2828
done

0 commit comments

Comments
 (0)