View Javadoc
1   package org.apache.maven.shared.release.exec;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.maven.settings.Proxy;
27  import org.apache.maven.settings.Server;
28  import org.apache.maven.settings.Settings;
29  import org.apache.maven.settings.SettingsUtils;
30  import org.apache.maven.settings.io.xpp3.SettingsXpp3Writer;
31  import org.apache.maven.shared.release.ReleaseResult;
32  import org.apache.maven.shared.release.env.DefaultReleaseEnvironment;
33  import org.apache.maven.shared.release.env.ReleaseEnvironment;
34  import org.codehaus.plexus.logging.LogEnabled;
35  import org.codehaus.plexus.logging.Logger;
36  import org.codehaus.plexus.util.StringUtils;
37  import org.sonatype.plexus.components.cipher.DefaultPlexusCipher;
38  import org.sonatype.plexus.components.cipher.PlexusCipher;
39  import org.sonatype.plexus.components.cipher.PlexusCipherException;
40  import org.sonatype.plexus.components.sec.dispatcher.DefaultSecDispatcher;
41  import org.sonatype.plexus.components.sec.dispatcher.SecDispatcherException;
42  import org.sonatype.plexus.components.sec.dispatcher.SecUtil;
43  import org.sonatype.plexus.components.sec.dispatcher.model.SettingsSecurity;
44  
45  /**
46   * 
47   */
48  public abstract class AbstractMavenExecutor
49      implements MavenExecutor, LogEnabled
50  {
51  
52      private Logger logger;
53  
54      /**
55       * When this plugin requires Maven 3.0 as minimum, this component can be removed and o.a.m.s.c.SettingsDecrypter be
56       * used instead.
57       * 
58       * @plexus.requirement role="org.sonatype.plexus.components.sec.dispatcher.SecDispatcher" role-hint="mng-4384"
59       */
60      private DefaultSecDispatcher secDispatcher;
61  
62      /**
63       * @plexus.requirement
64       */
65      private PlexusCipher cipher;
66      
67      protected AbstractMavenExecutor()
68      {
69      }
70  
71      /** {@inheritDoc} */
72      public void executeGoals( File workingDirectory, String goals, boolean interactive, String additionalArguments,
73                                String pomFileName, ReleaseResult result )
74          throws MavenExecutorException
75      {
76          executeGoals( workingDirectory, goals, new DefaultReleaseEnvironment(), interactive, additionalArguments,
77                        pomFileName, result );
78      }
79  
80      /** {@inheritDoc} */
81      public void executeGoals( File workingDirectory, String goals, boolean interactive, String additionalArguments,
82                                ReleaseResult result )
83          throws MavenExecutorException
84      {
85          executeGoals( workingDirectory, goals, new DefaultReleaseEnvironment(), interactive, additionalArguments,
86                        result );
87      }
88  
89      /** {@inheritDoc} */
90      public void executeGoals( File workingDirectory, String goals, ReleaseEnvironment releaseEnvironment,
91                                boolean interactive, String arguments, ReleaseResult result )
92          throws MavenExecutorException
93      {
94          executeGoals( workingDirectory, goals, releaseEnvironment, interactive, arguments, null, result );
95      }
96  
97      /** {@inheritDoc} */
98      public void executeGoals( File workingDirectory, String goals, ReleaseEnvironment releaseEnvironment,
99                                boolean interactive, String additionalArguments, String pomFileName,
100                               ReleaseResult result )
101         throws MavenExecutorException
102     {
103         List<String> goalsList = new ArrayList<String>();
104         if ( goals != null )
105         {
106             // accept both space and comma, so the old way still work
107             // also accept line separators, so that goal lists can be spread
108             // across multiple lines in the POM.
109             for ( String token : StringUtils.split( goals, ", \n\r\t" ) )
110             {
111                 goalsList.add( token );
112             }
113         }
114         executeGoals( workingDirectory, goalsList, releaseEnvironment, interactive, additionalArguments, pomFileName,
115                       result );
116     }
117 
118     protected abstract void executeGoals( File workingDirectory, List<String> goals,
119                                           ReleaseEnvironment releaseEnvironment, boolean interactive,
120                                           String additionalArguments, String pomFileName, ReleaseResult result )
121         throws MavenExecutorException;
122 
123     protected final Logger getLogger()
124     {
125         return logger;
126     }
127 
128     /** {@inheritDoc} */
129     public void enableLogging( Logger logger )
130     {
131         this.logger = logger;
132     }
133 
134     
135     protected Settings encryptSettings( Settings settings )
136     {
137         Settings encryptedSettings = SettingsUtils.copySettings( settings );
138         
139         for ( Server server : encryptedSettings.getServers() )
140         {
141             String password = server.getPassword(); 
142             if ( password != null && !isEncryptedString( password ) )
143             {
144                 try
145                 {
146                     server.setPassword( encryptAndDecorate( password ) );
147                 }
148                 catch ( IllegalStateException e )
149                 {
150                     // ignore
151                 }
152                 catch ( SecDispatcherException e )
153                 {
154                     // ignore
155                 }
156                 catch ( PlexusCipherException e )
157                 {
158                     // ignore
159                 }
160             }
161 
162             String passphrase = server.getPassphrase(); 
163             if ( passphrase != null && !isEncryptedString( passphrase ) )
164             {
165                 try
166                 {
167                     server.setPassphrase( encryptAndDecorate( passphrase ) );
168                 }
169                 catch ( IllegalStateException e )
170                 {
171                     // ignore
172                 }
173                 catch ( SecDispatcherException e )
174                 {
175                     // ignore
176                 }
177                 catch ( PlexusCipherException e )
178                 {
179                     // ignore
180                 }
181             }
182         }
183         
184         for ( Proxy proxy : encryptedSettings.getProxies() )
185         {
186             String password = proxy.getPassword();
187             if ( password != null && !isEncryptedString( password ) )
188             {
189                 try
190                 {
191                     proxy.setPassword( encryptAndDecorate( password ) );
192                 }
193                 catch ( IllegalStateException e )
194                 {
195                     // ignore
196                 }
197                 catch ( SecDispatcherException e )
198                 {
199                     // ignore
200                 }
201                 catch ( PlexusCipherException e )
202                 {
203                     // ignore
204                 }
205             }
206         }
207         
208         return encryptedSettings;
209     }
210     
211     // From org.apache.maven.cli.MavenCli.encryption(CliRequest)
212     private String encryptAndDecorate( String passwd )
213         throws IllegalStateException, SecDispatcherException, PlexusCipherException
214     {
215         String configurationFile = secDispatcher.getConfigurationFile();
216 
217         if ( configurationFile.startsWith( "~" ) )
218         {
219             configurationFile = System.getProperty( "user.home" ) + configurationFile.substring( 1 );
220         }
221 
222         String file = System.getProperty( DefaultSecDispatcher.SYSTEM_PROPERTY_SEC_LOCATION, configurationFile );
223 
224         String master = null;
225 
226         SettingsSecurity sec = SecUtil.read( file, true );
227         if ( sec != null )
228         {
229             master = sec.getMaster();
230         }
231 
232         if ( master == null )
233         {
234             throw new IllegalStateException( "Master password is not set in the setting security file: " + file );
235         }
236 
237         DefaultPlexusCipher cipher = new DefaultPlexusCipher();
238         String masterPasswd = cipher.decryptDecorated( master, DefaultSecDispatcher.SYSTEM_PROPERTY_SEC_LOCATION );
239         return cipher.encryptAndDecorate( passwd, masterPasswd );
240     }
241     
242     private boolean isEncryptedString( String str )
243     {
244         return cipher.isEncryptedString( str );
245     }
246 
247     protected SettingsXpp3Writer getSettingsWriter()
248     {
249         return new SettingsXpp3Writer();
250     }
251 }