Skip to content

Commit

Permalink
Fix DSE Integration, Username/Password credential based token (#1030)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuqi-Du authored Apr 16, 2024
1 parent b56fec3 commit 28b6213
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ public final class ThrowableToErrorMapper {
if (throwable instanceof UnauthorizedException) {
return ErrorCode.UNAUTHENTICATED_REQUEST
.toApiException()
.getCommandResultError(
ErrorCode.UNAUTHENTICATED_REQUEST.getMessage(), Response.Status.UNAUTHORIZED);
.getCommandResultError(message, Response.Status.UNAUTHORIZED);
}

// handle all driver exceptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.stargate.sgv2.jsonapi.JsonApiStartUp;
import io.stargate.sgv2.jsonapi.api.request.DataApiRequestInfo;
import io.stargate.sgv2.jsonapi.config.OperationsConfig;
import io.stargate.sgv2.jsonapi.exception.ErrorCode;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import java.net.InetSocketAddress;
Expand Down Expand Up @@ -135,7 +136,7 @@ private CqlSession getNewSession(SessionCacheKey cacheKey) {
builder.withAuthCredentials(
Objects.requireNonNull(upc.userName()), Objects.requireNonNull(upc.password()));
} else {
throw new RuntimeException(
throw new UnauthorizedException(
"Invalid credentials format, expected `Cassandra:Base64(username):Base64(password)`");
}
} else {
Expand Down Expand Up @@ -166,7 +167,7 @@ public CqlSession getSession() {
String fixedToken;
if ((fixedToken = getFixedToken()) != null
&& !dataApiRequestInfo.getCassandraToken().orElseThrow().equals(fixedToken)) {
throw new UnauthorizedException("Unauthorized");
throw new UnauthorizedException(ErrorCode.UNAUTHENTICATED_REQUEST.getMessage());
}
return sessionCache.get(getSessionCacheKey());
}
Expand Down Expand Up @@ -239,15 +240,15 @@ private record UsernamePasswordCredentials(String userName, String password)
public static UsernamePasswordCredentials from(String encodedCredentials) {
String[] parts = encodedCredentials.split(":");
if (parts.length != 3) {
throw new RuntimeException(
throw new UnauthorizedException(
"Invalid credentials format, expected `Cassandra:Base64(username):Base64(password)`");
}
try {
String userName = new String(Base64.getDecoder().decode(parts[1]));
String password = new String(Base64.getDecoder().decode(parts[2]));
return new UsernamePasswordCredentials(userName, password);
} catch (Exception e) {
throw new RuntimeException(
throw new UnauthorizedException(
"Invalid credentials format, expected `Cassandra:Base64(username):Base64(password)`");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@ public void testOSSCxCQLSessionCacheWithInvalidFixedToken()
operationsConfigField.set(cqlSessionCacheForTest, operationsConfig);
// Throwable
Throwable t = catchThrowable(cqlSessionCacheForTest::getSession);
assertThat(t).isNotNull().isInstanceOf(UnauthorizedException.class).hasMessage("Unauthorized");
assertThat(t)
.isNotNull()
.isInstanceOf(UnauthorizedException.class)
.hasMessage("UNAUTHENTICATED: Invalid token");
// metrics test
Gauge cacheSizeMetric =
meterRegistry.find("cache.size").tag("cache", "cql_sessions_cache").gauge();
Expand Down

0 comments on commit 28b6213

Please sign in to comment.