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   * For example, the value for the property "dba.password.encrypted" will be decrypted and stored as "dba.password"
33   *
34   * @goal decryptall
35   */
36  public class DecryptAllPropertiesMojo extends AbstractMojo {
37  
38      /**
39       * @parameter default-value="${project}"
40       * @required
41       * @readonly
42       */
43      private MavenProject project;
44  
45      /**
46       * If true, the plugin will emit no logging information
47       *
48       * @parameter expression="${properties.quiet}" default-value="false"
49       * @required
50       */
51      private boolean quiet;
52  
53      /**
54       * The pattern for matching properties in need of decryption
55       *
56       * @parameter expression="${properties.endsWith}" default-value=".encrypted"
57       * @required
58       */
59      private String endsWith;
60  
61      /**
62       * If true the plain text decrypted values are displayed to the console.
63       *
64       * @parameter expression="${properties.show}" default-value="false"
65       * @required
66       */
67      private boolean show;
68  
69      /**
70       * The password for decrypting property values. This same password must have been used to encrypt them.
71       *
72       * @parameter expression="${properties.password}"
73       * @required
74       */
75      private String password;
76  
77      @Override
78      public void execute() throws MojoExecutionException {
79          BasicTextEncryptor encryptor = new BasicTextEncryptor();
80          encryptor.setPassword(password);
81          Properties props = project.getProperties();
82          List<String> keys = new ArrayList<String>(props.stringPropertyNames());
83          Collections.sort(keys);
84          for (String key : keys) {
85              boolean decrypt = key.endsWith(endsWith);
86              if (!decrypt) {
87                  continue;
88              }
89              String value = getProperty(key);
90              if (StringUtils.isBlank(value) && !quiet) {
91                  getLog().info("Skipping blank property " + key);
92                  continue;
93              }
94              String newValue = encryptor.decrypt(value);
95              int length = endsWith.length();
96              String newKey = key.substring(0, key.length() - length);
97              props.setProperty(newKey, newValue);
98              if (quiet) {
99                  continue;
100             }
101             if (show) {
102                 getLog().info("Setting " + newKey + "=" + newValue + " - " + value);
103             } else {
104                 getLog().info("Setting " + newKey);
105             }
106         }
107     }
108 
109     protected String getProperty(String key) {
110         String sys = System.getProperty(key);
111         String proj = project.getProperties().getProperty(key);
112         if (!StringUtils.isBlank(sys)) {
113             return sys;
114         } else {
115             return proj;
116         }
117     }
118 
119     public boolean isQuiet() {
120         return quiet;
121     }
122 
123     public void setQuiet(boolean quiet) {
124         this.quiet = quiet;
125     }
126 
127     public String getEndsWith() {
128         return endsWith;
129     }
130 
131     public void setEndsWith(String endsWith) {
132         this.endsWith = endsWith;
133     }
134 
135     public boolean isShow() {
136         return show;
137     }
138 
139     public void setShow(boolean show) {
140         this.show = show;
141     }
142 
143     public String getPassword() {
144         return password;
145     }
146 
147     public void setPassword(String password) {
148         this.password = password;
149     }
150 
151     public MavenProject getProject() {
152         return project;
153     }
154 }