001 /** 002 * Copyright 2009-2012 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.codehaus.mojo.properties; 017 018 import java.util.Properties; 019 020 import org.apache.commons.lang.StringUtils; 021 import org.apache.maven.plugin.AbstractMojo; 022 import org.apache.maven.plugin.MojoExecutionException; 023 import org.apache.maven.project.MavenProject; 024 import org.jasypt.util.text.BasicTextEncryptor; 025 026 /** 027 * Generate encrypted values for the specified system or project properties. 028 * 029 * @goal encrypt 030 */ 031 public class EncryptPropertiesMojo extends AbstractMojo { 032 033 /** 034 * @parameter default-value="${project}" 035 * @required 036 * @readonly 037 */ 038 private MavenProject project; 039 040 /** 041 * The list of properties containing values to encrypt 042 * 043 * @parameter 044 * @required 045 */ 046 private String[] properties; 047 048 /** 049 * The encrypted value for the properties are stored under their original property key with this text appended to 050 * the end. 051 * 052 * eg "database.password" is stored as "database.password.encrypted" 053 * 054 * @parameter expression="${properties.suffix}" default-value="encrypted" 055 * @required 056 */ 057 private String suffix; 058 059 /** 060 * If true, the plain text values being encrypted are displayed to the console. 061 * 062 * @parameter expression="${properties.show}" default-value="false" 063 * @required 064 */ 065 private boolean show; 066 067 /** 068 * If true, the plugin will emit no logging information 069 * 070 * @parameter expression="${properties.quiet}" default-value="false" 071 * @required 072 */ 073 private boolean quiet; 074 075 /** 076 * 077 * The password for encrypting property values. This same password can be used to to decrypt the encrypted values. 078 * 079 * @parameter expression="${properties.password}" 080 * @required 081 */ 082 private String password; 083 084 @Override 085 public void execute() throws MojoExecutionException { 086 BasicTextEncryptor encryptor = new BasicTextEncryptor(); 087 encryptor.setPassword(password); 088 Properties props = project.getProperties(); 089 for (String key : properties) { 090 String value = getProperty(key); 091 if (StringUtils.isBlank(value) && !quiet) { 092 getLog().info("Skipping " + key); 093 continue; 094 } 095 if (quiet) { 096 continue; 097 } 098 String newValue = encryptor.encrypt(value); 099 String newKey = key + "." + suffix; 100 props.setProperty(newKey, newValue); 101 if (show) { 102 getLog().info("Setting " + newKey + "=" + newValue + " - " + value); 103 } else { 104 getLog().info("Setting " + newKey + "=" + newValue); 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 120 public String[] getProperties() { 121 return properties; 122 } 123 124 public void setProperties(String[] properties) { 125 this.properties = properties; 126 } 127 128 public String getSuffix() { 129 return suffix; 130 } 131 132 public void setSuffix(String suffix) { 133 this.suffix = suffix; 134 } 135 136 public boolean isShow() { 137 return show; 138 } 139 140 public void setShow(boolean show) { 141 this.show = show; 142 } 143 144 public boolean isQuiet() { 145 return quiet; 146 } 147 148 public void setQuiet(boolean quiet) { 149 this.quiet = quiet; 150 } 151 152 public String getPassword() { 153 return password; 154 } 155 156 public void setPassword(String password) { 157 this.password = password; 158 } 159 160 public MavenProject getProject() { 161 return project; 162 } 163 }