/** * Pass an existent database instance to be used as current. */ public OConsoleDatabaseApp setCurrentDatabase(final ODatabaseDocumentInternal iCurrentDatabase) { currentDatabase = iCurrentDatabase; currentDatabaseName = iCurrentDatabase.getName(); return this; }
public String getName() { return database.getName(); }
protected ODatabaseDocumentTx(ODatabaseDocumentInternal ref, String baseUrl) { url = ref.getURL(); type = ref.getType(); this.baseUrl = baseUrl; dbName = ref.getName(); internal = ref; this.ownerProtection = true; }
public OCoreException(final String message, final String componentName, final OErrorCode errorCode) { super(message); this.errorCode = errorCode; if (componentName != null) { this.componentName = componentName; } else { this.componentName = null; } final ODatabaseRecordThreadLocal instance = ODatabaseRecordThreadLocal.instance(); if (instance != null) { final ODatabaseDocumentInternal database = instance.getIfDefined(); if (database != null) { dbName = database.getName(); } else { dbName = null; } } else { dbName = null; } }
public void close() { database.declareIntent(null); if (writer == null) return; try { writer.endObject(); writer.close(); writer = null; } catch (IOException e) { OLogManager.instance().error(this, "Error on exporting database '%s' to: %s", e, database.getName(), fileName); throw new ODatabaseExportException("Error on exporting database '" + database.getName() + "' to: " + fileName, e); } if (tempFileName != null) // may be null if writing to an output stream w/o file try { OFileUtils.atomicMoveWithFallback(Paths.get(tempFileName), Paths.get(fileName), this); } catch (IOException e) { OLogManager.instance().error(this, "Error on exporting database '%s' to: %s", e, database.getName(), fileName); throw new ODatabaseExportException("Error on exporting database '" + database.getName() + "' to: " + fileName, e); } }
private void onFunctionsChanged(ODatabaseDocumentInternal database) { for (OMetadataUpdateListener listener : database.getSharedContext().browseListeners()) { listener.onFunctionLibraryUpdate(database.getName()); } }
private void onSequenceLibraryUpdate(ODatabaseDocumentInternal database) { for (OMetadataUpdateListener one : database.getSharedContext().browseListeners()) { one.onSequenceLibraryUpdate(database.getName()); } }
protected void releaseExclusiveLock() { int val = writeLockNesting.decrementAndGet(); internalReleaseExclusiveLock(); if (val == 0) { ODatabaseDocumentInternal database = getDatabase(); for (OMetadataUpdateListener listener : database.getSharedContext().browseListeners()) { listener.onIndexManagerUpdate(database.getName(), this); } } }
@ConsoleCommand(description = "Delete the current database", onlineHelp = "Console-Command-Drop-Database") public void dropDatabase( @ConsoleParameter(name = "storage-type", description = "Storage type of server database", optional = true) String storageType) throws IOException { checkForDatabase(); final String dbName = currentDatabase.getName(); currentDatabase.close(); if (storageType != null && !"plocal".equalsIgnoreCase(storageType)&& !"local".equalsIgnoreCase(storageType)&& !"memory".equalsIgnoreCase(storageType)) { message("\n\nInvalid storage type for db: '" + storageType + "'"); return; } orientDB.drop(dbName); currentDatabase = null; currentDatabaseName = null; message("\n\nDatabase '" + dbName + "' deleted successfully"); }
public OUser authenticate(final String iUserName, final String iUserPassword) { final String dbName = getDatabase().getName(); final OUser user = getUser(iUserName); if (user == null) throw new OSecurityAccessException(dbName, "User or password not valid for database: '" + dbName + "'"); if (user.getAccountStatus() != OSecurityUser.STATUSES.ACTIVE) throw new OSecurityAccessException(dbName, "User '" + iUserName + "' is not active"); if (!(getDatabase().getStorage() instanceof OStorageProxy)) { // CHECK USER & PASSWORD if (!user.checkPassword(iUserPassword)) { // WAIT A BIT TO AVOID BRUTE FORCE try { Thread.sleep(200); } catch (InterruptedException ignore) { Thread.currentThread().interrupt(); } throw new OSecurityAccessException(dbName, "User or password not valid for database: '" + dbName + "'"); } } return user; }
/** * Checks if the user has the permission to access to the requested resource for the requested operation. * * @param iOperation Requested operation * * @return The role that has granted the permission if any, otherwise a OSecurityAccessException exception is raised * * @throws OSecurityAccessException */ public ORole allow(final ORule.ResourceGeneric resourceGeneric, String resourceSpecific, final int iOperation) { if (roles == null || roles.isEmpty()) { if (document.field("roles") != null && !((Collection<OIdentifiable>) document.field("roles")).isEmpty()) { final ODocument doc = document; document = null; fromStream(doc); } else throw new OSecurityAccessException(document.getDatabase().getName(), "User '" + document.field("name") + "' has no role defined"); } final ORole role = checkIfAllowed(resourceGeneric, resourceSpecific, iOperation); if (role == null) throw new OSecurityAccessException(document.getDatabase().getName(), "User '" + document.field("name") + "' does not have permission to execute the operation '" + ORole .permissionToString(iOperation) + "' against the resource: " + resourceGeneric + "." + resourceSpecific); return role; }
protected void revertSubclassesProfiler(final OCommandContext iContext, int num) { final OProfiler profiler = Orient.instance().getProfiler(); if (profiler.isRecording()) { profiler.updateCounter(profiler.getDatabaseMetric(getDatabase().getName(), "query.indexUseAttemptedAndReverted"), "Reverted index usage in query", num); } }
private void logProfiling() { if (executionPlan.getStatement() != null && Orient.instance().getProfiler().isRecording()) { final ODatabaseDocumentInternal db = ODatabaseRecordThreadLocal.instance().getIfDefined(); if (db != null) { final OSecurityUser user = db.getUser(); final String userString = user != null ? user.toString() : null; Orient.instance().getProfiler() .stopChrono("db." + ODatabaseRecordThreadLocal.instance().get().getName() + ".command.sql." + executionPlan.getStatement(), "Command executed against the database", System.currentTimeMillis() - totalExecutionTime, "db.*.command.*", null, userString); } } }
@Override public OUser getUser(final String username) { OUser user = null; if (Orient.instance().getSecurity() != null) { // See if there's a system user first. user = Orient.instance().getSecurity().getSystemUser(username, getDatabase().getName()); } // If not found, try the local database. if (user == null) user = super.getUser(username); return user; } }
@ConsoleCommand(description = "Freeze database and flush on the disk", onlineHelp = "Console-Command-Freeze-Database") public void freezeDatabase( @ConsoleParameter(name = "storage-type", description = "Storage type of server database", optional = true) String storageType) throws IOException { checkForDatabase(); final String dbName = currentDatabase.getName(); if (currentDatabase.getURL().startsWith(OEngineRemote.NAME)) { if (storageType == null) storageType = "plocal"; new OServerAdmin(currentDatabase.getURL()).connect(currentDatabaseUserName, currentDatabaseUserPassword) .freezeDatabase(storageType); } else { // LOCAL CONNECTION currentDatabase.freeze(); } message("\n\nDatabase '" + dbName + "' was frozen successfully"); }
@ConsoleCommand(description = "Release database after freeze", onlineHelp = "Console-Command-Release-Db") public void releaseDatabase( @ConsoleParameter(name = "storage-type", description = "Storage type of server database", optional = true) String storageType) throws IOException { checkForDatabase(); final String dbName = currentDatabase.getName(); if (currentDatabase.getURL().startsWith(OEngineRemote.NAME)) { if (storageType == null) storageType = "plocal"; new OServerAdmin(currentDatabase.getURL()).connect(currentDatabaseUserName, currentDatabaseUserPassword) .releaseDatabase(storageType); } else { // LOCAL CONNECTION currentDatabase.release(); } message("\n\nDatabase '" + dbName + "' was released successfully"); }
public OUser authenticate(final String username, final String password) { if (delegate == null) throw new OSecurityAccessException("OSymmetricKeySecurity.authenticate() Delegate is null for username: " + username); if (database == null) throw new OSecurityAccessException("OSymmetricKeySecurity.authenticate() Database is null for username: " + username); final String dbName = database.getName(); OUser user = delegate.getUser(username); if (user == null) throw new OSecurityAccessException(dbName, "OSymmetricKeySecurity.authenticate() Username or Key is invalid for username: " + username); if (user.getAccountStatus() != OSecurityUser.STATUSES.ACTIVE) throw new OSecurityAccessException(dbName, "OSymmetricKeySecurity.authenticate() User '" + username + "' is not active"); try { OUserSymmetricKeyConfig userConfig = new OUserSymmetricKeyConfig(user); OSymmetricKey sk = OSymmetricKey.fromConfig(userConfig); String decryptedUsername = sk.decryptAsString(password); if (OSecurityManager.instance().checkPassword(username, decryptedUsername)) return user; } catch (Exception ex) { throw OException.wrapException(new OSecurityAccessException(dbName, "OSymmetricKeySecurity.authenticate() Exception for database: " + dbName + ", username: " + username + " " + ex .getMessage()), ex); } throw new OSecurityAccessException(dbName, "OSymmetricKeySecurity.authenticate() Username or Key is invalid for database: " + dbName + ", username: " + username); }
public OUser authenticate(final OToken authToken) { final String dbName = getDatabase().getName(); if (authToken.getIsValid() != true) { throw new OSecurityAccessException(dbName, "Token not valid"); } OUser user = authToken.getUser(getDatabase()); if (user == null && authToken.getUserName() != null) { // Token handler may not support returning an OUser so let's get username (subject) and query: user = getUser(authToken.getUserName()); } if (user == null) { throw new OSecurityAccessException(dbName, "Authentication failed, could not load user from token"); } if (user.getAccountStatus() != STATUSES.ACTIVE) throw new OSecurityAccessException(dbName, "User '" + user.getName() + "' is not active"); return user; }
@Deprecated public Object execute(final Map<Object, Object> iArgs) { final long start = Orient.instance().getProfiler().startChrono(); Object result; while (true) { try { if (callback != null) return callback.call(iArgs); final OCommandExecutorScript command = new OCommandExecutorScript(); command.parse(new OCommandScript(getLanguage(), getCode())); result = command.execute(iArgs); break; } catch (ONeedRetryException | ORetryQueryException ignore) { } } if (Orient.instance().getProfiler().isRecording()) Orient.instance().getProfiler().stopChrono("db." + ODatabaseRecordThreadLocal.instance().get().getName() + ".function.execute", "Time to execute a function", start, "db.*.function.execute"); return result; }
private void exportInfo() throws IOException { listener.onMessage("\nExporting database info..."); writer.beginObject(1, true, "info"); writer.writeAttribute(2, true, "name", database.getName().replace('\\', '/')); writer.writeAttribute(2, true, "default-cluster-id", database.getDefaultClusterId()); writer.writeAttribute(2, true, "exporter-version", VERSION); writer.writeAttribute(2, true, "engine-version", OConstants.getVersion()); final String engineBuild = OConstants.getBuildNumber(); if (engineBuild != null) writer.writeAttribute(2, true, "engine-build", engineBuild); writer.writeAttribute(2, true, "storage-config-version", OStorageConfiguration.CURRENT_VERSION); writer.writeAttribute(2, true, "schema-version", OSchemaShared.CURRENT_VERSION_NUMBER); writer.writeAttribute(2, true, "schemaRecordId", database.getStorage().getConfiguration().getSchemaRecordId()); writer.writeAttribute(2, true, "indexMgrRecordId", database.getStorage().getConfiguration().getIndexMgrRecordId()); writer.endObject(1, true); listener.onMessage("OK"); }