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.ArrayList; 019 import java.util.Collections; 020 import java.util.List; 021 import java.util.Properties; 022 023 import org.apache.commons.lang.StringUtils; 024 import org.apache.maven.plugin.AbstractMojo; 025 import org.apache.maven.plugin.MojoExecutionException; 026 import org.apache.maven.project.MavenProject; 027 import org.jasypt.util.text.BasicTextEncryptor; 028 029 /** 030 * Inspect project and system properties for any keys ending with <code>endsWith</code>. Any matching properties are 031 * assumed to be encrypted. They are decrypted and stored as project properties minus the <code>endsWith</code> suffix. 032 * For example, the value for the property "dba.password.encrypted" will be decrypted and stored as "dba.password" 033 * 034 * @goal decryptall 035 */ 036 public class DecryptAllPropertiesMojo extends AbstractMojo { 037 038 /** 039 * @parameter default-value="${project}" 040 * @required 041 * @readonly 042 */ 043 private MavenProject project; 044 045 /** 046 * If true, the plugin will emit no logging information 047 * 048 * @parameter expression="${properties.quiet}" default-value="false" 049 * @required 050 */ 051 private boolean quiet; 052 053 /** 054 * The pattern for matching properties in need of decryption 055 * 056 * @parameter expression="${properties.endsWith}" default-value=".encrypted" 057 * @required 058 */ 059 private String endsWith; 060 061 /** 062 * If true the plain text decrypted values are displayed to the console. 063 * 064 * @parameter expression="${properties.show}" default-value="false" 065 * @required 066 */ 067 private boolean show; 068 069 /** 070 * The password for decrypting property values. This same password must have been used to encrypt them. 071 * 072 * @parameter expression="${properties.password}" 073 * @required 074 */ 075 private String password; 076 077 @Override 078 public void execute() throws MojoExecutionException { 079 BasicTextEncryptor encryptor = new BasicTextEncryptor(); 080 encryptor.setPassword(password); 081 Properties props = project.getProperties(); 082 List<String> keys = new ArrayList<String>(props.stringPropertyNames()); 083 Collections.sort(keys); 084 for (String key : keys) { 085 boolean decrypt = key.endsWith(endsWith); 086 if (!decrypt) { 087 continue; 088 } 089 String value = getProperty(key); 090 if (StringUtils.isBlank(value) && !quiet) { 091 getLog().info("Skipping blank property " + key); 092 continue; 093 } 094 String newValue = encryptor.decrypt(value); 095 int length = endsWith.length(); 096 String newKey = key.substring(0, key.length() - length); 097 props.setProperty(newKey, newValue); 098 if (quiet) { 099 continue; 100 } 101 if (show) { 102 getLog().info("Setting " + newKey + "=" + newValue + " - " + value); 103 } else { 104 getLog().info("Setting " + newKey); 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 public boolean isQuiet() { 120 return quiet; 121 } 122 123 public void setQuiet(boolean quiet) { 124 this.quiet = quiet; 125 } 126 127 public String getEndsWith() { 128 return endsWith; 129 } 130 131 public void setEndsWith(String endsWith) { 132 this.endsWith = endsWith; 133 } 134 135 public boolean isShow() { 136 return show; 137 } 138 139 public void setShow(boolean show) { 140 this.show = show; 141 } 142 143 public String getPassword() { 144 return password; 145 } 146 147 public void setPassword(String password) { 148 this.password = password; 149 } 150 151 public MavenProject getProject() { 152 return project; 153 } 154 }