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.util.ResourceExtractor;
23  import org.eclipse.jetty.security.ConstraintMapping;
24  import org.eclipse.jetty.security.ConstraintSecurityHandler;
25  import org.eclipse.jetty.security.HashLoginService;
26  import org.eclipse.jetty.server.Handler;
27  import org.eclipse.jetty.server.NetworkConnector;
28  import org.eclipse.jetty.server.Request;
29  import org.eclipse.jetty.server.Server;
30  import org.eclipse.jetty.server.handler.AbstractHandler;
31  import org.eclipse.jetty.server.handler.HandlerList;
32  import org.eclipse.jetty.servlet.ServletContextHandler;
33  import org.eclipse.jetty.util.security.Constraint;
34  import org.eclipse.jetty.util.security.Password;
35  
36  import javax.servlet.http.HttpServletRequest;
37  import javax.servlet.http.HttpServletResponse;
38  import java.io.File;
39  
40  import static org.eclipse.jetty.servlet.ServletContextHandler.SECURITY;
41  import static org.eclipse.jetty.servlet.ServletContextHandler.SESSIONS;
42  import static org.eclipse.jetty.util.security.Constraint.__BASIC_AUTH;
43  
44  /**
45   * This is a test set for <a href="https://issues.apache.org/jira/browse/MNG-3953">MNG-3953</a>.
46   *
47   * @author Benjamin Bentmann
48   *
49   */
50  public class MavenITmng3953AuthenticatedDeploymentTest
51      extends AbstractMavenIntegrationTestCase
52  {
53      private Server server;
54  
55      private int port;
56  
57      private volatile boolean deployed;
58  
59      public MavenITmng3953AuthenticatedDeploymentTest()
60      {
61          super( "(2.0.1,)" );
62      }
63  
64      @Override
65      protected void setUp()
66          throws Exception
67      {
68          Handler repoHandler = new AbstractHandler()
69          {
70              @Override
71              public void handle( String target, Request baseRequest, HttpServletRequest request,
72                                  HttpServletResponse response )
73              {
74                  System.out.println( "Handling " + request.getMethod() + " " + request.getRequestURL() );
75  
76                  if ( "PUT".equalsIgnoreCase( request.getMethod() ) )
77                  {
78                      response.setStatus( HttpServletResponse.SC_OK );
79                      deployed = true;
80                  }
81                  else
82                  {
83                      response.setStatus( HttpServletResponse.SC_NOT_FOUND );
84                  }
85  
86                  ( (Request) request ).setHandled( true );
87              }
88          };
89  
90          Constraint constraint = new Constraint();
91          constraint.setName( Constraint.__BASIC_AUTH );
92          constraint.setRoles( new String[] { "deployer" } );
93          constraint.setAuthenticate( true );
94  
95          ConstraintMapping constraintMapping = new ConstraintMapping();
96          constraintMapping.setConstraint( constraint );
97          constraintMapping.setPathSpec( "/*" );
98  
99          HashLoginService userRealm = new HashLoginService( "TestRealm" );
100         userRealm.putUser( "testuser", new Password( "testtest" ), new String[] { "deployer" } );
101 
102         server = new Server( 0 );
103         ServletContextHandler ctx = new ServletContextHandler( server, "/", SESSIONS | SECURITY );
104         ConstraintSecurityHandler securityHandler = (ConstraintSecurityHandler) ctx.getSecurityHandler();
105         securityHandler.setLoginService( userRealm );
106         securityHandler.setAuthMethod( __BASIC_AUTH );
107         securityHandler.setConstraintMappings( new ConstraintMapping[] { constraintMapping } );
108 
109         HandlerList handlerList = new HandlerList();
110         handlerList.addHandler( securityHandler );
111         handlerList.addHandler( repoHandler );
112 
113         server.setHandler( handlerList );
114         server.start();
115         if ( server.isFailed() )
116         {
117             fail( "Couldn't bind the server socket to a free port!" );
118         }
119         port = ( (NetworkConnector) server.getConnectors()[0] ).getLocalPort();
120         System.out.println( "Bound server socket to the port " + port );
121         deployed = false;
122     }
123 
124     @Override
125     protected void tearDown()
126         throws Exception
127     {
128         if ( server != null )
129         {
130             server.stop();
131             server.join();
132         }
133     }
134 
135     /**
136      * Test that deployment (of a release) to a repository that requires authentification works.
137      *
138      * @throws Exception in case of failure
139      */
140     public void testitRelease()
141         throws Exception
142     {
143         testitMNG3953( "release" );
144     }
145 
146     /**
147      * Test that deployment (of a snapshot) to a repository that requires authentification works.
148      *
149      * @throws Exception in case of failure
150      */
151     public void testitSnapshot()
152         throws Exception
153     {
154         testitMNG3953( "snapshot" );
155     }
156 
157     private void testitMNG3953( String project )
158         throws Exception
159     {
160         File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-3953/" + project );
161 
162         Verifier verifier = newVerifier( testDir.getAbsolutePath() );
163         verifier.setAutoclean( false );
164         verifier.addCliOption( "--settings" );
165         verifier.addCliOption( "settings.xml" );
166         verifier.addCliOption( "-DdeploymentPort=" + port );
167         verifier.executeGoal( "validate" );
168         verifier.verifyErrorFreeLog();
169         verifier.resetStreams();
170 
171         assertTrue( deployed );
172     }
173 
174 }