Skip to content
This repository was archived by the owner on May 14, 2025. It is now read-only.

Commit 167132d

Browse files
author
Corneil du Plessis
authored
Fix handling of docker uri (#5130)
- Addresses docker uri that have colon in the port. See #5111
1 parent f2aed23 commit 167132d

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

spring-cloud-dataflow-container-registry/src/main/java/org/springframework/cloud/dataflow/container/registry/authorization/DockerConfigJsonSecretToRegistryConfigurationConverter.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.cloud.dataflow.container.registry.authorization;
1818

19+
import java.net.URI;
1920
import java.util.Collections;
2021
import java.util.HashMap;
2122
import java.util.List;
@@ -43,6 +44,7 @@
4344

4445
/**
4546
* @author Christian Tzolov
47+
* @author Corneil du Plessis
4648
*/
4749
public class DockerConfigJsonSecretToRegistryConfigurationConverter implements Converter<String, Map<String, ContainerRegistryConfiguration>> {
4850

@@ -164,9 +166,29 @@ public Optional<String> getDockerTokenServiceUri(String registryHost, boolean di
164166

165167
try {
166168
RestTemplate restTemplate = this.containerImageRestTemplate.getContainerRestTemplate(disableSSl, useHttpProxy);
167-
restTemplate.exchange(
168-
UriComponentsBuilder.newInstance().scheme("https").host(registryHost).path("v2/").build().toUri(),
169-
HttpMethod.GET, new HttpEntity<>(new HttpHeaders()), Map.class);
169+
String host = registryHost;
170+
Integer port = null;
171+
if (registryHost.contains(":")) {
172+
int colon = registryHost.lastIndexOf(":");
173+
String portString = registryHost.substring(colon+1);
174+
try {
175+
int intPort = Integer.parseInt(portString);
176+
if (Integer.toString(intPort).equals(portString) && intPort > 0 && intPort < 32767) {
177+
port = intPort;
178+
host = registryHost.substring(0, colon);
179+
}
180+
} catch (NumberFormatException x) {
181+
// not valid integer
182+
}
183+
}
184+
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.newInstance().scheme("https").host(host);
185+
if (port != null) {
186+
uriComponentsBuilder.port(port);
187+
}
188+
uriComponentsBuilder.path("v2/");
189+
URI uri = uriComponentsBuilder.build().toUri();
190+
logger.info("getDockerTokenServiceUri:" + uri);
191+
restTemplate.exchange(uri, HttpMethod.GET, new HttpEntity<>(new HttpHeaders()), Map.class);
170192
return Optional.empty();
171193
}
172194
catch (HttpClientErrorException httpError) {
@@ -209,6 +231,8 @@ public Optional<String> getDockerTokenServiceUri(String registryHost, boolean di
209231
return Optional.of(tokenServiceUri);
210232
}
211233
catch (Exception e) {
234+
// Log error because we cannot change the contract that returns empty optional.
235+
logger.error("Ignoring:" + e, e);
212236
return Optional.empty();
213237
}
214238
}

spring-cloud-dataflow-container-registry/src/test/java/org/springframework/cloud/dataflow/container/registry/authorization/DockerConfigJsonSecretToContainerRegistryConfigurationConverterTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747

4848
/**
4949
* @author Christian Tzolov
50+
* @author Corneil du Plessis
5051
*/
5152
public class DockerConfigJsonSecretToContainerRegistryConfigurationConverterTest {
5253

@@ -107,6 +108,27 @@ public void testConvertBasicAuthRegistry() throws URISyntaxException {
107108
assertThat(registryConfiguration.getAuthorizationType(), is(ContainerRegistryConfiguration.AuthorizationType.basicauth));
108109
}
109110

111+
@Test
112+
public void testConvertWithPort() throws URISyntaxException {
113+
114+
when(mockRestTemplate.exchange(
115+
eq(new URI("https://demo.repository.io/v2/_catalog")), eq(HttpMethod.GET), any(), eq(Map.class)))
116+
.thenReturn(new ResponseEntity<>(new HashMap<>(), HttpStatus.OK));
117+
118+
String b = "{\"auths\":{\"demo.repository.io:5050\":{\"username\":\"testuser\",\"password\":\"testpassword\",\"auth\":\"YWRtaW46SGFyYm9yMTIzNDU=\"}}}";
119+
Map<String, ContainerRegistryConfiguration> result = converter.convert(b);
120+
121+
assertThat(result).hasSize(1);
122+
assertThat(result).containsKey("demo.repository.io:5050");
123+
124+
ContainerRegistryConfiguration registryConfiguration = result.get("demo.repository.io:5050");
125+
126+
assertThat(registryConfiguration.getRegistryHost()).isEqualTo("demo.repository.io:5050");
127+
assertThat(registryConfiguration.getUser()).isEqualTo("testuser");
128+
assertThat(registryConfiguration.getSecret()).isEqualTo("testpassword");
129+
assertThat(registryConfiguration.getAuthorizationType()).isEqualTo(ContainerRegistryConfiguration.AuthorizationType.basicauth);
130+
}
131+
110132
@Test
111133
public void testConvertDockerHubRegistry() throws URISyntaxException {
112134

0 commit comments

Comments
 (0)