Skip to content

Commit

Permalink
Minor fix to error message given for invalid providerKey (#1113)
Browse files Browse the repository at this point in the history
Co-authored-by: Yuqi Du <istimdu@gmail.com>
  • Loading branch information
tatu-at-datastax and Yuqi-Du authored May 23, 2024
1 parent db4b55d commit 36a2b63
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -342,21 +342,23 @@ private void validateAuthentication(
String sharedKeyValue = userAuth.getValue();
if (sharedKeyValue == null || sharedKeyValue.isEmpty()) {
throw ErrorCode.VECTORIZE_INVALID_SHARED_KEY_VALUE_FORMAT.toApiException(
"providerKey value should be formatted as keyName.providerKey");
"missing value");
}
int dotIndex = sharedKeyValue.lastIndexOf('.');
if (dotIndex <= 0 || dotIndex >= sharedKeyValue.length() - 1) {
if (dotIndex <= 0) {
throw ErrorCode.VECTORIZE_INVALID_SHARED_KEY_VALUE_FORMAT.toApiException(
"providerKey value should be formatted as keyName.providerKey");
"providerKey value should be formatted as '[keyName].providerKey'");
}

String providerKeyString = sharedKeyValue.substring(dotIndex + 1);
if (!"providerKey".equals(providerKeyString)) {
throw ErrorCode.VECTORIZE_INVALID_SHARED_KEY_VALUE_FORMAT.toApiException(
"providerKey value should be formatted as keyName.providerKey");
"providerKey value should be formatted as '[keyName].providerKey'");
}
}
if (operationsConfig.enableEmbeddingGateway())
if (operationsConfig.enableEmbeddingGateway()) {
validateCredentials.validate(userConfig.provider(), userAuth.getValue());
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package io.stargate.sgv2.jsonapi.api.v1;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.startsWith;
import static org.hamcrest.Matchers.*;

import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.junit.QuarkusIntegrationTest;
Expand Down Expand Up @@ -1265,6 +1262,50 @@ public void happyValidAuthKey() {

deleteCollection("collection_with_vector_service");
}

@Test
public void failForBadProviderKeyFormat() {
String json =
"""
{
"createCollection": {
"name": "incorrectProviderKeyFormat",
"options": {
"vector": {
"metric": "cosine",
"dimension": 5,
"service": {
"provider": "openai",
"modelName": "text-embedding-ada-002",
"authentication": {
"providerKey" : "shared_creds"
},
"parameters": {
"projectId": "test project"
}
}
}
}
}
}
""";
given()
.headers(getHeaders())
.contentType(ContentType.JSON)
.body(json)
.when()
.post(NamespaceResource.BASE_PATH, namespaceName)
.then()
.statusCode(200)
.body("status", is(nullValue()))
.body("data", is(nullValue()))
.body("errors[0].errorCode", is("VECTORIZE_INVALID_SHARED_KEY_VALUE_FORMAT"))
.body("errors[0].exceptionClass", is("JsonApiException"))
.body(
"errors[0].message",
startsWith(
"Invalid authentication value format: providerKey value should be formatted as '[keyName].providerKey'"));
}
}

@Nested
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,46 +66,6 @@ public void happyPathVectorSearch() {
.statusCode(200)
.body("status.ok", is(1));

json =
"""
{
"createCollection": {
"name": "incorrectProviderKeyFormat",
"options": {
"vector": {
"metric": "cosine",
"dimension": 5,
"service": {
"provider": "custom",
"modelName": "text-embedding-ada-002",
"authentication": {
"providerKey" : "shared_creds"
},
"parameters": {
"projectId": "test project"
}
}
}
}
}
}
""";
given()
.headers(getHeaders())
.contentType(ContentType.JSON)
.body(json)
.when()
.post(NamespaceResource.BASE_PATH, namespaceName)
.then()
.statusCode(200)
.body("errors", is(notNullValue()))
.body("errors", hasSize(1))
.body(
"errors[0].message",
contains("providerKey value should be formatted as keyName.providerKey"))
.body("errors[0].errorCode", is("VECTORIZE_INVALID_SHARED_KEY_VALUE_FORMAT"))
.body("errors[0].exceptionClass", is("JsonApiException"));

json =
"""
{
Expand Down

0 comments on commit 36a2b63

Please sign in to comment.