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 * 033 * @goal decryptall 034 */ 035 public class DecryptAllPropertiesMojo extends AbstractMojo { 036 037 /** 038 * @parameter default-value="${project}" 039 * @required 040 * @readonly 041 */ 042 private MavenProject project; 043 044 /** 045 * If true, the plugin will emit no logging information 046 * 047 * @parameter expression="${properties.quiet}" default-value="false" 048 * @required 049 */ 050 private boolean quiet; 051 052 /** 053 * The pattern for matching properties in need of decryption 054 * 055 * @parameter expression="${properties.endsWith}" default-value=".encrypted" 056 * @required 057 */ 058 private String endsWith; 059 060 /** 061 * If true the plain text decrypted values are displayed to the console. 062 * 063 * @parameter expression="${properties.show}" default-value="false" 064 * @required 065 */ 066 private boolean show; 067 068 /** 069 * The password for decrypting property values. This same password must have been used to encrypt them. 070 * 071 * @parameter expression="${properties.password}" 072 * @required 073 */ 074 private String password; 075 076 @Override 077 public void execute() throws MojoExecutionException { 078 BasicTextEncryptor encryptor = new BasicTextEncryptor(); 079 encryptor.setPassword(password); 080 Properties props = project.getProperties(); 081 List<String> keys = new ArrayList<String>(props.stringPropertyNames()); 082 Collections.sort(keys); 083 for (String key : keys) { 084 boolean decrypt = key.endsWith(endsWith); 085 if (!decrypt) { 086 continue; 087 } 088 String value = getProperty(key); 089 if (StringUtils.isBlank(value) && !quiet) { 090 getLog().info("Skipping blank property " + key); 091 continue; 092 } 093 String newValue = encryptor.decrypt(value); 094 int length = endsWith.length(); 095 String newKey = key.substring(0, key.length() - length); 096 props.setProperty(newKey, newValue); 097 if (quiet) { 098 continue; 099 } 100 if (show) { 101 getLog().info("Setting " + newKey + "=" + newValue + " - " + value); 102 } else { 103 getLog().info("Setting " + newKey); 104 } 105 } 106 } 107 108 protected String getProperty(String key) { 109 String sys = System.getProperty(key); 110 String proj = project.getProperties().getProperty(key); 111 if (!StringUtils.isBlank(sys)) { 112 return sys; 113 } else { 114 return proj; 115 } 116 } 117 118 public boolean isQuiet() { 119 return quiet; 120 } 121 122 public void setQuiet(boolean quiet) { 123 this.quiet = quiet; 124 } 125 126 public String getEndsWith() { 127 return endsWith; 128 } 129 130 public void setEndsWith(String endsWith) { 131 this.endsWith = endsWith; 132 } 133 134 public boolean isShow() { 135 return show; 136 } 137 138 public void setShow(boolean show) { 139 this.show = show; 140 } 141 142 public String getPassword() { 143 return password; 144 } 145 146 public void setPassword(String password) { 147 this.password = password; 148 } 149 150 public MavenProject getProject() { 151 return project; 152 } 153 }