Skip to content

Commit

Permalink
Recognize SaaS patterns (#3586)
Browse files Browse the repository at this point in the history
* Recognize SaaS patterns

* Refactor code

Move hostMatchesSaasPattern to getVersionFormattedHostAddress

---------

Co-authored-by: Chetan Gudisagar <chetangudisagar@gmail.com>
  • Loading branch information
PavelZaytsev and chetangudisagar committed Apr 17, 2023
1 parent 630d4ae commit 16175ee
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
31 changes: 28 additions & 3 deletions common/src/main/java/org/corfudb/common/util/URLUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.function.Predicate;
import java.util.regex.Pattern;

/**
* Contains Utility methods to work with URIs or the Socket Addresses.
Expand All @@ -19,10 +21,22 @@ public final class URLUtils {

private static final String COLON_SEPERATOR = ":";

// Matches things like "corfu" or "log-replication-0"
private static final Predicate<String> SAAS_PATTERN1 = Pattern.compile("^[a-z-]+(-\\d+)?$").asPredicate();

// Matches things like "corfu-0.corfu-headless.dev-env-23.svc.cluster.local"
private static final Predicate<String> SAAS_PATTERN2 = Pattern.compile("^[a-z-]+(-\\d+)?.[a-z-]+.[a-z0-9-]+.svc.cluster.local$").asPredicate();

private static final Predicate<String> SAAS_PATTERN = SAAS_PATTERN1.or(SAAS_PATTERN2);

private URLUtils() {
// prevent instantiation of this class
}

public static boolean hostMatchesSaasPattern(String host) {
return SAAS_PATTERN.test(host);
}

/**
* Returns a version-formatted URL that contains the formatted host address along with the port.
*
Expand Down Expand Up @@ -54,12 +68,21 @@ public static String getVersionFormattedEndpointURL(String host, String port) {
* @return a version-formatted endpoint
*/
public static String getVersionFormattedEndpointURL(String address) {
return getVersionFormattedHostAddress(address.substring(0, address.lastIndexOf(':'))) +
address.substring(address.lastIndexOf(COLON_SEPERATOR));
int lastColonIndex = address.lastIndexOf(COLON_SEPERATOR);
// if ':' is present, return a substring
if (lastColonIndex != -1) {
String host = address.substring(0, lastColonIndex);
return getVersionFormattedHostAddress(host) +
address.substring(lastColonIndex);
} else {
log.warn("getVersionFormattedEndpointURL: Could not find colon in the address '{}'.", address);
return address;
}
}

/**
* Returns a version-formatted URL that contains the formatted host address.
* If host matches saas pattern, returns as is.
*
* @param host host address that needs formatting
* @return version-formatted address
Expand All @@ -78,7 +101,9 @@ public static String getVersionFormattedHostAddress(String host) {
String formattedHost = host.trim().split("%")[0];

try {
if (InetAddressUtils.isIPv6Address(InetAddress.getByName(formattedHost).getHostAddress())
if (hostMatchesSaasPattern(formattedHost)) {
return formattedHost;
} else if (InetAddressUtils.isIPv6Address(InetAddress.getByName(formattedHost).getHostAddress())
&& formattedHost.charAt(0)!='[' && formattedHost.charAt(formattedHost.length()-1)!=']') {
formattedHost = '[' + formattedHost + ']';
}
Expand Down
15 changes: 15 additions & 0 deletions common/src/test/java/org/corfudb/common/util/URLUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public class URLUtilsTest {
private static final String IPV6_ENDPOINT_URL_3_SHORT_MALFORMED = "::1:9000";
private static final String IPV6_ENDPOINT_URL_MALFORMED_1 = "0:0:0:0:0:0:0:1:9000";
private static final String IPV6_ENDPOINT_URL_MALFORMED_2 = "0:0:0:0:0:0:0:2:9001";
private static final String SAAS_ENDPOINT_1 = "corfu:9000";
private static final String SAAS_ENDPOINT_2 = "corfu-0:9000";
private static final String SAAS_ENDPOINT_3 = "log-replication-0:9001";
private static final String SAAS_ENDPOINT_4 = "corfu-0.corfu.gmns.svc.cluster.local:9000";
private static final String SAAS_ENDPOINT_5 = "log-replication-0.log-replication.dev-testing-23.svc.cluster.local:9000";
private static final String EMPTY_STRING = "";

/**
Expand Down Expand Up @@ -74,6 +79,16 @@ void testGetVersionFormattedEndpointURL() {
.isEqualTo(IPV6_ENDPOINT_URL_2);
assertThat(URLUtils.getVersionFormattedEndpointURL(IPV6_ENDPOINT_URL_3_SHORT_MALFORMED))
.isEqualTo(IPV6_ENDPOINT_URL_1_SHORT);
assertThat(URLUtils.getVersionFormattedEndpointURL(SAAS_ENDPOINT_1))
.isEqualTo(SAAS_ENDPOINT_1);
assertThat(URLUtils.getVersionFormattedEndpointURL(SAAS_ENDPOINT_2))
.isEqualTo(SAAS_ENDPOINT_2);
assertThat(URLUtils.getVersionFormattedEndpointURL(SAAS_ENDPOINT_3))
.isEqualTo(SAAS_ENDPOINT_3);
assertThat(URLUtils.getVersionFormattedEndpointURL(SAAS_ENDPOINT_4))
.isEqualTo(SAAS_ENDPOINT_4);
assertThat(URLUtils.getVersionFormattedEndpointURL(SAAS_ENDPOINT_5))
.isEqualTo(SAAS_ENDPOINT_5);
}

/**
Expand Down

0 comments on commit 16175ee

Please sign in to comment.