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.Properties;
19
20 import org.apache.commons.lang.StringUtils;
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.BasicTextEncryptor;
25
26 /**
27 * Generate encrypted values for the specified system or project properties.
28 *
29 * @goal encrypt
30 */
31 public class EncryptPropertiesMojo extends AbstractMojo {
32
33 /**
34 * @parameter default-value="${project}"
35 * @required
36 * @readonly
37 */
38 private MavenProject project;
39
40 /**
41 * The list of properties containing values to encrypt
42 *
43 * @parameter
44 * @required
45 */
46 private String[] properties;
47
48 /**
49 * The encrypted value for the properties are stored under their original property key with this text appended to
50 * the end.
51 *
52 * eg "database.password" is stored as "database.password.encrypted"
53 *
54 * @parameter expression="${properties.suffix}" default-value="encrypted"
55 * @required
56 */
57 private String suffix;
58
59 /**
60 * If true, the plain text values being encrypted are displayed to the console.
61 *
62 * @parameter expression="${properties.show}" default-value="false"
63 * @required
64 */
65 private boolean show;
66
67 /**
68 *
69 * The password for encrypting property values. This same password can be used to to decrypt the encrypted values.
70 *
71 * @parameter expression="${properties.password}"
72 * @required
73 */
74 private String password;
75
76 @Override
77 public void execute() throws MojoExecutionException {
78 BasicTextEncryptor encryptor = new BasicTextEncryptor();
79 encryptor.setPassword(password);
80 Properties props = project.getProperties();
81 for (String key : properties) {
82 String value = getProperty(key);
83 if (StringUtils.isBlank(value)) {
84 getLog().info("Skipping " + key);
85 continue;
86 }
87 String newValue = encryptor.encrypt(value);
88 String newKey = key + "." + suffix;
89 props.setProperty(newKey, newValue);
90 if (show) {
91 getLog().info("Setting " + newKey + "=" + newValue + " - " + value);
92 } else {
93 getLog().info("Setting " + newKey + "=" + newValue);
94 }
95 }
96 }
97
98 protected String getProperty(String key) {
99 String sys = System.getProperty(key);
100 String proj = project.getProperties().getProperty(key);
101 if (!StringUtils.isBlank(sys)) {
102 return sys;
103 } else {
104 return proj;
105 }
106
107 }
108
109 public String[] getProperties() {
110 return properties;
111 }
112
113 public void setProperties(String[] properties) {
114 this.properties = properties;
115 }
116 }