1 package org.apache.maven.archiva.webdav;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
23 import java.io.IOException;
24 import java.io.InputStream;
26 import javax.servlet.http.HttpServletResponse;
28 import net.sf.ehcache.CacheManager;
30 import org.apache.commons.io.FileUtils;
31 import org.apache.jackrabbit.webdav.DavSessionProvider;
32 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
33 import org.apache.maven.archiva.configuration.Configuration;
34 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
35 import org.apache.maven.archiva.security.ArchivaXworkUser;
36 import org.apache.maven.archiva.security.ServletAuthenticator;
37 import org.codehaus.plexus.redback.authentication.AuthenticationException;
38 import org.codehaus.plexus.redback.authentication.AuthenticationResult;
39 import org.codehaus.plexus.redback.authorization.UnauthorizedException;
40 import org.codehaus.plexus.redback.system.DefaultSecuritySession;
41 import org.codehaus.plexus.redback.system.SecuritySession;
42 import org.codehaus.plexus.redback.users.memory.SimpleUser;
43 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
44 import org.codehaus.redback.integration.filter.authentication.HttpAuthenticator;
45 import org.codehaus.redback.integration.filter.authentication.basic.HttpBasicAuthentication;
46 import org.easymock.MockControl;
47 import org.easymock.classextension.MockClassControl;
49 import com.meterware.httpunit.GetMethodWebRequest;
50 import com.meterware.httpunit.HttpUnitOptions;
51 import com.meterware.httpunit.PutMethodWebRequest;
52 import com.meterware.httpunit.WebRequest;
53 import com.meterware.httpunit.WebResponse;
54 import com.meterware.servletunit.InvocationContext;
55 import com.meterware.servletunit.ServletRunner;
56 import com.meterware.servletunit.ServletUnitClient;
59 * RepositoryServletSecurityTest
61 * Test the flow of the authentication and authorization checks. This does not necessarily
62 * perform redback security checking.
66 public class RepositoryServletSecurityTest
67 extends PlexusInSpringTestCase
69 protected static final String REPOID_INTERNAL = "internal";
71 protected ServletUnitClient sc;
73 protected File repoRootInternal;
75 private ServletRunner sr;
77 protected ArchivaConfiguration archivaConfiguration;
79 private DavSessionProvider davSessionProvider;
81 private MockControl servletAuthControl;
83 private ServletAuthenticator servletAuth;
85 private MockClassControl httpAuthControl;
87 private HttpAuthenticator httpAuth;
89 private RepositoryServlet servlet;
96 String appserverBase = getTestFile( "target/appserver-base" ).getAbsolutePath();
97 System.setProperty( "appserver.base", appserverBase );
99 File testConf = getTestFile( "src/test/resources/repository-archiva.xml" );
100 File testConfDest = new File( appserverBase, "conf/archiva.xml" );
101 FileUtils.copyFile( testConf, testConfDest );
103 archivaConfiguration = (ArchivaConfiguration) lookup( ArchivaConfiguration.class );
104 repoRootInternal = new File( appserverBase, "data/repositories/internal" );
105 Configuration config = archivaConfiguration.getConfiguration();
107 config.addManagedRepository( createManagedRepository( REPOID_INTERNAL, "Internal Test Repo", repoRootInternal ) );
108 saveConfiguration( archivaConfiguration );
110 CacheManager.getInstance().removeCache( "url-failures-cache" );
112 HttpUnitOptions.setExceptionsThrownOnErrorStatus( false );
114 sr = new ServletRunner( getTestFile( "src/test/resources/WEB-INF/repository-servlet-security-test/web.xml" ) );
115 sr.registerServlet( "/repository/*", RepositoryServlet.class.getName() );
118 servletAuthControl = MockControl.createControl( ServletAuthenticator.class );
119 servletAuthControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
120 servletAuth = (ServletAuthenticator) servletAuthControl.getMock();
123 MockClassControl.createControl( HttpBasicAuthentication.class, HttpBasicAuthentication.class.getMethods() );
124 httpAuthControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
125 httpAuth = (HttpAuthenticator) httpAuthControl.getMock();
127 ArchivaXworkUser archivaXworkUser = (ArchivaXworkUser) lookup( ArchivaXworkUser.class );
129 davSessionProvider = new ArchivaDavSessionProvider( servletAuth, httpAuth, archivaXworkUser );
132 protected ManagedRepositoryConfiguration createManagedRepository( String id, String name, File location )
134 ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
136 repo.setName( name );
137 repo.setLocation( location.getAbsolutePath() );
141 protected void saveConfiguration()
144 saveConfiguration( archivaConfiguration );
147 protected void saveConfiguration( ArchivaConfiguration archivaConfiguration )
150 archivaConfiguration.save( archivaConfiguration.getConfiguration() );
153 protected void setupCleanRepo( File repoRootDir )
156 FileUtils.deleteDirectory( repoRootDir );
157 if ( !repoRootDir.exists() )
159 repoRootDir.mkdirs();
164 protected String getPlexusConfigLocation()
166 return "org/apache/maven/archiva/webdav/RepositoryServletSecurityTest.xml";
170 protected void tearDown()
183 if ( repoRootInternal.exists() )
185 FileUtils.deleteDirectory(repoRootInternal);
193 // test deploy with invalid user, and guest has no write access to repo
194 // 401 must be returned
195 public void testPutWithInvalidUserAndGuestHasNoWriteAccess()
198 setupCleanRepo( repoRootInternal );
200 String putUrl = "http://machine.com/repository/internal/path/to/artifact.jar";
201 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
202 assertNotNull( "artifact.jar inputstream", is );
204 WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
205 InvocationContext ic = sc.newInvocation( request );
206 servlet = (RepositoryServlet) ic.getServlet();
207 servlet.setDavSessionProvider( davSessionProvider );
209 AuthenticationResult result = new AuthenticationResult();
210 httpAuthControl.expectAndReturn( httpAuth.getAuthenticationResult( null, null ), result );
211 servletAuthControl.expectAndThrow( servletAuth.isAuthenticated( null, null ),
212 new AuthenticationException( "Authentication error" ) );
214 servletAuth.isAuthorized( "guest", "internal", true );
215 servletAuthControl.setMatcher( MockControl.EQUALS_MATCHER );
216 servletAuthControl.setThrowable( new UnauthorizedException( "'guest' has no write access to repository" ) );
218 httpAuthControl.replay();
219 servletAuthControl.replay();
221 servlet.service( ic.getRequest(), ic.getResponse() );
223 httpAuthControl.verify();
224 servletAuthControl.verify();
226 //assertEquals(HttpServletResponse.SC_UNAUTHORIZED, response.getResponseCode());
229 // test deploy with invalid user, but guest has write access to repo
230 public void testPutWithInvalidUserAndGuestHasWriteAccess()
233 setupCleanRepo( repoRootInternal );
235 String putUrl = "http://machine.com/repository/internal/path/to/artifact.jar";
236 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
237 assertNotNull( "artifact.jar inputstream", is );
239 WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
241 InvocationContext ic = sc.newInvocation( request );
242 servlet = (RepositoryServlet) ic.getServlet();
243 servlet.setDavSessionProvider( davSessionProvider );
245 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
246 archivaDavResourceFactory.setHttpAuth( httpAuth );
247 archivaDavResourceFactory.setServletAuth( servletAuth );
249 servlet.setResourceFactory( archivaDavResourceFactory );
251 AuthenticationResult result = new AuthenticationResult();
252 httpAuthControl.expectAndReturn( httpAuth.getAuthenticationResult( null, null ), result );
253 servletAuthControl.expectAndThrow( servletAuth.isAuthenticated( null, null ),
254 new AuthenticationException( "Authentication error" ) );
256 servletAuth.isAuthorized( "guest", "internal", true );
257 servletAuthControl.setMatcher( MockControl.EQUALS_MATCHER );
258 servletAuthControl.setReturnValue( true );
260 // ArchivaDavResourceFactory#isAuthorized()
261 SecuritySession session = new DefaultSecuritySession();
262 httpAuthControl.expectAndReturn( httpAuth.getAuthenticationResult( null, null ), result );
263 httpAuthControl.expectAndReturn( httpAuth.getSecuritySession( ic.getRequest().getSession( true) ), session );
264 servletAuthControl.expectAndThrow( servletAuth.isAuthenticated( null, result ),
265 new AuthenticationException( "Authentication error" ) );
267 httpAuthControl.expectAndReturn( httpAuth.getSessionUser( ic.getRequest().getSession() ), null );
269 // check if guest has write access
270 servletAuth.isAuthorized( "guest", "internal", true );
271 servletAuthControl.setMatcher( MockControl.EQUALS_MATCHER );
272 servletAuthControl.setReturnValue( true );
274 httpAuthControl.replay();
275 servletAuthControl.replay();
277 servlet.service( ic.getRequest(), ic.getResponse() );
279 httpAuthControl.verify();
280 servletAuthControl.verify();
282 // assertEquals( HttpServletResponse.SC_CREATED, response.getResponseCode() );
285 // test deploy with a valid user with no write access
286 public void testPutWithValidUserWithNoWriteAccess()
289 setupCleanRepo( repoRootInternal );
291 String putUrl = "http://machine.com/repository/internal/path/to/artifact.jar";
292 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
293 assertNotNull( "artifact.jar inputstream", is );
295 WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
297 InvocationContext ic = sc.newInvocation( request );
298 servlet = (RepositoryServlet) ic.getServlet();
299 servlet.setDavSessionProvider( davSessionProvider );
301 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
302 archivaDavResourceFactory.setHttpAuth( httpAuth );
303 archivaDavResourceFactory.setServletAuth( servletAuth );
304 servlet.setResourceFactory( archivaDavResourceFactory );
306 AuthenticationResult result = new AuthenticationResult();
307 httpAuthControl.expectAndReturn( httpAuth.getAuthenticationResult( null, null ), result );
308 servletAuthControl.expectAndReturn( servletAuth.isAuthenticated( null, null ), true );
310 // ArchivaDavResourceFactory#isAuthorized()
311 SecuritySession session = new DefaultSecuritySession();
312 httpAuthControl.expectAndReturn( httpAuth.getAuthenticationResult( null, null ), result );
313 httpAuthControl.expectAndReturn( httpAuth.getSecuritySession( ic.getRequest().getSession( true ) ), session );
314 servletAuthControl.expectAndReturn( servletAuth.isAuthenticated( null, result ), true );
315 servletAuthControl.expectAndThrow( servletAuth.isAuthorized( null, session, "internal", true ),
316 new UnauthorizedException( "User not authorized" ) );
318 httpAuthControl.replay();
319 servletAuthControl.replay();
321 servlet.service( ic.getRequest(), ic.getResponse() );
323 httpAuthControl.verify();
324 servletAuthControl.verify();
326 // assertEquals(HttpServletResponse.SC_UNAUTHORIZED, response.getResponseCode());
329 // test deploy with a valid user with write access
330 public void testPutWithValidUserWithWriteAccess()
333 setupCleanRepo( repoRootInternal );
334 assertTrue( repoRootInternal.exists() );
336 String putUrl = "http://machine.com/repository/internal/path/to/artifact.jar";
337 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
338 assertNotNull( "artifact.jar inputstream", is );
340 WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
342 InvocationContext ic = sc.newInvocation( request );
343 servlet = (RepositoryServlet) ic.getServlet();
344 servlet.setDavSessionProvider( davSessionProvider );
346 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
347 archivaDavResourceFactory.setHttpAuth( httpAuth );
348 archivaDavResourceFactory.setServletAuth( servletAuth );
350 servlet.setResourceFactory( archivaDavResourceFactory );
352 AuthenticationResult result = new AuthenticationResult();
353 httpAuthControl.expectAndReturn( httpAuth.getAuthenticationResult( null, null ), result );
354 servletAuthControl.expectAndReturn( servletAuth.isAuthenticated( null, null ), true );
356 // ArchivaDavResourceFactory#isAuthorized()
357 SecuritySession session = new DefaultSecuritySession();
358 httpAuthControl.expectAndReturn( httpAuth.getAuthenticationResult( null, null ), result );
359 httpAuthControl.expectAndReturn( httpAuth.getSecuritySession( ic.getRequest().getSession( true ) ), session );
360 httpAuthControl.expectAndReturn( httpAuth.getSessionUser( ic.getRequest().getSession() ), new SimpleUser() );
361 servletAuthControl.expectAndReturn( servletAuth.isAuthenticated( null, result ), true );
362 servletAuthControl.expectAndReturn( servletAuth.isAuthorized( null, session, "internal", true ), true );
364 httpAuthControl.replay();
365 servletAuthControl.replay();
367 servlet.service( ic.getRequest(), ic.getResponse() );
369 httpAuthControl.verify();
370 servletAuthControl.verify();
372 // assertEquals(HttpServletResponse.SC_CREATED, response.getResponseCode());
375 // test get with invalid user, and guest has read access to repo
376 public void testGetWithInvalidUserAndGuestHasReadAccess()
379 String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
380 String expectedArtifactContents = "dummy-commons-lang-artifact";
382 File artifactFile = new File( repoRootInternal, commonsLangJar );
383 artifactFile.getParentFile().mkdirs();
385 FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, null );
387 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/" + commonsLangJar );
388 InvocationContext ic = sc.newInvocation( request );
389 servlet = (RepositoryServlet) ic.getServlet();
390 servlet.setDavSessionProvider( davSessionProvider );
392 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
393 archivaDavResourceFactory.setHttpAuth( httpAuth );
394 archivaDavResourceFactory.setServletAuth( servletAuth );
396 servlet.setResourceFactory( archivaDavResourceFactory );
398 AuthenticationResult result = new AuthenticationResult();
399 httpAuthControl.expectAndReturn( httpAuth.getAuthenticationResult( null, null ), result );
400 servletAuthControl.expectAndThrow( servletAuth.isAuthenticated( null, null ),
401 new AuthenticationException( "Authentication error" ) );
402 servletAuthControl.expectAndReturn( servletAuth.isAuthorized( "guest", "internal", false ), true );
404 // ArchivaDavResourceFactory#isAuthorized()
405 SecuritySession session = new DefaultSecuritySession();
406 httpAuthControl.expectAndReturn( httpAuth.getAuthenticationResult( null, null ), result );
407 httpAuthControl.expectAndReturn( httpAuth.getSecuritySession( ic.getRequest().getSession( true ) ), session );
408 httpAuthControl.expectAndReturn( httpAuth.getSessionUser( ic.getRequest().getSession() ), null );
409 servletAuthControl.expectAndReturn( servletAuth.isAuthenticated( null, result ), true );
410 servletAuthControl.expectAndReturn( servletAuth.isAuthorized( null, session, "internal", true ), true );
412 httpAuthControl.replay();
413 servletAuthControl.replay();
415 WebResponse response = sc.getResponse( request );
417 httpAuthControl.verify();
418 servletAuthControl.verify();
420 assertEquals( HttpServletResponse.SC_OK, response.getResponseCode() );
421 assertEquals( "Expected file contents", expectedArtifactContents, response.getText() );
424 // test get with invalid user, and guest has no read access to repo
425 public void testGetWithInvalidUserAndGuestHasNoReadAccess()
428 String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
429 String expectedArtifactContents = "dummy-commons-lang-artifact";
431 File artifactFile = new File( repoRootInternal, commonsLangJar );
432 artifactFile.getParentFile().mkdirs();
434 FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, null );
436 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/" + commonsLangJar );
437 InvocationContext ic = sc.newInvocation( request );
438 servlet = (RepositoryServlet) ic.getServlet();
439 servlet.setDavSessionProvider( davSessionProvider );
441 AuthenticationResult result = new AuthenticationResult();
442 httpAuthControl.expectAndReturn( httpAuth.getAuthenticationResult( null, null ), result );
443 servletAuthControl.expectAndThrow( servletAuth.isAuthenticated( null, null ),
444 new AuthenticationException( "Authentication error" ) );
445 servletAuthControl.expectAndReturn( servletAuth.isAuthorized( "guest", "internal", false ), false );
447 httpAuthControl.replay();
448 servletAuthControl.replay();
450 WebResponse response = sc.getResponse( request );
452 httpAuthControl.verify();
453 servletAuthControl.verify();
455 assertEquals( HttpServletResponse.SC_UNAUTHORIZED, response.getResponseCode() );
458 // test get with valid user with read access to repo
459 public void testGetWithAValidUserWithReadAccess()
462 String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
463 String expectedArtifactContents = "dummy-commons-lang-artifact";
465 File artifactFile = new File( repoRootInternal, commonsLangJar );
466 artifactFile.getParentFile().mkdirs();
468 FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, null );
470 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/" + commonsLangJar );
471 InvocationContext ic = sc.newInvocation( request );
472 servlet = (RepositoryServlet) ic.getServlet();
473 servlet.setDavSessionProvider( davSessionProvider );
475 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
476 archivaDavResourceFactory.setHttpAuth( httpAuth );
477 archivaDavResourceFactory.setServletAuth( servletAuth );
479 servlet.setResourceFactory( archivaDavResourceFactory );
481 AuthenticationResult result = new AuthenticationResult();
482 httpAuthControl.expectAndReturn( httpAuth.getAuthenticationResult( null, null ), result );
483 servletAuthControl.expectAndReturn( servletAuth.isAuthenticated( null, null ), true );
485 // ArchivaDavResourceFactory#isAuthorized()
486 SecuritySession session = new DefaultSecuritySession();
487 httpAuthControl.expectAndReturn( httpAuth.getAuthenticationResult( null, null ), result );
488 httpAuthControl.expectAndReturn( httpAuth.getSecuritySession( ic.getRequest().getSession( true ) ), session );
489 httpAuthControl.expectAndReturn( httpAuth.getSessionUser( ic.getRequest().getSession() ), new SimpleUser() );
490 servletAuthControl.expectAndReturn( servletAuth.isAuthenticated( null, result ), true );
491 servletAuthControl.expectAndReturn( servletAuth.isAuthorized( null, session, "internal", true ), true );
493 httpAuthControl.replay();
494 servletAuthControl.replay();
496 WebResponse response = sc.getResponse( request );
498 httpAuthControl.verify();
499 servletAuthControl.verify();
501 assertEquals( HttpServletResponse.SC_OK, response.getResponseCode() );
502 assertEquals( "Expected file contents", expectedArtifactContents, response.getText() );
505 // test get with valid user with no read access to repo
506 public void testGetWithAValidUserWithNoReadAccess()
509 String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
510 String expectedArtifactContents = "dummy-commons-lang-artifact";
512 File artifactFile = new File( repoRootInternal, commonsLangJar );
513 artifactFile.getParentFile().mkdirs();
515 FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, null );
517 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/" + commonsLangJar );
518 InvocationContext ic = sc.newInvocation( request );
519 servlet = (RepositoryServlet) ic.getServlet();
520 servlet.setDavSessionProvider( davSessionProvider );
522 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
523 archivaDavResourceFactory.setHttpAuth( httpAuth );
524 archivaDavResourceFactory.setServletAuth( servletAuth );
526 servlet.setResourceFactory( archivaDavResourceFactory );
528 AuthenticationResult result = new AuthenticationResult();
529 httpAuthControl.expectAndReturn( httpAuth.getAuthenticationResult( null, null ), result );
530 servletAuthControl.expectAndReturn( servletAuth.isAuthenticated( null, null ), true );
532 // ArchivaDavResourceFactory#isAuthorized()
533 SecuritySession session = new DefaultSecuritySession();
534 httpAuthControl.expectAndReturn( httpAuth.getAuthenticationResult( null, null ), result );
535 httpAuthControl.expectAndReturn( httpAuth.getSecuritySession( ic.getRequest().getSession( true ) ), session );
536 servletAuthControl.expectAndReturn( servletAuth.isAuthenticated( null, result ), true );
537 servletAuthControl.expectAndThrow( servletAuth.isAuthorized( null, session, "internal", true ),
538 new UnauthorizedException( "User not authorized to read repository." ) );
540 httpAuthControl.replay();
541 servletAuthControl.replay();
543 WebResponse response = sc.getResponse( request );
545 httpAuthControl.verify();
546 servletAuthControl.verify();
548 assertEquals( HttpServletResponse.SC_UNAUTHORIZED, response.getResponseCode() );