-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[frontend/backend] Import a xls chronogram as a scenario (#1229)
Co-authored-by: Johanah LEKEU <[email protected]> Co-authored-by: Romuald Lemesle <[email protected]>
- Loading branch information
1 parent
20e0fd4
commit 47af850
Showing
85 changed files
with
6,059 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 0 additions & 22 deletions
22
openbas-api/src/main/java/io/openbas/config/GlobalExceptionHandler.java
This file was deleted.
Oops, something went wrong.
70 changes: 70 additions & 0 deletions
70
openbas-api/src/main/java/io/openbas/migration/V3_29__Add_tables_xls_mappers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package io.openbas.migration; | ||
|
||
import org.flywaydb.core.api.migration.BaseJavaMigration; | ||
import org.flywaydb.core.api.migration.Context; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.sql.Connection; | ||
import java.sql.Statement; | ||
|
||
@Component | ||
public class V3_29__Add_tables_xls_mappers extends BaseJavaMigration { | ||
|
||
@Override | ||
public void migrate(Context context) throws Exception { | ||
Connection connection = context.getConnection(); | ||
Statement select = connection.createStatement(); | ||
// Create table | ||
select.execute(""" | ||
CREATE TABLE import_mappers ( | ||
mapper_id UUID NOT NULL CONSTRAINT import_mappers_pkey PRIMARY KEY, | ||
mapper_name VARCHAR(255) NOT NULL, | ||
mapper_inject_type_column VARCHAR(255) NOT NULL, | ||
mapper_created_at TIMESTAMP DEFAULT now(), | ||
mapper_updated_at TIMESTAMP DEFAULT now() | ||
); | ||
CREATE INDEX idx_import_mappers ON import_mappers(mapper_id); | ||
"""); | ||
|
||
select.execute(""" | ||
CREATE TABLE inject_importers ( | ||
importer_id UUID NOT NULL CONSTRAINT inject_importers_pkey PRIMARY KEY, | ||
importer_mapper_id UUID NOT NULL | ||
CONSTRAINT inject_importers_mapper_id_fkey REFERENCES import_mappers(mapper_id) ON DELETE SET NULL, | ||
importer_import_type_value VARCHAR(255) NOT NULL, | ||
importer_injector_contract_id VARCHAR(255) NOT NULL | ||
CONSTRAINT inject_importers_injector_contract_id_fkey REFERENCES injectors_contracts(injector_contract_id) ON DELETE SET NULL, | ||
importer_created_at TIMESTAMP DEFAULT now(), | ||
importer_updated_at TIMESTAMP DEFAULT now() | ||
); | ||
CREATE INDEX idx_inject_importers ON inject_importers(importer_id); | ||
"""); | ||
|
||
|
||
select.execute(""" | ||
CREATE TABLE rule_attributes ( | ||
attribute_id UUID NOT NULL CONSTRAINT rule_attributes_pkey PRIMARY KEY, | ||
attribute_inject_importer_id UUID NOT NULL | ||
CONSTRAINT rule_attributes_importer_id_fkey REFERENCES inject_importers(importer_id) ON DELETE SET NULL, | ||
attribute_name varchar(255) not null, | ||
attribute_columns varchar(255), | ||
attribute_default_value varchar(255), | ||
attribute_additional_config HSTORE, | ||
attribute_created_at TIMESTAMP DEFAULT now(), | ||
attribute_updated_at TIMESTAMP DEFAULT now() | ||
); | ||
CREATE INDEX idx_rule_attributes on rule_attributes(attribute_id); | ||
"""); | ||
|
||
|
||
select.execute(""" | ||
ALTER TABLE injectors_contracts ADD COLUMN injector_contract_import_available BOOLEAN NOT NULL DEFAULT FALSE; | ||
"""); | ||
|
||
select.execute(""" | ||
UPDATE injectors_contracts SET injector_contract_import_available = true WHERE injector_contract_labels -> 'en' LIKE ANY(ARRAY['%SMS%', '%Send%mail%']); | ||
"""); | ||
|
||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
openbas-api/src/main/java/io/openbas/rest/exception/BadRequestException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package io.openbas.rest.exception; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
|
||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
public class BadRequestException extends RuntimeException{ | ||
|
||
public BadRequestException() { | ||
super(); | ||
} | ||
|
||
public BadRequestException(String errorMessage) { | ||
super(errorMessage); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
openbas-api/src/main/java/io/openbas/rest/exception/FileTooBigException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package io.openbas.rest.exception; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
|
||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
public class FileTooBigException extends RuntimeException{ | ||
|
||
public FileTooBigException() { | ||
super(); | ||
} | ||
|
||
public FileTooBigException(String errorMessage) { | ||
super(errorMessage); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
openbas-api/src/main/java/io/openbas/rest/mapper/MapperApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package io.openbas.rest.mapper; | ||
|
||
import io.openbas.database.model.ImportMapper; | ||
import io.openbas.database.model.Scenario; | ||
import io.openbas.database.raw.RawPaginationImportMapper; | ||
import io.openbas.database.repository.ImportMapperRepository; | ||
import io.openbas.rest.exception.ElementNotFoundException; | ||
import io.openbas.rest.exception.FileTooBigException; | ||
import io.openbas.rest.helper.RestBehavior; | ||
import io.openbas.rest.mapper.form.ImportMapperAddInput; | ||
import io.openbas.rest.mapper.form.ImportMapperUpdateInput; | ||
import io.openbas.rest.scenario.form.InjectsImportTestInput; | ||
import io.openbas.rest.scenario.response.ImportPostSummary; | ||
import io.openbas.rest.scenario.response.ImportTestSummary; | ||
import io.openbas.service.InjectService; | ||
import io.openbas.service.MapperService; | ||
import io.openbas.utils.pagination.SearchPaginationInput; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import jakarta.transaction.Transactional; | ||
import jakarta.validation.Valid; | ||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.RequiredArgsConstructor; | ||
import org.apache.commons.io.FilenameUtils; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.security.access.annotation.Secured; | ||
import org.springframework.web.bind.annotation.*; | ||
import org.springframework.web.multipart.MultipartFile; | ||
import org.springframework.web.reactive.function.UnsupportedMediaTypeException; | ||
|
||
import java.time.Instant; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
import static io.openbas.database.model.User.ROLE_ADMIN; | ||
import static io.openbas.database.model.User.ROLE_USER; | ||
import static io.openbas.utils.pagination.PaginationUtils.buildPaginationJPA; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
public class MapperApi extends RestBehavior { | ||
|
||
private final ImportMapperRepository importMapperRepository; | ||
|
||
private final MapperService mapperService; | ||
|
||
private final InjectService injectService; | ||
|
||
// 25mb in byte | ||
private static final int MAXIMUM_FILE_SIZE_ALLOWED = 25 * 1000 * 1000; | ||
private static final List<String> ACCEPTED_FILE_TYPES = List.of("xls", "xlsx"); | ||
|
||
@Secured(ROLE_USER) | ||
@PostMapping("/api/mappers/search") | ||
public Page<RawPaginationImportMapper> getImportMapper(@RequestBody @Valid final SearchPaginationInput searchPaginationInput) { | ||
return buildPaginationJPA( | ||
this.importMapperRepository::findAll, | ||
searchPaginationInput, | ||
ImportMapper.class | ||
).map(RawPaginationImportMapper::new); | ||
} | ||
|
||
@Secured(ROLE_USER) | ||
@GetMapping("/api/mappers/{mapperId}") | ||
public ImportMapper getImportMapperById(@PathVariable String mapperId) { | ||
return importMapperRepository.findById(UUID.fromString(mapperId)).orElseThrow(ElementNotFoundException::new); | ||
} | ||
|
||
@Secured(ROLE_ADMIN) | ||
@PostMapping("/api/mappers") | ||
public ImportMapper createImportMapper(@RequestBody @Valid final ImportMapperAddInput importMapperAddInput) { | ||
return mapperService.createAndSaveImportMapper(importMapperAddInput); | ||
} | ||
|
||
@Secured(ROLE_ADMIN) | ||
@PutMapping("/api/mappers/{mapperId}") | ||
public ImportMapper updateImportMapper(@PathVariable String mapperId, @Valid @RequestBody ImportMapperUpdateInput importMapperUpdateInput) { | ||
return mapperService.updateImportMapper(mapperId, importMapperUpdateInput); | ||
} | ||
|
||
@Secured(ROLE_ADMIN) | ||
@DeleteMapping("/api/mappers/{mapperId}") | ||
public void deleteImportMapper(@PathVariable String mapperId) { | ||
importMapperRepository.deleteById(UUID.fromString(mapperId)); | ||
} | ||
|
||
@PostMapping("/api/mappers/store") | ||
@Transactional(rollbackOn = Exception.class) | ||
@Operation(summary = "Import injects into an xls file") | ||
@Secured(ROLE_USER) | ||
public ImportPostSummary importXLSFile(@RequestPart("file") @NotNull MultipartFile file) { | ||
validateUploadedFile(file); | ||
return injectService.storeXlsFileForImport(file); | ||
} | ||
|
||
@PostMapping("/api/mappers/store/{importId}") | ||
@Transactional(rollbackOn = Exception.class) | ||
@Operation(summary = "Test the import of injects from an xls file") | ||
@Secured(ROLE_USER) | ||
public ImportTestSummary testImportXLSFile(@PathVariable @NotBlank final String importId, | ||
@Valid @RequestBody final InjectsImportTestInput input) { | ||
ImportMapper importMapper = mapperService.createImportMapper(input.getImportMapper()); | ||
importMapper.getInjectImporters().forEach( | ||
injectImporter -> { | ||
injectImporter.setId(UUID.randomUUID().toString()); | ||
injectImporter.getRuleAttributes().forEach(ruleAttribute -> ruleAttribute.setId(UUID.randomUUID().toString())); | ||
} | ||
); | ||
Scenario scenario = new Scenario(); | ||
scenario.setRecurrenceStart(Instant.now()); | ||
return injectService.importInjectIntoScenarioFromXLS(scenario, importMapper, importId, input.getName(), input.getTimezoneOffset(), false); | ||
} | ||
|
||
private void validateUploadedFile(MultipartFile file) { | ||
validateExtension(file); | ||
validateFileSize(file); | ||
} | ||
|
||
private void validateExtension(MultipartFile file) { | ||
String extension = FilenameUtils.getExtension(file.getOriginalFilename()); | ||
if (!ACCEPTED_FILE_TYPES.contains(extension)) { | ||
throw new UnsupportedMediaTypeException("Only the following file types are accepted : " + String.join(", ", ACCEPTED_FILE_TYPES)); | ||
} | ||
} | ||
|
||
private void validateFileSize(MultipartFile file){ | ||
if (file.getSize() >= MAXIMUM_FILE_SIZE_ALLOWED) { | ||
throw new FileTooBigException("File size cannot be greater than 25 Mb"); | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
openbas-api/src/main/java/io/openbas/rest/mapper/form/ImportMapperAddInput.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package io.openbas.rest.mapper.form; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.NotNull; | ||
import jakarta.validation.constraints.Pattern; | ||
import lombok.Data; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static io.openbas.config.AppConfig.MANDATORY_MESSAGE; | ||
|
||
@Data | ||
public class ImportMapperAddInput { | ||
|
||
@NotBlank(message = MANDATORY_MESSAGE) | ||
@JsonProperty("mapper_name") | ||
private String name; | ||
|
||
@Pattern(regexp="^[A-Z]{1,2}$") | ||
@JsonProperty("mapper_inject_type_column") | ||
@NotBlank | ||
private String injectTypeColumn; | ||
|
||
@JsonProperty("mapper_inject_importers") | ||
@NotNull | ||
private List<InjectImporterAddInput> importers = new ArrayList<>(); | ||
|
||
} |
Oops, something went wrong.