Skip to content

Commit

Permalink
merge dev in uat (#192)
Browse files Browse the repository at this point in the history
Co-authored-by: adrrossi <[email protected]>
Co-authored-by: GitHub Action <[email protected]>
Co-authored-by: adrrss <[email protected]>
Co-authored-by: SMANUM <[email protected]>
Co-authored-by: LuciaM1 <[email protected]>
Co-authored-by: GiacomoB <[email protected]>
Co-authored-by: Simone Munao <[email protected]>
Co-authored-by: LuciaM1 <[email protected]>
Co-authored-by: EmVal <[email protected]>
Co-authored-by: Gabriele Maiocchi <[email protected]>
Co-authored-by: Giacomo Brancazi <[email protected]>
  • Loading branch information
12 people authored Sep 26, 2024
1 parent 0d2e34c commit a466066
Show file tree
Hide file tree
Showing 8 changed files with 15,760 additions and 15,627 deletions.
26 changes: 21 additions & 5 deletions _TMP/coverage-results.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

| Outcome | Value |
|-------------------------|---------------------------------------------------------------------|
| Code Coverage % | 91.79% |
| :heavy_check_mark: Number of Lines Covered | 2282 |
| :x: Number of Lines Missed | 204 |
| Total Number of Lines | 2486 |
| Code Coverage % | 91.64% |
| :heavy_check_mark: Number of Lines Covered | 2292 |
| :x: Number of Lines Missed | 209 |
| Total Number of Lines | 2501 |


## Details:
Expand Down Expand Up @@ -701,6 +701,18 @@
</details>



<details>
<summary>
:x: DirManager.java
</summary>

#### Lines Missed:
</details>


### it/gov/pagopa/atmlayer/service/model/enumeration

<details>
Expand Down Expand Up @@ -902,7 +914,11 @@
#### Lines Missed:
- Line #158
- Line #133
```
} catch (IOException e) {
```
- Line #142
```
} catch (IOException e) {
```
Expand Down
2 changes: 1 addition & 1 deletion helm-chart/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type: application
# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: v1.29.0
version: v1.29.0-dev.3
# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>it.gov.pagopa</groupId>
<artifactId>atm-layer-model</artifactId>
<version>1.29.0</version>
<version>1.29.0-dev.3</version>
<name>atm-layer-model</name>
<properties>
<compiler-plugin.version>3.11.0</compiler-plugin.version>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package it.gov.pagopa.atmlayer.service.model.configurations;

import io.quarkus.runtime.Shutdown;
import io.quarkus.runtime.ShutdownEvent;
import io.quarkus.runtime.Startup;
import io.quarkus.runtime.StartupEvent;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Observes;
import lombok.Getter;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.SystemUtils;

import java.io.File;
import java.io.IOException;
import java.nio.file.attribute.PosixFilePermission;
import java.util.EnumSet;
import java.util.Set;

@ApplicationScoped
public class DirManager {

@Getter
public static File decodedFilesDirectory;

@Startup
static void init(@Observes StartupEvent ev) throws IOException {
String secureDirPath = System.getProperty("java.io.tmpdir") + "/decodedFilesDirectory";
decodedFilesDirectory = new File(secureDirPath);
if (!decodedFilesDirectory.exists() && !decodedFilesDirectory.mkdirs()) {
throw new IOException("Impossibile creare una directory sicura per il salvataggio di file temporanei.");
}
if (SystemUtils.IS_OS_UNIX) {
Set<PosixFilePermission> dirPermissions = EnumSet.of(
PosixFilePermission.OWNER_READ,
PosixFilePermission.OWNER_WRITE,
PosixFilePermission.OWNER_EXECUTE
);
java.nio.file.Files.setPosixFilePermissions(decodedFilesDirectory.toPath(), dirPermissions);
}
}

@Shutdown
static void shutdown(@Observes ShutdownEvent ev) throws IOException {
FileUtils.deleteDirectory(decodedFilesDirectory);
}

private DirManager() {
throw new IllegalStateException("Utility class DirManager should not be instantiated");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import it.gov.pagopa.atmlayer.service.model.service.ResourceEntityStorageService;
import it.gov.pagopa.atmlayer.service.model.service.ResourceFileService;
import it.gov.pagopa.atmlayer.service.model.utils.CommonUtils;
import it.gov.pagopa.atmlayer.service.model.utils.FileUtilities;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.core.Response;
Expand All @@ -28,6 +29,7 @@
import org.eclipse.microprofile.rest.client.inject.RestClient;

import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.*;

Expand Down Expand Up @@ -185,8 +187,10 @@ public Uni<List<String>> createResourceMultiple(List<ResourceEntity> resourceEnt
if (!errors.isEmpty()) {
return deleteResourcesFromStorage(uploadedFiles, errors);
}
FileUtilities.cleanDecodedFilesDirectory();
return Uni.createFrom().item(errors); // This will be empty if no errors occurred
});

}

public Uni<List<String>> deleteResourcesFromStorage(List<String> storageKeys, List<String> errorMessages){
Expand All @@ -195,7 +199,8 @@ public Uni<List<String>> deleteResourcesFromStorage(List<String> storageKeys, Li
.onItem().transform(objectStoreResponse -> String.format("Deleted %s",objectStoreResponse.getStorageKey())))
.collect().asList()
.onItem().transform(deletedKeys -> {
throw new AtmLayerException("Errore nel caricamento dovuto ai seguenti file: " + String.join(", ", errorMessages),
FileUtilities.cleanDecodedFilesDirectory();
throw new AtmLayerException("Errore nel caricamento dovuto ai seguenti file: " + String.join(", ", errorMessages),
Response.Status.BAD_REQUEST, RESOURCES_CREATION_ERROR);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import it.gov.pagopa.atmlayer.service.model.configurations.DirManager;
import it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum;
import it.gov.pagopa.atmlayer.service.model.enumeration.DeployableResourceType;
import it.gov.pagopa.atmlayer.service.model.exception.AtmLayerException;
Expand All @@ -20,23 +21,20 @@
import java.io.IOException;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.EnumSet;
import java.util.Set;

import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum.ATMLM_500;
import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum.BPMN_FILE_DOES_NOT_HAVE_DEFINITION_KEY;
import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum.CANNOT_EXTRACT_FILE_DEFINITION_KEY;

@ApplicationScoped
@Slf4j
public class FileUtilities {

public static String extractIdValue(File file, DeployableResourceType resourceTypeEnum) {
switch (resourceTypeEnum) {
case BPMN, DMN -> {
Expand Down Expand Up @@ -102,55 +100,32 @@ public static String toHexString(byte[] hash) {
}

public static File fromStringToFile(String fileBase64) {
if (!DirManager.decodedFilesDirectory.exists()) {
throw new AtmLayerException("Impossibile convertire i file in input: non è stata creata una directory sicura per il salvataggio di file temporanei.", Response.Status.INTERNAL_SERVER_ERROR, AppErrorCodeEnum.ATMLM_500);
}
File tempFile = null;
try {

byte[] decodedBytes = Base64.getDecoder().decode(fileBase64);

String secureDirPath = System.getProperty("java.io.tmpdir") + "/mySecureDirectory";
File secureDir = new File(secureDirPath);
if (!secureDir.exists() && !secureDir.mkdirs()) {
throw new IOException("Impossibile creare la directory sicura.");
}


if (SystemUtils.IS_OS_UNIX) {
Set<PosixFilePermission> dirPermissions = EnumSet.of(
PosixFilePermission.OWNER_READ,
PosixFilePermission.OWNER_WRITE,
PosixFilePermission.OWNER_EXECUTE
);
java.nio.file.Files.setPosixFilePermissions(secureDir.toPath(), dirPermissions);
}

File tempFile;

if (SystemUtils.IS_OS_UNIX) {

Set<PosixFilePermission> filePermissions = EnumSet.of(
PosixFilePermission.OWNER_READ,
PosixFilePermission.OWNER_WRITE,
PosixFilePermission.OWNER_EXECUTE
);

tempFile = File.createTempFile("tempfile", ".tmp", secureDir);
tempFile = File.createTempFile("tempfile", ".tmp", DirManager.decodedFilesDirectory);
java.nio.file.Files.setPosixFilePermissions(tempFile.toPath(), filePermissions);
} else {

tempFile = File.createTempFile("tempfile", ".tmp", secureDir);

tempFile = File.createTempFile("tempfile", ".tmp", DirManager.decodedFilesDirectory);
boolean readable = tempFile.setReadable(true, true);
boolean writable = tempFile.setWritable(true, true);
boolean executable = tempFile.setExecutable(true, true);

if (!readable || !writable || !executable) {
throw new IOException("Impossibile impostare i permessi di sicurezza sul file temporaneo.");
}
}

try (FileOutputStream fos = new FileOutputStream(tempFile)) {
fos.write(decodedBytes);
}

return tempFile;
} catch (IllegalArgumentException e) {
log.error("Errore nella decodifica del Base64: " + e.getMessage());
Expand All @@ -161,5 +136,12 @@ public static File fromStringToFile(String fileBase64) {
}
}

public static void cleanDecodedFilesDirectory() {
try {
FileUtils.cleanDirectory(DirManager.decodedFilesDirectory);
} catch (IOException e) {
throw new AtmLayerException("Errore nell'eliminazione dei file temporanei", Response.Status.INTERNAL_SERVER_ERROR, ATMLM_500);
}
}

}
Loading

0 comments on commit a466066

Please sign in to comment.