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.Arrays;
019    import java.util.List;
020    import java.util.Properties;
021    
022    import org.apache.maven.plugin.AbstractMojo;
023    import org.apache.maven.plugin.MojoExecutionException;
024    import org.apache.maven.project.MavenProject;
025    import org.jasypt.util.text.BasicTextEncryptor;
026    import org.kuali.common.util.PropertyUtils;
027    
028    /**
029     * Generate encrypted values for the specified system or project properties.
030     * 
031     * @goal encrypt
032     */
033    public class EncryptPropertiesMojo extends AbstractMojo {
034    
035            /**
036             * @parameter default-value="${project}"
037             * @required
038             * @readonly
039             */
040            private MavenProject project;
041    
042            /**
043             * The list of properties containing values to encrypt
044             * 
045             * @parameter
046             * @required
047             */
048            private String[] properties;
049    
050            /**
051             * 
052             * The password for encrypting property values. This same password can be used to to decrypt the encrypted values.
053             * 
054             * @parameter expression="${properties.password}"
055             * @required
056             */
057            private String password;
058    
059            @Override
060            public void execute() throws MojoExecutionException {
061    
062                    // Setup the encryptor
063                    BasicTextEncryptor encryptor = new BasicTextEncryptor();
064                    encryptor.setPassword(password);
065    
066                    // Get project properties overridden by system/env properties
067                    Properties p = PropertyUtils.getGlobalProperties(project.getProperties());
068    
069                    // Set up the includes list
070                    List<String> includes = PropertyUtils.getSortedKeys(project.getProperties());
071                    if (properties != null) {
072                            includes = Arrays.asList(properties);
073                    }
074    
075                    // Trim things down to just the properties we want
076                    PropertyUtils.trim(p, includes, null);
077    
078                    // Encrypt the property values
079                    PropertyUtils.encrypt(p, encryptor);
080    
081                    // Update Maven with the encrypted values
082                    project.getProperties().putAll(p);
083            }
084    
085            public String[] getProperties() {
086                    return properties;
087            }
088    
089            public void setProperties(String[] properties) {
090                    this.properties = properties;
091            }
092    
093            public String getPassword() {
094                    return password;
095            }
096    
097            public void setPassword(String password) {
098                    this.password = password;
099            }
100    
101            public MavenProject getProject() {
102                    return project;
103            }
104    }