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         *
069         * The password for encrypting property values. This same password can be used to to decrypt the encrypted values.
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            for (String key : properties) {
082                String value = getProperty(key);
083                if (StringUtils.isBlank(value)) {
084                    getLog().info("Skipping " + key);
085                    continue;
086                }
087                String newValue = encryptor.encrypt(value);
088                String newKey = key + "." + suffix;
089                props.setProperty(newKey, newValue);
090                if (show) {
091                    getLog().info("Setting " + newKey + "=" + newValue + " - " + value);
092                } else {
093                    getLog().info("Setting " + newKey + "=" + newValue);
094                }
095            }
096        }
097    
098        protected String getProperty(String key) {
099            String sys = System.getProperty(key);
100            String proj = project.getProperties().getProperty(key);
101            if (!StringUtils.isBlank(sys)) {
102                return sys;
103            } else {
104                return proj;
105            }
106    
107        }
108    
109        public String[] getProperties() {
110            return properties;
111        }
112    
113        public void setProperties(String[] properties) {
114            this.properties = properties;
115        }
116    }