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-4469">MNG-4469</a>.
46   *
47   * @author Benjamin Bentmann
48   *
49   */
50  public class MavenITmng4469AuthenticatedDeploymentToCustomRepoTest
51      extends AbstractMavenIntegrationTestCase
52  {
53      private Server server;
54  
55      private int port;
56  
57      private volatile boolean deployed;
58  
59      public MavenITmng4469AuthenticatedDeploymentToCustomRepoTest()
60      {
61          super( "[2.0.3,3.0-alpha-3),[3.0-alpha-6,)" );
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 to a custom repository (i.e. created by a plugin) that requires authentification works.
137      *
138      * @throws Exception in case of failure
139      */
140     public void testit()
141         throws Exception
142     {
143         File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-4469" );
144 
145         Verifier verifier = newVerifier( testDir.getAbsolutePath() );
146         verifier.setAutoclean( false );
147         verifier.addCliOption( "--settings" );
148         verifier.addCliOption( "settings.xml" );
149         verifier.setSystemProperty( "file", "settings.xml" );
150         verifier.setSystemProperty( "groupId", "org.apache.maven.its.mng4469" );
151         verifier.setSystemProperty( "artifactId", "it" );
152         verifier.setSystemProperty( "version", "0.1" );
153         verifier.setSystemProperty( "repositoryId", "mng4469" );
154         verifier.setSystemProperty( "repositoryUrl", "http://localhost:" + port + "/repo" );
155         verifier.executeGoal( "org.apache.maven.its.plugins:maven-it-plugin-artifact:2.1-SNAPSHOT:deploy-file" );
156         verifier.verifyErrorFreeLog();
157         verifier.resetStreams();
158 
159         assertTrue( deployed );
160     }
161 }