Skip to content

Commit

Permalink
Exceptions improvement (Digital-Ecosystems#59)
Browse files Browse the repository at this point in the history
* Changes to improve exceptions messages in failure cases.

* Fix: importing the serviceAccount

* Fix: creating temporary key
  • Loading branch information
matheuscdantas authored Dec 21, 2023
1 parent bc919e5 commit 458a2da
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,10 @@
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.errors.ErrorResponseException;
import io.minio.errors.InsufficientDataException;
import io.minio.errors.InternalException;
import io.minio.errors.InvalidResponseException;
import io.minio.errors.ServerException;
import io.minio.errors.XmlParserException;
import org.eclipse.edc.spi.EdcException;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;


public class S3ConnectorApiImpl implements S3ConnectorApi {

Expand All @@ -59,14 +50,12 @@ public void createBucket(String bucketName) {
if (!bucketExists(bucketName.toLowerCase())) {
try {
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName.toLowerCase()).region(region).build());
} catch (InvalidKeyException | ErrorResponseException | InsufficientDataException | InternalException |
InvalidResponseException | NoSuchAlgorithmException | ServerException | XmlParserException |
IllegalArgumentException | IOException e) {
e.printStackTrace();
} catch (Exception e) {
throw new EdcException("Creating bucket: " + e.getMessage());
}
}
}

@Override
public void uploadParts(String bucketName, String fileName, ByteArrayInputStream part) {
if (!bucketExists(bucketName.toLowerCase())) {
Expand All @@ -75,51 +64,51 @@ public void uploadParts(String bucketName, String fileName, ByteArrayInputStream

try {
minioClient.putObject(PutObjectArgs.builder().bucket(bucketName.toLowerCase()).region(region).object(fileName).stream(part, part.available(), -1).build());
} catch (InvalidKeyException | ErrorResponseException | InsufficientDataException | InternalException |
InvalidResponseException | NoSuchAlgorithmException | ServerException | XmlParserException |
IllegalArgumentException | IOException e) {
e.printStackTrace();
} catch (Exception e) {
throw new EdcException("Uploading parts: " + e.getMessage());
}
}

@Override
public byte[] getFile(String bucketName, String fileName) {
if (!bucketExists(bucketName.toLowerCase())) {
return null;
throw new EdcException("Bucket not found - " + bucketName);
}

InputStream stream;
try {
stream = minioClient.getObject(GetObjectArgs.builder().bucket(bucketName.toLowerCase()).region(region).object(fileName).build());
return stream.readAllBytes();
} catch (InvalidKeyException | ErrorResponseException | InsufficientDataException | InternalException |
InvalidResponseException | NoSuchAlgorithmException | ServerException | XmlParserException |
IllegalArgumentException | IOException e) {
e.printStackTrace();
return null;
} catch (Exception e) {
throw new EdcException("Getting file - " + e.getMessage());
}
}

@Override
public boolean bucketExists(String bucketName) {
try {
return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName.toLowerCase()).region(this.region).build());
} catch (InvalidKeyException | ErrorResponseException | InsufficientDataException | InternalException |
InvalidResponseException | NoSuchAlgorithmException | ServerException | XmlParserException |
IllegalArgumentException | IOException e) {
e.printStackTrace();
return false;
} catch (Exception e) {
throw new EdcException("Verifying if bucket exists - " + e.getMessage());
}
}

@Override
public TemporaryKey createTemporaryKey() {
return ionosApi.createTemporaryKey(token);
try{
return ionosApi.createTemporaryKey(token);
} catch (Exception e) {
throw new EdcException("Creating temporary key - (Warning: max 5 keys on the storage) - " + e.getMessage());
}
}

@Override
public void deleteTemporaryKey(String accessKey) {
ionosApi.deleteTemporaryAccount(token,accessKey);
try{
ionosApi.deleteTemporaryAccount(token,accessKey);
} catch (Exception e) {
throw new EdcException("Deleting temporary key: " + e.getMessage());
}
}

private String getRegion(String endpoint) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.eclipse.edc.spi.EdcException;


public class HttpConnector {
Expand Down Expand Up @@ -45,8 +46,7 @@ public TemporaryKey createTemporaryKey(String token) {
TemporaryKey temp = new TemporaryKey(resp.getId().toString(),resp.getProperties().get("secretKey").toString());
return temp;
} catch (IOException e) {
e.printStackTrace();
return new TemporaryKey("", "");
throw new EdcException("Error getting S3 temporary key", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.eclipse.edc.connector.dataplane.spi.pipeline.DataSource;
import org.eclipse.edc.connector.dataplane.spi.pipeline.StreamResult;
import org.eclipse.edc.connector.dataplane.util.sink.ParallelSink;
import org.eclipse.edc.spi.EdcException;
import org.jetbrains.annotations.NotNull;

import java.io.ByteArrayInputStream;
Expand Down Expand Up @@ -46,6 +47,9 @@ protected StreamResult<Void> transferParts(List<DataSource.Part> parts) {
}

try (var input = part.openStream()) {
if(input == null) {
throw new EdcException("Error transferring file");
}
s3Api.uploadParts(bucketName, blobName, new ByteArrayInputStream(input.readAllBytes()));
} catch (Exception e) {
return uploadFailure(e, blobName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,23 @@
import com.ionos.edc.extension.s3.api.S3ConnectorApi;
import org.eclipse.edc.connector.dataplane.spi.pipeline.DataSource;
import org.eclipse.edc.connector.dataplane.spi.pipeline.StreamResult;
import org.eclipse.edc.spi.EdcException;
import org.eclipse.edc.spi.monitor.Monitor;
import org.jetbrains.annotations.NotNull;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.stream.Stream;

import static java.lang.String.format;
import static org.eclipse.edc.connector.dataplane.spi.pipeline.StreamResult.success;

class IonosDataSource implements DataSource {

private S3ConnectorApi s3Api;
private String bucketName;
private String blobName;
private static Monitor monitor;

private IonosDataSource() {
}
Expand Down Expand Up @@ -56,10 +62,30 @@ public String name() {

@Override
public InputStream openStream() {
return new ByteArrayInputStream(s3Api.getFile(bucketName, blobName));
try {
byte[] file = s3Api.getFile(bucketName, blobName);
if (file == null) {
throw new EdcException("Error trying to getFile");
}

InputStream targetStream = new ByteArrayInputStream(file);
return targetStream;
} catch (Exception e) {
openingFailure(e, blobName);
}
return null;
}

@NotNull
private StreamResult<Void> openingFailure(Exception e, String blobName) {
var message = format("Error opening file: %s", blobName, e.getMessage());
monitor.severe(message, e);
return StreamResult.error(message);
}
}



public static class Builder {
private final IonosDataSource source;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import com.ionos.edc.extension.s3.api.S3ConnectorApi;
import com.ionos.edc.extension.s3.configuration.IonosToken;
import com.ionos.edc.extension.s3.connector.ionosapi.TemporaryKey;

import dev.failsafe.RetryPolicy;
import org.eclipse.edc.connector.transfer.spi.provision.Provisioner;
Expand All @@ -30,6 +31,7 @@
import java.time.OffsetDateTime;
import java.util.concurrent.CompletableFuture;
import static dev.failsafe.Failsafe.with;
import static java.lang.String.format;

public class IonosS3Provisioner implements Provisioner<IonosS3ResourceDefinition, IonosS3ProvisionedResource> {
private final RetryPolicy<Object> retryPolicy;
Expand Down Expand Up @@ -61,9 +63,13 @@ public CompletableFuture<StatusResult<ProvisionResponse>> provision(IonosS3Resou
if (!s3Api.bucketExists(bucketName)) {
createBucket(bucketName);
}
TemporaryKey serviceAccount = null;

var serviceAccount = s3Api.createTemporaryKey();

try {
serviceAccount = s3Api.createTemporaryKey();
} catch (Exception e) {
failureCreatingKey(e);
}
String resourceName = resourceDefinition.getKeyName();

var resourceBuilder = IonosS3ProvisionedResource.Builder.newInstance()
Expand All @@ -89,6 +95,12 @@ public CompletableFuture<StatusResult<ProvisionResponse>> provision(IonosS3Resou
return CompletableFuture.completedFuture(StatusResult.success(response));
}

@NotNull
private void failureCreatingKey(Exception e) {
var message = format("Error creating temporary key: ", e.getMessage());
monitor.severe(message, e);
}

@Override
public CompletableFuture<StatusResult<DeprovisionedResource>> deprovision(
IonosS3ProvisionedResource provisionedResource, org.eclipse.edc.policy.model.Policy policy) {
Expand Down

0 comments on commit 458a2da

Please sign in to comment.