Skip to content

Commit

Permalink
Added clamav integration (cvat-ai#1712)
Browse files Browse the repository at this point in the history
* added clamav integration

* updated license headers and changelog
  • Loading branch information
azhavoro authored Jun 16, 2020
1 parent 1385dc4 commit b706546
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Shortcut to switch split mode (<https://github.com/opencv/cvat/pull/1683>)
- Built-in search for labels when create an object or change a label (<https://github.com/opencv/cvat/pull/1683>)
- Better validation of labels and attributes in raw viewer (<https://github.com/opencv/cvat/pull/1727>)
- ClamAV antivirus integration (<https://github.com/opencv/cvat/pull/1712>)

### Changed
- Removed information about e-mail from the basic user information (<https://github.com/opencv/cvat/pull/1627>)
Expand Down
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,19 @@ RUN if [ "$WITH_DEXTR" = "yes" ]; then \
7z e ${DEXTR_MODEL_DIR}/dextr.zip -o${DEXTR_MODEL_DIR} && rm ${DEXTR_MODEL_DIR}/dextr.zip; \
fi

ARG CLAM_AV
ENV CLAM_AV=${CLAM_AV}
RUN if [ "$CLAM_AV" = "yes" ]; then \
apt-get update && \
apt-get --no-install-recommends install -yq \
clamav \
libclamunrar9 && \
sed -i 's/ReceiveTimeout 30/ReceiveTimeout 300/g' /etc/clamav/freshclam.conf && \
freshclam && \
chown -R ${USER}:${USER} /var/lib/clamav && \
rm -rf /var/lib/apt/lists/*; \
fi

COPY ssh ${HOME}/.ssh
COPY utils ${HOME}/utils
COPY cvat/ ${HOME}/cvat
Expand Down
13 changes: 13 additions & 0 deletions cvat/apps/auto_annotation/model_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from cvat.apps.engine.serializers import LabeledDataSerializer
from cvat.apps.dataset_manager.task import put_task_data, patch_task_data
from cvat.apps.engine.frame_provider import FrameProvider
from cvat.apps.engine.utils import av_scan_paths

from .models import AnnotationModel, FrameworkChoice
from .model_loader import load_labelmap
Expand Down Expand Up @@ -139,6 +140,7 @@ def save_file_as_tmp(data):
tmp_file.write(chunk)
os.close(fd)
return filename

is_create_request = dl_model_id is None
if is_create_request:
dl_model_id = create_empty(owner=owner)
Expand All @@ -155,6 +157,17 @@ def save_file_as_tmp(data):
labelmap_file = save_file_as_tmp(labelmap_file)
interpretation_file = save_file_as_tmp(interpretation_file)

files_to_scan = []
if model_file:
files_to_scan.append(model_file)
if weights_file:
files_to_scan.append(weights_file)
if labelmap_file:
files_to_scan.append(labelmap_file)
if interpretation_file:
files_to_scan.append(interpretation_file)
av_scan_paths(*files_to_scan)

if owner:
restricted = not has_admin_role(owner)
else:
Expand Down
5 changes: 4 additions & 1 deletion cvat/apps/engine/task.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

# Copyright (C) 2018 Intel Corporation
# Copyright (C) 2018-2020 Intel Corporation
#
# SPDX-License-Identifier: MIT

Expand All @@ -15,6 +15,7 @@

from cvat.apps.engine.media_extractors import get_mime, MEDIA_TYPES, Mpeg4ChunkWriter, ZipChunkWriter, Mpeg4CompressedChunkWriter, ZipCompressedChunkWriter
from cvat.apps.engine.models import DataChoice
from cvat.apps.engine.utils import av_scan_paths

import django_rq
from django.conf import settings
Expand Down Expand Up @@ -223,6 +224,8 @@ def _create_thread(tid, data):
if data['server_files']:
_copy_data_from_share(data['server_files'], upload_dir)

av_scan_paths(upload_dir)

job = rq.get_current_job()
job.meta['status'] = 'Media files are being extracted...'
job.save_meta()
Expand Down
16 changes: 16 additions & 0 deletions cvat/apps/engine/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
# Copyright (C) 2020 Intel Corporation
#
# SPDX-License-Identifier: MIT

import ast
from collections import namedtuple
import importlib
import sys
import traceback
import subprocess
import os

from django.core.exceptions import ValidationError

Import = namedtuple("Import", ["module", "name", "alias"])

Expand Down Expand Up @@ -58,3 +66,11 @@ def execute_python_code(source_code, global_vars=None, local_vars=None):
_, _, tb = sys.exc_info()
line_number = traceback.extract_tb(tb)[-1][1]
raise InterpreterError("{} at line {}: {}".format(error_class, line_number, details))

def av_scan_paths(*paths):
if 'yes' == os.environ.get('CLAM_AV'):
command = ['clamscan', '--no-summary', '-i', '-o']
command.extend(paths)
res = subprocess.run(command, capture_output=True)
if res.returncode:
raise ValidationError(res.stdout)
5 changes: 4 additions & 1 deletion cvat/apps/engine/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2018-2019 Intel Corporation
# Copyright (C) 2018-2020 Intel Corporation
#
# SPDX-License-Identifier: MIT

Expand Down Expand Up @@ -45,6 +45,7 @@
LogEventSerializer, PluginSerializer, ProjectSerializer,
RqStatusSerializer, TaskSerializer, UserSerializer)
from cvat.settings.base import CSS_3RDPARTY, JS_3RDPARTY
from cvat.apps.engine.utils import av_scan_paths

from . import models, task
from .log import clogger, slogger
Expand Down Expand Up @@ -821,6 +822,8 @@ def _import_annotations(request, rq_id, rq_func, pk, format_name):
with open(filename, 'wb+') as f:
for chunk in anno_file.chunks():
f.write(chunk)

av_scan_paths(filename)
rq_job = queue.enqueue_call(
func=rq_func,
args=(pk, filename, format_name),
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ services:
DJANGO_CONFIGURATION: "production"
TZ: "Etc/UTC"
OPENVINO_TOOLKIT: "no"
CLAM_AV: "no"
environment:
DJANGO_MODWSGI_EXTRA_ARGS: ""
ALLOWED_HOSTS: '*'
Expand Down
5 changes: 5 additions & 0 deletions supervisord.conf
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ command=%(ENV_HOME)s/wait-for-it.sh %(ENV_CVAT_REDIS_HOST)s:6379 -t 0 -- bash -i
environment=SSH_AUTH_SOCK="/tmp/ssh-agent.sock"
numprocs=1

[program:clamav_update]
command=bash -c "if [ \"${CLAM_AV}\" = 'yes' ]; then /usr/bin/freshclam -d \
-l %(ENV_HOME)s/logs/freshclam.log --foreground=true; fi"
numprocs=1

[program:runserver]
; Here need to run a couple of commands to initialize DB and copy static files.
; We cannot initialize DB on build because the DB should be online. Also some
Expand Down

0 comments on commit b706546

Please sign in to comment.