Skip to content

Commit

Permalink
fix: Fix normalization of baseUrl in ZAAS client (#3123)
Browse files Browse the repository at this point in the history
Signed-off-by: Pavel Jareš <[email protected]>
  • Loading branch information
pj892031 authored Oct 9, 2023
1 parent 38d3b83 commit c8a23a8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
import lombok.Data;
import lombok.experimental.Tolerate;

import java.util.Objects;

@Data
@Builder
public class ConfigProperties {
Expand All @@ -32,7 +30,10 @@ public class ConfigProperties {
private boolean httpOnly;
private boolean nonStrictVerifySslCertificatesOfServices;

private static final String GATEWAY_SERVICE_ID = "gateway";
@SuppressWarnings("squid:S1075")
private static final String OLD_PATH_FORMAT = "/api/v1/gateway";
@SuppressWarnings("squid:S1075")
private static final String NEW_PATH_FORMAT = "/gateway/api/v1";

@Builder.Default
private String tokenPrefix = "apimlAuthenticationToken";
Expand All @@ -58,22 +59,22 @@ public ConfigProperties withoutKeyStore() {
}

public void setApimlBaseUrl(String baseUrl) {
if (baseUrl == null) { // default path
apimlBaseUrl = "/gateway/api/v1/auth";
} else if (baseUrl.contains("/") && baseUrl.contains(GATEWAY_SERVICE_ID)) {
String[] baseUrlParts = baseUrl.split("/");
if (Objects.equals(baseUrlParts[2], GATEWAY_SERVICE_ID)) {
apimlBaseUrl = "/gateway/" + baseUrlParts[0] + "/" + baseUrlParts[1] + "/auth";
} else if (Objects.equals(baseUrlParts[3], GATEWAY_SERVICE_ID)) {
apimlBaseUrl = "/gateway/" + baseUrlParts[1] + "/" + baseUrlParts[2] + "/auth";
} else if (!baseUrl.startsWith("/")) { // starts with gateway/..
apimlBaseUrl = "/" + baseUrl;
} else {
apimlBaseUrl = baseUrl;
}
} else {
apimlBaseUrl = baseUrl;
// set default path if it is missing
if (baseUrl == null) {
baseUrl = "/gateway/api/v1/auth";
}

// if path does not start with / add it
if (!baseUrl.startsWith("/")) {
baseUrl = "/" + baseUrl;
}

// replace old path format with the new one
if (baseUrl.startsWith(OLD_PATH_FORMAT)) {
baseUrl = NEW_PATH_FORMAT + baseUrl.substring(OLD_PATH_FORMAT.length());
}

apimlBaseUrl = baseUrl;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.zowe.apiml.zaasclient.config.ConfigProperties;

Expand All @@ -31,11 +32,26 @@ void givenBaseUrl_thenTransformToOrDontChangeZoweV2BaseUrl(String baseUrl) {
}

@ParameterizedTest
@ValueSource(strings = {"/api/v1/zaasClient/auth", "api.v1.zaasClient.auth"})
void givenBaseUrl_thenDontChangeBaseUrl(String baseUrl) {
@CsvSource({
"/api/v1/zaasClient/auth,/api/v1/zaasClient/auth",
"api/v1/zaasClient/auth,/api/v1/zaasClient/auth",
"anyUrl,/anyUrl",
"anyUrl/,/anyUrl/",
"/api/v1/gateway,/gateway/api/v1",
"api/v1/gateway,/gateway/api/v1",
"/api/v1/gateway/x,/gateway/api/v1/x",
"api/v1/gateway/x,/gateway/api/v1/x",
"/gateway/api/v1,/gateway/api/v1",
"gateway/api/v1,/gateway/api/v1",
"/gateway/api/v1/x,/gateway/api/v1/x",
"gateway/api/v1/x,/gateway/api/v1/x",
"/anyOther/gateway/doNotChange,/anyOther/gateway/doNotChange",
"anyOther/gateway/doNotChange,/anyOther/gateway/doNotChange"
})
void givenBaseUrl_thenNormalizeIt(String input, String normalized) {
ConfigProperties configProperties = new ConfigProperties();
configProperties.setApimlBaseUrl(baseUrl);
assertThat(configProperties.getApimlBaseUrl(), is(baseUrl));
configProperties.setApimlBaseUrl(input);
assertThat(configProperties.getApimlBaseUrl(), is(normalized));
}

@Test
Expand Down

0 comments on commit c8a23a8

Please sign in to comment.