Skip to content
This repository has been archived by the owner on May 16, 2023. It is now read-only.

Feat: Data-Export with encrypted ZIP Files #289

Merged
merged 3 commits into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>app.coronawarn</groupId>
<artifactId>cwa-parent</artifactId>
<version>1.8</version>
<version>1.8.1</version>
</parent>
<artifactId>cwa-quick-test-backend</artifactId>
<version>1.0.0-SNAPSHOT</version>
Expand Down Expand Up @@ -139,6 +139,12 @@
<groupId>eu.europa.ec.dgc</groupId>
<artifactId>dgc-lib</artifactId>
</dependency>
<dependency>
<groupId>net.lingala.zip4j</groupId>
<artifactId>zip4j</artifactId>
</dependency>


</dependencies>

<build>
Expand All @@ -158,6 +164,7 @@
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>license-maven-plugin</artifactId>
<version>2.0.0</version>
</plugin>

<!-- Spring Rest Docs (Deprecated, should be replaced with Springdoc OpenAPI) -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter {
public static final String ROLE_POC_NAT_ADMIN = "ROLE_c19_quick_test_poc_nat_admin";
public static final String ROLE_TERMINATOR = "ROLE_c19_quick_test_terminator";
public static final String ROLE_ARCHIVE_OPERATOR = "ROLE_c19_quick_test_archive_operator";
public static final String ROLE_ARCHIVE_ZIP_CREATOR = "ROLE_c19_quick_test_archive_operator_zip_creator";
public static final String ROLE_ARCHIVE_ZIP_DOWNLOADER = "ROLE_c19_quick_test_archive_operator_zip_downloader";

private static final String API_ROUTE = "/api/**";
private static final String CONFIG_ROUTE = "/api/config/*";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,27 @@
package app.coronawarn.quicktest.controller;

import static app.coronawarn.quicktest.config.SecurityConfig.ROLE_ARCHIVE_OPERATOR;
import static app.coronawarn.quicktest.config.SecurityConfig.ROLE_ARCHIVE_ZIP_CREATOR;
import static app.coronawarn.quicktest.config.SecurityConfig.ROLE_ARCHIVE_ZIP_DOWNLOADER;

import app.coronawarn.quicktest.model.cancellation.ZipRequest;
import app.coronawarn.quicktest.service.ArchiveService;
import app.coronawarn.quicktest.service.ArchiveZipService;
import com.opencsv.exceptions.CsvDataTypeMismatchException;
import com.opencsv.exceptions.CsvRequiredFieldEmptyException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import java.io.IOException;
import java.net.URL;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;
import javax.validation.Valid;
import javax.validation.constraints.Pattern;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Profile;
Expand All @@ -39,9 +51,13 @@
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.Authentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;

Expand All @@ -51,10 +67,15 @@
@RequestMapping(value = "/api/archive")
@RequiredArgsConstructor
@Profile("archive_export")
@Validated
public class ArchiveExportController {

private final ArchiveService archiveService;

private final ArchiveZipService archiveZipService;

private static final String zipFileNameRegex = "^\\w{0,20}.zip$";

/**
* Endpoint for downloading archived entities.
*
Expand All @@ -65,14 +86,14 @@ public class ArchiveExportController {
description = "Creates a CSV-File with all archived data for whole Partner or just one POC ID.",
parameters = {
@Parameter(
in = ParameterIn.PATH,
name = "partnerId",
description = "Partner ID of the PArtner to download data of",
required = true),
in = ParameterIn.PATH,
name = "partnerId",
description = "Partner ID of the PArtner to download data of",
required = true),
@Parameter(
in = ParameterIn.QUERY,
name = "pocId",
description = "Filter for entities with given pocId")
in = ParameterIn.QUERY,
name = "pocId",
description = "Filter for entities with given pocId")
}
)
@ApiResponses(value = {
Expand Down Expand Up @@ -101,4 +122,80 @@ public ResponseEntity<byte[]> exportArchive(@PathVariable("partnerId") String pa
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Failed to create CSV.");
}
}

/**
* Endpoint for creating a zip file with multiple CSV-files.
*
* @return Status Code.
*/
@Operation(
summary = "Create Archive ZIP-File",
description = "Creates a ZIP-File with all CSV-Files of provided partner ids."
+ "ZIP File will be stored in OBS bucket"
)
@ApiResponses(value = {
@ApiResponse(responseCode = "201", description = "ZIP Created")
})
@PostMapping(value = "/zip", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
@Secured({ROLE_ARCHIVE_ZIP_CREATOR})
public ResponseEntity<Void> createZip(@Valid @RequestBody ZipRequest zipRequest) {

try {
List<String> partnerIds = zipRequest.getPartnerIds();
partnerIds.add(zipRequest.getPartnerId());

archiveZipService.createZip(
zipRequest.getPartnerId() + ".zip",
zipRequest.getPartnerIds(),
zipRequest.getPassword());

} catch (ArchiveZipService.ArchiveZipServiceException e) {
if (e.getReason() == ArchiveZipService.ArchiveZipServiceException.Reason.CSV_FILE_NOT_FOUND) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "CSV File " + e.getMessage() + " not found");
} else {
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR,
"Unexpected Error while creating ZIP File.");
}
} catch (IOException e) {
log.error("Failed to create ZIP (IOException): {}", e.getMessage());
throw new RuntimeException(e);
}

return ResponseEntity
.status(HttpStatus.CREATED)
.build();
}

/**
* Endpoint for receiving download link for Archive ZIP File.
*
* @return Download link of zip
*/
@Operation(
summary = "Request Download Link (ZIP)",
description = "Returns a presigned URL to download the generated ZIP File from Bucket."
)
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Successful"),
@ApiResponse(responseCode = "404", description = "No ZIP file found")
})
@GetMapping(value = "/zip/download", produces = MediaType.APPLICATION_JSON_VALUE)
@Secured({ROLE_ARCHIVE_ZIP_DOWNLOADER})
public ResponseEntity<URL> getZipLink(
@Valid @Pattern(regexp = zipFileNameRegex) @RequestParam("filename") String filename) {

Date expiration = Date.from(Instant.now().plus(5, ChronoUnit.MINUTES));
URL url;
try {
url = archiveZipService.getDownloadUrl(filename, expiration);
} catch (ArchiveZipService.ArchiveZipServiceException e) {
if (e.getReason() == ArchiveZipService.ArchiveZipServiceException.Reason.ZIP_FILE_NOT_FOUND) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "ZIP File doesn't exist in Bucket");
} else {
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
}
}

return ResponseEntity.ok(url);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*-
* ---license-start
* Corona-Warn-App / cwa-quick-test-backend
* ---
* Copyright (C) 2021 - 2023 T-Systems International GmbH and all other contributors
* ---
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ---license-end
*/

package app.coronawarn.quicktest.model.cancellation;

import io.swagger.v3.oas.annotations.media.Schema;
import java.util.List;
import javax.validation.constraints.Size;
import lombok.Data;
import org.hibernate.validator.constraints.Length;

@Schema(
description = "The ZIP request model to create zip file for partners."
)
@Data
public class ZipRequest {

@Length(min = 10, max = 100)
private String password;

@Length(min = 1, max = 50)
private String partnerId;

@Size(min = 1, max = 20)
private List<@Length(min = 1, max = 100) String> partnerIds;

}
Loading