forked from eclipse-jkube/jkube
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat (jkube-kit/resource/helm) : Support for Pushing Helm Charts to O…
…CI (eclipse-jkube#2091) + Add SHAUtil for generating SHA digests (used in oci helm push) + Add OCIRepositoryUploader as an additional HelmUploader + Add OCI repository related helper classes in `org.eclipse.jkube.kit.resource.helm.oci` package Signed-off-by: Rohan Kumar <rohaan@redhat.com>
- Loading branch information
1 parent
552d5e9
commit 50c0dcf
Showing
33 changed files
with
1,680 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/util/SHAUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/** | ||
* Copyright (c) 2019 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at: | ||
* | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
package org.eclipse.jkube.kit.common.util; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
|
||
public class SHAUtil { | ||
private static final int BUFFER_SIZE = 8192; | ||
private static final String SHA_256 = "SHA-256"; | ||
private SHAUtil() { } | ||
|
||
public static String generateSHA256(File file) throws IOException { | ||
return generateSHA(file, SHA_256); | ||
} | ||
|
||
public static String generateSHA256(String input) { | ||
MessageDigest digest = createNewMessageDigest(SHA_256); | ||
return bytesToHex(digest.digest(input.getBytes(StandardCharsets.UTF_8))); | ||
} | ||
|
||
public static String generateSHA(File file, String algorithm) throws IOException { | ||
try (InputStream input = Files.newInputStream(file.toPath())) { | ||
MessageDigest shaDigest = createNewMessageDigest(algorithm); | ||
byte[] buffer = new byte[BUFFER_SIZE]; | ||
int len = input.read(buffer); | ||
|
||
while (len != -1) { | ||
shaDigest.update(buffer, 0, len); | ||
len = input.read(buffer); | ||
} | ||
|
||
return bytesToHex(shaDigest.digest()); | ||
} | ||
} | ||
|
||
static MessageDigest createNewMessageDigest(String algorithm) { | ||
try { | ||
return MessageDigest.getInstance(algorithm); | ||
} catch (NoSuchAlgorithmException e) { | ||
throw new IllegalArgumentException("Error while generating SHA, no such algorithm : " + algorithm); | ||
} | ||
} | ||
|
||
private static String bytesToHex(byte[] hash) { | ||
StringBuilder hexString = new StringBuilder(2 * hash.length); | ||
for (byte b : hash) { | ||
String hex = Integer.toHexString(0xff & b); | ||
if (hex.length() == 1) { | ||
hexString.append('0'); | ||
} | ||
hexString.append(hex); | ||
} | ||
return hexString.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
jkube-kit/common/src/test/java/org/eclipse/jkube/kit/common/util/SHAUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* Copyright (c) 2019 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at: | ||
* | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
package org.eclipse.jkube.kit.common.util; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.security.MessageDigest; | ||
import java.util.Objects; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; | ||
|
||
class SHAUtilTest { | ||
@Test | ||
void generateSHA256_whenFileProvided_shouldGenerateDigest() throws IOException { | ||
// Given | ||
File file = new File(Objects.requireNonNull(getClass().getResource("/crontab-cr.yml")).getFile()); | ||
|
||
// When | ||
String result = SHAUtil.generateSHA256(file); | ||
|
||
// Then | ||
assertThat(result).isEqualTo("da723cee661dd0368f1b50274720e189372c336c2f78b9848826b7ff4f303b61"); | ||
} | ||
|
||
@Test | ||
void generateSHA256_whenStringProvided_shouldGenerateDigest() { | ||
// Given + When | ||
String result = SHAUtil.generateSHA256("somerandomstring"); | ||
|
||
// Then | ||
assertThat(result).isEqualTo("39b5931a5dd8980254782830a1134644035573a1d75a50527e233f45f6afc5ce"); | ||
} | ||
|
||
@Test | ||
void createNewMessageDigest_whenValidAlgorithmProvided_thenCreateMessageDigestObj() { | ||
// Given | ||
String algorithm = "SHA-256"; | ||
// When | ||
MessageDigest digest = SHAUtil.createNewMessageDigest(algorithm); | ||
// Then | ||
assertThat(digest.getAlgorithm()).isEqualTo(algorithm); | ||
} | ||
|
||
@Test | ||
void createNewMessageDigest_whenInvalidAlgorithmProvided_thenThrowException() { | ||
// Given | ||
String algorithm = "invalid"; | ||
// When + Then | ||
assertThatIllegalArgumentException() | ||
.isThrownBy(() -> SHAUtil.createNewMessageDigest(algorithm)) | ||
.withMessage("Error while generating SHA, no such algorithm : invalid"); | ||
} | ||
} |
62 changes: 0 additions & 62 deletions
62
...e-kit/common/src/test/java/org/eclipse/jkube/kit/common/util/TestFabric8HttpResponse.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.