|
16 | 16 |
|
17 | 17 | package org.springframework.cloud.dataflow.container.registry.authorization; |
18 | 18 |
|
| 19 | +import java.net.URI; |
19 | 20 | import java.util.Collections; |
20 | 21 | import java.util.HashMap; |
21 | 22 | import java.util.List; |
|
43 | 44 |
|
44 | 45 | /** |
45 | 46 | * @author Christian Tzolov |
| 47 | + * @author Corneil du Plessis |
46 | 48 | */ |
47 | 49 | public class DockerConfigJsonSecretToRegistryConfigurationConverter implements Converter<String, Map<String, ContainerRegistryConfiguration>> { |
48 | 50 |
|
@@ -164,9 +166,29 @@ public Optional<String> getDockerTokenServiceUri(String registryHost, boolean di |
164 | 166 |
|
165 | 167 | try { |
166 | 168 | 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); |
170 | 192 | return Optional.empty(); |
171 | 193 | } |
172 | 194 | catch (HttpClientErrorException httpError) { |
@@ -209,6 +231,8 @@ public Optional<String> getDockerTokenServiceUri(String registryHost, boolean di |
209 | 231 | return Optional.of(tokenServiceUri); |
210 | 232 | } |
211 | 233 | catch (Exception e) { |
| 234 | + // Log error because we cannot change the contract that returns empty optional. |
| 235 | + logger.error("Ignoring:" + e, e); |
212 | 236 | return Optional.empty(); |
213 | 237 | } |
214 | 238 | } |
|
0 commit comments