Skip to content

Commit

Permalink
catalog: create Organization db adaptor, #TASK-4389
Browse files Browse the repository at this point in the history
  • Loading branch information
pfurio committed Aug 10, 2023
1 parent b76f2dc commit a19c61e
Show file tree
Hide file tree
Showing 40 changed files with 1,414 additions and 192 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@
import org.opencb.opencga.core.models.alignment.AlignmentGeneCoverageStatsParams;
import org.opencb.opencga.core.models.file.File;
import org.opencb.opencga.core.models.file.FileLinkParams;
import org.opencb.opencga.core.models.user.Account;
import org.opencb.opencga.core.models.organizations.OrganizationCreateParams;
import org.opencb.opencga.core.models.organizations.OrganizationUpdateParams;
import org.opencb.opencga.core.models.user.User;
import org.opencb.opencga.core.testclassification.duration.MediumTests;
import org.opencb.opencga.storage.core.StorageEngineFactory;
import org.opencb.opencga.storage.core.variant.VariantStorageEngine;
Expand Down Expand Up @@ -218,8 +220,11 @@ public static void afterClass() {
opencga.after();
}

public void setUpCatalogManager() throws IOException, CatalogException {
catalogManager.getUserManager().create(USER, "User Name", "[email protected]", PASSWORD, "", null, Account.AccountType.FULL, opencga.getAdminToken());
public void setUpCatalogManager() throws CatalogException {
catalogManager.getOrganizationManager().create(new OrganizationCreateParams().setId("test"), null, opencga.getAdminToken());
catalogManager.getUserManager().create("test", new User().setId(USER).setName("User Name").setEmail("[email protected]"), PASSWORD, opencga.getAdminToken());
catalogManager.getOrganizationManager().update("test", new OrganizationUpdateParams().setOwner(USER), null, opencga.getAdminToken());

token = catalogManager.getUserManager().login("user", PASSWORD).getToken();

String projectId = catalogManager.getProjectManager().create(PROJECT, "Project about some genomes", "", "Homo sapiens",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,9 +383,6 @@ public class InstallCatalogCommandOptions extends CatalogDatabaseCommandOptions
@Parameter(names = {"--email"}, description = "Administrator e-mail", arity = 1)
public String email;

@Parameter(names = {"--organization"}, description = "Administrator organization", arity = 1)
public String organization;

@Parameter(names = {"--secret-key"}, description = "Secret key needed to authenticate through OpenCGA (JWT).")
public String secretKey;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ private void install() throws CatalogException {

try (CatalogManager catalogManager = new CatalogManager(configuration)) {
catalogManager.installCatalogDB(configuration.getAdmin().getSecretKey(), commandOptions.commonOptions.adminPassword,
commandOptions.email, commandOptions.organization, commandOptions.force);
commandOptions.email, commandOptions.force);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.opencb.opencga.catalog.exceptions.CatalogAuthenticationException;
import org.opencb.opencga.catalog.exceptions.CatalogException;
import org.opencb.opencga.core.api.ParamConstants;
import org.opencb.opencga.core.models.JwtPayload;
import org.opencb.opencga.core.models.user.AuthenticationResponse;
import org.opencb.opencga.core.models.user.User;
import org.opencb.opencga.core.response.OpenCGAResult;
Expand Down Expand Up @@ -79,6 +80,14 @@ Key converStringToKeyObject(String keyString, String jcaAlgorithm) {
*/
public abstract AuthenticationResponse refreshToken(String refreshToken) throws CatalogAuthenticationException;

public JwtPayload getPayload(String token) throws CatalogAuthenticationException {
if (StringUtils.isEmpty(token) || "null".equalsIgnoreCase(token)) {
return new JwtPayload().setUserId(ParamConstants.ANONYMOUS_USER_ID);
}

return jwtManager.getPayload(token);
}

/**
* Obtains the userId corresponding to the token.
*
Expand Down Expand Up @@ -130,6 +139,16 @@ public String getUserId(String token) throws CatalogAuthenticationException {
*/
public abstract void newPassword(String userId, String newPassword) throws CatalogException;

/**
* Create a token for the user with default expiration time.
*
* @param user user.
* @return A token.
*/
public String createToken(User user) {
return createToken(user, Collections.emptyMap(), expiration);
}

/**
* Create a token for the user with default expiration time.
*
Expand Down Expand Up @@ -172,6 +191,16 @@ public String createToken(String userId, Map<String, Object> claims) {
*/
public abstract String createToken(String userId, Map<String, Object> claims, long expiration);

/**
* Create a token for the user.
*
* @param user user.
* @param claims claims.
* @param expiration Expiration time in seconds.
* @return A token.
*/
public abstract String createToken(User user, Map<String, Object> claims, long expiration);

/**
* Create a token for the user with no expiration time.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,12 @@ public String createToken(String userId, Map<String, Object> claims, long expira
throw new UnsupportedOperationException("Tokens are generated by Azure via authorization code or user-password");
}

@Override
public String createToken(User user, Map<String, Object> claims, long expiration) {
// Tokens are generated by Azure via authorization code or user-password
throw new UnsupportedOperationException("Tokens are generated by Azure via authorization code or user-password");
}

@Override
public String createNonExpiringToken(String userId, Map<String, Object> claims) {
// Tokens are generated by Azure via authorization code or user-password
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ public String createToken(String userId, Map<String, Object> claims, long expira
return jwtManager.createJWTToken(userId, claims, expiration);
}

@Override
public String createToken(User user, Map<String, Object> claims, long expiration) {
return jwtManager.createJWTToken(user, claims, expiration);
}

@Override
public String createNonExpiringToken(String userId, Map<String, Object> claims) {
return jwtManager.createJWTToken(userId, claims, 0L);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import io.jsonwebtoken.*;
import org.opencb.opencga.catalog.exceptions.CatalogAuthenticationException;
import org.opencb.opencga.core.models.JwtPayload;
import org.opencb.opencga.core.models.user.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -87,6 +89,27 @@ public String createJWTToken(String userId, long expiration) {
return createJWTToken(userId, Collections.emptyMap(), expiration);
}

public String createJWTToken(User user, Map<String, Object> claims, long expiration) {
long currentTime = System.currentTimeMillis();

JwtBuilder jwtBuilder = Jwts.builder();
if (claims != null && !claims.isEmpty()) {
jwtBuilder.setClaims(claims);
}
jwtBuilder.setSubject(user.getId())
.setIssuer(user.getOrganization())
.setAudience("OpenCGA")
.setIssuedAt(new Date(currentTime))
.signWith(privateKey, algorithm);

// Set the expiration in number of seconds only if 'expiration' is greater than 0
if (expiration > 0) {
jwtBuilder.setExpiration(new Date(currentTime + expiration * 1000L));
}

return jwtBuilder.compact();
}

public String createJWTToken(String userId, Map<String, Object> claims, long expiration) {
long currentTime = System.currentTimeMillis();

Expand Down Expand Up @@ -115,6 +138,11 @@ public void validateToken(String token, Key publicKey) throws CatalogAuthenticat
parseClaims(token, publicKey);
}

public JwtPayload getPayload(String token) throws CatalogAuthenticationException {
Claims body = parseClaims(token, publicKey).getBody();
return new JwtPayload(body.getSubject(), body.getIssuer(), body.getAudience(), body.getIssuedAt(), body.getExpiration());
}

public String getAudience(String token) throws CatalogAuthenticationException {
return getAudience(token, this.publicKey);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,11 @@ public String createToken(String userId, Map<String, Object> claims, long expira
return jwtManager.createJWTToken(userId, claims, expiration);
}

@Override
public String createToken(User user, Map<String, Object> claims, long expiration) {
return jwtManager.createJWTToken(user, claims, expiration);
}

@Override
public String createNonExpiringToken(String userId, Map<String, Object> claims) {
return jwtManager.createJWTToken(userId, claims, 0L);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@

package org.opencb.opencga.catalog.db;

import org.apache.commons.lang3.StringUtils;
import org.opencb.commons.datastore.core.DataResult;
import org.opencb.commons.datastore.core.Event;
import org.opencb.commons.datastore.core.ObjectMap;
import org.opencb.opencga.catalog.db.api.DBIterator;
import org.opencb.opencga.catalog.exceptions.CatalogDBException;
import org.opencb.opencga.catalog.exceptions.CatalogParameterException;
import org.opencb.opencga.core.response.OpenCGAResult;
import org.slf4j.Logger;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.*;

public abstract class AbstractDBAdaptor {

Expand Down Expand Up @@ -101,6 +101,21 @@ protected void checkParameter(Object param, String name) throws CatalogDBExcepti
}
}

protected void checkUpdatedParams(ObjectMap parameters, List<String> updateableKeys) throws CatalogParameterException {
Set<String> keysToUpdate = parameters.keySet();
Set<String> updateableKeysSet = new HashSet<>(updateableKeys);

List<String> unexpectedKeys = new ArrayList<>(keysToUpdate.size());
for (String key : keysToUpdate) {
if (!updateableKeysSet.contains(key)) {
unexpectedKeys.add(key);
}
}
if (!unexpectedKeys.isEmpty()) {
throw new CatalogParameterException("Unexpected fields passed to update: " + StringUtils.join(unexpectedKeys, ", "));
}
}

public interface FilterOption {
String getKey();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public interface DBAdaptorFactory extends AutoCloseable {

MetaDBAdaptor getCatalogMetaDBAdaptor();

OrganizationDBAdaptor getCatalogOrganizationDBAdaptor();

UserDBAdaptor getCatalogUserDBAdaptor();

ProjectDBAdaptor getCatalogProjectDbAdaptor();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package org.opencb.opencga.catalog.db.api;

import org.opencb.commons.datastore.core.ObjectMap;
import org.opencb.commons.datastore.core.Query;
import org.opencb.commons.datastore.core.QueryOptions;
import org.opencb.commons.datastore.core.QueryParam;
import org.opencb.opencga.catalog.exceptions.CatalogAuthorizationException;
import org.opencb.opencga.catalog.exceptions.CatalogDBException;
import org.opencb.opencga.catalog.exceptions.CatalogParameterException;
import org.opencb.opencga.core.models.organizations.Organization;
import org.opencb.opencga.core.response.OpenCGAResult;

import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -85,12 +89,14 @@ public static QueryParams getParam(String key) {
//
// OpenCGAResult nativeInsert(Map<String, Object> project, String userId) throws CatalogDBException;
//
// OpenCGAResult insert(Project project, String userId, QueryOptions options)
// throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException;
OpenCGAResult<Organization> insert(Organization organization, QueryOptions options)
throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException;
//
// OpenCGAResult<Project> get(String userId, QueryOptions options) throws CatalogDBException;
//
// OpenCGAResult<Project> get(long project, QueryOptions options) throws CatalogDBException;
OpenCGAResult<Organization> get(long organization, QueryOptions options) throws CatalogDBException;

OpenCGAResult<Organization> get(Query query, QueryOptions options) throws CatalogDBException;
//
// OpenCGAResult incrementCurrentRelease(long projectId) throws CatalogDBException;
//
Expand Down Expand Up @@ -149,12 +155,12 @@ public static QueryParams getParam(String key) {
// return queryResults;
// }
//
// OpenCGAResult<Project> update(long id, ObjectMap parameters, QueryOptions queryOptions)
// throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException;
OpenCGAResult<Organization> update(long id, ObjectMap parameters, QueryOptions queryOptions)
throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException;
//
// OpenCGAResult<Long> update(Query query, ObjectMap parameters, QueryOptions queryOptions) throws CatalogDBException;
//
// OpenCGAResult delete(Project project) throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException;
OpenCGAResult<Organization> delete(Organization organization) throws CatalogDBException;
//
// OpenCGAResult delete(Query query) throws CatalogDBException;
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ default void checkId(long projectId) throws CatalogDBException {

OpenCGAResult nativeInsert(Map<String, Object> project, String userId) throws CatalogDBException;

OpenCGAResult insert(Project project, String userId, QueryOptions options)
OpenCGAResult<Project> insert(String organizationId, Project project, QueryOptions options)
throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException;

OpenCGAResult<Project> get(String userId, QueryOptions options) throws CatalogDBException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.opencb.commons.datastore.mongodb.MongoDataStoreManager;
import org.opencb.opencga.catalog.db.DBAdaptorFactory;
import org.opencb.opencga.catalog.db.api.MigrationDBAdaptor;
import org.opencb.opencga.catalog.db.api.OrganizationDBAdaptor;
import org.opencb.opencga.catalog.exceptions.CatalogDBException;
import org.opencb.opencga.catalog.exceptions.CatalogException;
import org.opencb.opencga.core.config.Admin;
Expand Down Expand Up @@ -271,7 +272,8 @@ public MetaMongoDBAdaptor getCatalogMetaDBAdaptor() {
return metaDBAdaptor;
}

public OrganizationMongoDBAdaptor getOrganizationDBAdaptor() {
@Override
public OrganizationDBAdaptor getCatalogOrganizationDBAdaptor() {
return organizationDBAdaptor;
}

Expand Down
Loading

0 comments on commit a19c61e

Please sign in to comment.