Skip to content

Commit

Permalink
Resolve conflicts, #TASK-4641
Browse files Browse the repository at this point in the history
  • Loading branch information
jtarraga committed Sep 1, 2023
2 parents af38b8b + 7a01c43 commit 13b6dc5
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.opencb.opencga.catalog.managers.StudyManager;
import org.opencb.opencga.core.api.ParamConstants;
import org.opencb.opencga.core.cellbase.CellBaseValidator;
import org.opencb.opencga.core.common.ExceptionUtils;
import org.opencb.opencga.core.common.UriUtils;
import org.opencb.opencga.core.config.storage.CellBaseConfiguration;
import org.opencb.opencga.core.config.storage.SampleIndexConfiguration;
Expand Down Expand Up @@ -564,7 +565,10 @@ public OpenCGAResult<Job> setCellbaseConfiguration(String project, CellBaseConfi
String annotationSaveId, String token)
throws CatalogException, StorageEngineException {
StopWatch stopwatch = StopWatch.createStarted();
return secureOperationByProject("configureCellbase", project, new ObjectMap(), token, engine -> {
return secureOperationByProject("configureCellbase", project, new ObjectMap()
.append("cellbaseConfiguration", cellbaseConfiguration)
.append("annotate", annotate)
.append("annotationSaveId", annotationSaveId), token, engine -> {
OpenCGAResult<Job> result = new OpenCGAResult<>();
result.setResultType(Job.class.getCanonicalName());
result.setResults(new ArrayList<>());
Expand Down Expand Up @@ -1225,7 +1229,7 @@ private <R> R secureTool(String toolId, boolean isOperation, ObjectMap params, S
throw e;
} catch (Exception e) {
exception = e;
throw new StorageEngineException("Error executing operation " + toolId, e);
throw new StorageEngineException("Error executing operation '" + toolId + "' : " + e.getMessage(), e);
} finally {
if (result instanceof DataResult) {
auditAttributes.append("dbTime", ((DataResult) result).getTime());
Expand All @@ -1237,6 +1241,8 @@ private <R> R secureTool(String toolId, boolean isOperation, ObjectMap params, S
if (exception != null) {
auditAttributes.append("errorType", exception.getClass());
auditAttributes.append("errorMessage", exception.getMessage());
auditAttributes.append("errorMessageFull", ExceptionUtils.prettyExceptionMessage(exception, false, true));
auditAttributes.append("exceptionStackTrace", ExceptionUtils.prettyExceptionStackTrace(exception));
status = new AuditRecord.Status(AuditRecord.Status.Result.ERROR,
new Error(-1, exception.getClass().getName(), exception.getMessage()));
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
package org.opencb.opencga.core.cellbase;

import io.jsonwebtoken.JwtException;
import org.apache.commons.lang3.StringUtils;
import org.opencb.biodata.models.variant.avro.VariantAnnotation;
import org.opencb.cellbase.client.rest.CellBaseClient;
import org.opencb.cellbase.core.api.key.ApiKeyManager;
import org.opencb.cellbase.core.config.SpeciesConfiguration;
import org.opencb.cellbase.core.config.SpeciesProperties;
import org.opencb.cellbase.core.models.DataRelease;
import org.opencb.cellbase.core.result.CellBaseDataResponse;
import org.opencb.commons.datastore.core.QueryOptions;
import org.opencb.commons.utils.VersionUtils;
import org.opencb.opencga.core.config.storage.CellBaseConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -45,6 +49,7 @@ private CellBaseClient newCellBaseClient(CellBaseConfiguration cellBaseConfigura
toCellBaseSpeciesName(species),
assembly,
cellBaseConfiguration.getDataRelease(),
cellBaseConfiguration.getApiKey(),
cellBaseConfiguration.toClientConfiguration());
}

Expand Down Expand Up @@ -135,7 +140,7 @@ public void validate() throws IOException {
validate(false);
}

public CellBaseConfiguration validate(boolean autoComplete) throws IOException {
private CellBaseConfiguration validate(boolean autoComplete) throws IOException {
CellBaseConfiguration cellBaseConfiguration = getCellBaseConfiguration();
String inputVersion = getVersion();
CellBaseDataResponse<SpeciesProperties> species;
Expand Down Expand Up @@ -188,6 +193,41 @@ public CellBaseConfiguration validate(boolean autoComplete) throws IOException {
}
}
}
String apiKey = getApiKey();
if (StringUtils.isEmpty(apiKey)) {
cellBaseConfiguration.setApiKey(null);
} else {
// Check it's supported
if (!supportsToken(serverVersion)) {
throw new IllegalArgumentException("API key not supported for cellbase "
+ "url: '" + getURL() + "'"
+ ", version: '" + inputVersion + "'");
}

// Check it's an actual API key
ApiKeyManager apiKeyManager = new ApiKeyManager();
try {
apiKeyManager.decode(apiKey);
} catch (JwtException e) {
throw new IllegalArgumentException("Malformed API key for cellbase "
+ "url: '" + getURL() + "'"
+ ", version: '" + inputVersion
+ "', species: '" + getSpecies()
+ "', assembly: '" + getAssembly() + "'");
}

// Check it's a valid API key
CellBaseDataResponse<VariantAnnotation> response = cellBaseClient.getVariantClient()
.getAnnotationByVariantIds(Collections.singletonList("1:1:N:C"), new QueryOptions(), true);
if (response.firstResult() == null) {
throw new IllegalArgumentException("Invalid API key for cellbase "
+ "url: '" + getURL() + "'"
+ ", version: '" + inputVersion
+ "', species: '" + getSpecies()
+ "', assembly: '" + getAssembly() + "'");
}
}

return cellBaseConfiguration;
}

Expand Down Expand Up @@ -230,6 +270,11 @@ public static boolean supportsDataReleaseActiveByDefaultIn(String serverVersion)
return VersionUtils.isMinVersion("5.5.0", serverVersion, true);
}

public static boolean supportsToken(String serverVersion) {
// Tokens support starts at version 5.4.0
return VersionUtils.isMinVersion("5.4.0", serverVersion);
}

public String getVersionFromServerMajor() throws IOException {
return major(getVersionFromServer());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.opencb.opencga.core.cellbase;

import org.apache.commons.lang3.StringUtils;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
Expand Down Expand Up @@ -70,4 +72,44 @@ public void testNoActiveReleases() throws IOException {
thrown.expectMessage("No active data releases found on cellbase");
CellBaseValidator.validate(new CellBaseConfiguration(ParamConstants.CELLBASE_URL, "v5.1", null, null), "mmusculus", "GRCm38", true);
}

@Test
public void testApiKey() throws IOException {
String apiKey = System.getenv("CELLBASE_HGMD_APIKEY");
Assume.assumeTrue(StringUtils.isNotEmpty(apiKey));
CellBaseConfiguration validated = CellBaseValidator.validate(new CellBaseConfiguration(ParamConstants.CELLBASE_URL, "v5.4", null, apiKey), "hsapiens", "grch38", true);
Assert.assertNotNull(validated.getApiKey());
}

@Test
public void testApiKeyNotSupported() throws IOException {
String apiKey = System.getenv("CELLBASE_HGMD_APIKEY");
Assume.assumeTrue(StringUtils.isNotEmpty(apiKey));
thrown.expectMessage("API key not supported");
CellBaseConfiguration validated = CellBaseValidator.validate(new CellBaseConfiguration(ParamConstants.CELLBASE_URL, "v5.1", null, apiKey), "hsapiens", "grch38", true);
Assert.assertNotNull(validated.getApiKey());
}

@Test
public void testApiKeyEmpty() throws IOException {
String apiKey = "";
CellBaseConfiguration validated = CellBaseValidator.validate(new CellBaseConfiguration(ParamConstants.CELLBASE_URL, "v5.1", null, apiKey), "hsapiens", "grch38", true);
Assert.assertNull(validated.getApiKey());
}

@Test
public void testMalformedApiKey() throws IOException {
thrown.expectMessage("Malformed API key for cellbase");
String apiKey = "MALFORMED_API_KEY";
CellBaseConfiguration validated = CellBaseValidator.validate(new CellBaseConfiguration(ParamConstants.CELLBASE_URL, "v5.4", null, apiKey), "hsapiens", "grch38", true);
Assert.assertNotNull(validated.getApiKey());
}

@Test
public void testUnsignedApiKey() throws IOException {
thrown.expectMessage("Invalid API key for cellbase");
String apiKey = "eyJhbGciOiJIUzI1NiJ9.eyJzb3VyY2VzIjp7ImhnbWQiOjkyMjMzNzIwMzY4NTQ3NzU4MDd9LCJ2ZXJzaW9uIjoiMS4wIiwic3ViIjoiWkVUVEEiLCJpYXQiOjE2OTMyMTY5MDd9.invalidsignature";
CellBaseConfiguration validated = CellBaseValidator.validate(new CellBaseConfiguration(ParamConstants.CELLBASE_URL, "v5.4", null, apiKey), "hsapiens", "grch38", true);
Assert.assertNotNull(validated.getApiKey());
}
}

0 comments on commit 13b6dc5

Please sign in to comment.