View Javadoc

1   package org.apache.maven.it;
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 org.apache.maven.it.Verifier;
23  import org.apache.maven.it.util.ResourceExtractor;
24  
25  import java.io.File;
26  import java.util.Collections;
27  import java.util.Iterator;
28  import java.util.List;
29  import java.util.Properties;
30  
31  import org.mortbay.jetty.Server;
32  import org.mortbay.jetty.handler.DefaultHandler;
33  import org.mortbay.jetty.handler.HandlerList;
34  import org.mortbay.jetty.handler.ResourceHandler;
35  import org.mortbay.jetty.security.Constraint;
36  import org.mortbay.jetty.security.ConstraintMapping;
37  import org.mortbay.jetty.security.HashUserRealm;
38  import org.mortbay.jetty.security.SecurityHandler;
39  
40  /**
41   * This is a test set for <a href="http://jira.codehaus.org/browse/MNG-553">MNG-553</a>.
42   * 
43   * @author Benjamin Bentmann
44   */
45  public class MavenITmng0553SettingsAuthzEncryptionTest
46      extends AbstractMavenIntegrationTestCase
47  {
48  
49      private File testDir;
50  
51      private Server server;
52  
53      private int port;
54  
55      public MavenITmng0553SettingsAuthzEncryptionTest()
56      {
57          super( "[2.1.0,3.0-alpha-1),[3.0-alpha-3,)" );
58      }
59  
60      public void setUp()
61          throws Exception
62      {
63          super.setUp();
64  
65          testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-0553" );
66  
67          Constraint constraint = new Constraint();
68          constraint.setName( Constraint.__BASIC_AUTH );
69          constraint.setRoles( new String[] { "user" } );
70          constraint.setAuthenticate( true );
71  
72          ConstraintMapping constraintMapping = new ConstraintMapping();
73          constraintMapping.setConstraint( constraint );
74          constraintMapping.setPathSpec( "/*" );
75  
76          HashUserRealm userRealm = new HashUserRealm( "TestRealm" );
77          userRealm.put( "testuser", "testtest" );
78          userRealm.addUserToRole( "testuser", "user" );
79  
80          SecurityHandler securityHandler = new SecurityHandler();
81          securityHandler.setUserRealm( userRealm );
82          securityHandler.setConstraintMappings( new ConstraintMapping[] { constraintMapping } );
83  
84          ResourceHandler repoHandler = new ResourceHandler();
85          repoHandler.setResourceBase( new File( testDir, "repo" ).getAbsolutePath() );
86  
87          HandlerList handlerList = new HandlerList();
88          handlerList.addHandler( securityHandler );
89          handlerList.addHandler( repoHandler );
90          handlerList.addHandler( new DefaultHandler() );
91  
92          server = new Server( 0 );
93          server.setHandler( handlerList );
94          server.start();
95  
96          port = server.getConnectors()[0].getLocalPort();
97      }
98  
99      protected void tearDown()
100         throws Exception
101     {
102         if ( server != null )
103         {
104             server.stop();
105             server = null;
106         }
107 
108         super.tearDown();
109     }
110 
111     /**
112      * Test that the encrypted auth infos given in the settings.xml are decrypted.
113      */
114     public void testitBasic()
115         throws Exception
116     {
117         testDir = new File( testDir, "test-1" );
118 
119         Properties filterProps = new Properties();
120         filterProps.setProperty( "@port@", Integer.toString( port ) );
121 
122         Verifier verifier = newVerifier( testDir.getAbsolutePath() );
123         verifier.setAutoclean( false );
124         verifier.deleteArtifacts( "org.apache.maven.its.mng0553" );
125         verifier.assertArtifactNotPresent( "org.apache.maven.its.mng0553", "a", "0.1-SNAPSHOT", "jar" );
126         verifier.filterFile( "settings-template.xml", "settings.xml", "UTF-8", filterProps );
127         setUserHome( verifier, new File( testDir, "userhome" ) );
128         verifier.getCliOptions().add( "--settings" );
129         verifier.getCliOptions().add( "settings.xml" );
130         verifier.executeGoal( "validate" );
131         verifier.verifyErrorFreeLog();
132         verifier.resetStreams();
133 
134         verifier.assertArtifactPresent( "org.apache.maven.its.mng0553", "a", "0.1-SNAPSHOT", "jar" );
135     }
136 
137     /**
138      * Test that the encrypted auth infos given in the settings.xml are decrypted when the master password resides
139      * in an external file.
140      */
141     public void testitRelocation()
142         throws Exception
143     {
144         testDir = new File( testDir, "test-2" );
145 
146         Properties filterProps = new Properties();
147         filterProps.setProperty( "@port@", Integer.toString( port ) );
148         // NOTE: The upper-case scheme name is essential part of the test
149         String secUrl = "FILE://" + new File( testDir, "relocated-settings-security.xml" ).toURI().getRawPath();
150         filterProps.setProperty( "@relocation@", secUrl );
151 
152         Verifier verifier = newVerifier( testDir.getAbsolutePath() );
153         verifier.setAutoclean( false );
154         verifier.deleteArtifacts( "org.apache.maven.its.mng0553" );
155         verifier.assertArtifactNotPresent( "org.apache.maven.its.mng0553", "a", "0.1-SNAPSHOT", "jar" );
156 
157         // NOTE: The tilde ~ in the file name is essential part of the test
158         verifier.filterFile( "security-template.xml", "settings~security.xml", "UTF-8", filterProps );
159         verifier.filterFile( "settings-template.xml", "settings.xml", "UTF-8", filterProps );
160 
161         verifier.getSystemProperties().setProperty( "settings.security", 
162             new File( testDir, "settings~security.xml" ).getAbsolutePath() );
163         verifier.getCliOptions().add( "--settings" );
164         verifier.getCliOptions().add( "settings.xml" );
165         // NOTE: The selection of the Turkish language for the JVM locale is essential part of the test
166         verifier.executeGoal( "validate", Collections.singletonMap( "MAVEN_OPTS", "-Duser.language=tr" ) );
167         verifier.verifyErrorFreeLog();
168         verifier.resetStreams();
169 
170         verifier.assertArtifactPresent( "org.apache.maven.its.mng0553", "a", "0.1-SNAPSHOT", "jar" );
171     }
172 
173     /**
174      * Test that the CLI supports generation of encrypted (master) passwords.
175      */
176     public void testitEncryption()
177         throws Exception
178     {
179         requiresMavenVersion( "[2.1.0,3.0-alpha-1),[3.0-alpha-7,)" );
180 
181         testDir = new File( testDir, "test-3" );
182 
183         Verifier verifier = newVerifier( testDir.getAbsolutePath() );
184         verifier.setAutoclean( false );
185         setUserHome( verifier, new File( testDir, "userhome" ) );
186         verifier.getCliOptions().add( "--encrypt-master-password" );
187         verifier.getCliOptions().add( "test" );
188         verifier.setLogFileName( "log-emp.txt" );
189         verifier.executeGoal( "-e" );
190         verifier.verifyErrorFreeLog();
191         verifier.resetStreams();
192 
193         List log = verifier.loadLines( verifier.getLogFileName(), null );
194         assertNotNull( findPassword( log ) );
195 
196         verifier = newVerifier( testDir.getAbsolutePath() );
197         verifier.setAutoclean( false );
198         setUserHome( verifier, new File( testDir, "userhome" ) );
199         verifier.getCliOptions().add( "--encrypt-password" );
200         verifier.getCliOptions().add( "testpass" );
201         verifier.setLogFileName( "log-ep.txt" );
202         verifier.executeGoal( "-e" );
203         verifier.verifyErrorFreeLog();
204         verifier.resetStreams();
205 
206         log = verifier.loadLines( verifier.getLogFileName(), null );
207         assertNotNull( findPassword( log ) );
208     }
209 
210     private String findPassword( List log )
211     {
212         for ( Iterator it = log.iterator(); it.hasNext(); )
213         {
214             String line = (String) it.next();
215 
216             if ( line.matches( ".*\\{[A-Za-z0-9+/=]+\\}.*" ) )
217             {
218                 return line;
219             }
220         }
221         
222         return null;
223     }
224 
225     private void setUserHome( Verifier verifier, File home )
226     {
227         // NOTE: We set the user.home directory instead of say settings.security to reflect Maven's normal behavior
228         String path = home.getAbsolutePath();
229         if ( path.indexOf( ' ' ) < 0 )
230         {
231             verifier.setEnvironmentVariable( "MAVEN_OPTS", "-Duser.home=" + path );
232         }
233         else
234         {
235             verifier.setEnvironmentVariable( "MAVEN_OPTS", "\"-Duser.home=" + path + "\"" );
236         }
237     }
238 
239 }