Transparent Data Encryption for PostgreSQL Free Edition (TDE)を使ってみた!!

土曜日 , 6, 6月 2015 Leave a comment

Transparent Data Encryption for PostgreSQL Free Edition (TDE)をインストールしてみた!!の続きです。

JavaのJDBCとHibernateから呼び出してみました。(ユーザ名、パスワード、データベース名をすべて「postgres」にしてしまったので、非常にわかりづらいサンプルとなってしまいました…orz)

まず、JDBCから。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class JdbcTest {

	public static void main(String[] args) {
		try {
			JdbcTest.test();
		} catch(Exception e) {
			e.printStackTrace();;
		}
	}
	
	public static void test() throws Exception {
		
		Class.forName("org.postgresql.Driver");
		Connection conn = DriverManager.getConnection(
				"jdbc:postgresql://pgtde-server/postgres", "postgres", "postgres");
		
		Statement stmt = conn.createStatement();
		stmt.execute("SELECT pgtde_begin_session('pg')");
		
		ResultSet rs = stmt.executeQuery("SELECT * FROM foo");
		while (rs.next()) {
			System.out.println(rs.getString(1));
		}
		
		stmt.execute("SELECT pgtde_end_session()");
		
		rs.close();
		stmt.close();
	}
}

事前に、pgtde_begin_sessionを呼び出すだけで問題なくいけそうです。

次に、Hibernate。

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

import com.ria_lab.hb.Foo;

public class HbTest {

	public static void main(String[] args) {
		try {
			HbTest.test();
		} catch(Exception e) {
			e.printStackTrace();;
		}
	}
	
	public static void test() throws Exception {
		
		Configuration configuration = new Configuration();
		configuration.configure();
		
		StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().
		applySettings(configuration.getProperties());
		SessionFactory sf = configuration.buildSessionFactory(builder.build());
					
		Session session = sf.openSession();
		
		session.createSQLQuery("SELECT pgtde_begin_session('pg')").list();
		
		Criteria criteria =  session.createCriteria(Foo.class);
		Listl = criteria.list();
		for (Foo e:l) {
			System.out.println(e.getId().getPlain() + "/" + e.getId().getEncrypt());
		}
		
		session.createSQLQuery("SELECT pgtde_end_session()").list();
	}
}




    
        
            
                
            
            
                
            
        
    

package com.ria_lab.hb;

// Generated 2015/06/06 21:13:30 by Hibernate Tools 3.4.0.CR1

/**
 * Foo generated by hbm2java
 */
public class Foo implements java.io.Serializable {

	private FooId id;

	public Foo() {
	}

	public Foo(FooId id) {
		this.id = id;
	}

	public FooId getId() {
		return this.id;
	}

	public void setId(FooId id) {
		this.id = id;
	}

}
package com.ria_lab.hb;

// Generated 2015/06/06 21:13:30 by Hibernate Tools 3.4.0.CR1

import java.io.Serializable;

/**
 * FooId generated by hbm2java
 */
public class FooId implements java.io.Serializable {

	private String plain;
	private Serializable encrypt;

	public FooId() {
	}

	public FooId(String plain, Serializable encrypt) {
		this.plain = plain;
		this.encrypt = encrypt;
	}

	public String getPlain() {
		return this.plain;
	}

	public void setPlain(String plain) {
		this.plain = plain;
	}

	public Serializable getEncrypt() {
		return this.encrypt;
	}

	public void setEncrypt(Serializable encrypt) {
		this.encrypt = encrypt;
	}

	public boolean equals(Object other) {
		if ((this == other))
			return true;
		if ((other == null))
			return false;
		if (!(other instanceof FooId))
			return false;
		FooId castOther = (FooId) other;

		return ((this.getPlain() == castOther.getPlain()) || (this.getPlain() != null
				&& castOther.getPlain() != null && this.getPlain().equals(
				castOther.getPlain())))
				&& ((this.getEncrypt() == castOther.getEncrypt()) || (this
						.getEncrypt() != null && castOther.getEncrypt() != null && this
						.getEncrypt().equals(castOther.getEncrypt())));
	}

	public int hashCode() {
		int result = 17;

		result = 37 * result
				+ (getPlain() == null ? 0 : this.getPlain().hashCode());
		result = 37 * result
				+ (getEncrypt() == null ? 0 : this.getEncrypt().hashCode());
		return result;
	}

}

キーなしでテーブルを作ってしまったので、自動生成(EclipseのHibenate Toolsプラグインを使用)させたコードは、Foo.javaとFooId.javaに分かれる変則的な構造になっています。

こちらも処理の前に、ネイティブSQLでpgtde_begin_sessionを呼び出せば問題なくいけそうな雰囲気です。
Hibernate Toolsプラグインで自動生成すると、TDEで暗号化したフィールドの型がserializableとなってしまうので、Stringに変換しておいた方が事後の処理を簡略化できそうです。

コネクションプールを使った場合など、実務で使うにはまだ検証作業が必要ですが、ひとまず。

Please give us your valuable comment

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください