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