<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Gerbrand on ICT &#187; sql</title>
	<atom:link href="http://www.gerbrand-ict.nl/tag/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.gerbrand-ict.nl</link>
	<description>Weblog on JEE and software-engineering</description>
	<lastBuildDate>Wed, 11 Aug 2010 20:51:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Proxying authentication using JBoss</title>
		<link>http://www.gerbrand-ict.nl/2010/06/proxying-authentication-using-jboss/</link>
		<comments>http://www.gerbrand-ict.nl/2010/06/proxying-authentication-using-jboss/#comments</comments>
		<pubDate>Mon, 21 Jun 2010 23:00:19 +0000</pubDate>
		<dc:creator>gerbrand</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jboss]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://www.gerbrand-ict.nl/?p=264</guid>
		<description><![CDATA[Wouldn&#8217;t it be nice if the connection to the database is done using the same username as the username used to login to a (JEE) application? Oracle has a solution for that: proxy authentication.  When using proxy authentication, every application user is also a database user: meaning when someone logins to your JEE webapplication using [...]]]></description>
			<content:encoded><![CDATA[<p>Wouldn&#8217;t it be nice if the connection to the database is done using the same username as the username used to login to a (JEE) application? Oracle has a solution for that: <a href="http://download.oracle.com/docs/cd/B28359_01/java.111/b31224/proxya.htm">proxy authentication</a>.  When using proxy authentication, every application user is also a database user: meaning when someone logins to your JEE webapplication using username john, he&#8217;ll also access the database as user john. This way all actions of the user are logged at the database-level: an administrator or auditer can see exactly what data a certain user modified or accessed during a JEE session.</p>
<p><span id="more-264"></span></p>
<p>Also, this allows for fine-grained security at database level: <a href="http://www.oracle.com/technology/deploy/security/database-security-10g/virtual-private-database/index.html">Virtual Private Database</a>.</p>
<div id="attachment_297" class="wp-caption alignnone" style="width: 466px"><a href="http://www.oracle.com/technology/deploy/security/database-security-10g/virtual-private-database/index.html"><img class="size-full wp-image-297" title="virtual-private-database1" src="http://www.gerbrand-ict.nl/wp-content/uploads/2009/06/virtual-private-database1.gif" alt="Virtual private database" width="456" height="175" /></a><p class="wp-caption-text">(c) Oracle</p></div>
<p>As described in the above image and referenced article, the user identified by userid 106 (let&#8217;s say that&#8217;s user john) will access the database using a private database connection. The user can only access rows that have that userid as primary or foreign key.  John can&#8217;t see the passwords, orders, credit card data or anything of other users even if he would somehow hack the webapplication. How to set up this finegrained securiy is beyond this article, but I hope the above example explains what the purpose VPD is.</p>
<p>If you develop your JEE software using Oracle software  proxy authentication requires only a bit of configuration, for example, see the following article how to setup proxy authentication using <a href="http://blogs.oracle.com/jheadstart/2008/01/28/">JHeadstart</a>.</p>
<p>However, what if you&#8217;re JEE applicication consists of non-oracle software? Can you still use proxy authentication when you use software like JBoss, Hibernate, IBatis, MyFaces, Wicket etc? Yes you can! I&#8217;ll explain below how to set up proxy authentication using JBoss in such a way you don&#8217;t have to modify any of the code that uses JDBC, directly or indirectly.</p>
<ul>
<li>First a way is needed to set a username for each (JDBC) database connection retrieved. <a href="http://www.it-eye.nl/weblog/2005/09/12/oracle-proxy-users-by-example/">It-eye weblog</a> explains how to open a connection to a database using java, and then switch to another username.</li>
<li>Secondly, in your application users should authenticate them self using the default mechanism of J2EE 1.4 (and higher) application, using <a href="http://java.sun.com/javase/technologies/security/">JAAS</a>. Usually creating a security policy inside your web.xml or inside your ear is enough. Here&#8217;s the information how to do this under JBoss: <a href="http://www.jboss.org/community/wiki/SecureAWebApplicationUsingACustomForm">Secure a webapplication</a>.<br />
Since we&#8217;re using a database, the best option would be to use database based authentication, meaning user information comes out of a database table. There&#8217;s a lot of documentation on the web how to do add security, so I won&#8217;t repeat that here.</li>
<li>Database connection in JBoss are retrieved using connection pooling, as is custom in any JEE server.  You can create a custom connection pool, that changes the switches to the user name that is currently logged in at the webapplication. That way, every action on the database is done under a database user that is currently logged in.Do to be able to do that, first, you&#8217;ll need a custom connection factory that extends the default connection factory. Our connection factory will return a customized datasource that modifies code.Here&#8217;s a code listing:
<pre class="brush: java;">package nl.gerbrandict.dbconnaudit;

import java.sql.SQLException;
import javax.resource.ResourceException;
import javax.resource.spi.ConnectionManager;

import org.apache.log4j.Logger;
import org.apache.commons.lang.StringUtils;
import org.jboss.resource.adapter.jdbc.local.LocalManagedConnectionFactory;

/**
 * An extended connection factory, that uses the Oracle feature to change the username of an existing connection
 *

 * When a user authenticated on the application server, the database connection will switch to that username.
 * This allows for better auditing and potentially for improved security.
 *
 * Properties (get'ers and set'ters) can be set via the configuration section of the -ds file
 * @author gvdieijen
 */
public class DBConnAuditConnectionFactory extends LocalManagedConnectionFactory {

    private String defaultProxyUser;

    public DBConnAuditConnectionFactory() throws SQLException {
        super();

    }

    @Override
    public Object createConnectionFactory(ConnectionManager cm) throws ResourceException {
        return new OracleWrapperDataSource(this, cm);
    }

    public void setEnableProxySession(Boolean enableProxySession) {
        this.enableDbConnAudit = enableProxySession;
    }

    public void setDefaultProxyUser(final String defaultProxyUser) {
        if (StringUtils.isEmpty(defaultProxyUser)) {
            this.defaultProxyUser=null;
        } else {
             this.defaultProxyUser = defaultProxyUser;
        }
    }

    /**
     * Default db user to open proxy session for, when no authenticated user is active
     * @return
     */
    public String getDefaultProxyUser() {
    	return this.defaultProxyUser;
    }
}</pre>
<p>As you can see, the file returns a OracleWrapperDatasource. That&#8217;s custom class, that extends the default WrapperDatasource of JBoss, and changes the user of jdbc connection just before the connection is handed of to the application. To speak in terms of the fine book</li>
<li>Now, How can you use that new class? They have to be packed into a rar file. Functionally, that&#8217;s a Resource Adapter, a module that allows a J2EE application to use resources. Technically (and practically), it&#8217;s just a jar-archive similar to a war, with a different extension. Maven can create these files automatically for you, if you set the packaging type to rar instead of jar (which is the default).</li>
<li>When you download JBoss, you&#8217;ll get a sample connection pool for a in-memory database: default-ds.xml, located in the server/default/deploy directory of jboss. To use the custom classses, copy the file into (for example) myoracleproxy-ds.xml and create a minor modification so a custom connection factory is used &#8211; update the managedconnectionfactory property, that a custom connectionfactory is used, that returns the proxied connections:
<pre class="brush: xml;">&lt;managedconnectionfactory-class&gt;nl.gerbrandict.dbconnaudit.DBConnAuditConnectionFactory&lt;/managedconnectionfactory-class&gt;</pre>
</li>
</ul>
<p>All in all, after some tweaking, all queries and updates to your Oracle database are done under the J2EE username. This means when user Joe logs in, all his database access will be logged under user Joe as well. This can improve auditing as well as security.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gerbrand-ict.nl/2010/06/proxying-authentication-using-jboss/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Alternative to SQL</title>
		<link>http://www.gerbrand-ict.nl/2009/05/alternative-to-sql/</link>
		<comments>http://www.gerbrand-ict.nl/2009/05/alternative-to-sql/#comments</comments>
		<pubDate>Mon, 04 May 2009 14:55:57 +0000</pubDate>
		<dc:creator>gerbrand</dc:creator>
				<category><![CDATA[Reviews]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://www.gerbrand-ict.nl/?p=241</guid>
		<description><![CDATA[Database alternative, for people just browsing and scanning, I&#8217;ll write in staccato. Relational model pretty good, but we need a better implementation of the relational model, SQL, at least the way it&#8217;s used now, is just to primitive and cumbersome. Lot&#8217;s of new languages for the JVM, but we&#8217;re still using SQL to retrieve data [...]]]></description>
			<content:encoded><![CDATA[<p>Database alternative, for people just browsing and scanning, I&#8217;ll write in staccato.</p>
<ul>
<li>Relational model pretty good, but we need a better implementation of the relational model, SQL, at least the way it&#8217;s used now, is just to primitive and cumbersome.</li>
<li>Lot&#8217;s of new languages for the JVM, but we&#8217;re still using SQL to retrieve data from databases. Many programmers may even think that is the only way to retrieve data!</li>
<li>ORM is worst of both world
<ul>
<li>Reinventing the wheel, caching, optimal data retrieval, query optimization</li>
<li>Relational model is more natural to retrieve data then object oriented model</li>
<li>Inheritence is not possible the way it&#8217;s possible in OO, but same functionality is <a href="http://fyi.oreilly.com/2009/02/the-relational-model-is-much-m.html">still possible</a>.</li>
</ul>
</li>
<li>Java Programmers are spending a LOT of time creating queries, doing optimization, thinking how to retrieve data. Java is improving, but SQL is just largely the same as it was 10 ago.</li>
<li>SQL / Database integration in Java is still poor. ORM frameworks just hide away the database &#8211; usually resulting in a lot of work for database administrators and programmer&#8217;s just to tweak ill formed sql.</li>
</ul>
<p>In short, where&#8217;s a new language for the relation model and why isn&#8217;t that language or technology emerging? What would be nice is:</p>
<ol>
<li>A better language to access a relation database, that doesn&#8217;t involve lots of subqueries and joins to retrieve data.</li>
<li>A database query langauge that is truly embedded in the programming language I daily work with (Java), and not hidden behind persistence managers, xml files,  application servers.  Or just as worse, SQL hidden in quoted string or text files.</li>
</ol>
<ul>
<li>For 1. After some searching, I found an <a href="http://developers.slashdot.org/article.pl?sid=04/10/12/2159209&amp;tid=221&amp;tid=218">alternative to SQL</a>, <a href="http://www.techworld.com/applications/features/index.cfm?featureid=910">Tutorial D</a>.  The language isn&#8217;t main stream, but is interesting to <a href="http://tech.inhelsinki.nl/2007-01-27/">read about</a>. Especially if you think SQL and (relational) databases are the same.</li>
<li>Also I heard a while ago about<a href="http://en.wikipedia.org/wiki/.QL"> .QL</a> on  the <a onclick="javascript:pageTracker._trackPageview('/outgoing/25jaar.cs.uu.nl/');" href="http://25jaar.cs.uu.nl/">25 year</a> anniversary of my university, but that&#8217;s far from mainstream either.</li>
<li>For .Net there&#8217;s <a href="http://www.infoq.com/interviews/erik-meijer-linq">LinQ</a>, but that&#8217;s little use for me as Java software developer.  At least Microsoft has a solution for 2.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.gerbrand-ict.nl/2009/05/alternative-to-sql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
