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.NetworkConnector;
27  import org.eclipse.jetty.server.Server;
28  import org.eclipse.jetty.server.handler.DefaultHandler;
29  import org.eclipse.jetty.server.handler.HandlerList;
30  import org.eclipse.jetty.server.handler.ResourceHandler;
31  import org.eclipse.jetty.servlet.ServletContextHandler;
32  import org.eclipse.jetty.util.security.Constraint;
33  import org.eclipse.jetty.util.security.Password;
34  
35  import java.io.File;
36  import java.util.Properties;
37  
38  import static org.eclipse.jetty.servlet.ServletContextHandler.SECURITY;
39  import static org.eclipse.jetty.servlet.ServletContextHandler.SESSIONS;
40  import static org.eclipse.jetty.util.security.Constraint.__BASIC_AUTH;
41  
42  /**
43   * This is a test set for <a href="https://issues.apache.org/jira/browse/MNG-4068">MNG-4068</a>.
44   *
45   * @author Benjamin Bentmann
46   *
47   */
48  public class MavenITmng4068AuthenticatedMirrorTest
49      extends AbstractMavenIntegrationTestCase
50  {
51  
52      private File testDir;
53  
54      private Server server;
55  
56      private int port;
57  
58      public MavenITmng4068AuthenticatedMirrorTest()
59      {
60          super( ALL_MAVEN_VERSIONS );
61      }
62  
63      @Override
64      protected void setUp()
65          throws Exception
66      {
67          testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-4068" );
68  
69          Constraint constraint = new Constraint();
70          constraint.setName( Constraint.__BASIC_AUTH );
71          constraint.setRoles( new String[] { "user" } );
72          constraint.setAuthenticate( true );
73  
74          ConstraintMapping constraintMapping = new ConstraintMapping();
75          constraintMapping.setConstraint( constraint );
76          constraintMapping.setPathSpec( "/*" );
77  
78          HashLoginService userRealm = new HashLoginService( "TestRealm" );
79          userRealm.putUser( "testuser", new Password( "testtest" ), new String[] { "user" } );
80  
81          server = new Server( 0 );
82          ServletContextHandler ctx = new ServletContextHandler( server, "/", SESSIONS | SECURITY );
83          ConstraintSecurityHandler securityHandler = (ConstraintSecurityHandler) ctx.getSecurityHandler();
84          securityHandler.setLoginService( userRealm );
85          securityHandler.setAuthMethod( __BASIC_AUTH );
86          securityHandler.setConstraintMappings( new ConstraintMapping[] { constraintMapping } );
87  
88          ResourceHandler repoHandler = new ResourceHandler();
89          repoHandler.setResourceBase( new File( testDir, "repo" ).getAbsolutePath() );
90  
91          HandlerList handlerList = new HandlerList();
92          handlerList.addHandler( securityHandler );
93          handlerList.addHandler( repoHandler );
94          handlerList.addHandler( new DefaultHandler() );
95  
96          server.setHandler( handlerList );
97          server.start();
98          if ( server.isFailed() )
99          {
100             fail( "Couldn't bind the server socket to a free port!" );
101         }
102         port = ( (NetworkConnector) server.getConnectors()[0] ).getLocalPort();
103         System.out.println( "Bound server socket to the port " + port );
104     }
105 
106     @Override
107     protected void tearDown()
108         throws Exception
109     {
110         if ( server != null )
111         {
112             server.stop();
113             server.join();
114         }
115     }
116 
117     /**
118      * Test that downloading of release/snapshot artifacts from an authenticated mirror works. This basically boils down
119      * to using the proper id for the mirrored repository when looking up the credentials.
120      *
121      * @throws Exception in case of failure
122      */
123     public void testit()
124         throws Exception
125     {
126         Properties filterProps = new Properties();
127         filterProps.setProperty( "@mirrorPort@", Integer.toString( port ) );
128 
129         Verifier verifier = newVerifier( testDir.getAbsolutePath() );
130         verifier.filterFile( "settings-template.xml", "settings.xml", "UTF-8", filterProps );
131         verifier.setAutoclean( false );
132         verifier.deleteArtifacts( "org.apache.maven.its.mng4068" );
133         verifier.addCliOption( "--settings" );
134         verifier.addCliOption( "settings.xml" );
135         verifier.executeGoal( "validate" );
136         verifier.verifyErrorFreeLog();
137         verifier.resetStreams();
138 
139         verifier.assertArtifactPresent( "org.apache.maven.its.mng4068", "a", "0.1", "jar" );
140         verifier.assertArtifactPresent( "org.apache.maven.its.mng4068", "b", "0.1-SNAPSHOT", "jar" );
141     }
142 
143 }