Skip to content

Commit

Permalink
api_app: AnalysisResponseSerializer, md5 calc fixes (#731)
Browse files Browse the repository at this point in the history
  • Loading branch information
eshaan7 committed Oct 11, 2021
1 parent aa8c27f commit de44555
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 58 deletions.
50 changes: 17 additions & 33 deletions api_app/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,25 @@ def _analysis_request(
),
)

response_dict = {
"status": "accepted",
"job_id": job.pk,
"warnings": warnings,
"analyzers_running": cleaned_analyzer_list,
"connectors_running": cleaned_connectors_list,
}
ser = serializers.AnalysisResponseSerializer(
data={
"status": "accepted",
"job_id": job.pk,
"warnings": warnings,
"analyzers_running": cleaned_analyzer_list,
"connectors_running": cleaned_connectors_list,
}
)
ser.is_valid(raise_exception=True)

response_dict = ser.data

logger.debug(response_dict)

return Response(
response_dict, status=status.HTTP_200_OK
) # lgtm [py/stack-trace-exposure]
response_dict,
status=status.HTTP_200_OK,
)


""" REST API endpoints """
Expand Down Expand Up @@ -188,18 +194,7 @@ def ask_analysis_availability(request):
@add_docs(
description="This endpoint allows to start a Job related to a file",
request=serializers.FileAnalysisSerializer,
responses={
200: inline_serializer(
"FileAnalysisResponseSerializer",
fields={
"status": rfs.StringRelatedField(),
"job_id": rfs.IntegerField(),
"warnings": OpenApiTypes.OBJECT,
"analyzers_running": OpenApiTypes.OBJECT,
"connectors_running": OpenApiTypes.OBJECT,
},
),
},
responses={200: serializers.AnalysisResponseSerializer},
)
@api_view(["POST"])
@permission_required_or_403("api_app.add_job")
Expand All @@ -210,18 +205,7 @@ def analyze_file(request):
@add_docs(
description="This endpoint allows to start a Job related to an observable",
request=serializers.ObservableAnalysisSerializer,
responses={
200: inline_serializer(
"ObservableAnalysisResponseSerializer",
fields={
"status": rfs.StringRelatedField(),
"job_id": rfs.IntegerField(),
"warnings": OpenApiTypes.OBJECT,
"analyzers_running": OpenApiTypes.OBJECT,
"connectors_running": OpenApiTypes.OBJECT,
},
),
},
responses={200: serializers.AnalysisResponseSerializer},
)
@api_view(["POST"])
@permission_required_or_403("api_app.add_job")
Expand Down
13 changes: 12 additions & 1 deletion api_app/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"JobSerializer",
"FileAnalysisSerializer",
"ObservableAnalysisSerializer",
"AnalysisResponseSerializer",
]


Expand Down Expand Up @@ -221,7 +222,9 @@ def validate(self, attrs: dict) -> dict:
# calculate ``file_mimetype``
attrs["file_mimetype"] = calculate_mimetype(attrs["file"], attrs["file_name"])
# calculate ``md5``
file_buffer = attrs["file"].file.read()
file_obj = attrs["file"].file
file_obj.seek(0)
file_buffer = file_obj.read()
attrs["md5"] = calculate_md5(file_buffer)
logger.debug(f"after attrs: {attrs}")
return attrs
Expand Down Expand Up @@ -268,3 +271,11 @@ def validate(self, attrs: dict) -> dict:
attrs["md5"] = calculate_md5(attrs["observable_name"].encode("utf-8"))
logger.debug(f"after attrs: {attrs}")
return attrs


class AnalysisResponseSerializer(serializers.Serializer):
job_id = serializers.IntegerField()
status = serializers.CharField()
warnings = serializers.ListField()
analyzers_running = serializers.ListField()
connectors_running = serializers.ListField()
48 changes: 24 additions & 24 deletions docs/source/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/FileAnalysisResponse'
$ref: '#/components/schemas/AnalysisResponse'
description: ''
/api/analyze_observable:
post:
Expand All @@ -55,7 +55,7 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/ObservableAnalysisResponse'
$ref: '#/components/schemas/AnalysisResponse'
description: ''
/api/analyzer/{name}/healthcheck:
get:
Expand Down Expand Up @@ -593,6 +593,28 @@ paths:
description: No response body
components:
schemas:
AnalysisResponse:
type: object
properties:
job_id:
type: integer
status:
type: string
warnings:
type: array
items: {}
analyzers_running:
type: array
items: {}
connectors_running:
type: array
items: {}
required:
- analyzers_running
- connectors_running
- job_id
- status
- warnings
AnalyzerConfig:
type: object
description: Serializer for `analyzer_config.json`.
Expand Down Expand Up @@ -840,17 +862,6 @@ components:
- file
- file_name
- id
FileAnalysisResponse:
type: object
properties:
status:
type: string
readOnly: true
job_id:
type: integer
required:
- job_id
- status
GetAnalyzerConfigsFailedResponse:
type: object
properties:
Expand Down Expand Up @@ -1126,17 +1137,6 @@ components:
required:
- id
- observable_name
ObservableAnalysisResponse:
type: object
properties:
status:
type: string
readOnly: true
job_id:
type: integer
required:
- job_id
- status
ObservableSupportedEnum:
enum:
- ip
Expand Down

0 comments on commit de44555

Please sign in to comment.