001    /**
002     * Copyright 2009-2013 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.List;
019    import java.util.Properties;
020    
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.TextEncryptor;
025    import org.kuali.common.util.EncUtils;
026    import org.kuali.common.util.PropertyUtils;
027    
028    /**
029     * 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
030     * 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"
031     * 
032     * @goal decryptall
033     */
034    public class DecryptAllPropertiesMojo extends AbstractMojo {
035    
036            /**
037             * @parameter default-value="${project}"
038             * @required
039             * @readonly
040             */
041            private MavenProject project;
042    
043            /**
044             * The pattern for matching properties in need of decryption
045             * 
046             * @parameter expression="${properties.endsWith}" default-value=".encrypted"
047             * @required
048             */
049            private String endsWith;
050    
051            /**
052             * The password for decrypting property values. This same password must have been used to encrypt them.
053             * 
054             * @parameter expression="${properties.password}"
055             * @required
056             */
057            private String password;
058    
059            @Override
060            public void execute() throws MojoExecutionException {
061                    TextEncryptor encryptor = EncUtils.getTextEncryptor(password);
062                    Properties props = PropertyUtils.getGlobalProperties(project.getProperties());
063                    List<String> keys = PropertyUtils.getEndsWithKeys(props, endsWith);
064                    for (String key : keys) {
065                            String value = props.getProperty(key);
066                            String decryptedValue = getDecryptedValue(encryptor, value);
067                            int length = endsWith.length();
068                            String newKey = key.substring(0, key.length() - length);
069                            project.getProperties().setProperty(newKey, decryptedValue);
070                    }
071            }
072    
073            protected String getDecryptedValue(TextEncryptor encryptor, String value) {
074                    if (PropertyUtils.isEncryptedPropertyValue(value)) {
075                            return PropertyUtils.decryptPropertyValue(encryptor, value);
076                    } else {
077                            return encryptor.decrypt(value);
078                    }
079            }
080    
081            public MavenProject getProject() {
082                    return project;
083            }
084    
085            public void setProject(MavenProject project) {
086                    this.project = project;
087            }
088    
089            public String getEndsWith() {
090                    return endsWith;
091            }
092    
093            public void setEndsWith(String endsWith) {
094                    this.endsWith = endsWith;
095            }
096    
097            public String getPassword() {
098                    return password;
099            }
100    
101            public void setPassword(String password) {
102                    this.password = password;
103            }
104    }