Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor fix to error message given for invalid providerKey #1113

Merged
merged 4 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just change the provider to 'openai'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, let's wait for CI to pass!

"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