Hibernate envers auditing for tables with encrypted column

Posted on

We have implemented Hibernate envers audit (@audited) for one of our table and everything is working fine as expected . But in the table we are encrypting some fields using symmetric key encryption logic and in our entity we have included @column transfer for read and write operations .

Here whenever we are performing any CRUD operation first we will write a native query to open symmetric key and once CRUD operation is done we will close it . If symmetric key is not opened then the value will be null. In our Audit table every value is audited except those columns which are encrypted .

So the root cause is when hibernate creates a query for inserting into audit table it is not opening the symmetric key and hence value is getting stored as null.

In short is there any way to open symmetric key before hibernate executes any insertion into audit table and once insertion is done key needs to be closed

We are using hibernate envers version 5.0.12 with spring JPA

@Entity
@Table(name = "CUSTOMER")
@Audited
public class Customer {
@Id
@GeneratedValue
@Column(name = "CUSTOMER_ID")
private long customerId;

@Column(name = "USERNAME")
private String userName;


@Column(name = "FIRST_NAME")
@ColumnTransformer(
        read = "CONVERT(VARCHAR(50), DecryptByKey(FIRST_NAME))",
        write = "EncryptByKey (Key_GUID('DBSymKey'), ?)")
private byte[] firstName;

@Column(name = "LAST_NAME")
@ColumnTransformer(
        read = "CONVERT(VARCHAR(50), DecryptByKey(LAST_NAME))",
        write = "EncryptByKey (Key_GUID('DBSymKey'), ?)")
private byte[] lastName;

}

Close

Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.