Skip to content

Commit f8c257e

Browse files
committed
Refactor artifact management
1 parent c0f03e4 commit f8c257e

File tree

78 files changed

+792
-1202
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+792
-1202
lines changed

hawkbit-artifact/hawkbit-artifact-api/src/main/java/org/eclipse/hawkbit/repository/artifact/AbstractArtifactRepository.java

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
/**
2-
* Copyright (c) 2025 Bosch Digital GmbH, Germany. All rights reserved.
2+
* Copyright (c) 2025 Contributors to the Eclipse Foundation
3+
*
4+
* This program and the accompanying materials are made
5+
* available under the terms of the Eclipse Public License 2.0
6+
* which is available at https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
39
*/
410
package org.eclipse.hawkbit.repository.artifact;
511

@@ -18,8 +24,8 @@
1824
import lombok.extern.slf4j.Slf4j;
1925
import org.eclipse.hawkbit.repository.artifact.exception.ArtifactStoreException;
2026
import org.eclipse.hawkbit.repository.artifact.exception.HashNotMatchException;
21-
import org.eclipse.hawkbit.repository.artifact.model.AbstractDbArtifact;
22-
import org.eclipse.hawkbit.repository.artifact.model.DbArtifactHash;
27+
import org.eclipse.hawkbit.repository.artifact.model.StoredArtifactInfo;
28+
import org.eclipse.hawkbit.repository.artifact.model.ArtifactHashes;
2329
import org.springframework.util.ObjectUtils;
2430

2531
/**
@@ -34,9 +40,9 @@ public abstract class AbstractArtifactRepository implements ArtifactRepository {
3440
// suppress warning, of not strong enough hashing algorithm, SHA-1 and MD5 is not used security related
3541
@SuppressWarnings("squid:S2070")
3642
@Override
37-
public AbstractDbArtifact store(
43+
public StoredArtifactInfo store(
3844
final String tenant, final InputStream content, final String filename, final String contentType,
39-
final DbArtifactHash providedHashes) {
45+
final ArtifactHashes providedHashes) {
4046
final MessageDigest mdSHA1;
4147
final MessageDigest mdMD5;
4248
final MessageDigest mdSHA256;
@@ -54,19 +60,19 @@ public AbstractDbArtifact store(
5460

5561
final HexFormat hexFormat = HexFormat.of().withLowerCase();
5662

57-
final String sha1Hash16 = hexFormat.formatHex(mdSHA1.digest());
58-
final String md5Hash16 = hexFormat.formatHex(mdMD5.digest());
59-
final String sha256Hash16 = hexFormat.formatHex(mdSHA256.digest());
63+
final String sha1Hash = hexFormat.formatHex(mdSHA1.digest());
64+
final String md5Hash = hexFormat.formatHex(mdMD5.digest());
65+
final String sha256Hash = hexFormat.formatHex(mdSHA256.digest());
6066

61-
checkHashes(providedHashes, sha1Hash16, md5Hash16, sha256Hash16);
67+
checkHashes(providedHashes, sha1Hash, md5Hash, sha256Hash);
6268

6369
// Check if file with same sha1 hash exists and if so return it
64-
if (existsBySha1(tenant, sha1Hash16)) {
70+
if (existsBySha1(tenant, sha1Hash)) {
6571
// TODO - shall check if the file is really the same as bytes or just sha1 hash is the same
66-
return addMissingHashes(getBySha1(tenant, sha1Hash16), sha1Hash16, md5Hash16, sha256Hash16);
72+
return new StoredArtifactInfo(contentType, tempFile.length(), new ArtifactHashes(sha1Hash, md5Hash, sha256Hash));
6773
}
6874

69-
return store(sanitizeTenant(tenant), new DbArtifactHash(sha1Hash16, md5Hash16, sha256Hash16), contentType, tempFile);
75+
return store(sanitizeTenant(tenant), new ArtifactHashes(sha1Hash, md5Hash, sha256Hash), contentType, tempFile);
7076
} catch (final IOException e) {
7177
throw new ArtifactStoreException(e.getMessage(), e);
7278
} finally {
@@ -98,8 +104,8 @@ protected String storeTempFile(final InputStream content) throws IOException {
98104
return file.getPath();
99105
}
100106

101-
protected abstract AbstractDbArtifact store(final String tenant, final DbArtifactHash base16Hashes,
102-
final String contentType, final String tempFile) throws IOException;
107+
protected abstract StoredArtifactInfo store(
108+
final String tenant, final ArtifactHashes base16Hashes, final String contentType, final String tempFile) throws IOException;
103109

104110
// java:S1066 - more readable with separate "if" statements
105111
// java:S4042 - delete reason is not needed
@@ -132,8 +138,8 @@ public static File createTempFile(final boolean directory) {
132138
}
133139
}
134140

135-
private static void checkHashes(final DbArtifactHash providedHashes,
136-
final String sha1Hash16, final String md5Hash16, final String sha256Hash16) {
141+
private static void checkHashes(
142+
final ArtifactHashes providedHashes, final String sha1Hash16, final String md5Hash16, final String sha256Hash16) {
137143
if (providedHashes == null) {
138144
return;
139145
}
@@ -161,16 +167,6 @@ private static DigestInputStream wrapInDigestInputStream(final InputStream input
161167
return new DigestInputStream(new DigestInputStream(new DigestInputStream(input, mdSHA256), mdMD5), mdSHA1);
162168
}
163169

164-
private AbstractDbArtifact addMissingHashes(final AbstractDbArtifact existing,
165-
final String calculatedSha1, final String calculatedMd5, final String calculatedSha256) {
166-
final String sha1 = checkEmpty(existing.getHashes().sha1(), calculatedSha1);
167-
final String md5 = checkEmpty(existing.getHashes().md5(), calculatedMd5);
168-
final String sha256 = checkEmpty(existing.getHashes().sha256(), calculatedSha256);
169-
170-
existing.setHashes(new DbArtifactHash(sha1, md5, sha256));
171-
return existing;
172-
}
173-
174170
private String checkEmpty(final String value, final String fallback) {
175171
return ObjectUtils.isEmpty(value) ? fallback : value;
176172
}

hawkbit-artifact/hawkbit-artifact-api/src/main/java/org/eclipse/hawkbit/repository/artifact/ArtifactRepository.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
/**
2-
* Copyright (c) 2025 Bosch Digital GmbH, Germany. All rights reserved.
2+
* Copyright (c) 2025 Contributors to the Eclipse Foundation
3+
*
4+
* This program and the accompanying materials are made
5+
* available under the terms of the Eclipse Public License 2.0
6+
* which is available at https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
39
*/
410
package org.eclipse.hawkbit.repository.artifact;
511

@@ -11,8 +17,8 @@
1117
import org.eclipse.hawkbit.repository.artifact.exception.ArtifactBinaryNotFoundException;
1218
import org.eclipse.hawkbit.repository.artifact.exception.ArtifactStoreException;
1319
import org.eclipse.hawkbit.repository.artifact.exception.HashNotMatchException;
14-
import org.eclipse.hawkbit.repository.artifact.model.AbstractDbArtifact;
15-
import org.eclipse.hawkbit.repository.artifact.model.DbArtifactHash;
20+
import org.eclipse.hawkbit.repository.artifact.model.StoredArtifactInfo;
21+
import org.eclipse.hawkbit.repository.artifact.model.ArtifactHashes;
1622

1723
/**
1824
* ArtifactRepository service interface.
@@ -32,19 +38,20 @@ public interface ArtifactRepository {
3238
* @throws ArtifactStoreException in case storing of the artifact was not successful
3339
* @throws HashNotMatchException in case {@code hash} is provided and not matching to the calculated hashes during storing
3440
*/
35-
AbstractDbArtifact store(
41+
StoredArtifactInfo store(
3642
@NotEmpty String tenant, @NotNull InputStream content, @NotEmpty String filename,
37-
String contentType, DbArtifactHash hash);
43+
String contentType, ArtifactHashes hash);
3844

3945
/**
40-
* Retrieves a {@link AbstractDbArtifact} from the store by its SHA1 hash. Throws {@link ArtifactBinaryNotFoundException} if not found.
46+
* Retrieves a {@link StoredArtifactInfo} from the store by its SHA1 hash. Throws {@link ArtifactBinaryNotFoundException} if not found.
47+
* The caller is responsible to close the InputStream.
4148
*
4249
* @param tenant the tenant to store the artifact
4350
* @param sha1Hash the sha1-hash of the file to lookup.
4451
* @return The artifact file object or {@code null} if no file exists.
4552
* @throws UnsupportedOperationException if implementation does not support the operation
4653
*/
47-
AbstractDbArtifact getBySha1(@NotEmpty String tenant, @NotEmpty String sha1Hash);
54+
InputStream getBySha1(@NotEmpty String tenant, @NotEmpty String sha1Hash);
4855

4956
/**
5057
* Checks if an artifact exists for a given tenant by its sha1 hash

hawkbit-artifact/hawkbit-artifact-api/src/main/java/org/eclipse/hawkbit/repository/artifact/encryption/ArtifactEncryption.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
/**
2-
* Copyright (c) 2025 Bosch Digital GmbH, Germany. All rights reserved.
2+
* Copyright (c) 2025 Contributors to the Eclipse Foundation
3+
*
4+
* This program and the accompanying materials are made
5+
* available under the terms of the Eclipse Public License 2.0
6+
* which is available at https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
39
*/
410
package org.eclipse.hawkbit.repository.artifact.encryption;
511

@@ -55,4 +61,4 @@ public interface ArtifactEncryption {
5561
* @return encryption overhead in byte
5662
*/
5763
int encryptionSizeOverhead();
58-
}
64+
}

hawkbit-artifact/hawkbit-artifact-api/src/main/java/org/eclipse/hawkbit/repository/artifact/encryption/ArtifactEncryptionSecretsStore.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
/**
2-
* Copyright (c) 2025 Bosch Digital GmbH, Germany. All rights reserved.
2+
* Copyright (c) 2025 Contributors to the Eclipse Foundation
3+
*
4+
* This program and the accompanying materials are made
5+
* available under the terms of the Eclipse Public License 2.0
6+
* which is available at https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
39
*/
410
package org.eclipse.hawkbit.repository.artifact.encryption;
511

hawkbit-artifact/hawkbit-artifact-api/src/main/java/org/eclipse/hawkbit/repository/artifact/encryption/ArtifactEncryptionService.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
/**
2-
* Copyright (c) 2025 Bosch Digital GmbH, Germany. All rights reserved.
2+
* Copyright (c) 2025 Contributors to the Eclipse Foundation
3+
*
4+
* This program and the accompanying materials are made
5+
* available under the terms of the Eclipse Public License 2.0
6+
* which is available at https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
39
*/
410
package org.eclipse.hawkbit.repository.artifact.encryption;
511

@@ -15,8 +21,7 @@
1521
import org.springframework.beans.factory.annotation.Autowired;
1622

1723
/**
18-
* Service responsible for encryption operations. Should be registered as a bean in order its autowired dependencies
19-
* to be injected.
24+
* Service responsible for encryption operations. Should be registered as a bean in order its autowired dependencies to be injected.
2025
*/
2126
@NoArgsConstructor(access = AccessLevel.PRIVATE)
2227
@SuppressWarnings("java:S6548") // singleton holder ensures static access to spring resources in some places
@@ -105,7 +110,7 @@ public InputStream decryptArtifact(final long id, final InputStream encryptedArt
105110
throw new ArtifactEncryptionUnsupportedException("Artifact decryption is not supported.");
106111
}
107112

108-
final var secrets = getEncryptionSecrets(id);
113+
final Map<String, String> secrets = getEncryptionSecrets(id);
109114
try {
110115
return artifactEncryption.decryptStream(secrets, encryptedArtifactStream);
111116
} finally {

hawkbit-artifact/hawkbit-artifact-api/src/main/java/org/eclipse/hawkbit/repository/artifact/exception/ArtifactBinaryNoLongerExistsException.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
/**
2-
* Copyright (c) 2025 Bosch Digital GmbH, Germany. All rights reserved.
2+
* Copyright (c) 2021 Bosch.IO GmbH and others
3+
*
4+
* This program and the accompanying materials are made
5+
* available under the terms of the Eclipse Public License 2.0
6+
* which is available at https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
39
*/
410
package org.eclipse.hawkbit.repository.artifact.exception;
511

hawkbit-artifact/hawkbit-artifact-api/src/main/java/org/eclipse/hawkbit/repository/artifact/exception/ArtifactBinaryNotFoundException.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
/**
2-
* Copyright (c) 2025 Bosch Digital GmbH, Germany. All rights reserved.
2+
* Copyright (c) 2015 Bosch Software Innovations GmbH and others
3+
*
4+
* This program and the accompanying materials are made
5+
* available under the terms of the Eclipse Public License 2.0
6+
* which is available at https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
39
*/
410
package org.eclipse.hawkbit.repository.artifact.exception;
511

hawkbit-artifact/hawkbit-artifact-api/src/main/java/org/eclipse/hawkbit/repository/artifact/exception/ArtifactDeleteFailedException.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
/**
2-
* Copyright (c) 2025 Bosch Digital GmbH, Germany. All rights reserved.
2+
* Copyright (c) 2015 Bosch Software Innovations GmbH and others
3+
*
4+
* This program and the accompanying materials are made
5+
* available under the terms of the Eclipse Public License 2.0
6+
* which is available at https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
39
*/
410
package org.eclipse.hawkbit.repository.artifact.exception;
511

hawkbit-artifact/hawkbit-artifact-api/src/main/java/org/eclipse/hawkbit/repository/artifact/exception/ArtifactEncryptionFailedException.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
/**
2-
* Copyright (c) 2025 Bosch Digital GmbH, Germany. All rights reserved.
2+
* Copyright (c) 2025 Contributors to the Eclipse Foundation
3+
*
4+
* This program and the accompanying materials are made
5+
* available under the terms of the Eclipse Public License 2.0
6+
* which is available at https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
39
*/
410
package org.eclipse.hawkbit.repository.artifact.exception;
511

hawkbit-artifact/hawkbit-artifact-api/src/main/java/org/eclipse/hawkbit/repository/artifact/exception/ArtifactEncryptionUnsupportedException.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
/**
2-
* Copyright (c) 2025 Bosch Digital GmbH, Germany. All rights reserved.
2+
* Copyright (c) 2025 Contributors to the Eclipse Foundation
3+
*
4+
* This program and the accompanying materials are made
5+
* available under the terms of the Eclipse Public License 2.0
6+
* which is available at https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
39
*/
410
package org.eclipse.hawkbit.repository.artifact.exception;
511

0 commit comments

Comments
 (0)