When developing enterprise applications it is best practice check in the source code into a version control system. Additionally to the code also configuration settings and credentials are often checked in.
This is usually no problem for test environments, but configurations for the production environments perhaps should not be readable for everyone having access to the version control system.
This can be solved using some type of access control on the version control system.
Or the credentials can be entered as parameters during the build or deployment for production. That is only a feasible solution for one or two credentials.
An other solution is to encrypt the confidential data and decrypt it with a master password. This solution is supported by the java library jasypt.
Credits for the basic implementation with ant goes to my former colleague Mr. Wiedemann. I have adapted the solution for the use with Maven.
Jasypt doesn’t provide a Maven plugin. That’s why we need some type of glue code to use the jasypt library. We use the language groovy to write the glue code. For Maven there exists a plugin to execute groovy code in an easy way. The final solutions looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
<div><!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><span style="color: #008080;"> 1</span> <span style="color: #000000;"> </span><span style="color: #0000FF;"><</span><span style="color: #800000;">plugin</span><span style="color: #0000FF;">></span><span style="color: #000000;"> </span><span style="color: #008080;"> 2</span> <span style="color: #000000;"> </span><span style="color: #0000FF;"><</span><span style="color: #800000;">groupId</span><span style="color: #0000FF;">></span><span style="color: #000000;">org.codehaus.gmaven</span><span style="color: #0000FF;"></</span><span style="color: #800000;">groupId</span><span style="color: #0000FF;">></span><span style="color: #000000;"> </span><span style="color: #008080;"> 3</span> <span style="color: #000000;"> </span><span style="color: #0000FF;"><</span><span style="color: #800000;">artifactId</span><span style="color: #0000FF;">></span><span style="color: #000000;">groovy-maven-plugin</span><span style="color: #0000FF;"></</span><span style="color: #800000;">artifactId</span><span style="color: #0000FF;">></span><span style="color: #000000;"> </span><span style="color: #008080;"> 4</span> <span style="color: #000000;"> </span><span style="color: #0000FF;"><</span><span style="color: #800000;">executions</span><span style="color: #0000FF;">></span><span style="color: #000000;"> </span><span style="color: #008080;"> 5</span> <span style="color: #000000;"> </span><span style="color: #0000FF;"><</span><span style="color: #800000;">execution</span><span style="color: #0000FF;">></span><span style="color: #000000;"> </span><span style="color: #008080;"> 6</span> <span style="color: #000000;"> </span><span style="color: #0000FF;"><</span><span style="color: #800000;">id</span><span style="color: #0000FF;">></span><span style="color: #000000;">decrypt-passwords</span><span style="color: #0000FF;"></</span><span style="color: #800000;">id</span><span style="color: #0000FF;">></span><span style="color: #000000;"> </span><span style="color: #008080;"> 7</span> <span style="color: #000000;"> </span><span style="color: #0000FF;"><</span><span style="color: #800000;">phase</span><span style="color: #0000FF;">></span><span style="color: #000000;">validate</span><span style="color: #0000FF;"></</span><span style="color: #800000;">phase</span><span style="color: #0000FF;">></span><span style="color: #000000;"> </span><span style="color: #008080;"> 8</span> <span style="color: #000000;"> </span><span style="color: #0000FF;"><</span><span style="color: #800000;">goals</span><span style="color: #0000FF;">></span><span style="color: #000000;"> </span><span style="color: #008080;"> 9</span> <span style="color: #000000;"> </span><span style="color: #0000FF;"><</span><span style="color: #800000;">goal</span><span style="color: #0000FF;">></span><span style="color: #000000;">execute</span><span style="color: #0000FF;"></</span><span style="color: #800000;">goal</span><span style="color: #0000FF;">></span><span style="color: #000000;"> </span><span style="color: #008080;">10</span> <span style="color: #000000;"> </span><span style="color: #0000FF;"></</span><span style="color: #800000;">goals</span><span style="color: #0000FF;">></span><span style="color: #000000;"> </span><span style="color: #008080;">11</span> <span style="color: #000000;"> </span><span style="color: #0000FF;"><</span><span style="color: #800000;">configuration</span><span style="color: #0000FF;">></span><span style="color: #000000;"> </span><span style="color: #008080;">12</span> <span style="color: #000000;"> </span><span style="color: #0000FF;"><</span><span style="color: #800000;">source</span><span style="color: #0000FF;">></span><span style="color: #000000;"> </span><span style="color: #008080;">13</span> <span style="color: #000000;"> import org.jasypt.properties.EncryptableProperties </span><span style="color: #008080;">14</span> <span style="color: #000000;"> import org.jasypt.encryption.pbe.StandardPBEStringEncryptor </span><span style="color: #008080;">15</span> <span style="color: #000000;"> </span><span style="color: #008080;">16</span> <span style="color: #000000;"> if(properties["jasypt.encryption.password"]==null){ </span><span style="color: #008080;">17</span> <span style="color: #000000;"> ant.echo "[INFO] jasypt.encryption.password not set, passwords will not be decrypted." </span><span style="color: #008080;">18</span> <span style="color: #000000;"> return </span><span style="color: #008080;">19</span> <span style="color: #000000;"> } </span><span style="color: #008080;">20</span> <span style="color: #000000;"> </span><span style="color: #008080;">21</span> <span style="color: #000000;"> StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor() </span><span style="color: #008080;">22</span> <span style="color: #000000;"> encryptor.setPassword(properties["jasypt.encryption.password"]) </span><span style="color: #008080;">23</span> <span style="color: #000000;"> </span><span style="color: #008080;">24</span> <span style="color: #000000;"> Properties props = new EncryptableProperties((Properties)properties, encryptor); </span><span style="color: #008080;">25</span> <span style="color: #000000;"> </span><span style="color: #008080;">26</span> <span style="color: #000000;"> props.propertyNames().each { </span><span style="color: #008080;">27</span> <span style="color: #000000;"> if(!it.startsWith("resolved")){ </span><span style="color: #008080;">28</span> <span style="color: #000000;"> project.properties[it] = props.getProperty(it) </span><span style="color: #008080;">29</span> <span style="color: #000000;"> } </span><span style="color: #008080;">30</span> <span style="color: #000000;"> } </span><span style="color: #008080;">31</span> <span style="color: #000000;"> </span><span style="color: #0000FF;"></</span><span style="color: #800000;">source</span><span style="color: #0000FF;">></span><span style="color: #000000;"> </span><span style="color: #008080;">32</span> <span style="color: #000000;"> </span><span style="color: #0000FF;"></</span><span style="color: #800000;">configuration</span><span style="color: #0000FF;">></span><span style="color: #000000;"> </span><span style="color: #008080;">33</span> <span style="color: #000000;"> </span><span style="color: #0000FF;"></</span><span style="color: #800000;">execution</span><span style="color: #0000FF;">></span><span style="color: #000000;"> </span><span style="color: #008080;">34</span> <span style="color: #000000;"> </span><span style="color: #0000FF;"></</span><span style="color: #800000;">executions</span><span style="color: #0000FF;">></span><span style="color: #000000;"> </span><span style="color: #008080;">35</span> <span style="color: #000000;"> </span><span style="color: #0000FF;"><</span><span style="color: #800000;">dependencies</span><span style="color: #0000FF;">></span><span style="color: #000000;"> </span><span style="color: #008080;">36</span> <span style="color: #000000;"> </span><span style="color: #0000FF;"><</span><span style="color: #800000;">dependency</span><span style="color: #0000FF;">></span><span style="color: #000000;"> </span><span style="color: #008080;">37</span> <span style="color: #000000;"> </span><span style="color: #0000FF;"><</span><span style="color: #800000;">groupId</span><span style="color: #0000FF;">></span><span style="color: #000000;">org.jasypt</span><span style="color: #0000FF;"></</span><span style="color: #800000;">groupId</span><span style="color: #0000FF;">></span><span style="color: #000000;"> </span><span style="color: #008080;">38</span> <span style="color: #000000;"> </span><span style="color: #0000FF;"><</span><span style="color: #800000;">artifactId</span><span style="color: #0000FF;">></span><span style="color: #000000;">jasypt</span><span style="color: #0000FF;"></</span><span style="color: #800000;">artifactId</span><span style="color: #0000FF;">></span><span style="color: #000000;"> </span><span style="color: #008080;">39</span> <span style="color: #000000;"> </span><span style="color: #0000FF;"><</span><span style="color: #800000;">version</span><span style="color: #0000FF;">></span><span style="color: #000000;">1.9.2</span><span style="color: #0000FF;"></</span><span style="color: #800000;">version</span><span style="color: #0000FF;">></span><span style="color: #000000;"> </span><span style="color: #008080;">40</span> <span style="color: #000000;"> </span><span style="color: #0000FF;"></</span><span style="color: #800000;">dependency</span><span style="color: #0000FF;">></span><span style="color: #000000;"> </span><span style="color: #008080;">41</span> <span style="color: #000000;"> </span><span style="color: #0000FF;"></</span><span style="color: #800000;">dependencies</span><span style="color: #0000FF;">></span><span style="color: #000000;"> </span><span style="color: #008080;">42</span> <span style="color: #000000;"> </span><span style="color: #0000FF;"></</span><span style="color: #800000;">plugin</span><span style="color: #0000FF;">></span></div> |
If the property jasypt.encryption.password is not set the plugin prints an info message and does nothing. Otherwise the plugin decrypts every encrypted property and replaces the value with the decrypted value. The encryption of the value can be done with the command line. The encrypted value has to be embedded into ENC(). Finally the property looks like this:
1 2 3 4 5 6 |
<div><!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><span style="color: #008080;">1</span> <span style="color: #000000;">admin.password</span><span style="color: #000000;">=</span><span style="color: #000000;">ENC(RJJ37UPsM2CstIQPohskn7Yn05gtrIK0)</span></div> |
At the dependencies section we add the library of jasypt. The plugin should run during the validate phase.
Bernhard Mähr @ OPITZ-CONSULTING published at http://thecattlecrew.wordpress.com/