Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix open S3Object InputStream #71

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

herdt-michael
Copy link

Open S3Object InputStream when creating an instance of S3Artifact to make it reusable and prevent a wrong usage because of an unused open InputStream.

Signed-off-by: Michael Herdt [email protected]

…make it reusable and prevent a wrong usage because of an unused open InputStream.

Signed-off-by: Michael Herdt <[email protected]>
Signed-off-by: Michael Herdt <[email protected]>
Signed-off-by: Michael Herdt <[email protected]>
Copy link
Contributor

@StefanKlt StefanKlt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks @herdt-michael 👍

@bogdan-bondar
Copy link
Contributor

@hawkbit-bot verify please


private static DbArtifactHash createArtifactHash(final String artifactId, ObjectMetadata metadata) {
return new DbArtifactHash(artifactId, BaseEncoding.base16().lowerCase()
.encode(BaseEncoding.base64().decode(sanitizeEtag(metadata.getETag()))), null);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MINOR Use "java.util.Base64" instead. rule

}

private static S3Object getS3ObjectOrThrowException(AmazonS3 amazonS3, String bucketName, String key)
throws S3ArtifactNotFoundException {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MINOR Remove the declaration of thrown exception 'org.eclipse.hawkbit.artifact.repository.S3ArtifactNotFoundException' which is a runtime exception. rule

* in case that no artifact could be found for the given values
*/
public static S3Artifact get(final AmazonS3 amazonS3, final S3RepositoryProperties s3Properties, final String key,
final String artifactId) throws S3ArtifactNotFoundException {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MINOR Remove the declaration of thrown exception 'org.eclipse.hawkbit.artifact.repository.S3ArtifactNotFoundException' which is a runtime exception. rule

@hawkbit-bot
Copy link

SonarQube analysis reported 3 issues

  • MINOR 3 minor

Watch the comments in this conversation to review them.

Signed-off-by: Michael Herdt <[email protected]>

private void refreshS3ObjectIfNeeded() {
if (s3Object == null || s3InputStream == null) {
LOG.info("Initialize S3Object in bucket {} with key {}", s3Properties.getBucketName(), key);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we could change the log level to debug otherwise we would get a lot of log messages for each file download

private void refreshS3ObjectIfNeeded() {
if (s3Object == null || s3InputStream == null) {
LOG.info("Initialize S3Object in bucket {} with key {}", s3Properties.getBucketName(), key);
s3Object = amazonS3.getObject(s3Properties.getBucketName(), key);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why isn't the getS3ObjectOrThrowException method used here? s3Object can be potentially null

try {
return S3Artifact.get(amazonS3, s3Properties, key, sha1Hash);
} catch (final S3ArtifactNotFoundException e) {
LOG.debug("Cannot find artifact for bucket {} with key {}", e.getBucket(), e.getKey(), e);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here, however I would use level "warn" because we always execute existsByTenantAndSha1 beforehand in usage scenarios

}

private static S3Object getS3ObjectOrThrowException(AmazonS3 amazonS3, String bucketName, String key) {
final S3Object s3Object = amazonS3.getObject(bucketName, key);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should also catch SdkClientException as well as AmazonServiceException here

@bogdan-bondar
Copy link
Contributor

Frankly speaking I believe that proposed changes make the code less understandable with S3Artifact being mutable and used in different context (e.g. exists() is a duplicate of existsByTenantAndSha1() but used in different context). We need to define more strict borders of responsibility between S3Artifact and S3Repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants