From c6c8fffcad4ddc93241178fb451820a43118967f Mon Sep 17 00:00:00 2001 From: YuqiDu Date: Thu, 16 Nov 2023 03:40:39 -0800 Subject: [PATCH] fixed token (#647) Co-authored-by: maheshrajamani <99678631+maheshrajamani@users.noreply.github.com> Co-authored-by: Kathiresan Selvaraj Co-authored-by: Kathiresan Selvaraj <96088452+kathirsvn@users.noreply.github.com> --- .../sgv2/jsonapi/config/OperationsConfig.java | 4 ++-- .../service/cqldriver/CQLSessionCache.java | 6 ++++-- .../service/cqldriver/CqlSessionCacheTest.java | 6 ++++-- .../cqldriver/FixedTokenOverrideProfile.java | 15 +++++++++++++++ .../service/cqldriver/FixedTokenTests.java | 4 +++- 5 files changed, 28 insertions(+), 7 deletions(-) create mode 100644 src/test/java/io/stargate/sgv2/jsonapi/service/cqldriver/FixedTokenOverrideProfile.java diff --git a/src/main/java/io/stargate/sgv2/jsonapi/config/OperationsConfig.java b/src/main/java/io/stargate/sgv2/jsonapi/config/OperationsConfig.java index edcab068e3..4804559cf6 100644 --- a/src/main/java/io/stargate/sgv2/jsonapi/config/OperationsConfig.java +++ b/src/main/java/io/stargate/sgv2/jsonapi/config/OperationsConfig.java @@ -28,6 +28,7 @@ import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.Positive; import java.util.List; +import java.util.Optional; import javax.annotation.Nullable; /** Configuration for the operation execution. */ @@ -139,8 +140,7 @@ interface DatabaseConfig { /** Fixed Token used for Integration Test authentication */ @Nullable - @WithDefault("not in tests") - String fixedToken(); + Optional fixedToken(); /** Cassandra contact points (when type is cassandra) */ @Nullable diff --git a/src/main/java/io/stargate/sgv2/jsonapi/service/cqldriver/CQLSessionCache.java b/src/main/java/io/stargate/sgv2/jsonapi/service/cqldriver/CQLSessionCache.java index 48b95b072d..0a26ca2aca 100644 --- a/src/main/java/io/stargate/sgv2/jsonapi/service/cqldriver/CQLSessionCache.java +++ b/src/main/java/io/stargate/sgv2/jsonapi/service/cqldriver/CQLSessionCache.java @@ -123,7 +123,7 @@ private CqlSession getNewSession(SessionCacheKey cacheKey) { */ public CqlSession getSession() { String fixedToken; - if (!(fixedToken = getFixedToken()).equals("not in test") + if ((fixedToken = getFixedToken()) != null && !stargateRequestInfo.getCassandraToken().orElseThrow().equals(fixedToken)) { throw new UnauthorizedException("Unauthorized"); } @@ -135,7 +135,9 @@ public CqlSession getSession() { * token from the request will be compared with this to perform authentication. */ private String getFixedToken() { - return operationsConfig.databaseConfig().fixedToken(); + return operationsConfig.databaseConfig().fixedToken().isPresent() + ? operationsConfig.databaseConfig().fixedToken().get() + : null; } /** diff --git a/src/test/java/io/stargate/sgv2/jsonapi/service/cqldriver/CqlSessionCacheTest.java b/src/test/java/io/stargate/sgv2/jsonapi/service/cqldriver/CqlSessionCacheTest.java index f6a4351d5c..d40eb42d7c 100644 --- a/src/test/java/io/stargate/sgv2/jsonapi/service/cqldriver/CqlSessionCacheTest.java +++ b/src/test/java/io/stargate/sgv2/jsonapi/service/cqldriver/CqlSessionCacheTest.java @@ -8,6 +8,7 @@ import com.datastax.oss.driver.internal.core.context.DefaultDriverContext; import io.quarkus.test.InjectMock; import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.TestProfile; import io.stargate.sgv2.api.common.StargateRequestInfo; import io.stargate.sgv2.jsonapi.config.OperationsConfig; import jakarta.inject.Inject; @@ -16,6 +17,7 @@ import org.junit.jupiter.api.Test; @QuarkusTest +@TestProfile(FixedTokenOverrideProfile.class) public class CqlSessionCacheTest { private static final String TENANT_ID_FOR_TEST = "test_tenant"; @@ -31,7 +33,7 @@ public void testOSSCxCQLSessionCacheDefaultTenant() throws NoSuchFieldException, IllegalAccessException { StargateRequestInfo stargateRequestInfo = mock(StargateRequestInfo.class); when(stargateRequestInfo.getCassandraToken()) - .thenReturn(Optional.ofNullable(operationsConfig.databaseConfig().fixedToken())); + .thenReturn(operationsConfig.databaseConfig().fixedToken()); CQLSessionCache cqlSessionCacheForTest = new CQLSessionCache(operationsConfig); Field stargateRequestInfoField = cqlSessionCacheForTest.getClass().getDeclaredField("stargateRequestInfo"); @@ -49,7 +51,7 @@ public void testOSSCxCQLSessionCache() throws NoSuchFieldException, IllegalAcces StargateRequestInfo stargateRequestInfo = mock(StargateRequestInfo.class); when(stargateRequestInfo.getTenantId()).thenReturn(Optional.of(TENANT_ID_FOR_TEST)); when(stargateRequestInfo.getCassandraToken()) - .thenReturn(Optional.ofNullable(operationsConfig.databaseConfig().fixedToken())); + .thenReturn(operationsConfig.databaseConfig().fixedToken()); CQLSessionCache cqlSessionCacheForTest = new CQLSessionCache(operationsConfig); Field stargateRequestInfoField = cqlSessionCacheForTest.getClass().getDeclaredField("stargateRequestInfo"); diff --git a/src/test/java/io/stargate/sgv2/jsonapi/service/cqldriver/FixedTokenOverrideProfile.java b/src/test/java/io/stargate/sgv2/jsonapi/service/cqldriver/FixedTokenOverrideProfile.java new file mode 100644 index 0000000000..9d288cd8c7 --- /dev/null +++ b/src/test/java/io/stargate/sgv2/jsonapi/service/cqldriver/FixedTokenOverrideProfile.java @@ -0,0 +1,15 @@ +package io.stargate.sgv2.jsonapi.service.cqldriver; + +import io.quarkus.test.junit.QuarkusTestProfile; +import java.util.Map; +import org.testcontainers.shaded.com.google.common.collect.ImmutableMap; + +public class FixedTokenOverrideProfile implements QuarkusTestProfile { + + @Override + public Map getConfigOverrides() { + return ImmutableMap.builder() + .put("stargate.jsonapi.operations.database-config.fixed-token", "test-token") + .build(); + } +} diff --git a/src/test/java/io/stargate/sgv2/jsonapi/service/cqldriver/FixedTokenTests.java b/src/test/java/io/stargate/sgv2/jsonapi/service/cqldriver/FixedTokenTests.java index 8d848a4d2c..9e3c7cdbeb 100644 --- a/src/test/java/io/stargate/sgv2/jsonapi/service/cqldriver/FixedTokenTests.java +++ b/src/test/java/io/stargate/sgv2/jsonapi/service/cqldriver/FixedTokenTests.java @@ -9,6 +9,7 @@ import com.datastax.oss.driver.internal.core.context.DefaultDriverContext; import io.quarkus.security.UnauthorizedException; import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.TestProfile; import io.stargate.sgv2.api.common.StargateRequestInfo; import io.stargate.sgv2.jsonapi.config.OperationsConfig; import jakarta.inject.Inject; @@ -17,6 +18,7 @@ import org.junit.jupiter.api.Test; @QuarkusTest +@TestProfile(FixedTokenOverrideProfile.class) public class FixedTokenTests { private static final String TENANT_ID_FOR_TEST = "test_tenant"; @@ -29,7 +31,7 @@ public void testOSSCxCQLSessionCacheWithFixedToken() StargateRequestInfo stargateRequestInfo = mock(StargateRequestInfo.class); when(stargateRequestInfo.getTenantId()).thenReturn(Optional.of(TENANT_ID_FOR_TEST)); when(stargateRequestInfo.getCassandraToken()) - .thenReturn(Optional.ofNullable(operationsConfig.databaseConfig().fixedToken())); + .thenReturn(operationsConfig.databaseConfig().fixedToken()); CQLSessionCache cqlSessionCacheForTest = new CQLSessionCache(operationsConfig); Field stargateRequestInfoField = cqlSessionCacheForTest.getClass().getDeclaredField("stargateRequestInfo");