SUPPORT-9157: change to list

This commit is contained in:
Alexandr Shalaginov 2025-05-05 12:17:45 +03:00
parent bad10b0324
commit a0eaa3ed59
6 changed files with 70 additions and 20 deletions

View file

@ -35,19 +35,15 @@ spring:
server: server:
port: 8080 # Порт, на котором работает сервис port: 8080 # Порт, на котором работает сервис
``` ```
Если KESL использует русскую локализацию, то следует в файл `application.yaml` добавить следующий параметр: Для проверки ответов от KESL, при обнаружении архива с паролем, следует в файл `application.yaml` добавить следующий параметр:
```yaml ```yaml
password: password:
protected: protected:
result: result:
name: 'Объекты, защищенные паролем' names: [
``` 'Объекты, защищенные паролем',
Для английской локализации: 'Password-protected objects'
```yaml ]
password:
protected:
result:
name: 'Password-protected objects'
``` ```
### **2. Запуск JAR-файла с конфигурационным файлом** ### **2. Запуск JAR-файла с конфигурационным файлом**
```bash ```bash

View file

@ -22,6 +22,12 @@
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<scope>compile</scope>
</dependency>
<dependency> <dependency>
<groupId>org.zeroturnaround</groupId> <groupId>org.zeroturnaround</groupId>
<artifactId>zt-exec</artifactId> <artifactId>zt-exec</artifactId>

View file

@ -2,8 +2,11 @@ package ru.micord.av.service;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import ru.micord.av.service.property.PasswordProtectedResultNamesProperty;
@SpringBootApplication @SpringBootApplication
@EnableConfigurationProperties(value = PasswordProtectedResultNamesProperty.class)
public class AvServiceApplication { public class AvServiceApplication {
public static void main(String[] args) { public static void main(String[] args) {

View file

@ -0,0 +1,26 @@
package ru.micord.av.service.property;
import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @author Alexandr Shalaginov
*/
@Component
@ConfigurationProperties(prefix = "password.protected.result")
public class PasswordProtectedResultNamesProperty {
private List<String> names = List.of(
"Объекты, защищенные паролем",
"Password-protected objects"
);
public List<String> getNames() {
return names;
}
public void setNames(List<String> names) {
this.names = names;
}
}

View file

@ -9,6 +9,7 @@ import java.util.concurrent.TimeoutException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
@ -16,6 +17,7 @@ import org.zeroturnaround.exec.ProcessExecutor;
import org.zeroturnaround.exec.ProcessResult; import org.zeroturnaround.exec.ProcessResult;
import org.zeroturnaround.exec.stream.slf4j.Slf4jStream; import org.zeroturnaround.exec.stream.slf4j.Slf4jStream;
import ru.micord.av.service.exception.AvException; import ru.micord.av.service.exception.AvException;
import ru.micord.av.service.property.PasswordProtectedResultNamesProperty;
/** /**
* @author Adel Kalimullin * @author Adel Kalimullin
@ -25,10 +27,15 @@ public class FileScanService {
private static final String KESL_CONTROL = "kesl-control"; private static final String KESL_CONTROL = "kesl-control";
private static final String KESL_SCAN = "--scan-file"; private static final String KESL_SCAN = "--scan-file";
private static final Logger LOGGER = LoggerFactory.getLogger(FileScanService.class); private static final Logger LOGGER = LoggerFactory.getLogger(FileScanService.class);
@Value("${file.upload.directory:/tmp/uploaded_files}") private final String uploadDirectory;
private String uploadDirectory; private final PasswordProtectedResultNamesProperty passwordProtectedResultNamesProperty;
@Value("${password.protected.result.name:Объекты, защищенные паролем}")
private String passwordProtectedResultName; @Autowired
public FileScanService(@Value("${file.upload.directory:/tmp/uploaded_files}") String uploadDirectory,
PasswordProtectedResultNamesProperty passwordProtectedResultNamesProperty) {
this.uploadDirectory = uploadDirectory;
this.passwordProtectedResultNamesProperty = passwordProtectedResultNamesProperty;
}
public int scanFile(MultipartFile file) { public int scanFile(MultipartFile file) {
File tempFile = null; File tempFile = null;
@ -80,19 +87,28 @@ public class FileScanService {
} }
private void checkScanResult(String result, String originalFileName) { private void checkScanResult(String result, String originalFileName) {
if (!result.contains(passwordProtectedResultName)) { boolean isResultContains = passwordProtectedResultNamesProperty.getNames().stream()
.filter(result::contains)
.toList()
.isEmpty();
if (!isResultContains) {
throw new AvException(String.format( throw new AvException(String.format(
"File scan result doesn't contains \"%s\", " "File scan result doesn't contains any of values: \"%s\". "
+ "please check property \"password.protected.result.name\" is correct", + "Please check property \"password.protected.result.names\" is correct",
passwordProtectedResultName passwordProtectedResultNamesProperty.getNames()
)); ));
} }
for (String line : result.split("\n")) { for (String line : result.split("\n")) {
String[] lineParts = line.split(":"); String[] lineParts = line.split(":");
if (lineParts.length > 1) { if (lineParts.length > 1) {
if (lineParts[0].startsWith(passwordProtectedResultName) boolean isLinePartStartWith = passwordProtectedResultNamesProperty.getNames().stream()
&& Integer.parseInt(lineParts[1].trim()) > 0) { .filter(lineParts[0]::startsWith)
.toList()
.isEmpty();
if (isLinePartStartWith && Integer.parseInt(lineParts[1].trim()) > 0) {
throw new AvException("Detected password-protected file: " + originalFileName); throw new AvException("Detected password-protected file: " + originalFileName);
} }
} }

View file

@ -5,4 +5,7 @@ file:
password: password:
protected: protected:
result: result:
name: 'Объекты, защищенные паролем' names: [
'Объекты, защищенные паролем',
'Password-protected objects'
]