From 2b634813de41c73f4373c736f91fcacaf2e2e0fb Mon Sep 17 00:00:00 2001 From: tscan89 Date: Thu, 16 Mar 2023 23:18:27 -0400 Subject: [PATCH 1/6] update constants for test server string --- .../java/com/salesforce/cdp/queryservice/util/Constants.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/salesforce/cdp/queryservice/util/Constants.java b/src/main/java/com/salesforce/cdp/queryservice/util/Constants.java index 59d2e5e..d574206 100644 --- a/src/main/java/com/salesforce/cdp/queryservice/util/Constants.java +++ b/src/main/java/com/salesforce/cdp/queryservice/util/Constants.java @@ -37,8 +37,8 @@ public class Constants { // Common server information public static final String PROD_SERVER_URL = ".salesforce.com"; - public static final String NA45_SERVER_URL = "na45.test1.pc-rnd.salesforce.com"; - public static final String NA46_SERVER_URL = "na46.test1.pc-rnd.salesforce.com"; + public static final String TEST_SERVER_URL = "test1.pc-rnd.salesforce.com"; + //Audience constants for different environments public static final String PROD_SERVER_AUD = "login.salesforce.com"; From aed3c75a8244f5eb80210075540b70d7a85f0872 Mon Sep 17 00:00:00 2001 From: tscan89 Date: Thu, 16 Mar 2023 23:20:11 -0400 Subject: [PATCH 2/6] update tokenhelper test server string match --- .../java/com/salesforce/cdp/queryservice/util/TokenHelper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/salesforce/cdp/queryservice/util/TokenHelper.java b/src/main/java/com/salesforce/cdp/queryservice/util/TokenHelper.java index f23a860..0afc5b6 100644 --- a/src/main/java/com/salesforce/cdp/queryservice/util/TokenHelper.java +++ b/src/main/java/com/salesforce/cdp/queryservice/util/TokenHelper.java @@ -190,7 +190,7 @@ private static Token retrieveTokenWithJWTBearerGrant(Properties properties, OkHt private static String getAudienceForJWTAssertion(String serviceRootUrl) throws SQLException { String serverUrl = serviceRootUrl.toLowerCase(); - if (serverUrl.endsWith(Constants.NA45_SERVER_URL) || serverUrl.endsWith(Constants.NA46_SERVER_URL)) { + if (serverUrl.contains(Constants.TEST_SERVER_URL)) { return Constants.DEV_TEST_SERVER_AUD; } else if (serverUrl.endsWith(Constants.PROD_SERVER_URL)) { return Constants.PROD_SERVER_AUD; From 7eb01dd49e90fe9d9aafef2a86d8a44b998f3499 Mon Sep 17 00:00:00 2001 From: Manoj Inukolunu Date: Thu, 30 Mar 2023 12:52:23 +0530 Subject: [PATCH 3/6] Refresh token when core token is invalid but refresh token is valid --- .../cdp/queryservice/util/TokenHelper.java | 16 +++++++++++++++- .../cdp/queryservice/util/TokenHelperTest.java | 18 +++++++++--------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/salesforce/cdp/queryservice/util/TokenHelper.java b/src/main/java/com/salesforce/cdp/queryservice/util/TokenHelper.java index f23a860..96dfc8d 100644 --- a/src/main/java/com/salesforce/cdp/queryservice/util/TokenHelper.java +++ b/src/main/java/com/salesforce/cdp/queryservice/util/TokenHelper.java @@ -87,7 +87,10 @@ public static Token getToken(Properties properties, OkHttpClient client) throws && properties.containsKey(Constants.PRIVATE_KEY)) { return retrieveTokenWithJWTBearerGrant(properties, client); } - Token newToken = exchangeToken(properties.getProperty(Constants.LOGIN_URL), properties.getProperty(Constants.CORETOKEN), properties.getProperty(Constants.DATASPACE),client); + Token newToken = exchangeTokenWithRefresh(properties.getProperty(Constants.LOGIN_URL), + properties.getProperty(Constants.REFRESHTOKEN),properties.getProperty(Constants.CORETOKEN) + , properties.getProperty(Constants.CLIENT_ID),properties.getProperty(Constants.CLIENT_SECRET),properties.getProperty(Constants.DATASPACE), + client); tokenCache.put(properties.getProperty(Constants.CORETOKEN), newToken); return newToken; } @@ -236,6 +239,17 @@ public static CoreTokenRenewResponse getCoreToken(String url, String refreshToke return coreTokenRenewResponse; } + private static Token exchangeTokenWithRefresh(String url, String refreshToken, + String coreToken, String clientId, String secret, + String dataspace,OkHttpClient client) throws TokenException { + try{ + return exchangeToken(url,coreToken,dataspace,client); + } catch (TokenException e) { + log.error("Core token is not valid fetching a new token "); + return renewToken(url,refreshToken,clientId,secret,dataspace,client); + } + } + private static Token exchangeToken(String url, String coreToken,String dataspace, OkHttpClient client) throws TokenException { String token_url = url + Constants.TOKEN_EXCHANGE_URL; Map requestBody = new HashMap<>(); diff --git a/src/test/java/com/salesforce/cdp/queryservice/util/TokenHelperTest.java b/src/test/java/com/salesforce/cdp/queryservice/util/TokenHelperTest.java index 5fd20e3..e94cb76 100644 --- a/src/test/java/com/salesforce/cdp/queryservice/util/TokenHelperTest.java +++ b/src/test/java/com/salesforce/cdp/queryservice/util/TokenHelperTest.java @@ -24,15 +24,12 @@ import org.apache.http.HttpStatus; import org.junit.Assert; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; import org.mockito.Mock; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.catchThrowable; import static org.assertj.core.api.Assertions.catchThrowableOfType; import static org.mockito.Mockito.*; import org.powermock.core.classloader.annotations.PowerMockIgnore; @@ -151,7 +148,6 @@ public void testTokenExchangeWithException() throws Exception { TokenHelper.getToken(properties, client); }, TokenException.class); assertThat(ex.getCause()).isInstanceOf(IOException.class); - assertThat(ex.getCause().getMessage()).contains("expired authorization code"); } @Test @@ -188,8 +184,12 @@ public void testInvalidateCoreToken() throws Exception { request(buildRequest()).protocol(Protocol.HTTP_1_1). message("Internal Server Error"). body(ResponseBody.create(errorString, MediaType.parse("application/json"))).build(); - when(remoteCall.execute()).thenReturn(errorResponse); - when(client.newCall(any())).thenReturn(remoteCall); + Response refreshResponse = new Response.Builder().code(HttpStatus.SC_OK). + request(buildRequest()).protocol(Protocol.HTTP_1_1). + message("Internal Server Error"). + body(ResponseBody.create(errorString, MediaType.parse("application/json"))).build(); + when(remoteCall.execute()).thenReturn(errorResponse).thenReturn(refreshResponse); + when(client.newCall(any())).thenReturn(remoteCall).thenReturn(remoteCall); ArgumentCaptor eventCaptor = ArgumentCaptor.forClass(Request.class); @@ -197,12 +197,12 @@ public void testInvalidateCoreToken() throws Exception { TokenHelper.getToken(properties, client); }, TokenException.class); assertThat(ex.getCause()).isInstanceOf(JsonParseException.class); - assertThat(ex.getMessage()).contains("Token exchange failed. Please login again"); + assertThat(ex.getMessage()).contains("Failed to Renew Token. Please retry"); - verify(client, times(2)).newCall(eventCaptor.capture()); + verify(client, times(3)).newCall(eventCaptor.capture()); Request request = eventCaptor.getValue(); String url = request.url().toString(); - Assert.assertTrue(url.contains(Constants.TOKEN_REVOKE_URL)); + Assert.assertTrue(url.contains(Constants.CORE_TOKEN_URL)); } private Request buildRequest() { From e7e93dd168e18587ff14a46d4e262989717090f4 Mon Sep 17 00:00:00 2001 From: Manoj Inukolunu Date: Fri, 31 Mar 2023 10:34:14 +0530 Subject: [PATCH 4/6] Add exception to the log --- .../java/com/salesforce/cdp/queryservice/util/TokenHelper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/salesforce/cdp/queryservice/util/TokenHelper.java b/src/main/java/com/salesforce/cdp/queryservice/util/TokenHelper.java index 96dfc8d..f27cd47 100644 --- a/src/main/java/com/salesforce/cdp/queryservice/util/TokenHelper.java +++ b/src/main/java/com/salesforce/cdp/queryservice/util/TokenHelper.java @@ -245,7 +245,7 @@ private static Token exchangeTokenWithRefresh(String url, String refreshToken, try{ return exchangeToken(url,coreToken,dataspace,client); } catch (TokenException e) { - log.error("Core token is not valid fetching a new token "); + log.error("Core token is not valid fetching a new token ", e); return renewToken(url,refreshToken,clientId,secret,dataspace,client); } } From 08453779c86d34448ff2b33db326f0fcc0920636 Mon Sep 17 00:00:00 2001 From: tscan89 Date: Thu, 6 Apr 2023 16:29:07 -0400 Subject: [PATCH 5/6] Update pom.xml Filter slf4j from shaded/uber jar --- pom.xml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pom.xml b/pom.xml index 9b27fb7..85fa13e 100644 --- a/pom.xml +++ b/pom.xml @@ -280,6 +280,13 @@ codegen/** + + org.slf4j:slf4j-simple + + + org/slf4j/** + + Date: Tue, 18 Apr 2023 11:02:37 +0530 Subject: [PATCH 6/6] minor releasing --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 85fa13e..8eb621c 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.queryService Salesforce-CDP-jdbc - 1.19.0 + 1.19.1 UTF-8