At present i am keeping the password [ unencrypted ] in a property file. This password get placed as is in the configuration xml using ant.
[ The configuration xml is for datasource, it is creating the object of dbcp.BasicDataSource ]
Now, is it possible that after the ant target the password is copied in encrypted form. Heard the Jasypt can do that! Till now i haven't tried this. But, the problem doesn't end here. BasicDataSource do not accept encrypted password. Is there any replacement for BasicDatasource.
FYI: I am using Spring, if that matters.
Extend BasicDataSource, override setPassword and setUserName methods. Decrypt the values in those methods and pass them to super class methods.
The following jasypt link explains how a properties file containing encrypted content can be read from within your application:
http://www.jasypt.org/encrypting-configuration.html
To create the properties file from within ANT my suggestion is to use the groovy task as follows:
Not entirely true in the case of
BasicDataSource
.If you read the javadocs for
BasicDataSource
,setPassword()
has no effect once the pool has been initialized. The pool is initialized the first time one of the following methods is invoked:getConnection
,setLogwriter
,setLoginTimeout
,getLoginTimeout
,getLogWriter
.Ref: http://www.docjar.com/html/api/org/apache/commons/dbcp/BasicDataSource.java.html
All these methods call
createDataSource()
eventually.So your new BasicDataSource class only needs to override the method
createDataSource()
Something like this:Create a new task by extending existing task
Copy
( responsible for file-copy ). Create a new type by extendingFilterSet
( responsible for filtering of tokens ).see the code here:- How to create nested element for ant task?
build.xml
blah-blah.properties
configuration xml
After the execution of the target the xml is copied with values from properties file. Password will be encrypted.
This will handle the encrypted password. EncryptionAwareDataSource
That' all ;)
With Spring there is a better way: use the PropertyPlaceholderConfigurer class.
When you specify a subclass of PropertiesPersister in the property placeholder, Spring load the
jdbc.properties
and decrypt the file using that class. Maybe something like:Hope it helps.
EDIT If you use Jasypt, you do not need to define any
PropertiesPersister
. From the Jasypt documentation:With this, you can define
jdbc.properties
like thisand the Spring config may be like this
This way, you can put the password for decrypt the hidden property in an environment variable when you start the application and unset it later.