View Javadoc

1   /**
2    * Copyright 2009-2013 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.List;
19  import java.util.Properties;
20  
21  import org.apache.maven.plugin.AbstractMojo;
22  import org.apache.maven.plugin.MojoExecutionException;
23  import org.apache.maven.project.MavenProject;
24  import org.jasypt.util.text.TextEncryptor;
25  import org.kuali.common.util.EncUtils;
26  import org.kuali.common.util.PropertyUtils;
27  
28  /**
29   * Inspect project and system properties for any keys ending with <code>endsWith</code>. Any matching properties are assumed to be encrypted. They are decrypted and stored as
30   * project properties minus the <code>endsWith</code> suffix. For example, the value for the property "dba.password.encrypted" will be decrypted and stored as "dba.password"
31   * 
32   * @goal decryptall
33   */
34  public class DecryptAllPropertiesMojo extends AbstractMojo {
35  
36  	/**
37  	 * @parameter default-value="${project}"
38  	 * @required
39  	 * @readonly
40  	 */
41  	private MavenProject project;
42  
43  	/**
44  	 * The pattern for matching properties in need of decryption
45  	 * 
46  	 * @parameter expression="${properties.endsWith}" default-value=".encrypted"
47  	 * @required
48  	 */
49  	private String endsWith;
50  
51  	/**
52  	 * The password for decrypting property values. This same password must have been used to encrypt them.
53  	 * 
54  	 * @parameter expression="${properties.password}"
55  	 * @required
56  	 */
57  	private String password;
58  
59  	@Override
60  	public void execute() throws MojoExecutionException {
61  		TextEncryptor encryptor = EncUtils.getTextEncryptor(password);
62  		Properties props = PropertyUtils.getGlobalProperties(project.getProperties());
63  		List<String> keys = PropertyUtils.getEndsWithKeys(props, endsWith);
64  		for (String key : keys) {
65  			String value = props.getProperty(key);
66  			String decryptedValue = getDecryptedValue(encryptor, value);
67  			int length = endsWith.length();
68  			String newKey = key.substring(0, key.length() - length);
69  			project.getProperties().setProperty(newKey, decryptedValue);
70  		}
71  	}
72  
73  	protected String getDecryptedValue(TextEncryptor encryptor, String value) {
74  		if (PropertyUtils.isEncryptedPropertyValue(value)) {
75  			return PropertyUtils.decryptPropertyValue(encryptor, value);
76  		} else {
77  			return encryptor.decrypt(value);
78  		}
79  	}
80  
81  	public MavenProject getProject() {
82  		return project;
83  	}
84  
85  	public void setProject(MavenProject project) {
86  		this.project = project;
87  	}
88  
89  	public String getEndsWith() {
90  		return endsWith;
91  	}
92  
93  	public void setEndsWith(String endsWith) {
94  		this.endsWith = endsWith;
95  	}
96  
97  	public String getPassword() {
98  		return password;
99  	}
100 
101 	public void setPassword(String password) {
102 		this.password = password;
103 	}
104 }