You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airavata.apache.org by la...@apache.org on 2012/11/20 02:49:34 UTC
svn commit: r1411502 - in /airavata/trunk/modules: commons/utils/
commons/utils/src/main/java/org/apache/airavata/common/utils/
rest/service/src/main/java/org/apache/airavata/services/registry/rest/security/local/
rest/service/src/test/java/org/apache/...
Author: lahiru
Date: Tue Nov 20 01:49:32 2012
New Revision: 1411502
URL: http://svn.apache.org/viewvc?rev=1411502&view=rev
Log:
Appying amilas patch for https://issues.apache.org/jira/browse/AIRAVATA-649. Thanks Amila for the contribution.
Added:
airavata/trunk/modules/commons/utils/src/main/java/org/apache/airavata/common/utils/DBUtil.java
airavata/trunk/modules/rest/service/src/main/java/org/apache/airavata/services/registry/rest/security/local/
airavata/trunk/modules/rest/service/src/main/java/org/apache/airavata/services/registry/rest/security/local/LocalUserStore.java
airavata/trunk/modules/rest/service/src/test/java/org/apache/airavata/services/
airavata/trunk/modules/rest/service/src/test/java/org/apache/airavata/services/registry/
airavata/trunk/modules/rest/service/src/test/java/org/apache/airavata/services/registry/rest/
airavata/trunk/modules/rest/service/src/test/java/org/apache/airavata/services/registry/rest/security/
airavata/trunk/modules/rest/service/src/test/java/org/apache/airavata/services/registry/rest/security/local/
airavata/trunk/modules/rest/service/src/test/java/org/apache/airavata/services/registry/rest/security/local/LocalUserStoreTest.java
airavata/trunk/modules/security/src/main/java/org/apache/airavata/security/util/SecurityUtil.java
Modified:
airavata/trunk/modules/commons/utils/pom.xml
airavata/trunk/modules/rest/service/src/test/resources/testdb/test.trace.db
airavata/trunk/modules/security/pom.xml
airavata/trunk/modules/security/src/main/java/org/apache/airavata/security/userstore/JDBCUserStore.java
airavata/trunk/modules/security/src/main/java/org/apache/airavata/security/userstore/SessionDBUserStore.java
airavata/trunk/modules/security/src/main/java/org/apache/airavata/security/util/DBLookup.java
Modified: airavata/trunk/modules/commons/utils/pom.xml
URL: http://svn.apache.org/viewvc/airavata/trunk/modules/commons/utils/pom.xml?rev=1411502&r1=1411501&r2=1411502&view=diff
==============================================================================
--- airavata/trunk/modules/commons/utils/pom.xml (original)
+++ airavata/trunk/modules/commons/utils/pom.xml Tue Nov 20 01:49:32 2012
@@ -59,6 +59,11 @@
<artifactId>axis2-kernel</artifactId>
<version>${axis2.version}</version>
</dependency>
+ <dependency>
+ <groupId>commons-dbcp</groupId>
+ <artifactId>commons-dbcp</artifactId>
+ <version>1.4</version>
+ </dependency>
<!-- Logging -->
<dependency>
Added: airavata/trunk/modules/commons/utils/src/main/java/org/apache/airavata/common/utils/DBUtil.java
URL: http://svn.apache.org/viewvc/airavata/trunk/modules/commons/utils/src/main/java/org/apache/airavata/common/utils/DBUtil.java?rev=1411502&view=auto
==============================================================================
--- airavata/trunk/modules/commons/utils/src/main/java/org/apache/airavata/common/utils/DBUtil.java (added)
+++ airavata/trunk/modules/commons/utils/src/main/java/org/apache/airavata/common/utils/DBUtil.java Tue Nov 20 01:49:32 2012
@@ -0,0 +1,211 @@
+package org.apache.airavata.common.utils;
+
+import org.apache.commons.dbcp.BasicDataSource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.servlet.ServletContext;
+import javax.sql.DataSource;
+import java.sql.*;
+import java.util.Properties;
+
+/**
+ * Database lookup. Abstracts out JDBC operations.
+ */
+public class DBUtil {
+
+ private String jdbcUrl;
+ private String databaseUserName;
+ private String databasePassword;
+ private String driverName;
+
+ protected static Logger log = LoggerFactory.getLogger(DBUtil.class);
+
+ private Properties properties;
+
+ public DBUtil(String jdbcUrl, String userName, String password, String driver) {
+
+ this.jdbcUrl = jdbcUrl;
+ this.databaseUserName = userName;
+ this.databasePassword = password;
+ this.driverName = driver;
+ }
+
+ /**
+ * Initializes and load driver. Must be called this before calling anyother method.
+ * @throws ClassNotFoundException If DB driver is not found.
+ * @throws InstantiationException If unable to create driver class.
+ * @throws IllegalAccessException If security does not allow users to instantiate driver object.
+ */
+ public void init() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
+ properties = new Properties();
+
+ properties.put("user", databaseUserName);
+ properties.put("password", databasePassword);
+ properties.put("characterEncoding", "ISO-8859-1");
+ properties.put("useUnicode", "true");
+
+ loadDriver();
+ }
+
+ /**
+ * Generic method to query values in the database.
+ * @param tableName Table name to query
+ * @param selectColumn The column selecting
+ * @param whereValue The condition query
+ * @return The value appropriate to the query.
+ * @throws SQLException If an error occurred while querying
+ */
+ public String getMatchingColumnValue(String tableName, String selectColumn, String whereValue)
+ throws SQLException {
+ return getMatchingColumnValue(tableName, selectColumn, selectColumn, whereValue);
+ }
+
+ /**
+ * Generic method to query values in the database.
+ * @param tableName Table name to query
+ * @param selectColumn The column selecting
+ * @param whereColumn The column which condition should apply
+ * @param whereValue The condition query
+ * @return The value appropriate to the query.
+ * @throws SQLException If an error occurred while querying
+ */
+ public String getMatchingColumnValue(String tableName, String selectColumn, String whereColumn, String whereValue)
+ throws SQLException {
+
+ StringBuilder stringBuilder = new StringBuilder();
+
+ stringBuilder.append("SELECT ").append(selectColumn).append(" FROM ").append(tableName)
+ .append(" WHERE ").append(whereColumn).append(" = ?");
+
+ String sql = stringBuilder.toString();
+
+ Connection connection = getConnection();
+
+ PreparedStatement ps = connection.prepareStatement(sql);
+ ResultSet rs = null;
+
+ try {
+ ps.setString(1, whereValue);
+ rs = ps.executeQuery();
+
+ if (rs.next()) {
+ return rs.getString(1);
+ }
+
+ } finally {
+ try {
+ if (rs != null) {
+ rs.close();
+ }
+
+ ps.close();
+ connection.close();
+
+ } catch (Exception ignore) {
+ log.error("An error occurred while closing database connections ", ignore);
+ }
+ }
+
+ return null;
+ }
+
+ private void loadDriver() throws ClassNotFoundException, IllegalAccessException, InstantiationException {
+ Class.forName(driverName).newInstance();
+ }
+
+ /**
+ * Gets a new DBCP data source.
+ * @return A new data source.
+ */
+ public DataSource getDataSource() {
+ BasicDataSource ds = new BasicDataSource();
+ ds.setDriverClassName(this.driverName);
+ ds.setUsername(this.databaseUserName);
+ ds.setPassword(this.databasePassword);
+ ds.setUrl(this.jdbcUrl);
+
+ return ds;
+ }
+
+ /**
+ * Creates a new JDBC connections based on provided DBCP properties.
+ * @return A new DB connection.
+ * @throws SQLException If an error occurred while creating the connection.
+ */
+ public Connection getConnection() throws SQLException {
+ return DriverManager.getConnection(jdbcUrl, properties);
+ }
+
+ /**
+ * Utility method to close statements and connections.
+ * @param preparedStatement The prepared statement to close.
+ * @param connection The connection to close.
+ */
+ public void cleanup(PreparedStatement preparedStatement, Connection connection) {
+ if (preparedStatement != null) {
+ try {
+ preparedStatement.close();
+ } catch (SQLException e) {
+ log.error("Error closing prepared statement.", e);
+ }
+ }
+ if (connection != null) {
+ try {
+ connection.close();
+ } catch (SQLException e) {
+ log.error("Error closing database connection.", e);
+ }
+ }
+ }
+
+ /**
+ * Mainly useful for tests.
+ * @param tableName The table name.
+ * @param connection The connection to be used.
+ */
+ public static void truncate(String tableName, Connection connection) throws SQLException {
+
+ String sql = "delete from " + tableName;
+
+ PreparedStatement preparedStatement = connection.prepareStatement(sql);
+ preparedStatement.executeUpdate();
+
+ connection.commit();
+
+ }
+
+ /**
+ * Creates a DBUtil object based on servlet context configurations.
+ * @param servletContext The servlet context.
+ * @return DBUtil object.
+ * @throws Exception If an error occurred while reading configurations or while creating
+ * database object.
+ */
+ public static DBUtil getDBUtil(ServletContext servletContext) throws Exception{
+
+ String jdbcUrl = servletContext.getInitParameter("credential-store-jdbc-url");
+ String userName = servletContext.getInitParameter("credential-store-db-user");
+ String password = servletContext.getInitParameter("credential-store-db-password");
+ String driverName = servletContext.getInitParameter("credential-store-db-driver");
+
+ StringBuilder stringBuilder = new StringBuilder("Starting credential store, connecting to database - ");
+ stringBuilder.append(jdbcUrl).append(" DB user - ").append(userName).
+ append(" driver name - ").append(driverName);
+
+ log.info(stringBuilder.toString());
+
+ DBUtil dbUtil = new DBUtil(jdbcUrl, userName, password, driverName);
+ try {
+ dbUtil.init();
+ } catch (Exception e) {
+ log.error("Error initializing database operations.", e);
+ throw e;
+ }
+
+ return dbUtil;
+ }
+
+
+
+}
Added: airavata/trunk/modules/rest/service/src/main/java/org/apache/airavata/services/registry/rest/security/local/LocalUserStore.java
URL: http://svn.apache.org/viewvc/airavata/trunk/modules/rest/service/src/main/java/org/apache/airavata/services/registry/rest/security/local/LocalUserStore.java?rev=1411502&view=auto
==============================================================================
--- airavata/trunk/modules/rest/service/src/main/java/org/apache/airavata/services/registry/rest/security/local/LocalUserStore.java (added)
+++ airavata/trunk/modules/rest/service/src/main/java/org/apache/airavata/services/registry/rest/security/local/LocalUserStore.java Tue Nov 20 01:49:32 2012
@@ -0,0 +1,268 @@
+package org.apache.airavata.services.registry.rest.security.local;
+
+import org.apache.airavata.common.utils.DBUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * User store to maintain internal DB database.
+ */
+public class LocalUserStore {
+
+ protected static Logger log = LoggerFactory.getLogger(LocalUserStore.class);
+
+ private DBUtil dbUtil;
+
+ public LocalUserStore(DBUtil db) {
+ dbUtil = db;
+ }
+
+ public void addUser(String userName, String password) {
+
+ String sql = "insert into users values (?, ?)";
+
+ Connection connection = null;
+ PreparedStatement preparedStatement = null;
+
+ try {
+ connection = dbUtil.getConnection();
+ preparedStatement = connection.prepareStatement(sql);
+
+ preparedStatement.setString(1, userName);
+ preparedStatement.setString(2, password);
+
+ preparedStatement.executeUpdate();
+
+ connection.commit();
+
+ } catch (SQLException e) {
+ StringBuilder stringBuilder = new StringBuilder("Error persisting user information.");
+ stringBuilder.append(" user - ").append(userName);
+
+ log.error(stringBuilder.toString(), e);
+
+ throw new RuntimeException(stringBuilder.toString(), e);
+ } finally {
+
+ dbUtil.cleanup(preparedStatement, connection);
+ }
+
+ }
+
+ protected String getPassword(String userName, Connection connection) {
+
+ String sql = "select password from users where user_name = ?";
+
+ PreparedStatement preparedStatement = null;
+ ResultSet resultSet = null;
+
+ try {
+ preparedStatement = connection.prepareStatement(sql);
+
+ preparedStatement.setString(1, userName);
+
+ resultSet = preparedStatement.executeQuery();
+
+ if (resultSet.next()) {
+ return resultSet.getString("password");
+ }
+
+ } catch (SQLException e) {
+ StringBuilder stringBuilder = new StringBuilder("Error retrieving credentials for user.");
+ stringBuilder.append("name - ").append(userName);
+
+ log.error(stringBuilder.toString(), e);
+
+ throw new RuntimeException(stringBuilder.toString(), e);
+ } finally {
+
+ if (resultSet != null) {
+ try {
+ resultSet.close();
+ } catch (SQLException e) {
+ log.error("Error closing result set", e);
+ }
+ }
+
+ if (preparedStatement != null) {
+ try {
+ preparedStatement.close();
+ } catch (SQLException e) {
+ log.error("Error closing prepared statement", e);
+ }
+ }
+ }
+
+ return null;
+ }
+
+ public void changePassword(String userName, String oldPassword, String newPassword) {
+
+ Connection connection = null;
+ PreparedStatement preparedStatement = null;
+
+ try {
+ connection = dbUtil.getConnection();
+
+ String storedPassword = getPassword(userName, connection);
+
+ if (storedPassword != null) {
+ if (!storedPassword.equals(oldPassword)) {
+ throw new RuntimeException("Previous password did not match correctly. Please specify old password" +
+ " correctly.");
+ }
+ }
+
+ String sql = "update users set password = ? where user_name = ?";
+
+ preparedStatement = connection.prepareStatement(sql);
+
+ preparedStatement.setString(1, newPassword);
+ preparedStatement.setString(2, userName);
+
+ preparedStatement.executeUpdate();
+
+ connection.commit();
+
+ } catch (SQLException e) {
+ StringBuilder stringBuilder = new StringBuilder("Error updating credentials.");
+ stringBuilder.append(" user - ").append(userName);
+
+ log.error(stringBuilder.toString(), e);
+
+ throw new RuntimeException(stringBuilder.toString(), e);
+ } finally {
+
+ dbUtil.cleanup(preparedStatement, connection);
+ }
+
+ }
+
+ public void changePasswordByAdmin(String userName, String newPassword) {
+
+ Connection connection = null;
+ PreparedStatement preparedStatement = null;
+
+ try {
+ connection = dbUtil.getConnection();
+
+ String sql = "update users set password = ? where user_name = ?";
+
+ preparedStatement = connection.prepareStatement(sql);
+
+ preparedStatement.setString(1, newPassword);
+ preparedStatement.setString(2, userName);
+
+ preparedStatement.executeUpdate();
+
+ connection.commit();
+
+ } catch (SQLException e) {
+ StringBuilder stringBuilder = new StringBuilder("Error updating credentials.");
+ stringBuilder.append(" user - ").append(userName);
+
+ log.error(stringBuilder.toString(), e);
+
+ throw new RuntimeException(stringBuilder.toString(), e);
+ } finally {
+
+ dbUtil.cleanup(preparedStatement, connection);
+ }
+
+ }
+
+
+ public void deleteUser(String userName) {
+
+ String sql = "delete from users where user_name=?;";
+
+ Connection connection = null;
+ PreparedStatement preparedStatement = null;
+
+ try {
+ connection = dbUtil.getConnection();
+ preparedStatement = connection.prepareStatement(sql);
+
+ preparedStatement.setString(1, userName);
+
+ preparedStatement.executeUpdate();
+
+ connection.commit();
+
+ } catch (SQLException e) {
+ StringBuilder stringBuilder = new StringBuilder("Error deleting user.");
+ stringBuilder.append("user - ").append(userName);
+
+ log.error(stringBuilder.toString(), e);
+
+ throw new RuntimeException(stringBuilder.toString(), e);
+ } finally {
+ dbUtil.cleanup(preparedStatement, connection);
+ }
+
+ }
+
+ public List<String> getUsers() {
+
+ List<String> userList = new ArrayList<String>();
+
+ String sql = "select user_name from users";
+
+ PreparedStatement preparedStatement = null;
+ ResultSet resultSet = null;
+ Connection connection = null;
+
+ try {
+
+ connection = dbUtil.getConnection();
+ preparedStatement = connection.prepareStatement(sql);
+
+ resultSet = preparedStatement.executeQuery();
+
+ if (resultSet.next()) {
+ userList.add(resultSet.getString("user_name"));
+ }
+
+ } catch (SQLException e) {
+ String errorString = "Error retrieving users.";
+ log.error(errorString, e);
+
+ throw new RuntimeException(errorString, e);
+ } finally {
+
+ if (resultSet != null) {
+ try {
+ resultSet.close();
+ } catch (SQLException e) {
+ log.error("Error closing result set", e);
+ }
+ }
+
+ if (preparedStatement != null) {
+ try {
+ preparedStatement.close();
+ } catch (SQLException e) {
+ log.error("Error closing prepared statement", e);
+ }
+ }
+
+ if (connection != null) {
+ try {
+ connection.close();
+ } catch (SQLException e) {
+ log.error("Error closing prepared statement", e);
+ }
+ }
+ }
+
+ return userList;
+
+ }
+}
Added: airavata/trunk/modules/rest/service/src/test/java/org/apache/airavata/services/registry/rest/security/local/LocalUserStoreTest.java
URL: http://svn.apache.org/viewvc/airavata/trunk/modules/rest/service/src/test/java/org/apache/airavata/services/registry/rest/security/local/LocalUserStoreTest.java?rev=1411502&view=auto
==============================================================================
--- airavata/trunk/modules/rest/service/src/test/java/org/apache/airavata/services/registry/rest/security/local/LocalUserStoreTest.java (added)
+++ airavata/trunk/modules/rest/service/src/test/java/org/apache/airavata/services/registry/rest/security/local/LocalUserStoreTest.java Tue Nov 20 01:49:32 2012
@@ -0,0 +1,112 @@
+package org.apache.airavata.services.registry.rest.security.local;
+
+import junit.framework.TestCase;
+import org.apache.airavata.common.utils.DBUtil;
+
+import java.io.File;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.List;
+
+/**
+ * A test class for local user store.
+ */
+public class LocalUserStoreTest extends TestCase {
+
+ private DBUtil dbUtil = new DBUtil("jdbc:h2:modules/airavata-rest-services/src/test/resources/testdb/test",
+ "sa", "sa", "org.h2.Driver");
+
+ private LocalUserStore localUserStore;
+
+ private static final String createTableScript =
+ "create table Users\n" +
+ "(\n" +
+ " user_name varchar(255),\n" +
+ " password varchar(255),\n" +
+ " PRIMARY KEY(user_name)\n" +
+ ");";
+
+ private static final String dropTableScript =
+ "drop table Users";
+
+ private void createTable(Connection connection, String query) {
+
+
+ Statement stmt = null;
+
+ try {
+ stmt = connection.createStatement();
+ stmt.executeUpdate(query);
+ }
+ catch (Exception e) {
+ e.printStackTrace();
+ } finally {
+ if (stmt != null) {
+ try {
+ stmt.close();
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ }
+
+ public void setUp() throws Exception{
+
+ dbUtil.init();
+
+ File f = new File(".");
+ System.out.println(f.getAbsolutePath());
+
+ Connection connection = dbUtil.getConnection();
+ createTable(connection, dropTableScript);
+ createTable(connection, createTableScript);
+
+ try {
+ connection.close();
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+
+ localUserStore = new LocalUserStore(dbUtil);
+ }
+
+ public void testAddUser() throws Exception {
+
+ localUserStore.addUser("thejaka", "qwqwqw");
+
+ List<String> users = localUserStore.getUsers();
+ assertEquals(1, users.size());
+ assertEquals("thejaka", users.get(0));
+ }
+
+ public void testChangePassword() throws Exception {
+
+ localUserStore.addUser("thejaka", "qwqwqw");
+
+ localUserStore.changePassword("thejaka", "qwqwqw", "sadsad");
+ }
+
+ public void testChangePasswordByAdmin() throws Exception {
+
+ localUserStore.addUser("thejaka", "qwqwqw");
+
+ localUserStore.changePasswordByAdmin("thejaka", "sadsad");
+ }
+
+ public void testDeleteUser() throws Exception {
+
+ localUserStore.addUser("thejaka", "qwqwqw");
+
+ List<String> users = localUserStore.getUsers();
+ assertEquals(1, users.size());
+ assertEquals("thejaka", users.get(0));
+
+ localUserStore.deleteUser("thejaka");
+
+ users = localUserStore.getUsers();
+ assertEquals(0, users.size());
+
+ }
+}
Modified: airavata/trunk/modules/rest/service/src/test/resources/testdb/test.trace.db
URL: http://svn.apache.org/viewvc/airavata/trunk/modules/rest/service/src/test/resources/testdb/test.trace.db?rev=1411502&r1=1411501&r2=1411502&view=diff
==============================================================================
--- airavata/trunk/modules/rest/service/src/test/resources/testdb/test.trace.db (original)
+++ airavata/trunk/modules/rest/service/src/test/resources/testdb/test.trace.db Tue Nov 20 01:49:32 2012
@@ -95,3 +95,68 @@ org.h2.message.DbException: The connecti
Caused by: org.h2.jdbc.JdbcSQLException: The connection was not closed by the application and is garbage collected [90018-168]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:329)
... 39 more
+11-19 14:35:19 jdbc[2]: exception
+org.h2.jdbc.JdbcSQLException: Table "USERS" not found; SQL statement:
+drop table Users [42102-168]
+11-19 14:35:19 jdbc[2]: exception
+org.h2.jdbc.JdbcSQLException: The object is already closed [90007-168]
+ at org.h2.message.DbException.getJdbcSQLException(DbException.java:329)
+ at org.h2.message.DbException.get(DbException.java:169)
+ at org.h2.message.DbException.get(DbException.java:146)
+ at org.h2.message.DbException.get(DbException.java:135)
+ at org.h2.jdbc.JdbcConnection.checkClosed(JdbcConnection.java:1381)
+ at org.h2.jdbc.JdbcConnection.checkClosed(JdbcConnection.java:1359)
+ at org.h2.jdbc.JdbcConnection.createStatement(JdbcConnection.java:189)
+ at org.apache.airavata.services.registry.rest.security.local.LocalUserStoreTest.createTable(LocalUserStoreTest.java:36)
+ at org.apache.airavata.services.registry.rest.security.local.LocalUserStoreTest.setUp(LocalUserStoreTest.java:66)
+ at junit.framework.TestCase.runBare(TestCase.java:132)
+ at junit.framework.TestResult$1.protect(TestResult.java:110)
+ at junit.framework.TestResult.runProtected(TestResult.java:128)
+ at junit.framework.TestResult.run(TestResult.java:113)
+ at junit.framework.TestCase.run(TestCase.java:124)
+ at junit.framework.TestSuite.runTest(TestSuite.java:232)
+ at junit.framework.TestSuite.run(TestSuite.java:227)
+ at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
+ at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
+ at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:76)
+ at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
+ at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
+ at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
+ at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
+ at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
+ at java.lang.reflect.Method.invoke(Method.java:597)
+ at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
+11-19 14:35:31 jdbc[2]: exception
+org.h2.jdbc.JdbcSQLException: Table "USERS" not found; SQL statement:
+drop table Users [42102-168]
+11-19 14:35:31 jdbc[2]: exception
+org.h2.jdbc.JdbcSQLException: The object is already closed [90007-168]
+ at org.h2.message.DbException.getJdbcSQLException(DbException.java:329)
+ at org.h2.message.DbException.get(DbException.java:169)
+ at org.h2.message.DbException.get(DbException.java:146)
+ at org.h2.message.DbException.get(DbException.java:135)
+ at org.h2.jdbc.JdbcConnection.checkClosed(JdbcConnection.java:1381)
+ at org.h2.jdbc.JdbcConnection.checkClosed(JdbcConnection.java:1359)
+ at org.h2.jdbc.JdbcConnection.createStatement(JdbcConnection.java:189)
+ at org.apache.airavata.services.registry.rest.security.local.LocalUserStoreTest.createTable(LocalUserStoreTest.java:36)
+ at org.apache.airavata.services.registry.rest.security.local.LocalUserStoreTest.setUp(LocalUserStoreTest.java:66)
+ at junit.framework.TestCase.runBare(TestCase.java:132)
+ at junit.framework.TestResult$1.protect(TestResult.java:110)
+ at junit.framework.TestResult.runProtected(TestResult.java:128)
+ at junit.framework.TestResult.run(TestResult.java:113)
+ at junit.framework.TestCase.run(TestCase.java:124)
+ at junit.framework.TestSuite.runTest(TestSuite.java:232)
+ at junit.framework.TestSuite.run(TestSuite.java:227)
+ at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
+ at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
+ at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:76)
+ at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
+ at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
+ at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
+ at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
+ at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
+ at java.lang.reflect.Method.invoke(Method.java:597)
+ at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
+11-19 14:36:26 jdbc[2]: exception
+org.h2.jdbc.JdbcSQLException: Table "USERS" not found; SQL statement:
+drop table Users [42102-168]
Modified: airavata/trunk/modules/security/pom.xml
URL: http://svn.apache.org/viewvc/airavata/trunk/modules/security/pom.xml?rev=1411502&r1=1411501&r2=1411502&view=diff
==============================================================================
--- airavata/trunk/modules/security/pom.xml (original)
+++ airavata/trunk/modules/security/pom.xml Tue Nov 20 01:49:32 2012
@@ -24,6 +24,11 @@
<dependencies>
<dependency>
+ <groupId>org.apache.airavata</groupId>
+ <artifactId>airavata-common-utils</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
Modified: airavata/trunk/modules/security/src/main/java/org/apache/airavata/security/userstore/JDBCUserStore.java
URL: http://svn.apache.org/viewvc/airavata/trunk/modules/security/src/main/java/org/apache/airavata/security/userstore/JDBCUserStore.java?rev=1411502&r1=1411501&r2=1411502&view=diff
==============================================================================
--- airavata/trunk/modules/security/src/main/java/org/apache/airavata/security/userstore/JDBCUserStore.java (original)
+++ airavata/trunk/modules/security/src/main/java/org/apache/airavata/security/userstore/JDBCUserStore.java Tue Nov 20 01:49:32 2012
@@ -23,9 +23,8 @@
package org.apache.airavata.security.userstore;
-import org.apache.airavata.security.UserStore;
import org.apache.airavata.security.UserStoreException;
-import org.apache.airavata.security.util.DBLookup;
+import org.apache.airavata.common.utils.DBUtil;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
@@ -143,9 +142,9 @@ public class JDBCUserStore extends Abstr
protected void initializeDatabaseLookup(String passwordColumn, String userTable,
String userNameColumn) {
- DBLookup dbLookup = new DBLookup(getDatabaseURL(), getDatabaseUserName(), getDatabasePassword(),
+ DBUtil dbUtil = new DBUtil(getDatabaseURL(), getDatabaseUserName(), getDatabasePassword(),
getDatabaseDriver());
- DataSource dataSource = dbLookup.getDataSource();
+ DataSource dataSource = dbUtil.getDataSource();
jdbcRealm.setDataSource(dataSource);
StringBuilder stringBuilder = new StringBuilder();
Modified: airavata/trunk/modules/security/src/main/java/org/apache/airavata/security/userstore/SessionDBUserStore.java
URL: http://svn.apache.org/viewvc/airavata/trunk/modules/security/src/main/java/org/apache/airavata/security/userstore/SessionDBUserStore.java?rev=1411502&r1=1411501&r2=1411502&view=diff
==============================================================================
--- airavata/trunk/modules/security/src/main/java/org/apache/airavata/security/userstore/SessionDBUserStore.java (original)
+++ airavata/trunk/modules/security/src/main/java/org/apache/airavata/security/userstore/SessionDBUserStore.java Tue Nov 20 01:49:32 2012
@@ -24,7 +24,7 @@
package org.apache.airavata.security.userstore;
import org.apache.airavata.security.UserStoreException;
-import org.apache.airavata.security.util.DBLookup;
+import org.apache.airavata.common.utils.DBUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Element;
@@ -44,7 +44,7 @@ public class SessionDBUserStore extends
private String sessionColumn;
private String comparingColumn;
- protected DBLookup dbLookup;
+ protected DBUtil dbUtil;
protected static Logger log = LoggerFactory.getLogger(SessionDBUserStore.class);
@@ -61,7 +61,7 @@ public class SessionDBUserStore extends
String sessionTicket = (String)credentials;
try {
- String sessionString = dbLookup.getMatchingColumnValue(sessionTable, sessionColumn, sessionTicket);
+ String sessionString = dbUtil.getMatchingColumnValue(sessionTable, sessionColumn, sessionTicket);
return (sessionString != null);
} catch (SQLException e) {
throw new UserStoreException("Error querying database for session information.", e);
@@ -126,10 +126,10 @@ public class SessionDBUserStore extends
private void initializeDatabaseLookup() throws RuntimeException {
- this.dbLookup = new DBLookup(getDatabaseURL(), getDatabaseUserName(), getDatabasePassword(), getDatabaseDriver());
+ this.dbUtil = new DBUtil(getDatabaseURL(), getDatabaseUserName(), getDatabasePassword(), getDatabaseDriver());
try {
- this.dbLookup.init();
+ this.dbUtil.init();
} catch (ClassNotFoundException e) {
throw new RuntimeException("Error loading database driver. Driver class not found.", e);
} catch (InstantiationException e) {
Modified: airavata/trunk/modules/security/src/main/java/org/apache/airavata/security/util/DBLookup.java
URL: http://svn.apache.org/viewvc/airavata/trunk/modules/security/src/main/java/org/apache/airavata/security/util/DBLookup.java?rev=1411502&r1=1411501&r2=1411502&view=diff
==============================================================================
--- airavata/trunk/modules/security/src/main/java/org/apache/airavata/security/util/DBLookup.java (original)
+++ airavata/trunk/modules/security/src/main/java/org/apache/airavata/security/util/DBLookup.java Tue Nov 20 01:49:32 2012
@@ -1,108 +0,0 @@
-package org.apache.airavata.security.util;
-
-import org.apache.commons.dbcp.BasicDataSource;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import javax.sql.DataSource;
-import java.sql.*;
-import java.util.Properties;
-
-/**
- * Database lookup.
- */
-public class DBLookup {
-
- private String jdbcUrl;
- private String databaseUserName;
- private String databasePassword;
- private String driverName;
-
- protected static Logger log = LoggerFactory.getLogger(DBLookup.class);
-
- private Properties properties;
-
-
- public DBLookup(String jdbcUrl, String userName, String password, String driver) {
-
- this.jdbcUrl = jdbcUrl;
- this.databaseUserName = userName;
- this.databasePassword = password;
- this.driverName = driver;
- }
-
- public void init() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
- properties = new Properties();
-
- properties.put("user", databaseUserName);
- properties.put("password", databasePassword);
- properties.put("characterEncoding", "ISO-8859-1");
- properties.put("useUnicode", "true");
-
- loadDriver();
- }
-
- public String getMatchingColumnValue(String tableName, String selectColumn, String whereValue)
- throws SQLException {
- return getMatchingColumnValue(tableName, selectColumn, selectColumn, whereValue);
- }
-
- public String getMatchingColumnValue(String tableName, String selectColumn, String whereColumn, String whereValue)
- throws SQLException {
-
- StringBuilder stringBuilder = new StringBuilder();
-
- stringBuilder.append("SELECT ").append(selectColumn).append(" FROM ").append(tableName)
- .append(" WHERE ").append(whereColumn).append(" = ?");
-
- String sql = stringBuilder.toString();
-
- Connection connection = getConnection();
-
- PreparedStatement ps = connection.prepareStatement(sql);
- ResultSet rs = null;
-
- try {
- ps.setString(1, whereValue);
- rs = ps.executeQuery();
-
- if (rs.next()) {
- return rs.getString(1);
- }
-
- } finally {
- try {
- if (rs != null) {
- rs.close();
- }
-
- ps.close();
- connection.close();
-
- } catch (Exception ignore) {
- log.error("An error occurred while closing database connections ", ignore);
- }
- }
-
- return null;
- }
-
- private void loadDriver() throws ClassNotFoundException, IllegalAccessException, InstantiationException {
- Class.forName(driverName).newInstance();
- }
-
- public DataSource getDataSource() {
- BasicDataSource ds = new BasicDataSource();
- ds.setDriverClassName(this.driverName);
- ds.setUsername(this.databaseUserName);
- ds.setPassword(this.databasePassword);
- ds.setUrl(this.jdbcUrl);
-
- return ds;
- }
-
- public Connection getConnection() throws SQLException {
- return DriverManager.getConnection(jdbcUrl, properties);
- }
-
-}
Added: airavata/trunk/modules/security/src/main/java/org/apache/airavata/security/util/SecurityUtil.java
URL: http://svn.apache.org/viewvc/airavata/trunk/modules/security/src/main/java/org/apache/airavata/security/util/SecurityUtil.java?rev=1411502&view=auto
==============================================================================
--- airavata/trunk/modules/security/src/main/java/org/apache/airavata/security/util/SecurityUtil.java (added)
+++ airavata/trunk/modules/security/src/main/java/org/apache/airavata/security/util/SecurityUtil.java Tue Nov 20 01:49:32 2012
@@ -0,0 +1,22 @@
+package org.apache.airavata.security.util;
+
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+
+/**
+ * Contains some utility methods related to security.
+ */
+public class SecurityUtil {
+
+ /**
+ * Gets the hash value of a password.
+ * @param hashMethod The hash method.
+ * @param password Password.
+ * @return Hashed password.
+ * @throws NoSuchAlgorithmException If an invalid hash method is given.
+ */
+ public static byte[] getHashedPassword (String hashMethod, String password) throws NoSuchAlgorithmException {
+ MessageDigest md = MessageDigest.getInstance(hashMethod);
+ return md.digest(password.getBytes());
+ }
+}