Skip to content

Commit

Permalink
#26068: Removing cache as storage for access tokens (#26073)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Silva <[email protected]>
  • Loading branch information
victoralfaro-dotcms and dsilvam authored Oct 2, 2023
1 parent 7842da4 commit ed071e0
Show file tree
Hide file tree
Showing 15 changed files with 314 additions and 360 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import com.dotcms.IntegrationTestBase;
import com.dotcms.analytics.app.AnalyticsApp;
import com.dotcms.analytics.cache.AnalyticsCache;
import com.dotcms.analytics.helper.AnalyticsHelper;
import com.dotcms.analytics.model.AccessToken;
import com.dotcms.analytics.model.AccessTokenFetchMode;
Expand All @@ -13,8 +12,8 @@
import com.dotcms.util.IntegrationTestInitService;
import com.dotmarketing.beans.Host;
import com.dotmarketing.business.APILocator;
import com.dotmarketing.business.CacheLocator;
import com.dotmarketing.util.Config;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
Expand All @@ -35,16 +34,22 @@
public class AnalyticsAPIImplTest extends IntegrationTestBase {

private static AnalyticsAPI analyticsAPI;
private static AnalyticsCache analyticsCache;
private Host host;
private AnalyticsApp analyticsApp;

@BeforeClass
public static void beforeClass() throws Exception {
IntegrationTestInitService.getInstance().init();
analyticsAPI = APILocator.getAnalyticsAPI();
analyticsCache = CacheLocator.getAnalyticsCache();
Config.setProperty("ALLOW_ACCESS_TO_PRIVATE_SUBNETS", true);
Config.setProperty(AnalyticsAPI.ANALYTICS_USE_DUMMY_TOKEN_KEY, false);
analyticsAPI = new AnalyticsAPIImpl();
}

@AfterClass
public static void afterClass() {
Config.setProperty(AnalyticsAPI.ANALYTICS_USE_DUMMY_TOKEN_KEY, true);
Config.setProperty("ALLOW_ACCESS_TO_PRIVATE_SUBNETS", false);
}

@Before
Expand All @@ -62,7 +67,9 @@ public void before() throws Exception {
public void test_getAccessToken_fetchWhenNotCached() throws Exception {
AccessToken accessToken = analyticsAPI.getAccessToken(analyticsApp, AccessTokenFetchMode.BACKEND_FALLBACK);
assertNotNull(accessToken);
assertTrue(analyticsCache.getAccessToken(analyticsApp.getAnalyticsProperties().clientId(), null).isPresent());
assertTrue(AccessTokens.get()
.getAccessToken(analyticsApp.getAnalyticsProperties().clientId(), null)
.isPresent());
}

/**
Expand All @@ -71,21 +78,21 @@ public void test_getAccessToken_fetchWhenNotCached() throws Exception {
*/
@Test
public void test_getAccessToken() throws Exception {
analyticsCache.removeAccessToken(
AccessTokens.get().removeAccessToken(
analyticsApp.getAnalyticsProperties().clientId(),
AnalyticsHelper.get().resolveAudience(analyticsApp));

analyticsAPI.getAccessToken(analyticsApp);
AccessToken accessToken = analyticsAPI.getAccessToken(analyticsApp);
analyticsAPI.getCachedAccessToken(analyticsApp);
AccessToken accessToken = analyticsAPI.getCachedAccessToken(analyticsApp);
assertNull(accessToken);

analyticsAPI.getAccessToken(analyticsApp, AccessTokenFetchMode.BACKEND_FALLBACK);
accessToken = analyticsAPI.getAccessToken(analyticsApp);
accessToken = analyticsAPI.getCachedAccessToken(analyticsApp);
assertNotNull(accessToken);

final Instant issueDate = accessToken.issueDate();
analyticsAPI.getAccessToken(analyticsApp, AccessTokenFetchMode.FORCE_RENEW);
accessToken = analyticsAPI.getAccessToken(analyticsApp);
accessToken = analyticsAPI.getCachedAccessToken(analyticsApp);
assertNotEquals(issueDate, accessToken.issueDate());
assertNotNull(accessToken);
}
Expand All @@ -97,8 +104,8 @@ public void test_getAccessToken() throws Exception {
*/
@Test(expected = AnalyticsException.class)
public void test_getAccessToken_fail() throws Exception {
analyticsCache.removeAccessToken(analyticsApp.getAnalyticsProperties().clientId(), null);
new AnalyticsAPIImpl("http://some-host:9999", analyticsCache)
AccessTokens.get().removeAccessToken(analyticsApp.getAnalyticsProperties().clientId(), null);
new AnalyticsAPIImpl("http://some-host:9999")
.getAccessToken(analyticsApp, AccessTokenFetchMode.FORCE_RENEW);
}

Expand All @@ -109,15 +116,15 @@ public void test_getAccessToken_fail() throws Exception {
*/
@Test
public void test_refreshAccessToken() throws AnalyticsException {
analyticsCache.removeAccessToken(
AccessTokens.get().removeAccessToken(
analyticsApp.getAnalyticsProperties().clientId(),
AnalyticsHelper.get().resolveAudience(analyticsApp));

AccessToken accessToken = analyticsAPI.getAccessToken(analyticsApp);
AccessToken accessToken = analyticsAPI.getCachedAccessToken(analyticsApp);
assertNull(accessToken);

analyticsAPI.refreshAccessToken(analyticsApp);
accessToken = analyticsAPI.getAccessToken(analyticsApp);
accessToken = analyticsAPI.getCachedAccessToken(analyticsApp);
assertNotNull(accessToken);
}

Expand All @@ -129,14 +136,14 @@ public void test_refreshAccessToken() throws AnalyticsException {
*/
@Test(expected = AnalyticsException.class)
public void test_refreshAccessToken_fail_wrongIdp() throws AnalyticsException {
analyticsCache.removeAccessToken(
AccessTokens.get().removeAccessToken(
analyticsApp.getAnalyticsProperties().clientId(),
AnalyticsHelper.get().resolveAudience(analyticsApp));

AccessToken accessToken = analyticsAPI.getAccessToken(analyticsApp);
AccessToken accessToken = analyticsAPI.getCachedAccessToken(analyticsApp);
assertNull(accessToken);

new AnalyticsAPIImpl("http://some-host:9999", analyticsCache).refreshAccessToken(analyticsApp);
new AnalyticsAPIImpl("http://some-host:9999").refreshAccessToken(analyticsApp);
}

/**
Expand All @@ -149,11 +156,11 @@ public void test_refreshAccessToken_fail_wrongIdp() throws AnalyticsException {
public void test_refreshAccessToken_fail_wrong_clientId() throws Exception {
analyticsApp = AnalyticsTestUtils.prepareAnalyticsApp(host, "some-client-id");

analyticsCache.removeAccessToken(
AccessTokens.get().removeAccessToken(
analyticsApp.getAnalyticsProperties().clientId(),
AnalyticsHelper.get().resolveAudience(analyticsApp));

AccessToken accessToken = analyticsAPI.getAccessToken(analyticsApp);
AccessToken accessToken = analyticsAPI.getCachedAccessToken(analyticsApp);
assertNull(accessToken);

analyticsAPI.refreshAccessToken(analyticsApp);
Expand All @@ -170,7 +177,7 @@ public void test_resetAccessToken() throws Exception {
assertNotNull(accessToken);

analyticsAPI.resetAccessToken(analyticsApp);
accessToken = analyticsAPI.getAccessToken(analyticsApp);
accessToken = analyticsAPI.getCachedAccessToken(analyticsApp);
assertNull(accessToken);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@
import com.dotmarketing.portlets.folders.model.Folder;
import com.dotmarketing.portlets.htmlpageasset.business.render.PageContextBuilder;
import com.dotmarketing.portlets.htmlpageasset.model.HTMLPageAsset;
import com.dotmarketing.portlets.htmlpageasset.model.IHTMLPage;
import com.dotmarketing.portlets.languagesmanager.model.Language;
import com.dotmarketing.portlets.templates.model.Template;
import com.dotmarketing.util.Logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import com.dotcms.IntegrationTestBase;
import com.dotcms.LicenseTestUtil;
import com.dotcms.analytics.AccessTokens;
import com.dotcms.analytics.AnalyticsAPI;
import com.dotcms.analytics.AnalyticsAPIImpl;
import com.dotcms.analytics.AnalyticsTestUtils;
import com.dotcms.analytics.app.AnalyticsApp;
import com.dotcms.analytics.cache.AnalyticsCache;
import com.dotcms.analytics.helper.AnalyticsHelper;
import com.dotcms.analytics.model.AccessToken;
import com.dotcms.analytics.model.AccessTokenFetchMode;
Expand All @@ -14,7 +15,6 @@
import com.dotcms.util.IntegrationTestInitService;
import com.dotmarketing.beans.Host;
import com.dotmarketing.business.APILocator;
import com.dotmarketing.business.CacheLocator;
import com.dotmarketing.common.db.DotConnect;
import com.dotmarketing.exception.DotDataException;
import com.dotmarketing.init.DotInitScheduler;
Expand All @@ -37,6 +37,7 @@
import static com.dotmarketing.quartz.job.AccessTokenRenewJob.ANALYTICS_ACCESS_TOKEN_RENEW_TRIGGER_GROUP;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

Expand All @@ -49,7 +50,6 @@
public class AccessTokenRenewJobTest extends IntegrationTestBase {

private static AnalyticsAPI analyticsAPI;
private static AnalyticsCache analyticsCache;
private AccessTokenRenewJob accessTokenRenewJob;
private Host host;
private AnalyticsApp analyticsApp;
Expand All @@ -60,7 +60,6 @@ public static void beforeClass() throws Exception {
LicenseTestUtil.getLicense();

analyticsAPI = APILocator.getAnalyticsAPI();
analyticsCache = CacheLocator.getAnalyticsCache();
Config.setProperty("ALLOW_ACCESS_TO_PRIVATE_SUBNETS", true);
Config.setProperty(ANALYTICS_ACCESS_TOKEN_RENEW_JOB_CRON_KEY, "0 0 0 * * ?");

Expand Down Expand Up @@ -99,7 +98,7 @@ public void test_accessTokenRenew_none() throws Exception {
accessTokenRenewJob.execute(getJobContext());
Thread.sleep(2000);

AccessToken accessToken = analyticsAPI.getAccessToken(analyticsApp);
AccessToken accessToken = analyticsAPI.getCachedAccessToken(analyticsApp);
assertNotNull(accessToken);
}

Expand All @@ -118,12 +117,12 @@ public void test_accessTokenRenew_expired() throws Exception {
analyticsApp,
AccessTokenFetchMode.FORCE_RENEW)
.withIssueDate(issueDate);
analyticsCache.putAccessToken(accessToken);
AccessTokens.get().putAccessToken(accessToken);

accessTokenRenewJob.execute(getJobContext());
Thread.sleep(2000);

accessToken = analyticsAPI.getAccessToken(analyticsApp);
accessToken = analyticsAPI.getCachedAccessToken(analyticsApp);
assertNotNull(accessToken);
assertTrue(accessToken.issueDate().isAfter(issueDate));
}
Expand All @@ -143,12 +142,12 @@ public void test_accessTokenRenew_inWindow() throws Exception {
analyticsApp,
AccessTokenFetchMode.FORCE_RENEW)
.withIssueDate(issueDate);
analyticsCache.putAccessToken(accessToken);
AccessTokens.get().putAccessToken(accessToken);

accessTokenRenewJob.execute(getJobContext());
Thread.sleep(2000);

accessToken = analyticsAPI.getAccessToken(analyticsApp);
accessToken = analyticsAPI.getCachedAccessToken(analyticsApp);
assertNotNull(accessToken);
assertTrue(accessToken.issueDate().isAfter(issueDate));
}
Expand All @@ -160,16 +159,22 @@ public void test_accessTokenRenew_inWindow() throws Exception {
*/
@Test
public void test_accessTokenRenew_whenNoop() throws Exception {
Config.setProperty(AnalyticsAPI.ANALYTICS_USE_DUMMY_TOKEN_KEY, "false");
analyticsAPI = new AnalyticsAPIImpl();

final String reason = "some-reason";
final String clientId = analyticsApp.getAnalyticsProperties().clientId();
analyticsCache.putAccessToken(AnalyticsHelper.get().createNoopToken(analyticsApp, reason).withClientId(clientId));
AccessTokens.get().putAccessToken(AnalyticsHelper.get().createNoopToken(analyticsApp, reason).withClientId(clientId));

accessTokenRenewJob.execute(getJobContext());
Thread.sleep(2000);

AccessToken accessToken = analyticsAPI.getAccessToken(analyticsApp);
AccessToken accessToken = analyticsAPI.getCachedAccessToken(analyticsApp);
assertSame(TokenStatus.NOOP, accessToken.status().tokenStatus());
assertEquals(reason, accessToken.status().reason());

Config.setProperty(AnalyticsAPI.ANALYTICS_USE_DUMMY_TOKEN_KEY, "true");
analyticsAPI = APILocator.getAnalyticsAPI();
}

/**
Expand All @@ -179,16 +184,22 @@ public void test_accessTokenRenew_whenNoop() throws Exception {
*/
@Test
public void test_accessTokenRenew_whenBlocked() throws Exception {
Config.setProperty(AnalyticsAPI.ANALYTICS_USE_DUMMY_TOKEN_KEY, "false");
analyticsAPI = new AnalyticsAPIImpl();

final String reason = "some-reason";
final String clientId = analyticsApp.getAnalyticsProperties().clientId();
analyticsCache.putAccessToken(AnalyticsHelper.get().createBlockedToken(analyticsApp, reason).withClientId(clientId));
AccessTokens.get().putAccessToken(AnalyticsHelper.get().createBlockedToken(analyticsApp, reason).withClientId(clientId));

accessTokenRenewJob.execute(getJobContext());
Thread.sleep(2000);

AccessToken accessToken = analyticsAPI.getAccessToken(analyticsApp);
AccessToken accessToken = analyticsAPI.getCachedAccessToken(analyticsApp);
assertSame(TokenStatus.BLOCKED, accessToken.status().tokenStatus());
assertEquals(reason, accessToken.status().reason());

Config.setProperty(AnalyticsAPI.ANALYTICS_USE_DUMMY_TOKEN_KEY, "true");
analyticsAPI = APILocator.getAnalyticsAPI();
}

/**
Expand All @@ -204,7 +215,7 @@ public void test_accessTokenRenew_whenOk() throws Exception {
accessTokenRenewJob.execute(getJobContext());
Thread.sleep(2000);

AccessToken accessToken = analyticsAPI.getAccessToken(analyticsApp);
AccessToken accessToken = analyticsAPI.getCachedAccessToken(analyticsApp);
assertSame(TokenStatus.OK, accessToken.status().tokenStatus());
}

Expand All @@ -215,13 +226,19 @@ public void test_accessTokenRenew_whenOk() throws Exception {
*/
@Test
public void test_accessTokenRenew_fail_wrongClientId() throws Exception {
analyticsApp = AnalyticsTestUtils.prepareAnalyticsApp(host, "some-client-id");
Config.setProperty(AnalyticsAPI.ANALYTICS_USE_DUMMY_TOKEN_KEY, "false");
analyticsAPI = new AnalyticsAPIImpl();

analyticsApp = AnalyticsTestUtils.prepareAnalyticsApp(host, "some-client-id-xxx");

accessTokenRenewJob.execute(getJobContext());
Thread.sleep(2000);

AccessToken accessToken = analyticsAPI.getAccessToken(analyticsApp);
assertSame(TokenStatus.NOOP, accessToken.status().tokenStatus());
AccessToken accessToken = analyticsAPI.getCachedAccessToken(analyticsApp);
assertNull(accessToken);

Config.setProperty(AnalyticsAPI.ANALYTICS_USE_DUMMY_TOKEN_KEY, "true");
analyticsAPI = APILocator.getAnalyticsAPI();
}

private static void unscheduleJob() throws SchedulerException {
Expand Down
Loading

0 comments on commit ed071e0

Please sign in to comment.