From c9f33d3afafc970ccf5bd853172a256541998029 Mon Sep 17 00:00:00 2001 From: LSmyrnaios Date: Wed, 1 Feb 2023 16:42:22 +0200 Subject: [PATCH] Add an extra precaution-check to allow the emptying or deletion of an S3-Object-Store bucket, only when the app runs in "TestEnvironment". --- .../urls_controller/controllers/UrlController.java | 2 +- .../openaire/urls_controller/util/S3ObjectStore.java | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/main/java/eu/openaire/urls_controller/controllers/UrlController.java b/src/main/java/eu/openaire/urls_controller/controllers/UrlController.java index b221493..5461cab 100644 --- a/src/main/java/eu/openaire/urls_controller/controllers/UrlController.java +++ b/src/main/java/eu/openaire/urls_controller/controllers/UrlController.java @@ -333,7 +333,7 @@ public class UrlController { logger.error(errorMsg, e); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorMsg); } finally { - logger.debug("Deleting directory: " + currentParquetPath); + logger.debug("Deleting parquet directory: " + currentParquetPath); FileUtils.deleteDirectory(new File(currentParquetPath)); } diff --git a/src/main/java/eu/openaire/urls_controller/util/S3ObjectStore.java b/src/main/java/eu/openaire/urls_controller/util/S3ObjectStore.java index b1a402d..5ba80aa 100644 --- a/src/main/java/eu/openaire/urls_controller/util/S3ObjectStore.java +++ b/src/main/java/eu/openaire/urls_controller/util/S3ObjectStore.java @@ -35,6 +35,9 @@ public class S3ObjectStore { @Value("${services.pdfaggregation.controller.s3.shouldShowAllS3Buckets}") private boolean shouldShowAllS3Buckets = false; + @Value("${services.pdfaggregation.controller.isTestEnvironment}") + private boolean isTestEnvironment = false; + private MinioClient minioClient; @@ -45,10 +48,8 @@ public class S3ObjectStore { boolean bucketExists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build()); // Keep this commented-out to avoid objects-deletion by accident. The code is open-sourced, so it's easy to enable this ability if we really want it (e.g. for testing). - if ( bucketExists && shouldEmptyBucket ) { + if ( shouldEmptyBucket && isTestEnvironment && bucketExists ) emptyBucket(bucketName, false); - //throw new RuntimeException("stop just for test!"); - } // Make the bucket, if not exist. if ( !bucketExists ) { @@ -72,7 +73,7 @@ public class S3ObjectStore { } - private final Pattern EXTENSION_PATTERN = Pattern.compile("(\\.[^.]+)$"); + private static final Pattern EXTENSION_PATTERN = Pattern.compile("(\\.[^.]+)$"); /** * @param fileObjKeyName = "**File object key name**"; @@ -117,7 +118,7 @@ public class S3ObjectStore { public void emptyBucket(String bucketName, boolean shouldDeleteBucket) throws Exception { - logger.warn("Going to " + (shouldDeleteBucket ? "delete" : "empty") + " bucket \"" + bucketName + "\""); + logger.warn("Going to " + (shouldDeleteBucket ? "delete" : "empty") + " bucket \"" + bucketName + "\"!"); // First list the objects of the bucket. Iterable> results = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).build());