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 include system properties when decrypting properties
47       * 
48       * @parameter default-value="false" expression="${properties.includeSystemProperties}"
49       */
50      private boolean includeSystemProperties;
51  
52      /**
53       * If true, the plugin will include environment variables when decrypting properties.
54       * 
55       * @parameter default-value="false" expression="${properties.includeEnvironmentVariables}"
56       */
57      private boolean includeEnvironmentVariables;
58  
59      /**
60       * If true, the plugin will emit no logging information
61       * 
62       * @parameter expression="${properties.quiet}" default-value="false"
63       * @required
64       */
65      private boolean quiet;
66  
67      /**
68       * The pattern for matching properties in need of decryption
69       * 
70       * @parameter expression="${properties.endsWith}" default-value=".encrypted"
71       * @required
72       */
73      private String endsWith;
74  
75      /**
76       * If true the plain text decrypted values are displayed to the console.
77       * 
78       * @parameter expression="${properties.show}" default-value="false"
79       * @required
80       */
81      private boolean show;
82  
83      /**
84       * The password for decrypting property values. This same password must have been used to encrypt them.
85       * 
86       * @parameter expression="${properties.password}"
87       * @required
88       */
89      private String password;
90  
91      @Override
92      public void execute() throws MojoExecutionException {
93          BasicTextEncryptor encryptor = new BasicTextEncryptor();
94          encryptor.setPassword(password);
95          Properties props = new Properties();
96          props.putAll(project.getProperties());
97          if (includeEnvironmentVariables) {
98              props.putAll(WriteProjectProperties.getEnvironmentVariables());
99          }
100         if (includeSystemProperties) {
101             props.putAll(System.getProperties());
102         }
103         List<String> keys = new ArrayList<String>(props.stringPropertyNames());
104         Collections.sort(keys);
105         for (String key : keys) {
106             boolean decrypt = key.endsWith(endsWith);
107             if (!decrypt) {
108                 continue;
109             }
110             String value = getProperty(key);
111             if (StringUtils.isBlank(value) && !quiet) {
112                 getLog().info("Skipping blank property " + key);
113                 continue;
114             }
115             String newValue = encryptor.decrypt(value);
116             int length = endsWith.length();
117             String newKey = key.substring(0, key.length() - length);
118             project.getProperties().setProperty(newKey, newValue);
119             if (quiet) {
120                 continue;
121             }
122             if (show) {
123                 getLog().info("Setting " + newKey + "=" + newValue + " - " + value);
124             } else {
125                 getLog().info("Setting " + newKey);
126             }
127         }
128     }
129 
130     protected String getProperty(String key) {
131         String sys = System.getProperty(key);
132         String proj = project.getProperties().getProperty(key);
133         if (!StringUtils.isBlank(sys)) {
134             return sys;
135         } else {
136             return proj;
137         }
138     }
139 
140     public boolean isQuiet() {
141         return quiet;
142     }
143 
144     public void setQuiet(boolean quiet) {
145         this.quiet = quiet;
146     }
147 
148     public String getEndsWith() {
149         return endsWith;
150     }
151 
152     public void setEndsWith(String endsWith) {
153         this.endsWith = endsWith;
154     }
155 
156     public boolean isShow() {
157         return show;
158     }
159 
160     public void setShow(boolean show) {
161         this.show = show;
162     }
163 
164     public String getPassword() {
165         return password;
166     }
167 
168     public void setPassword(String password) {
169         this.password = password;
170     }
171 
172     public MavenProject getProject() {
173         return project;
174     }
175 
176     public boolean isIncludeSystemProperties() {
177         return includeSystemProperties;
178     }
179 
180     public void setIncludeSystemProperties(boolean includeSystemProperties) {
181         this.includeSystemProperties = includeSystemProperties;
182     }
183 
184     public boolean isIncludeEnvironmentVariables() {
185         return includeEnvironmentVariables;
186     }
187 
188     public void setIncludeEnvironmentVariables(boolean includeEnvironmentVariables) {
189         this.includeEnvironmentVariables = includeEnvironmentVariables;
190     }
191 }