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 java.io.File;
23  import java.io.IOException;
24  import java.util.ArrayList;
25  import java.util.Collections;
26  import java.util.List;
27  
28  import javax.servlet.ServletException;
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  
32  import org.apache.maven.it.Verifier;
33  import org.apache.maven.it.util.ResourceExtractor;
34  import org.apache.maven.it.utils.DeployedResource;
35  import org.codehaus.plexus.util.StringUtils;
36  import org.mortbay.jetty.Handler;
37  import org.mortbay.jetty.Request;
38  import org.mortbay.jetty.Server;
39  import org.mortbay.jetty.handler.AbstractHandler;
40  import org.mortbay.jetty.handler.HandlerList;
41  import org.mortbay.jetty.security.B64Code;
42  import org.mortbay.jetty.security.Constraint;
43  import org.mortbay.jetty.security.ConstraintMapping;
44  import org.mortbay.jetty.security.HashUserRealm;
45  import org.mortbay.jetty.security.SecurityHandler;
46  
47  /**
48   * This is a test set for <a href="http://jira.codehaus.org/browse/MNG-4470">MNG-4470</a>.
49   * 
50   * @author Benjamin Bentmann
51   * @version $Id: MavenITmng4470AuthenticatedDeploymentToProxyTest.java 1214687 2011-12-15 10:19:47Z olamy $
52   */
53  public class MavenITmng4470AuthenticatedDeploymentToProxyTest
54      extends AbstractMavenIntegrationTestCase
55  {
56  
57      private Server server;
58  
59      private int port;
60  
61      private volatile boolean deployed;
62  
63      List<DeployedResource> deployedResources = new ArrayList<DeployedResource>();
64  
65      public MavenITmng4470AuthenticatedDeploymentToProxyTest()
66      {
67          super( "[2.0.3,3.0-alpha-1),[3.0-alpha-6,)" );
68      }
69  
70      public void setUp()
71          throws Exception
72      {
73          super.setUp();
74  
75          Handler proxyHandler = new AbstractHandler()
76          {
77              public void handle( String target, HttpServletRequest request, HttpServletResponse response, int dispatch )
78                  throws IOException, ServletException
79              {
80                  System.out.println( "Handling " + request.getMethod() + " " + request.getRequestURL() );
81  
82                  String auth = request.getHeader( "Proxy-Authorization" );
83                  if ( auth != null )
84                  {
85                      auth = auth.substring( auth.indexOf( ' ' ) + 1 ).trim();
86                      auth = B64Code.decode( auth );
87                  }
88                  System.out.println( "Proxy-Authorization: " + auth );
89  
90                  if ( !"proxyuser:proxypass".equals( auth ) )
91                  {
92                      response.setStatus( HttpServletResponse.SC_PROXY_AUTHENTICATION_REQUIRED );
93                      response.addHeader( "Proxy-Authenticate", "Basic realm=\"Squid proxy-caching web server\"" );
94                      ( (Request) request ).setHandled( true );
95                  }
96  
97                  DeployedResource deployedResource = new DeployedResource();
98  
99                  deployedResource.httpMethod = request.getMethod();
100                 deployedResource.requestUri = request.getRequestURI();
101                 deployedResource.transferEncoding = request.getHeader( "Transfer-Encoding" );
102                 deployedResource.contentLength = request.getHeader( "Content-Length" );
103 
104                 deployedResources.add( deployedResource );
105             }
106         };
107 
108         Handler repoHandler = new AbstractHandler()
109         {
110 
111             public void handle( String target, HttpServletRequest request, HttpServletResponse response, int dispatch )
112                 throws IOException, ServletException
113             {
114                 System.out.println( "Handling " + request.getMethod() + " " + request.getRequestURL() );
115 
116                 if ( "PUT".equalsIgnoreCase( request.getMethod() ) )
117                 {
118                     response.setStatus( HttpServletResponse.SC_OK );
119                     deployed = true;
120                 }
121                 else
122                 {
123                     response.setStatus( HttpServletResponse.SC_NOT_FOUND );
124                 }
125 
126                 ( (Request) request ).setHandled( true );
127 
128                 DeployedResource deployedResource = new DeployedResource();
129 
130                 deployedResource.httpMethod = request.getMethod();
131                 deployedResource.requestUri = request.getRequestURI();
132                 deployedResource.transferEncoding = request.getHeader( "Transfer-Encoding" );
133                 deployedResource.contentLength = request.getHeader( "Content-Length" );
134 
135                 deployedResources.add( deployedResource );
136             }
137         };
138 
139         Constraint constraint = new Constraint();
140         constraint.setName( Constraint.__BASIC_AUTH );
141         constraint.setRoles( new String[] { "deployer" } );
142         constraint.setAuthenticate( true );
143 
144         ConstraintMapping constraintMapping = new ConstraintMapping();
145         constraintMapping.setConstraint( constraint );
146         constraintMapping.setPathSpec( "/*" );
147 
148         HashUserRealm userRealm = new HashUserRealm( "TestRealm" );
149         userRealm.put( "testuser", "testtest" );
150         userRealm.addUserToRole( "testuser", "deployer" );
151 
152         SecurityHandler securityHandler = new SecurityHandler();
153         securityHandler.setUserRealm( userRealm );
154         securityHandler.setConstraintMappings( new ConstraintMapping[] { constraintMapping } );
155 
156         HandlerList handlerList = new HandlerList();
157         handlerList.addHandler( proxyHandler );
158         handlerList.addHandler( securityHandler );
159         handlerList.addHandler( repoHandler );
160 
161         server = new Server( 0 );
162         server.setHandler( handlerList );
163         server.start();
164 
165         port = server.getConnectors()[0].getLocalPort();
166 
167         deployed = false;
168     }
169 
170     protected void tearDown()
171         throws Exception
172     {
173         if ( server != null )
174         {
175             server.stop();
176             server = null;
177         }
178 
179         super.tearDown();
180     }
181 
182     /**
183      * Test that deployment (of a release) to a proxy that requires authentication works.
184      */
185     public void testitRelease()
186         throws Exception
187     {
188         testit( "release" );
189     }
190 
191     /**
192      * Test that deployment (of a snapshot) to a proxy that requires authentication works.
193      */
194     public void testitSnapshot()
195         throws Exception
196     {
197         testit( "snapshot" );
198     }
199 
200     private void testit( String project )
201         throws Exception
202     {
203 
204         deployedResources = new ArrayList<DeployedResource>();
205 
206         File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-4470/" + project );
207 
208         Verifier verifier = newVerifier( testDir.getAbsolutePath() );
209         verifier.setAutoclean( false );
210         verifier.filterFile( "settings-template.xml", "settings.xml", "UTF-8", 
211             Collections.singletonMap( "@port@", Integer.toString( port ) ) );
212         verifier.getCliOptions().add( "--settings" );
213         verifier.getCliOptions().add( "settings.xml" );
214         verifier.executeGoal( "validate" );
215         verifier.verifyErrorFreeLog();
216         verifier.resetStreams();
217 
218         for ( DeployedResource deployedResource : deployedResources )
219         {
220             if ( StringUtils.equalsIgnoreCase( "chunked", deployedResource.transferEncoding ) )
221             {
222                 fail( "deployedResource " + deployedResource
223                           + " use chuncked transfert encoding some http server doesn't support that" );
224             }
225         }
226 
227         assertTrue( deployed );
228     }
229 
230 }