View Javadoc

1   /**
2    * Copyright 2009-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.codehaus.mojo.properties;
17  
18  import java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.List;
21  import java.util.Properties;
22  
23  import org.apache.commons.lang.StringUtils;
24  import org.apache.maven.plugin.AbstractMojo;
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.apache.maven.project.MavenProject;
27  import org.jasypt.util.text.BasicTextEncryptor;
28  
29  /**
30   * Inspect project and system properties for any keys ending with <code>endsWith</code>. Any matching properties are
31   * assumed to be encrypted. They are decrypted and stored as project properties minus the <code>endsWith</code> suffix.
32   *
33   * @goal decryptall
34   */
35  public class DecryptAllPropertiesMojo extends AbstractMojo {
36  
37      /**
38       * @parameter default-value="${project}"
39       * @required
40       * @readonly
41       */
42      private MavenProject project;
43  
44      /**
45       * The pattern for matching properties in need of decryption
46       *
47       * @parameter expression="${properties.endsWith}" default-value=".encrypted"
48       * @required
49       */
50      private String endsWith;
51  
52      /**
53       * If true the plain text decrypted values are displayed to the console.
54       *
55       * @parameter expression="${properties.show}" default-value="false"
56       * @required
57       */
58      private boolean show;
59  
60      /**
61       * The password for decrypting property values. This same password must have been used to encrypt them.
62       *
63       * @parameter expression="${properties.password}"
64       * @required
65       */
66      private String password;
67  
68      @Override
69      public void execute() throws MojoExecutionException {
70          BasicTextEncryptor encryptor = new BasicTextEncryptor();
71          encryptor.setPassword(password);
72          Properties props = project.getProperties();
73          List<String> keys = new ArrayList<String>(props.stringPropertyNames());
74          Collections.sort(keys);
75          for (String key : keys) {
76              boolean decrypt = key.endsWith(endsWith);
77              if (!decrypt) {
78                  continue;
79              }
80              String value = getProperty(key);
81              if (StringUtils.isBlank(value)) {
82                  getLog().info("Skipping blank property " + key);
83                  continue;
84              }
85              String newValue = encryptor.decrypt(value);
86              int length = endsWith.length();
87              String newKey = key.substring(0, key.length() - length);
88              props.setProperty(newKey, newValue);
89              if (show) {
90                  getLog().info("Setting " + newKey + "=" + newValue + " - " + value);
91              } else {
92                  getLog().info("Setting " + newKey);
93              }
94          }
95      }
96  
97      protected String getProperty(String key) {
98          String sys = System.getProperty(key);
99          String proj = project.getProperties().getProperty(key);
100         if (!StringUtils.isBlank(sys)) {
101             return sys;
102         } else {
103             return proj;
104         }
105     }
106 }