Skip to content

Commit

Permalink
fix (jkube-kit/resource/helm) : Add chart name validation before doin…
Browse files Browse the repository at this point in the history
…g Helm OCI push

Add ImageName constructor call to verify name of the provided helm chart
is a valid OCI artifact name

Signed-off-by: Rohan Kumar <rohaan@redhat.com>
  • Loading branch information
rohanKanojia committed Jan 12, 2024
1 parent 0898414 commit e3566bb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Usage:
* Fix #2257: Provide guidance when the final project packaged file is not found in Quarkus projeicts
* Fix #1690: Base images based on ubi9
* Fix #2070: build goals/tasks log warning if user forgets to run package/build goal/task
* Fix #2314: Add chart name validation before doing Helm OCI push
* Fix #2381: Container Images based on Java 21 (Java-exec, Tomcat, Jetty, Karaf)
* Fix #2389: Helm `values.yaml` sorted alphabetically
* Fix #2390: support for all missing Chart.yaml fields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import io.fabric8.kubernetes.client.http.HttpClient;
import io.fabric8.kubernetes.client.utils.HttpClientUtils;
import org.eclipse.jkube.kit.common.util.Serialization;
import org.eclipse.jkube.kit.config.image.ImageName;
import org.eclipse.jkube.kit.resource.helm.BadUploadException;
import org.eclipse.jkube.kit.resource.helm.Chart;
import org.eclipse.jkube.kit.resource.helm.HelmRepository;
Expand Down Expand Up @@ -46,6 +47,7 @@ public void uploadSingle(File file, HelmRepository repository)
.build()
) {
final Chart chart = readGeneratedChartYamlFile(file);
validateChartName(chart);
final byte[] chartYamlBytes = Serialization.asJson(chart).getBytes(StandardCharsets.UTF_8);
final OCIRegistryClient oci = new OCIRegistryClient(repository, httpClient);
final OCIManifestLayer chartConfig = oci.uploadBlobIfNotUploadedYet(chart, new ByteArrayInputStream(chartYamlBytes));
Expand All @@ -61,4 +63,12 @@ private static Chart readGeneratedChartYamlFile(File chartArchive) throws IOExce
}
throw new IllegalStateException("Could not find Chart.yaml file in " + chartArchive.getParentFile());
}

private static void validateChartName(Chart chart) {
try {
new ImageName(chart.getName());
} catch (IllegalArgumentException illegalArgumentException) {
throw new IllegalArgumentException("Chart name " + chart.getName() + " is invalid for uploading to OCI registry", illegalArgumentException);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
package org.eclipse.jkube.kit.resource.helm.oci;

import org.eclipse.jkube.kit.common.util.Serialization;
import org.eclipse.jkube.kit.resource.helm.Chart;
import org.eclipse.jkube.kit.resource.helm.HelmRepository;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
Expand All @@ -22,6 +24,7 @@
import java.nio.file.Files;
import java.nio.file.Path;

import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;

class OCIRepositoryUploaderTest {
Expand All @@ -38,4 +41,17 @@ void withMissingChart_throwsException(@TempDir Path tempDir) throws IOException
.withMessageStartingWith("Could not find Chart.yaml file in ")
.withMessageEndingWith("helm");
}

@Test
void withInvalidChartName_throwsException(@TempDir Path tempDir) throws IOException {
// Given
Chart chart = Chart.builder().name("-invalid-chart-name^").build();
File chartFile = tempDir.resolve("Chart.yaml").toFile();
Serialization.saveJson(chartFile, chart);
final HelmRepository repository = HelmRepository.builder().build();
// When + Then
assertThatIllegalArgumentException()
.isThrownBy(() -> new OCIRepositoryUploader().uploadSingle(chartFile, repository))
.withMessageStartingWith("Chart name -invalid-chart-name^ is invalid for uploading to OCI registry");
}
}

0 comments on commit e3566bb

Please sign in to comment.