From 60e1c679966d6bb59627d58d471f81ef03317404 Mon Sep 17 00:00:00 2001 From: Amanda D'Errico Date: Fri, 25 Mar 2022 16:28:42 -0400 Subject: [PATCH 1/2] zaas client supports v2 compatibility with updated path Signed-off-by: Amanda D'Errico --- .../config/DefaultZaasClientConfiguration.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/zaas-client/src/main/java/org/zowe/apiml/zaasclient/config/DefaultZaasClientConfiguration.java b/zaas-client/src/main/java/org/zowe/apiml/zaasclient/config/DefaultZaasClientConfiguration.java index 6f2dbe5268..ecb6d5b52c 100644 --- a/zaas-client/src/main/java/org/zowe/apiml/zaasclient/config/DefaultZaasClientConfiguration.java +++ b/zaas-client/src/main/java/org/zowe/apiml/zaasclient/config/DefaultZaasClientConfiguration.java @@ -52,7 +52,6 @@ public ConfigProperties getConfigProperties() { ConfigProperties configProperties = new ConfigProperties(); configProperties.setApimlHost(host); configProperties.setApimlPort(port); - configProperties.setApimlBaseUrl(baseUrl); configProperties.setKeyStorePath(keyStorePath); configProperties.setKeyStorePassword(keyStorePassword); configProperties.setKeyStoreType(keyStoreType); @@ -60,6 +59,15 @@ public ConfigProperties getConfigProperties() { configProperties.setTrustStorePassword(trustStorePassword); configProperties.setTrustStoreType(trustStoreType); configProperties.setNonStrictVerifySslCertificatesOfServices(nonStrictVerifySslCertificatesOfServices); + + String updatedBaseUrl; + if (!baseUrl.startsWith("/gateway")) { + // rearrange path to match new base url format - {service id}/{type}/{api version} - both paths supported until March 2023 + updatedBaseUrl = "/gateway" + baseUrl.substring(0, baseUrl.indexOf("/gateway")) + "/auth"; + } else { + updatedBaseUrl = baseUrl; + } + configProperties.setApimlBaseUrl(updatedBaseUrl); return configProperties; } From 0fa4f7b121798f17a815beeee8ba38809f4d7bea Mon Sep 17 00:00:00 2001 From: Amanda D'Errico Date: Mon, 28 Mar 2022 16:03:25 -0400 Subject: [PATCH 2/2] addressed code review Signed-off-by: Amanda D'Errico --- .../zaasclient/config/ConfigProperties.java | 28 +++++++++++ .../DefaultZaasClientConfiguration.java | 10 +--- .../internal/ZaasClientApimlBaseUrlTest.java | 47 +++++++++++++++++++ 3 files changed, 76 insertions(+), 9 deletions(-) create mode 100644 zaas-client/src/test/java/org/zowe/apiml/zaasclient/service/internal/ZaasClientApimlBaseUrlTest.java diff --git a/zaas-client/src/main/java/org/zowe/apiml/zaasclient/config/ConfigProperties.java b/zaas-client/src/main/java/org/zowe/apiml/zaasclient/config/ConfigProperties.java index 350b413e12..89010adf51 100644 --- a/zaas-client/src/main/java/org/zowe/apiml/zaasclient/config/ConfigProperties.java +++ b/zaas-client/src/main/java/org/zowe/apiml/zaasclient/config/ConfigProperties.java @@ -13,6 +13,8 @@ import lombok.Data; import lombok.experimental.Tolerate; +import java.util.Objects; + @Data @Builder public class ConfigProperties { @@ -29,6 +31,8 @@ public class ConfigProperties { private boolean httpOnly; private boolean nonStrictVerifySslCertificatesOfServices; + private static final String GATEWAY_SERVICE_ID = "gateway"; + @Tolerate public ConfigProperties() { // no args constructor @@ -47,4 +51,28 @@ public ConfigProperties withoutKeyStore() { .build(); } + 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; + } + } + } diff --git a/zaas-client/src/main/java/org/zowe/apiml/zaasclient/config/DefaultZaasClientConfiguration.java b/zaas-client/src/main/java/org/zowe/apiml/zaasclient/config/DefaultZaasClientConfiguration.java index ecb6d5b52c..6f2dbe5268 100644 --- a/zaas-client/src/main/java/org/zowe/apiml/zaasclient/config/DefaultZaasClientConfiguration.java +++ b/zaas-client/src/main/java/org/zowe/apiml/zaasclient/config/DefaultZaasClientConfiguration.java @@ -52,6 +52,7 @@ public ConfigProperties getConfigProperties() { ConfigProperties configProperties = new ConfigProperties(); configProperties.setApimlHost(host); configProperties.setApimlPort(port); + configProperties.setApimlBaseUrl(baseUrl); configProperties.setKeyStorePath(keyStorePath); configProperties.setKeyStorePassword(keyStorePassword); configProperties.setKeyStoreType(keyStoreType); @@ -59,15 +60,6 @@ public ConfigProperties getConfigProperties() { configProperties.setTrustStorePassword(trustStorePassword); configProperties.setTrustStoreType(trustStoreType); configProperties.setNonStrictVerifySslCertificatesOfServices(nonStrictVerifySslCertificatesOfServices); - - String updatedBaseUrl; - if (!baseUrl.startsWith("/gateway")) { - // rearrange path to match new base url format - {service id}/{type}/{api version} - both paths supported until March 2023 - updatedBaseUrl = "/gateway" + baseUrl.substring(0, baseUrl.indexOf("/gateway")) + "/auth"; - } else { - updatedBaseUrl = baseUrl; - } - configProperties.setApimlBaseUrl(updatedBaseUrl); return configProperties; } diff --git a/zaas-client/src/test/java/org/zowe/apiml/zaasclient/service/internal/ZaasClientApimlBaseUrlTest.java b/zaas-client/src/test/java/org/zowe/apiml/zaasclient/service/internal/ZaasClientApimlBaseUrlTest.java new file mode 100644 index 0000000000..5f695e00c4 --- /dev/null +++ b/zaas-client/src/test/java/org/zowe/apiml/zaasclient/service/internal/ZaasClientApimlBaseUrlTest.java @@ -0,0 +1,47 @@ +/* + * This program and the accompanying materials are made available under the terms of the + * Eclipse Public License v2.0 which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-v20.html + * + * SPDX-License-Identifier: EPL-2.0 + * + * Copyright Contributors to the Zowe Project. + */ + +package org.zowe.apiml.zaasclient.service.internal; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import org.zowe.apiml.zaasclient.config.ConfigProperties; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; + +class ZaasClientApimlBaseUrlTest { + + private static final String ZOWE_V2_BASE_URL = "/gateway/api/v1/auth"; + + @ParameterizedTest + @ValueSource(strings = {"/api/v1/gateway/auth", "api/v1/gateway/auth", "/gateway/api/v1/auth", "gateway/api/v1/auth"}) + void givenBaseUrl_thenTransformToOrDontChangeZoweV2BaseUrl(String baseUrl) { + ConfigProperties configProperties = new ConfigProperties(); + configProperties.setApimlBaseUrl(baseUrl); + assertThat(configProperties.getApimlBaseUrl(), is(ZOWE_V2_BASE_URL)); + } + + @ParameterizedTest + @ValueSource(strings = {"/api/v1/zaasClient/auth", "api.v1.zaasClient.auth"}) + void givenBaseUrl_thenDontChangeBaseUrl(String baseUrl) { + ConfigProperties configProperties = new ConfigProperties(); + configProperties.setApimlBaseUrl(baseUrl); + assertThat(configProperties.getApimlBaseUrl(), is(baseUrl)); + } + + @Test + void givenBaseUrlIsNull_thenTransformToZoweV2BaseUrl() { + ConfigProperties configProperties = new ConfigProperties(); + configProperties.setApimlBaseUrl(null); + assertThat(configProperties.getApimlBaseUrl(), is(ZOWE_V2_BASE_URL)); + } +}