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    import org.kuali.common.util.PropertyUtils;
029    
030    /**
031     * Inspect project and system properties for any keys ending with <code>endsWith</code>. Any matching properties are assumed to be
032     * encrypted. They are decrypted and stored as project properties minus the <code>endsWith</code> suffix. For example, the value for the
033     * property "dba.password.encrypted" will be decrypted and stored as "dba.password"
034     *
035     * @goal decryptall
036     */
037    public class DecryptAllPropertiesMojo extends AbstractMojo {
038    
039            /**
040             * @parameter default-value="${project}"
041             * @required
042             * @readonly
043             */
044            private MavenProject project;
045    
046            /**
047             * If true, the plugin will include system properties when decrypting properties
048             *
049             * @parameter default-value="false" expression="${properties.includeSystemProperties}"
050             */
051            private boolean includeSystemProperties;
052    
053            /**
054             * If true, the plugin will include environment variables when decrypting properties.
055             *
056             * @parameter default-value="false" expression="${properties.includeEnvironmentVariables}"
057             */
058            private boolean includeEnvironmentVariables;
059    
060            /**
061             * If true, the plugin will emit no logging information
062             *
063             * @parameter expression="${properties.quiet}" default-value="false"
064             * @required
065             */
066            private boolean quiet;
067    
068            /**
069             * The pattern for matching properties in need of decryption
070             *
071             * @parameter expression="${properties.endsWith}" default-value=".encrypted"
072             * @required
073             */
074            private String endsWith;
075    
076            /**
077             * If true the plain text decrypted values are displayed to the console.
078             *
079             * @parameter expression="${properties.show}" default-value="false"
080             * @required
081             */
082            private boolean show;
083    
084            /**
085             * The password for decrypting property values. This same password must have been used to encrypt them.
086             *
087             * @parameter expression="${properties.password}"
088             * @required
089             */
090            private String password;
091    
092            @Override
093            public void execute() throws MojoExecutionException {
094                    BasicTextEncryptor encryptor = new BasicTextEncryptor();
095                    encryptor.setPassword(password);
096                    Properties props = new Properties();
097                    props.putAll(project.getProperties());
098                    if (includeEnvironmentVariables) {
099                            props.putAll(PropertyUtils.getEnvAsProperties());
100                    }
101                    if (includeSystemProperties) {
102                            props.putAll(System.getProperties());
103                    }
104                    List<String> keys = new ArrayList<String>(props.stringPropertyNames());
105                    Collections.sort(keys);
106                    for (String key : keys) {
107                            boolean decrypt = key.endsWith(endsWith);
108                            if (!decrypt) {
109                                    continue;
110                            }
111                            String value = props.getProperty(key);
112                            if (StringUtils.isBlank(value) && !quiet) {
113                                    getLog().info("Skipping blank property " + key);
114                                    continue;
115                            }
116                            String newValue = encryptor.decrypt(value);
117                            int length = endsWith.length();
118                            String newKey = key.substring(0, key.length() - length);
119                            project.getProperties().setProperty(newKey, newValue);
120                            if (quiet) {
121                                    continue;
122                            }
123                            if (show) {
124                                    getLog().info("Setting " + newKey + "=" + newValue + " - " + value);
125                            } else {
126                                    getLog().info("Setting " + newKey);
127                            }
128                    }
129            }
130    
131            public boolean isQuiet() {
132                    return quiet;
133            }
134    
135            public void setQuiet(boolean quiet) {
136                    this.quiet = quiet;
137            }
138    
139            public String getEndsWith() {
140                    return endsWith;
141            }
142    
143            public void setEndsWith(String endsWith) {
144                    this.endsWith = endsWith;
145            }
146    
147            public boolean isShow() {
148                    return show;
149            }
150    
151            public void setShow(boolean show) {
152                    this.show = show;
153            }
154    
155            public String getPassword() {
156                    return password;
157            }
158    
159            public void setPassword(String password) {
160                    this.password = password;
161            }
162    
163            public MavenProject getProject() {
164                    return project;
165            }
166    
167            public boolean isIncludeSystemProperties() {
168                    return includeSystemProperties;
169            }
170    
171            public void setIncludeSystemProperties(boolean includeSystemProperties) {
172                    this.includeSystemProperties = includeSystemProperties;
173            }
174    
175            public boolean isIncludeEnvironmentVariables() {
176                    return includeEnvironmentVariables;
177            }
178    
179            public void setIncludeEnvironmentVariables(boolean includeEnvironmentVariables) {
180                    this.includeEnvironmentVariables = includeEnvironmentVariables;
181            }
182    }