Skip to content

Commit

Permalink
fixed token (#647)
Browse files Browse the repository at this point in the history
Co-authored-by: maheshrajamani <99678631+maheshrajamani@users.noreply.github.com>
Co-authored-by: Kathiresan Selvaraj <kathiresan.selvaraj@datastax.com>
Co-authored-by: Kathiresan Selvaraj <96088452+kathirsvn@users.noreply.github.com>
  • Loading branch information
4 people authored Nov 16, 2023
1 parent de5180d commit c6c8fff
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -139,8 +140,7 @@ interface DatabaseConfig {

/** Fixed Token used for Integration Test authentication */
@Nullable
@WithDefault("not in tests")
String fixedToken();
Optional<String> fixedToken();

/** Cassandra contact points (when type is <code>cassandra</code>) */
@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand All @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
Expand All @@ -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");
Expand All @@ -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");
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, String> getConfigOverrides() {
return ImmutableMap.<String, String>builder()
.put("stargate.jsonapi.operations.database-config.fixed-token", "test-token")
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";

Expand All @@ -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");
Expand Down

0 comments on commit c6c8fff

Please sign in to comment.