Skip to content

Commit

Permalink
fix (jkube-kit) : Don't pass Invalid port in host headers for Helm OC…
Browse files Browse the repository at this point in the history
…I push

Passing host:port seems to work when host url contains a valid port.
However, this becomes host:-1 for some host that doesn't contain port in
URL string (like r.example.com). Add case for these kind of hosts so
that we pass correct host in Host HTTP headers.

Signed-off-by: Rohan Kumar <[email protected]>
  • Loading branch information
rohanKanojia authored and manusa committed Oct 11, 2023
1 parent cc6d239 commit fc55be3
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Usage:
* Fix #2399: Helm no longer generates default function; broadens support for different value types
* Fix #2400: Helm supports complex values in `values.yaml` fragments (such as annotations or arrays)
* Fix #2414: OpenShift Gradle Plugin picks up `jkube.build.pushSecret` property
* Fix #2417: Don't pass Invalid port in host headers for Helm OCI push

_**Note**_:
- Container Images generated using jkube opinionated defaults no longer contain full timestamp in `org.label-schema.build-date` label. The label contains the build date in the format `yyyy-MM-dd`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ private OCIManifestLayer uploadBlob(String uploadUrl, CountingInputStream blobSt

private HttpRequest.Builder newRequest() {
return httpClient.newHttpRequestBuilder()
.header("Host", ociRegistryEndpoint.getBaseUrl().getHost() + ":" + ociRegistryEndpoint.getBaseUrl().getPort())
.header("Host", ociRegistryEndpoint.getOCIRegistryHost())
.header("User-Agent", USER_AGENT);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.HashMap;
import java.util.Map;

import static org.apache.commons.lang3.StringUtils.EMPTY;

public class OCIRegistryEndpoint {

private static final Map<String, String> PROTOCOL_MAPPER = new HashMap<>();
Expand Down Expand Up @@ -53,6 +55,14 @@ public String getManifestUrl(Chart chart) {
return String.format("%s/%s/manifests/%s", apiV2Url, chart.getName(), chart.getVersion());
}

public String getOCIRegistryHost() {
final StringBuilder hostBuilder = new StringBuilder(baseUrl.getHost());
if (baseUrl.getPort() > 0 && baseUrl.getPort() != 80 && baseUrl.getPort() != 443) {
hostBuilder.append(":").append(baseUrl.getPort());
}
return hostBuilder.toString();
}

public URI getBaseUrl() {
return baseUrl;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import org.eclipse.jkube.kit.resource.helm.HelmRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import java.net.URI;

Expand Down Expand Up @@ -81,4 +83,22 @@ void getBlobUrl_whenInvoked_shouldReturnBlobUrl() {
assertThat(result)
.isEqualTo("https://r.example.com/v2/myuser/test-chart/blobs/sha256:7ed393daf1ffc94803c08ffcbecb798fa58e786bebffbab02da5458f68d0ecb0");
}

@ParameterizedTest(name = "getOCIRegistryHost with url {0} should return {1}")
@CsvSource({
"http://localhost:5000/myuser,localhost:5000",
"https://r.example.com/myuser,r.example.com",
"https://r.example.com:443/myuser,r.example.com",
"http://r.example.com:80/myuser,r.example.com"
})
void getOCIRegistryHost_whenBaseUrlContainsHostAndPort_thenReturnHostWithPort(String url, String expectedHost) {
// Given
this.registryEndpoint = new OCIRegistryEndpoint(HelmRepository.builder().url(url).build());

// When
String host = registryEndpoint.getOCIRegistryHost();

// Then
assertThat(host).isEqualTo(expectedHost);
}
}

0 comments on commit fc55be3

Please sign in to comment.