String getSecretKey() { if (StringUtils.isEmpty(secretKey)) { secretKey = configuration.getAdmin().getSecretKey(); } return secretKey; }
private void initializeAdmin() throws CatalogDBException { if (StringUtils.isEmpty(this.configuration.getAdmin().getSecretKey())) { this.configuration.getAdmin().setSecretKey(this.catalogDBAdaptorFactory.getCatalogMetaDBAdaptor().readSecretKey()); } this.configuration.getAdmin().setAlgorithm("HS256"); // if (StringUtils.isEmpty(this.configuration.getAdmin().getAlgorithm())) { // this.configuration.getAdmin().setAlgorithm(this.catalogDBAdaptorFactory.getCatalogMetaDBAdaptor().readAlgorithm()); // } }
JwtManager(Configuration configuration) { this.configuration = configuration; this.secretKey = this.configuration.getAdmin().getSecretKey(); this.expiration = this.configuration.getAuthentication().getExpiration(); logger = LoggerFactory.getLogger(JwtManager.class); }
checkSessionId(userId, sessionId); } else { if (configuration.getAdmin().getPassword() == null || configuration.getAdmin().getPassword().isEmpty()) { throw new CatalogException("Nor the administrator password nor the session id could be found. The user could not be " + "deleted."); .authenticate("admin", configuration.getAdmin().getPassword(), true);
public void deleteCatalogDB(boolean force) throws CatalogException, URISyntaxException { if (!force) { userManager.validatePassword("admin", configuration.getAdmin().getPassword(), true); } catalogDBAdaptorFactory.deleteCatalogDB(); clearCatalog(); }
String createJWTToken(String userId, long expiration) { String jwt = null; try { long currentTime = System.currentTimeMillis(); JwtBuilder jwtBuilder = Jwts.builder() .setSubject(userId) .setAudience("OpenCGA users") .setIssuedAt(new Date(currentTime)) .signWith(SignatureAlgorithm.forName(configuration.getAdmin().getAlgorithm()), getSecretKey().getBytes("UTF-8")); // Set the expiration in number of seconds only if 'expiration' is greater than 0 if (expiration > 0) { jwtBuilder.setExpiration(new Date(currentTime + expiration * 1000L)); } jwt = jwtBuilder.compact(); } catch (UnsupportedEncodingException e) { logger.error("error while creating jwt token"); } return jwt; }
@Before public void setUp() throws Exception { Configuration configuration = Configuration.load(getClass().getResource("/configuration-test.yml").openStream()); configuration.getAdmin().setSecretKey("12345"); configuration.getAdmin().setAlgorithm("HS256"); jwtSessionManager = new JwtManager(configuration); testCreateJWTToken(); }
public QueryResult<User> update(String userId, ObjectMap parameters, QueryOptions options, String sessionId) throws CatalogException { ParamUtils.checkParameter(userId, "userId"); ParamUtils.checkObj(parameters, "parameters"); if (sessionId != null && !sessionId.isEmpty()) { ParamUtils.checkParameter(sessionId, "sessionId"); checkSessionId(userId, sessionId); for (String s : parameters.keySet()) { if (!s.matches("name|email|organization|attributes")) { throw new CatalogDBException("Parameter '" + s + "' can't be changed"); } } } else { if (configuration.getAdmin().getPassword() == null || configuration.getAdmin().getPassword().isEmpty()) { throw new CatalogException("Nor the administrator password nor the session id could be found. The user could not be " + "updated."); } authenticationManagerMap.get(INTERNAL_AUTHORIZATION).authenticate("admin", configuration.getAdmin().getPassword(), true); } if (parameters.containsKey("email")) { checkEmail(parameters.getString("email")); } userDBAdaptor.updateUserLastModified(userId); QueryResult<User> queryResult = userDBAdaptor.update(userId, parameters); auditManager.recordUpdate(AuditRecord.Resource.user, userId, userId, parameters, null, null); return queryResult; }
/** * Populates the database with dummy data. * * @param configuration Catalog configuration file. * @param force Used in the case where a database already exists with the same name. When force = true, it will override it. * @throws CatalogException when there is already a database with the same name and force is false. * @throws URISyntaxException when there is a problem parsing the URI read from the configuration file. */ public static void createDemoDatabase(Configuration configuration, boolean force) throws CatalogException, URISyntaxException { CatalogManager catalogManager = new CatalogManager(configuration); if (catalogManager.existsCatalogDB()) { if (force) { catalogManager.deleteCatalogDB(force); } else { // throw new CatalogException("A database called " + catalogConfiguration.getDatabase().getDatabase() + " already exists"); throw new CatalogException("A database called " + catalogManager.getCatalogDatabase() + " already exists"); } } catalogManager.installCatalogDB(configuration.getAdmin().getSecretKey(), configuration.getAdmin().getPassword()); try { populateDatabase(catalogManager); } catch (IOException e) { throw new CatalogException(e.getMessage()); } }
@Override public void before() throws Exception { int c = 0; do { opencgaHome = Paths.get("target/test-data").resolve("junit_opencga_home_" + TimeUtils.getTimeMillis() + (c > 0 ? "_" + c : "")); c++; } while (opencgaHome.toFile().exists()); Files.createDirectories(opencgaHome); configuration = Configuration.load(getClass().getResource("/configuration-test.yml").openStream()); configuration.getAdmin().setAlgorithm("HS256"); configuration.setDataDir(opencgaHome.resolve("sessions").toUri().toString()); configuration.setTempJobsDir(opencgaHome.resolve("jobs").toUri().toString()); catalogManager = new CatalogManager(configuration); try { catalogManager.deleteCatalogDB(false); } catch (Exception ignore) {} clearCatalog(configuration); if (!opencgaHome.toFile().exists()) { deleteFolderTree(opencgaHome.toFile()); Files.createDirectory(opencgaHome); } catalogManager.installCatalogDB("dummy", "admin"); }
public void installCatalogDB(String secretKey, String password) throws CatalogException { if (existsCatalogDB()) { throw new CatalogException("Nothing to install. There already exists a catalog database"); } ParamUtils.checkParameter(secretKey, "secretKey"); ParamUtils.checkParameter(password, "password"); configuration.getAdmin().setPassword(password); configuration.getAdmin().setSecretKey(secretKey); // Check jobs folder is empty URI jobsURI; try { jobsURI = UriUtils.createDirectoryUri(configuration.getTempJobsDir()); } catch (URISyntaxException e) { throw new CatalogException("Failed to create a directory URI from " + configuration.getTempJobsDir()); } CatalogIOManager ioManager = getCatalogIOManagerFactory().get(jobsURI); if (!ioManager.isDirectory(jobsURI) || CollectionUtils.isNotEmpty(ioManager.listFiles(jobsURI))) { throw new CatalogException("Cannot install openCGA. Jobs folder is not empty.\nPlease, empty it first."); } catalogDBAdaptorFactory.installCatalogDB(configuration); }
public void initializeMetaCollection(Configuration configuration) throws CatalogException { Admin admin = configuration.getAdmin(); admin.setPassword(CatalogAuthenticationManager.cypherPassword(admin.getPassword())); Metadata metadata = new Metadata().setIdCounter(configuration.getCatalog().getOffset()).setVersion(VERSION); Document metadataObject = getMongoDBDocument(metadata, "Metadata"); metadataObject.put(PRIVATE_ID, MongoDBAdaptorFactory.METADATA_OBJECT_ID); Document adminDocument = getMongoDBDocument(admin, "Admin"); metadataObject.put("admin", adminDocument); metaCollection.insert(metadataObject, null); }
@BeforeClass public static void beforeClass() throws IOException, CatalogException, URISyntaxException { Configuration configuration = Configuration.load(CatalogSampleAnnotationsLoaderTest.class.getClassLoader() .getClass().getResource("/configuration-test.yml").openStream()); configuration.getAdmin().setAlgorithm("HS256"); catalogManager = new CatalogManager(configuration); catalogManager.deleteCatalogDB(true); catalogManager.installCatalogDB("dummy", "admin"); loader = new CatalogSampleAnnotationsLoader(catalogManager); String pedFileName = "20130606_g1k.ped"; URL pedFileURL = CatalogSampleAnnotationsLoader.class.getClassLoader().getResource(pedFileName); pedigree = loader.readPedigree(pedFileURL.getPath()); userId = "user1"; catalogManager.getUserManager().create(userId, userId, "asdasd@asd.asd", userId, "", -1L, Account.FULL, QueryOptions.empty(), null); sessionId = catalogManager.getUserManager().login(userId, userId); Project project = catalogManager.getProjectManager().create("def", "default", "", "ACME", "Homo sapiens", null, null, "GRCh38", new QueryOptions(), sessionId).getResult().get(0); Study study = catalogManager.getStudyManager().create(project.getFqn(), "def", null, "default", Study.Type.FAMILY, null, "", null, null, null, null, null, null, null, null, sessionId).getResult().get(0); studyId = study.getFqn(); pedFile = catalogManager.getFileManager().create(studyId, File.Type.FILE, File.Format.PED, File.Bioformat .OTHER_PED, "data/" + pedFileName, null, "", null, 0, -1, null, (long) -1, null, null, true, null, null, sessionId) .getResult().get(0); new FileUtils(catalogManager).upload(pedFileURL.toURI(), pedFile, null, sessionId, false, false, false, true, 10000000); pedFile = catalogManager.getFileManager().get(studyId, pedFile.getPath(), null, sessionId).getResult().get(0); }
Configuration configuration = Configuration.load(getClass().getResource("/configuration-test.yml") .openStream()); configuration.getAdmin().setAlgorithm("HS256"); MongoDBConfiguration mongoDBConfiguration = MongoDBConfiguration.builder() .add("username", configuration.getCatalog().getDatabase().getUser())
Configuration configuration = Configuration.load(getClass().getResource("/configuration-test.yml") .openStream()); configuration.getAdmin().setAlgorithm("HS256"); CatalogManagerExternalResource.clearCatalog(configuration);