From 166143d7d24b00af725bf05fa33eae4c9dbe8bb9 Mon Sep 17 00:00:00 2001 From: Yaroslav Svitlytskyi Date: Mon, 9 Dec 2024 10:35:56 +0100 Subject: [PATCH] fix: fixed bugs --- .../common/RepositoryConfigurationHelper.java | 6 +- .../service/vendor/gcs/GCSVendorService.java | 120 ++++++++++++++---- .../workspace/facade/WorkspaceFacade.java | 7 +- .../resources/liquibase/postgres/config.yaml | 1 - .../resources/liquibase/sqlite3/config.yaml | 1 - 5 files changed, 99 insertions(+), 36 deletions(-) diff --git a/api-server/src/main/java/com/objectstorage/repository/common/RepositoryConfigurationHelper.java b/api-server/src/main/java/com/objectstorage/repository/common/RepositoryConfigurationHelper.java index 2b74fb8..b0f0eab 100644 --- a/api-server/src/main/java/com/objectstorage/repository/common/RepositoryConfigurationHelper.java +++ b/api-server/src/main/java/com/objectstorage/repository/common/RepositoryConfigurationHelper.java @@ -7,9 +7,7 @@ import jakarta.enterprise.context.ApplicationScoped; import org.apache.commons.lang3.RandomStringUtils; -import java.util.Arrays; import java.util.List; -import java.util.Optional; import java.util.StringJoiner; /** @@ -24,7 +22,7 @@ public class RepositoryConfigurationHelper { * @return packed external credentials signature. */ private String packExternalCredentials(String ...values) { - StringJoiner result = new StringJoiner(":"); + StringJoiner result = new StringJoiner("|"); for (String value : values) { result.add(value); @@ -40,7 +38,7 @@ private String packExternalCredentials(String ...values) { * @return unpacked external credentials signature. */ private List unpackExternalCredentials(String credentials) { - return List.of(credentials.split(":")); + return List.of(credentials.split("\\|")); } /** diff --git a/api-server/src/main/java/com/objectstorage/service/vendor/gcs/GCSVendorService.java b/api-server/src/main/java/com/objectstorage/service/vendor/gcs/GCSVendorService.java index b664a08..f2416d9 100644 --- a/api-server/src/main/java/com/objectstorage/service/vendor/gcs/GCSVendorService.java +++ b/api-server/src/main/java/com/objectstorage/service/vendor/gcs/GCSVendorService.java @@ -6,12 +6,14 @@ import com.google.auth.oauth2.UserCredentials; import com.google.cloud.WriteChannel; import com.google.cloud.resourcemanager.ResourceManager; +import com.google.cloud.resourcemanager.ResourceManagerException; import com.google.cloud.resourcemanager.ResourceManagerOptions; import com.google.cloud.resourcemanager.Project; import com.google.cloud.storage.*; import com.objectstorage.dto.VendorObjectListingDto; import com.objectstorage.exception.GCPCredentialsInitializationFailureException; import com.objectstorage.exception.GCSBucketObjectUploadFailureException; +import com.objectstorage.exception.VendorOperationFailureException; import jakarta.enterprise.context.ApplicationScoped; import java.io.ByteArrayInputStream; @@ -50,18 +52,29 @@ public Credentials getCredentials(String secrets) throws GCPCredentialsInitializ * @param name given name of the GCS bucket. * @param credentials given credentials to be used for client configuration. * @return result of the check. + * @throws VendorOperationFailureException if vendor operation fails. */ public Boolean isGCSBucketPresent( Credentials credentials, - String name) { + String name) throws VendorOperationFailureException { Storage storage = StorageOptions.newBuilder() .setCredentials(credentials) .build() .getService(); - Bucket bucket = storage.get(name); + Bucket bucket; - return Objects.nonNull(bucket) && bucket.exists(); + try { + bucket = storage.get(name); + } catch (StorageException e) { + throw new VendorOperationFailureException(e.getMessage()); + } + + try { + return Objects.nonNull(bucket) && bucket.exists(); + } catch (StorageException e) { + throw new VendorOperationFailureException(e.getMessage()); + } } /** @@ -69,16 +82,21 @@ public Boolean isGCSBucketPresent( * * @param credentials given credentials to be used for client configuration. * @param name given name of the GCS bucket. + * @throws VendorOperationFailureException if vendor operation fails. */ public void createGCSBucket( Credentials credentials, - String name) { + String name) throws VendorOperationFailureException { Storage storage = StorageOptions.newBuilder() .setCredentials(credentials) .build() .getService(); - storage.create(BucketInfo.newBuilder(name).build()); + try { + storage.create(BucketInfo.newBuilder(name).build()); + } catch (StorageException e) { + throw new VendorOperationFailureException(e.getMessage()); + } } /** @@ -86,16 +104,21 @@ public void createGCSBucket( * * @param credentials given credentials to be used for client configuration. * @param name given name of the GCS bucket. + * @throws VendorOperationFailureException if vendor operation fails. */ public void removeGCSBucket( Credentials credentials, - String name) { + String name) throws VendorOperationFailureException { Storage storage = StorageOptions.newBuilder() .setCredentials(credentials) .build() .getService(); - storage.delete(name); + try { + storage.delete(name); + } catch (StorageException e) { + throw new VendorOperationFailureException(e.getMessage()); + } } /** @@ -133,19 +156,30 @@ public void uploadObjectToGCSBucket( * @param bucketName given name of the GCS bucket. * @param fileName given name of the file to be retrieved. * @return result of the check. + * @throws VendorOperationFailureException if vendor operation fails. */ public Boolean isObjectPresentInBucket( Credentials credentials, String bucketName, - String fileName) { + String fileName) throws VendorOperationFailureException { Storage storage = StorageOptions.newBuilder() .setCredentials(credentials) .build() .getService(); - Blob blob = storage.get(BlobId.of(bucketName, fileName)); + Blob blob; + + try { + blob = storage.get(BlobId.of(bucketName, fileName)); + } catch (StorageException e) { + throw new VendorOperationFailureException(e.getMessage()); + } - return Objects.nonNull(blob) && blob.exists(); + try { + return Objects.nonNull(blob) && blob.exists(); + } catch (StorageException e) { + throw new VendorOperationFailureException(e.getMessage()); + } } /** @@ -155,19 +189,30 @@ public Boolean isObjectPresentInBucket( * @param bucketName given name of the GCS bucket. * @param fileName given name of the file to be retrieved. * @return retrieved object content. + * @throws VendorOperationFailureException if vendor operation fails. */ public byte[] retrieveObjectFromGCSBucket( Credentials credentials, String bucketName, - String fileName) { + String fileName) throws VendorOperationFailureException { Storage storage = StorageOptions.newBuilder() .setCredentials(credentials) .build() .getService(); - Blob blob = storage.get(BlobId.of(bucketName, fileName)); + Blob blob; + + try { + blob = storage.get(BlobId.of(bucketName, fileName)); + } catch (StorageException e) { + throw new VendorOperationFailureException(e.getMessage()); + } - return blob.getContent(); + try { + return blob.getContent(); + } catch (StorageException e) { + throw new VendorOperationFailureException(e.getMessage()); + } } /** @@ -176,22 +221,27 @@ public byte[] retrieveObjectFromGCSBucket( * @param credentials given credentials to be used for client configuration. * @param bucketName given name of the GCS bucket. * @return listed objects. + * @throws VendorOperationFailureException if vendor operation fails. */ public List listObjectsFromGCSBucket( Credentials credentials, - String bucketName) { + String bucketName) throws VendorOperationFailureException { Storage storage = StorageOptions.newBuilder() .setCredentials(credentials) .build() .getService(); - Page blobs = storage.list(bucketName); + try { + Page blobs = storage.list(bucketName); - return StreamSupport.stream(blobs.iterateAll().spliterator(), false) - .map(element -> VendorObjectListingDto.of( - element.getBlobId().getName(), - element.getUpdateTimeOffsetDateTime().toEpochSecond())) - .toList(); + return StreamSupport.stream(blobs.iterateAll().spliterator(), false) + .map(element -> VendorObjectListingDto.of( + element.getBlobId().getName(), + element.getUpdateTimeOffsetDateTime().toEpochSecond())) + .toList(); + } catch (StorageException e) { + throw new VendorOperationFailureException(e.getMessage()); + } } /** @@ -200,17 +250,22 @@ public List listObjectsFromGCSBucket( * @param credentials given credentials to be used for client configuration. * @param bucketName given name of the GCS bucket. * @param fileName given name of the file to be removed. + * @throws VendorOperationFailureException if vendor operation fails. */ public void removeObjectFromGCSBucket( Credentials credentials, String bucketName, - String fileName) { + String fileName) throws VendorOperationFailureException { Storage storage = StorageOptions.newBuilder() .setCredentials(credentials) .build() .getService(); - storage.delete(BlobId.of(bucketName, fileName)); + try { + storage.delete(BlobId.of(bucketName, fileName)); + } catch (StorageException e) { + throw new VendorOperationFailureException(e.getMessage()); + } } /** @@ -218,19 +273,24 @@ public void removeObjectFromGCSBucket( * * @param credentials given credentials to be used for client configuration. * @param bucketName given name of the GCS bucket. + * @throws VendorOperationFailureException if vendor operation fails. */ public void removeAllObjectsFromGCSBucket( Credentials credentials, - String bucketName) { + String bucketName) throws VendorOperationFailureException { Storage storage = StorageOptions.newBuilder() .setCredentials(credentials) .build() .getService(); - Page blobs = storage.list(bucketName); + try { + Page blobs = storage.list(bucketName); - for (Blob blob : blobs.iterateAll()) { - blob.delete(Blob.BlobSourceOption.generationMatch()); + for (Blob blob : blobs.iterateAll()) { + blob.delete(Blob.BlobSourceOption.generationMatch()); + } + } catch (StorageException e) { + throw new VendorOperationFailureException(e.getMessage()); } } @@ -246,8 +306,12 @@ public Boolean isCallerValid(Credentials credentials) { .build() .getService(); - for (Project project : resourceManager.list().iterateAll()) { - return true; + try { + for (Project project : resourceManager.list().iterateAll()) { + return true; + } + } catch (ResourceManagerException e) { + return false; } return false; diff --git a/api-server/src/main/java/com/objectstorage/service/workspace/facade/WorkspaceFacade.java b/api-server/src/main/java/com/objectstorage/service/workspace/facade/WorkspaceFacade.java index 6d90ec5..570551c 100644 --- a/api-server/src/main/java/com/objectstorage/service/workspace/facade/WorkspaceFacade.java +++ b/api-server/src/main/java/com/objectstorage/service/workspace/facade/WorkspaceFacade.java @@ -12,6 +12,7 @@ import java.io.*; import java.time.Instant; +import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; @@ -36,7 +37,9 @@ public class WorkspaceFacade { * @return created workspace unit key. */ public String createWorkspaceUnitKey(ValidationSecretsApplication validationSecretsApplication) { - return validationSecretsApplication.getSecrets().stream().map(element -> + return validationSecretsApplication.getSecrets().stream() + .sorted(Comparator.comparing(element -> element.getProvider().toString())) + .map(element -> switch (element.getProvider()) { case S3 -> workspaceService.createUnitKey( element.getProvider().toString(), @@ -63,7 +66,7 @@ public String createFileUnitKey(String name) { String fileUnit = workspaceService.createUnitKey(name, Instant.now().toString()); - return String.format("%s-%s-%d", name, fileUnit, timestamp.toEpochMilli()); + return String.format("%s-%d", fileUnit, timestamp.toEpochMilli()); } /** diff --git a/api-server/src/main/resources/liquibase/postgres/config.yaml b/api-server/src/main/resources/liquibase/postgres/config.yaml index affff68..9b001fd 100644 --- a/api-server/src/main/resources/liquibase/postgres/config.yaml +++ b/api-server/src/main/resources/liquibase/postgres/config.yaml @@ -107,7 +107,6 @@ databaseChangeLog: type: TEXT constraints: nullable: false - unique: true - column: name: created_at type: BIGINT diff --git a/api-server/src/main/resources/liquibase/sqlite3/config.yaml b/api-server/src/main/resources/liquibase/sqlite3/config.yaml index 2443eb8..6119b6c 100644 --- a/api-server/src/main/resources/liquibase/sqlite3/config.yaml +++ b/api-server/src/main/resources/liquibase/sqlite3/config.yaml @@ -107,7 +107,6 @@ databaseChangeLog: type: VARCHAR constraints: nullable: false - unique: true - column: name: created_at type: LONG