Commit fc2fd5ff authored by Siiri Tann's avatar Siiri Tann
Browse files

Merge branch 'develop' into 'master'

Release: merge 'develop' into 'master' created by Siiri Tann

See merge request teis/files-service!62
parents 160a17e6 f5e861f7
# Changelog
## [1.20.0] - 2021-05-31
* TEIS-2127: added functionality for marking api test file scan status clean without scanning in development
environments if filename contains magic string
## [1.19.0] - 2021-04-12
* TEIS-2102: improved file upload and virus scan handling when file does not exist in Minio S3 bucket
......
theGroup=ee.sm.ti.teis
theVersion=1.19.0
commonsVersion=1.23.0
commonApiGatewayVersion=1.23.0
theVersion=1.20.0
commonsVersion=1.25.0
commonApiGatewayVersion=1.25.0
pluginVersion=1.3.0
schedulerVersion=1.15.0
......@@ -10,6 +10,7 @@ import ee.sm.ti.teis.file.FileRepository;
import ee.sm.ti.teis.file.FilesServiceAppTestBase;
import ee.sm.ti.teis.file.pointer.FilePointerEntity;
import ee.sm.ti.teis.file.pointer.FilePointerRepository;
import ee.sm.ti.teis.file.service.FileComposeService;
import ee.sm.ti.teis.fileclient.FileStorageServiceClient;
import ee.sm.ti.teis.servicerequest.RequestMetaDTO;
import ee.sm.ti.teis.servicerequest.UserType;
......@@ -19,11 +20,14 @@ import org.junit.jupiter.api.Test;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.util.ReflectionTestUtils;
import javax.transaction.Transactional;
import java.io.IOException;
import java.util.UUID;
import static ee.sm.ti.teis.types.enums.FileScanStatusType.CLEAN;
import static ee.sm.ti.teis.types.enums.FileScanStatusType.UNCHECKED;
import static ee.sm.ti.teis.types.enums.FileStatusType.FAILED;
import static ee.sm.ti.teis.types.enums.ObjectStatus.CURRENT;
import static ee.sm.ti.teis.types.enums.ObjectStatus.DELETED;
......@@ -53,6 +57,8 @@ class FileGwListenerTest extends FilesServiceAppTestBase {
FileRepository fileRepository;
@Autowired
FilePointerRepository filePointerRepository;
@Autowired
FileComposeService fileComposeService;
RequestMetaDTO requestMetaDTO;
UpdateFileMetaRequest updateMetadataRequest;
......@@ -60,6 +66,8 @@ class FileGwListenerTest extends FilesServiceAppTestBase {
@BeforeEach
void setUp() throws IOException {
ReflectionTestUtils.setField(fileComposeService, "cleanFileMagicString", "api-test");
requestMetaDTO = createRequestMeta(UUID.randomUUID().toString(), UserType.OFFICIAL_USER,
UUID.randomUUID().toString(), emptyList());
......@@ -106,6 +114,30 @@ class FileGwListenerTest extends FilesServiceAppTestBase {
assertThat(filePointerEntity.getObjectId()).isEqualTo(UUID.fromString("40395578-29c2-4bd7-ba7e-c1b48da1deed"));
}
@Test
void save_apiTestMagicString_success() throws JsonProcessingException {
UUID fileId = UUID.fromString("6681fc31-6756-45c1-a16e-63abcfef829f");
FileMetadataDto dto = FileMetadataDto.builder()
.fileId(fileId)
.contentType("image/jpeg")
.size(123L)
.fileName("file-for-api-test.jpg")
.objectStatus(CURRENT)
.objectId(UUID.fromString("40395578-29c2-4bd7-ba7e-c1b48da1deed"))
.build();
FileEntity fileEntity = fileRepository.findById(fileId).orElse(new FileEntity());
assertThat(fileEntity.getId()).isEqualTo(fileId);
assertThat(fileEntity.getScanStatus()).isEqualTo(UNCHECKED);
updateMetadataRequest.setPayload(dto, requestMetaDTO);
listener.updateMetadata(updateMetadataRequest);
fileEntity = fileRepository.findById(fileId).orElse(new FileEntity());
assertThat(fileEntity.getId()).isEqualTo(fileId);
assertThat(fileEntity.getScanStatus()).isEqualTo(CLEAN);
}
@Test
void handleFileFailure_success() {
Parameters parameters = new Parameters();
......
......@@ -8,9 +8,12 @@ import ee.sm.ti.teis.file.FileMapper;
import ee.sm.ti.teis.file.domain.FilePointer;
import ee.sm.ti.teis.file.pointer.FilePointerEntity;
import ee.sm.ti.teis.file.pointer.FilePointerMapper;
import ee.sm.ti.teis.servicecommon.util.ProfilesUtil;
import ee.sm.ti.teis.servicerequest.RequestMetaDTO;
import ee.sm.ti.teis.types.enums.FileScanStatusType;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import java.util.UUID;
......@@ -29,6 +32,12 @@ public class FileComposeService {
private final FileMapper fileMapper;
private final FilePointerMapper filePointerMapper;
@Value("${teis.api-test.file.clean-check-magic-string}")
private String cleanFileMagicString;
@Autowired
private Environment environment;
public FileEntity composeNewEntity(UUID fileId, RequestMetaDTO requestMetaDTO) {
FileEntity file = new FileEntity();
file.setId(fileId);
......@@ -57,12 +66,20 @@ public class FileComposeService {
entity.setCreatorId(UUID.fromString(requestMetaDTO.getUserId()));
entity.setBucket(dto.getBucket());
FileScanStatusType scanStatus = isSystemGenerated ? CLEAN : UNCHECKED;
entity.setScanStatus(scanStatus);
setFileScanStatus(isSystemGenerated, entity);
return entity;
}
private void setFileScanStatus(boolean isSystemGenerated, FileEntity entity) {
if (isSystemGenerated ||
(entity.getName().contains(cleanFileMagicString) && ProfilesUtil.isTestProfile(environment))) {
entity.setScanStatus(CLEAN);
} else {
entity.setScanStatus(UNCHECKED);
}
}
public FileMetadata composeFileMetadataDomain(FileEntity entity) {
return fileMapper.toFileMetadataDomain(entity);
}
......
......@@ -9,6 +9,8 @@ import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import static ee.sm.ti.teis.types.enums.FileScanStatusType.CLEAN;
import static ee.sm.ti.teis.types.enums.FileScanStatusType.INFECTED;
import static ee.sm.ti.teis.types.enums.ObjectStatus.CURRENT;
@Service
......@@ -37,7 +39,8 @@ public class FileMetadataService {
fileMetadataNotifyService.notifyMetadataSaved(fileMetadata, requestMetaDTO);
if (CURRENT.equals(savedEntity.getObjectStatus())) {
if (CURRENT.equals(savedEntity.getObjectStatus()) &&
!(CLEAN.equals(savedEntity.getScanStatus()) || INFECTED.equals(savedEntity.getScanStatus()))) {
virusScanProcessService.startProcess(dto.getFileId(), requestMetaDTO);
}
......
......@@ -62,6 +62,9 @@ teis:
test:
data:
resetmagic: ${TEST_DATA_RESET_MAGIC:}
api-test:
file:
clean-check-magic-string: api-test
camunda:
virus-scan:
retry-timeout: ${CAMUNDA_VIRUS_SCAN_RETRY_TIMEOUT:PT60M}
......
......@@ -30,7 +30,8 @@ VALUES ('27d766ca-e96c-4fac-800e-09ac2444732e', '2019-11-15 10:09:11.726442', nu
('124fefc1-0e35-4979-8dec-832a7413a005', '2019-11-15 10:09:11.726442', null, '2019-11-15 10:09:11.726442', null, 'test-riskassessment-file-scan-job.txt', '5439848f-684f-4cab-84b4-4305af15cd5b', 'SYSTEM', 1370, 'text/plain', 'ACTIVE', 'CURRENT', 'UNCHECKED', null),
('42b90d85-1be8-486b-b912-fe1c1de61529', '2019-11-15 10:09:11.726442', null, '2019-11-15 10:09:11.726442', null, 'test-riskassessment-file-scan-job.txt', '5439848f-684f-4cab-84b4-4305af15cd5b', 'SYSTEM', 1370, 'text/plain', 'ACTIVE', 'CURRENT', 'UNCHECKED', 4),
('4ad94c27-5dfb-4113-b13e-8b35b31f86df', '2020-01-03 11:23:33.000000', 'TEIS-1661-created-by-TEST', '2020-03-02 11:24:33.000000', 'TEIS-1661-modified-by-TEST', 'actTestFile.pdf', '5439848f-684f-4cab-84b4-4305af15cd5b', 'SYSTEM', 1370, 'text/plain', 'ACTIVE', 'CURRENT', 'UNCHECKED', 1),
('6e23a57e-1913-47bb-938e-79432eafe50c', '2020-02-03 11:23:33.000000', 'TEIS-1661-created-by-TEST', '2020-04-02 11:24:33.000000', 'TEIS-1661-modified-by-TEST', 'actTestFile.pdf', '5439848f-684f-4cab-84b4-4305af15cd5b', 'SYSTEM', 1370, 'text/plain', 'ACTIVE', 'CURRENT', 'UNCHECKED', 1);
('6e23a57e-1913-47bb-938e-79432eafe50c', '2020-02-03 11:23:33.000000', 'TEIS-1661-created-by-TEST', '2020-04-02 11:24:33.000000', 'TEIS-1661-modified-by-TEST', 'actTestFile.pdf', '5439848f-684f-4cab-84b4-4305af15cd5b', 'SYSTEM', 1370, 'text/plain', 'ACTIVE', 'CURRENT', 'UNCHECKED', 1),
('6681fc31-6756-45c1-a16e-63abcfef829f', '2020-02-03 11:23:33.000000', 'api-test-magic', '2020-04-02 11:24:33.000000', 'api-test-magic', 'file-for-api-test.jpg', '5439848f-684f-4cab-84b4-4305af15cd5b', 'SYSTEM', 1370, 'image.jpeg', 'ACTIVE', 'CURRENT', 'UNCHECKED', 1);
INSERT INTO file_pointer (id, created_at, created_by, modified_at, modified_by, object_id, object_type, file_id, object_status)
VALUES ('af1fa838-bf7f-4ec6-969e-af2904544e46', '2019-11-15 10:09:11.726442', null, '2019-11-15 10:09:11.755395', null, '40395578-29c2-4bd7-ba7e-c1b48da1deed', 'OBJECT_TYPE__VIOLATION', '27d766ca-e96c-4fac-800e-09ac2444732e', 'CURRENT'),
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment