-
Notifications
You must be signed in to change notification settings - Fork 71
fix: IPv6 host support #4367
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: v3.x.x
Are you sure you want to change the base?
fix: IPv6 host support #4367
Changes from 1 commit
c702446
623960f
99e1065
93d577c
0ca2efa
b1307ad
170d4e5
a473483
cb213d6
a3279b3
f739842
2907323
7ffa42f
c01baff
a0dc139
2a5ef3b
f4ad343
83d1253
a378cb8
52580af
c253933
fe7c7b9
f480f23
a00a7df
98d6b2c
d2a3ec4
c409868
d4f7a18
5cc5b65
68040eb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,6 +41,7 @@ | |
| import org.zowe.apiml.product.gateway.GatewayClient; | ||
| import org.zowe.apiml.product.instance.ServiceAddress; | ||
| import org.zowe.apiml.product.routing.RoutedService; | ||
| import org.zowe.apiml.util.UrlUtils; | ||
|
|
||
| import java.net.URI; | ||
| import java.util.Collections; | ||
|
|
@@ -106,7 +107,7 @@ private void updateServer(OpenAPI openAPI) { | |
| if (openAPI.getServers() != null) { | ||
| openAPI.getServers() | ||
| .forEach(server -> server.setUrl( | ||
| String.format("%s://%s/%s", scheme, getHostname(), server.getUrl()))); | ||
| UrlUtils.getUrl(scheme, UrlUtils.formatHostnameForUrl(getHostname())) + "/" + server.getUrl())); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. getHostname() returns host+port, which contains ":" even if there is no IPv6 address. This will probably create an incorrect URL
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've considered the possibility that getHostname() might be returning the host along with the port, and I've added handling for that here. But, I suspect the issue might be related to the formatHostnameForUrl() method. I'll take a closer look to better understand what's going wrong. |
||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -112,4 +112,77 @@ public boolean isValidUrl(String urlString) { | |
| return false; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Formats a hostname properly, ensuring IPv6 addresses are enclosed in square brackets. | ||
| * If the input is already a properly formatted IPv6 address (with brackets), it remains unchanged. | ||
| * | ||
| * @param hostname The hostname or IP address to format | ||
| * @return Properly formatted hostname, with IPv6 addresses enclosed in square brackets | ||
| */ | ||
| public String formatHostnameForUrl(String hostname) { | ||
| if (hostname == null) return null; | ||
|
|
||
| // If hostname already has IPv6 brackets, don't add them again | ||
| if (hostname.startsWith("[") && hostname.contains("]")) { | ||
| return hostname; | ||
| } | ||
|
|
||
| // Check if this is an IPv6 address (contains multiple colons) | ||
| if (hostname.contains(":") && !hostname.startsWith("[")) { | ||
|
||
| return "[" + hostname + "]"; | ||
| } | ||
|
|
||
| return hostname; | ||
| } | ||
|
|
||
| /** | ||
| * Creates a proper URL string with scheme, hostname, and port, | ||
| * handling IPv6 addresses correctly. | ||
| * | ||
| * @param scheme The URL scheme (http, https, etc.) | ||
| * @param hostname The hostname or IP address | ||
| * @param port The port number | ||
| * @return A properly formatted URL string with IPv6 address handling | ||
| */ | ||
| public String getUrl(String scheme, String hostname, int port) { | ||
| String formattedHostname = formatHostnameForUrl(hostname); | ||
| return String.format("%s://%s:%d", scheme, formattedHostname, port); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a proper URL string with scheme and host (which may include port), | ||
| * handling IPv6 addresses correctly. | ||
| * | ||
| * @param scheme The URL scheme (http, https, etc.) | ||
| * @param hostWithPort The hostname or IP address, possibly including a port | ||
| * @return A properly formatted URL string with IPv6 address handling | ||
| */ | ||
| public String getUrl(String scheme, String hostWithPort) { | ||
| // If host already includes port | ||
| if (hostWithPort.contains(":") && !hostWithPort.endsWith("]")) { | ||
| // Handle IPv6 address with port | ||
| if (hostWithPort.contains("]:")) { | ||
| // Already properly formatted IPv6 with port | ||
| return String.format("%s://%s", scheme, hostWithPort); | ||
| } else if (hostWithPort.contains("[") && hostWithPort.contains("]")) { | ||
| // IPv6 without port, just wrap in scheme | ||
| return String.format("%s://%s", scheme, hostWithPort); | ||
| } else { | ||
| // Might be IPv6 without brackets or IPv4 with port | ||
| int lastColonIndex = hostWithPort.lastIndexOf(':'); | ||
| if (hostWithPort.substring(0, lastColonIndex).contains(":")) { | ||
| // It's an IPv6 address with port, but without brackets | ||
| String host = hostWithPort.substring(0, lastColonIndex); | ||
| String port = hostWithPort.substring(lastColonIndex); | ||
| return String.format("%s://[%s]%s", scheme, host, port); | ||
| } | ||
| // IPv4 with port, no special handling needed | ||
| return String.format("%s://%s", scheme, hostWithPort); | ||
| } | ||
| } | ||
|
|
||
| // just hostname without port | ||
hrishikesh-nalawade marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return String.format("%s://%s", scheme, formatHostnameForUrl(hostWithPort)); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,15 +42,26 @@ ServiceAddress gatewayServiceAddress( | |
| ) throws URISyntaxException { | ||
| if (externalUrl != null) { | ||
| URI uri = new URI(externalUrl); | ||
| String host = uri.getHost(); | ||
| // Handle IPv6 address format | ||
| if (host != null && host.contains(":")) { | ||
| host = "[" + host + "]"; | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if host == null, is it ok to continue? it will probably throw some error in the builder
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for pointing that out, I have corrected the above |
||
| return ServiceAddress.builder() | ||
| .scheme(clientAttlsEnabled ? "http" : uri.getScheme()) | ||
| .hostname(uri.getHost() + ":" + uri.getPort()) | ||
| .hostname(host + ":" + uri.getPort()) | ||
| .build(); | ||
| } | ||
|
|
||
| // Handle IPv6 address format | ||
| String formattedHostname = hostname; | ||
| if (hostname != null && hostname.contains(":") && !hostname.startsWith("[")) { | ||
| formattedHostname = "[" + hostname + "]"; | ||
| } | ||
|
|
||
| return ServiceAddress.builder() | ||
| .scheme(determineScheme(serverAttlsEnabled, clientAttlsEnabled, sslEnabled)) | ||
| .hostname(hostname + ":" + port) | ||
| .hostname(formattedHostname + ":" + port) | ||
| .build(); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,10 +18,10 @@ | |
| import org.springframework.expression.Expression; | ||
| import org.springframework.expression.spel.standard.SpelExpressionParser; | ||
| import org.springframework.expression.spel.support.SimpleEvaluationContext; | ||
| import java.net.URI; | ||
| import java.net.URISyntaxException; | ||
| import org.springframework.util.StringUtils; | ||
| import org.zowe.apiml.product.routing.RoutedService; | ||
|
|
||
| import java.net.URI; | ||
| import java.util.LinkedHashMap; | ||
hrishikesh-nalawade marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import java.util.Map; | ||
|
|
||
|
|
@@ -58,6 +58,31 @@ protected String getHostname(ServiceInstance serviceInstance) { | |
| Map<String, String> metadata = serviceInstance.getMetadata(); | ||
| if (metadata != null) { | ||
| output = metadata.get(SERVICE_EXTERNAL_URL); | ||
|
|
||
| // If we have an external URL, ensure IPv6 addresses are properly formatted | ||
| if (output != null) { | ||
| try { | ||
| URI uri = new URI(output); | ||
| String host = uri.getHost(); | ||
|
|
||
| // Check if the host is an IPv6 address (contains colons) and needs brackets | ||
| if (host != null && host.contains(":") && !host.startsWith("[")) { | ||
| // Rebuild the URI with properly formatted IPv6 address | ||
| URI newUri = new URI( | ||
| uri.getScheme(), | ||
| uri.getUserInfo(), | ||
| "[" + host + "]", | ||
| uri.getPort(), | ||
| uri.getPath(), | ||
| uri.getQuery(), | ||
| uri.getFragment() | ||
| ); | ||
| output = newUri.toString(); | ||
| } | ||
| } catch (URISyntaxException e) { | ||
| // If there's an error parsing the URI, keeping the original URL | ||
hrishikesh-nalawade marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
| if (output == null) { | ||
| output = evalHostname(serviceInstance); | ||
|
|
@@ -83,7 +108,46 @@ protected RouteDefinition buildRouteDefinition(ServiceInstance serviceInstance, | |
| RouteDefinition routeDefinition = new RouteDefinition(); | ||
| routeDefinition.setId(serviceInstance.getInstanceId() + ":" + routeId); | ||
| routeDefinition.setOrder(getOrder()); | ||
| routeDefinition.setUri(URI.create(getHostname(serviceInstance))); | ||
| String hostname = getHostname(serviceInstance); | ||
|
|
||
| // Handle IPv6 formatted URLs properly | ||
| if (hostname.startsWith("lb://")) { | ||
| // If Load balancer URL format found,keeping it intact | ||
hrishikesh-nalawade marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| routeDefinition.setUri(URI.create(hostname)); | ||
| } else { | ||
| // For regular URL ensuring proper formatting for IPv6 addresses | ||
hrishikesh-nalawade marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| try { | ||
| URI uri = new URI(hostname); | ||
| String scheme = uri.getScheme(); | ||
| String host = uri.getHost(); | ||
| int port = uri.getPort(); | ||
| String userInfo = uri.getUserInfo(); | ||
| String path = uri.getPath(); | ||
| String query = uri.getQuery(); | ||
| String fragment = uri.getFragment(); | ||
|
|
||
| // Checking if the host is an IPv6 address and adding brackets if needed | ||
hrishikesh-nalawade marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| String formattedHost = host; | ||
| if (host != null && host.contains(":") && !host.startsWith("[")) { | ||
| formattedHost = "[" + host + "]"; | ||
| } | ||
|
|
||
| // Reconstructing the URI with formatted IPv6 address | ||
hrishikesh-nalawade marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| URI safeUri = new URI( | ||
| scheme, | ||
| userInfo, | ||
| formattedHost, | ||
| port, | ||
| path, | ||
| query, | ||
| fragment | ||
| ); | ||
| routeDefinition.setUri(safeUri); | ||
| } catch (URISyntaxException e) { | ||
| // Fallback to direct creation if URI parsing fails | ||
| routeDefinition.setUri(URI.create(hostname)); | ||
| } | ||
| } | ||
|
|
||
| // add instance metadata | ||
| routeDefinition.setMetadata(new LinkedHashMap<>(serviceInstance.getMetadata())); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.