Add an extra precaution-check to allow the emptying or deletion of an S3-Object-Store bucket, only when the app runs in "TestEnvironment".

This commit is contained in:
Lampros Smyrnaios 2023-02-01 16:42:22 +02:00
parent f89730f196
commit c9f33d3afa
2 changed files with 7 additions and 6 deletions

View File

@ -333,7 +333,7 @@ public class UrlController {
logger.error(errorMsg, e); logger.error(errorMsg, e);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorMsg); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorMsg);
} finally { } finally {
logger.debug("Deleting directory: " + currentParquetPath); logger.debug("Deleting parquet directory: " + currentParquetPath);
FileUtils.deleteDirectory(new File(currentParquetPath)); FileUtils.deleteDirectory(new File(currentParquetPath));
} }

View File

@ -35,6 +35,9 @@ public class S3ObjectStore {
@Value("${services.pdfaggregation.controller.s3.shouldShowAllS3Buckets}") @Value("${services.pdfaggregation.controller.s3.shouldShowAllS3Buckets}")
private boolean shouldShowAllS3Buckets = false; private boolean shouldShowAllS3Buckets = false;
@Value("${services.pdfaggregation.controller.isTestEnvironment}")
private boolean isTestEnvironment = false;
private MinioClient minioClient; private MinioClient minioClient;
@ -45,10 +48,8 @@ public class S3ObjectStore {
boolean bucketExists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build()); 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). // 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); emptyBucket(bucketName, false);
//throw new RuntimeException("stop just for test!");
}
// Make the bucket, if not exist. // Make the bucket, if not exist.
if ( !bucketExists ) { 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**"; * @param fileObjKeyName = "**File object key name**";
@ -117,7 +118,7 @@ public class S3ObjectStore {
public void emptyBucket(String bucketName, boolean shouldDeleteBucket) throws Exception { 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. // First list the objects of the bucket.
Iterable<Result<Item>> results = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).build()); Iterable<Result<Item>> results = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).build());