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.ArchivaRoleConstants;
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 Test the flow of the authentication and authorization checks. This does not necessarily
60 * perform redback security checking.
64 public class RepositoryServletSecurityTest
65 extends PlexusInSpringTestCase
67 protected static final String REPOID_INTERNAL = "internal";
69 protected ServletUnitClient sc;
71 protected File repoRootInternal;
73 private ServletRunner sr;
75 protected ArchivaConfiguration archivaConfiguration;
77 private DavSessionProvider davSessionProvider;
79 private MockControl servletAuthControl;
81 private ServletAuthenticator servletAuth;
83 private MockClassControl httpAuthControl;
85 private HttpAuthenticator httpAuth;
87 private RepositoryServlet servlet;
94 String appserverBase = getTestFile( "target/appserver-base" ).getAbsolutePath();
95 System.setProperty( "appserver.base", appserverBase );
97 File testConf = getTestFile( "src/test/resources/repository-archiva.xml" );
98 File testConfDest = new File( appserverBase, "conf/archiva.xml" );
99 FileUtils.copyFile( testConf, testConfDest );
101 archivaConfiguration = (ArchivaConfiguration) lookup( ArchivaConfiguration.class );
102 repoRootInternal = new File( appserverBase, "data/repositories/internal" );
103 Configuration config = archivaConfiguration.getConfiguration();
105 config.addManagedRepository( createManagedRepository( REPOID_INTERNAL, "Internal Test Repo", repoRootInternal ) );
106 saveConfiguration( archivaConfiguration );
108 CacheManager.getInstance().removeCache( "url-failures-cache" );
110 HttpUnitOptions.setExceptionsThrownOnErrorStatus( false );
112 sr = new ServletRunner( getTestFile( "src/test/resources/WEB-INF/repository-servlet-security-test/web.xml" ) );
113 sr.registerServlet( "/repository/*", RepositoryServlet.class.getName() );
116 servletAuthControl = MockControl.createControl( ServletAuthenticator.class );
117 servletAuthControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
118 servletAuth = (ServletAuthenticator) servletAuthControl.getMock();
121 MockClassControl.createControl( HttpBasicAuthentication.class, HttpBasicAuthentication.class.getMethods() );
122 httpAuthControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
123 httpAuth = (HttpAuthenticator) httpAuthControl.getMock();
125 davSessionProvider = new ArchivaDavSessionProvider( servletAuth, httpAuth );
128 protected ManagedRepositoryConfiguration createManagedRepository( String id, String name, File location )
130 ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
132 repo.setName( name );
133 repo.setLocation( location.getAbsolutePath() );
137 protected void saveConfiguration()
140 saveConfiguration( archivaConfiguration );
143 protected void saveConfiguration( ArchivaConfiguration archivaConfiguration )
146 archivaConfiguration.save( archivaConfiguration.getConfiguration() );
149 protected void setupCleanRepo( File repoRootDir )
152 FileUtils.deleteDirectory( repoRootDir );
153 if ( !repoRootDir.exists() )
155 repoRootDir.mkdirs();
160 protected String getPlexusConfigLocation()
162 return "org/apache/maven/archiva/webdav/RepositoryServletSecurityTest.xml";
166 protected void tearDown()
179 if ( repoRootInternal.exists() )
181 FileUtils.deleteDirectory( repoRootInternal );
189 // test deploy with invalid user, and guest has no write access to repo
190 // 401 must be returned
191 public void testPutWithInvalidUserAndGuestHasNoWriteAccess()
194 setupCleanRepo( repoRootInternal );
196 String putUrl = "http://machine.com/repository/internal/path/to/artifact.jar";
197 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
198 assertNotNull( "artifact.jar inputstream", is );
200 WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
201 InvocationContext ic = sc.newInvocation( request );
202 servlet = (RepositoryServlet) ic.getServlet();
203 servlet.setDavSessionProvider( davSessionProvider );
205 AuthenticationResult result = new AuthenticationResult();
206 httpAuthControl.expectAndReturn( httpAuth.getAuthenticationResult( null, null ), result );
207 servletAuthControl.expectAndThrow( servletAuth.isAuthenticated( null, null ),
208 new AuthenticationException( "Authentication error" ) );
210 servletAuth.isAuthorized( "guest", "internal", ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD );
211 servletAuthControl.setMatcher( MockControl.EQUALS_MATCHER );
212 servletAuthControl.setThrowable( new UnauthorizedException( "'guest' has no write access to repository" ) );
214 httpAuthControl.replay();
215 servletAuthControl.replay();
217 servlet.service( ic.getRequest(), ic.getResponse() );
219 httpAuthControl.verify();
220 servletAuthControl.verify();
222 // assertEquals(HttpServletResponse.SC_UNAUTHORIZED, response.getResponseCode());
225 // test deploy with invalid user, but guest has write access to repo
226 public void testPutWithInvalidUserAndGuestHasWriteAccess()
229 setupCleanRepo( repoRootInternal );
231 String putUrl = "http://machine.com/repository/internal/path/to/artifact.jar";
232 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
233 assertNotNull( "artifact.jar inputstream", is );
235 WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
237 InvocationContext ic = sc.newInvocation( request );
238 servlet = (RepositoryServlet) ic.getServlet();
239 servlet.setDavSessionProvider( davSessionProvider );
241 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
242 archivaDavResourceFactory.setHttpAuth( httpAuth );
243 archivaDavResourceFactory.setServletAuth( servletAuth );
245 servlet.setResourceFactory( archivaDavResourceFactory );
247 AuthenticationResult result = new AuthenticationResult();
248 httpAuthControl.expectAndReturn( httpAuth.getAuthenticationResult( null, null ), result );
249 servletAuthControl.expectAndThrow( servletAuth.isAuthenticated( null, null ),
250 new AuthenticationException( "Authentication error" ) );
252 servletAuth.isAuthorized( "guest", "internal", ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD );
253 servletAuthControl.setMatcher( MockControl.EQUALS_MATCHER );
254 servletAuthControl.setReturnValue( true );
256 // ArchivaDavResourceFactory#isAuthorized()
257 SecuritySession session = new DefaultSecuritySession();
258 httpAuthControl.expectAndReturn( httpAuth.getAuthenticationResult( null, null ), result );
259 httpAuthControl.expectAndReturn( httpAuth.getSecuritySession( ic.getRequest().getSession( true ) ), session );
260 servletAuthControl.expectAndThrow( servletAuth.isAuthenticated( null, result ),
261 new AuthenticationException( "Authentication error" ) );
263 httpAuthControl.expectAndReturn( httpAuth.getSessionUser( ic.getRequest().getSession() ), null );
265 // check if guest has write access
266 servletAuth.isAuthorized( "guest", "internal", ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD );
267 servletAuthControl.setMatcher( MockControl.EQUALS_MATCHER );
268 servletAuthControl.setReturnValue( true );
270 httpAuthControl.replay();
271 servletAuthControl.replay();
273 servlet.service( ic.getRequest(), ic.getResponse() );
275 httpAuthControl.verify();
276 servletAuthControl.verify();
278 // assertEquals( HttpServletResponse.SC_CREATED, response.getResponseCode() );
281 // test deploy with a valid user with no write access
282 public void testPutWithValidUserWithNoWriteAccess()
285 setupCleanRepo( repoRootInternal );
287 String putUrl = "http://machine.com/repository/internal/path/to/artifact.jar";
288 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
289 assertNotNull( "artifact.jar inputstream", is );
291 WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
293 InvocationContext ic = sc.newInvocation( request );
294 servlet = (RepositoryServlet) ic.getServlet();
295 servlet.setDavSessionProvider( davSessionProvider );
297 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
298 archivaDavResourceFactory.setHttpAuth( httpAuth );
299 archivaDavResourceFactory.setServletAuth( servletAuth );
300 servlet.setResourceFactory( archivaDavResourceFactory );
302 AuthenticationResult result = new AuthenticationResult();
303 httpAuthControl.expectAndReturn( httpAuth.getAuthenticationResult( null, null ), result );
304 servletAuthControl.expectAndReturn( servletAuth.isAuthenticated( null, null ), true );
306 // ArchivaDavResourceFactory#isAuthorized()
307 SecuritySession session = new DefaultSecuritySession();
308 httpAuthControl.expectAndReturn( httpAuth.getAuthenticationResult( null, null ), result );
309 httpAuthControl.expectAndReturn( httpAuth.getSecuritySession( ic.getRequest().getSession( true ) ), session );
310 httpAuthControl.expectAndReturn( httpAuth.getSessionUser( ic.getRequest().getSession() ), new SimpleUser() );
311 servletAuthControl.expectAndReturn( servletAuth.isAuthenticated( null, result ), true );
312 servletAuthControl.expectAndThrow(
313 servletAuth.isAuthorized( null, session, "internal",
314 ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD ),
315 new UnauthorizedException( "User not authorized" ) );
317 httpAuthControl.replay();
318 servletAuthControl.replay();
320 servlet.service( ic.getRequest(), ic.getResponse() );
322 httpAuthControl.verify();
323 servletAuthControl.verify();
325 // assertEquals(HttpServletResponse.SC_UNAUTHORIZED, response.getResponseCode());
328 // test deploy with a valid user with write access
329 public void testPutWithValidUserWithWriteAccess()
332 setupCleanRepo( repoRootInternal );
333 assertTrue( repoRootInternal.exists() );
335 String putUrl = "http://machine.com/repository/internal/path/to/artifact.jar";
336 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
337 assertNotNull( "artifact.jar inputstream", is );
339 WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
341 InvocationContext ic = sc.newInvocation( request );
342 servlet = (RepositoryServlet) ic.getServlet();
343 servlet.setDavSessionProvider( davSessionProvider );
345 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
346 archivaDavResourceFactory.setHttpAuth( httpAuth );
347 archivaDavResourceFactory.setServletAuth( servletAuth );
349 servlet.setResourceFactory( archivaDavResourceFactory );
351 AuthenticationResult result = new AuthenticationResult();
352 httpAuthControl.expectAndReturn( httpAuth.getAuthenticationResult( null, null ), result );
353 servletAuthControl.expectAndReturn( servletAuth.isAuthenticated( null, null ), true );
355 // ArchivaDavResourceFactory#isAuthorized()
356 SecuritySession session = new DefaultSecuritySession();
357 httpAuthControl.expectAndReturn( httpAuth.getAuthenticationResult( null, null ), result );
358 httpAuthControl.expectAndReturn( httpAuth.getSecuritySession( ic.getRequest().getSession( true ) ), session );
359 httpAuthControl.expectAndReturn( httpAuth.getSessionUser( ic.getRequest().getSession() ), new SimpleUser() );
360 servletAuthControl.expectAndReturn( servletAuth.isAuthenticated( null, result ), true );
361 servletAuthControl.expectAndReturn(
362 servletAuth.isAuthorized( null, session, "internal",
363 ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD ),
366 httpAuthControl.replay();
367 servletAuthControl.replay();
369 servlet.service( ic.getRequest(), ic.getResponse() );
371 httpAuthControl.verify();
372 servletAuthControl.verify();
374 // assertEquals(HttpServletResponse.SC_CREATED, response.getResponseCode());
377 // test get with invalid user, and guest has read access to repo
378 public void testGetWithInvalidUserAndGuestHasReadAccess()
381 String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
382 String expectedArtifactContents = "dummy-commons-lang-artifact";
384 File artifactFile = new File( repoRootInternal, commonsLangJar );
385 artifactFile.getParentFile().mkdirs();
387 FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, null );
389 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/" + commonsLangJar );
390 InvocationContext ic = sc.newInvocation( request );
391 servlet = (RepositoryServlet) ic.getServlet();
392 servlet.setDavSessionProvider( davSessionProvider );
394 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
395 archivaDavResourceFactory.setHttpAuth( httpAuth );
396 archivaDavResourceFactory.setServletAuth( servletAuth );
398 servlet.setResourceFactory( archivaDavResourceFactory );
400 AuthenticationResult result = new AuthenticationResult();
401 httpAuthControl.expectAndReturn( httpAuth.getAuthenticationResult( null, null ), result );
402 servletAuthControl.expectAndThrow( servletAuth.isAuthenticated( null, null ),
403 new AuthenticationException( "Authentication error" ) );
404 servletAuthControl.expectAndReturn(
405 servletAuth.isAuthorized( "guest", "internal",
406 ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS ),
409 // ArchivaDavResourceFactory#isAuthorized()
410 SecuritySession session = new DefaultSecuritySession();
411 httpAuthControl.expectAndReturn( httpAuth.getAuthenticationResult( null, null ), result );
412 httpAuthControl.expectAndReturn( httpAuth.getSecuritySession( ic.getRequest().getSession( true ) ), session );
413 httpAuthControl.expectAndReturn( httpAuth.getSessionUser( ic.getRequest().getSession() ), null );
414 servletAuthControl.expectAndReturn( servletAuth.isAuthenticated( null, result ), true );
415 servletAuthControl.expectAndReturn(
416 servletAuth.isAuthorized( null, session, "internal",
417 ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD ),
420 httpAuthControl.replay();
421 servletAuthControl.replay();
423 WebResponse response = sc.getResponse( request );
425 httpAuthControl.verify();
426 servletAuthControl.verify();
428 assertEquals( HttpServletResponse.SC_OK, response.getResponseCode() );
429 assertEquals( "Expected file contents", expectedArtifactContents, response.getText() );
432 // test get with invalid user, and guest has no read access to repo
433 public void testGetWithInvalidUserAndGuestHasNoReadAccess()
436 String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
437 String expectedArtifactContents = "dummy-commons-lang-artifact";
439 File artifactFile = new File( repoRootInternal, commonsLangJar );
440 artifactFile.getParentFile().mkdirs();
442 FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, null );
444 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/" + commonsLangJar );
445 InvocationContext ic = sc.newInvocation( request );
446 servlet = (RepositoryServlet) ic.getServlet();
447 servlet.setDavSessionProvider( davSessionProvider );
449 AuthenticationResult result = new AuthenticationResult();
450 httpAuthControl.expectAndReturn( httpAuth.getAuthenticationResult( null, null ), result );
451 servletAuthControl.expectAndThrow( servletAuth.isAuthenticated( null, null ),
452 new AuthenticationException( "Authentication error" ) );
453 servletAuthControl.expectAndReturn(
454 servletAuth.isAuthorized( "guest", "internal",
455 ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS ),
458 httpAuthControl.replay();
459 servletAuthControl.replay();
461 WebResponse response = sc.getResponse( request );
463 httpAuthControl.verify();
464 servletAuthControl.verify();
466 assertEquals( HttpServletResponse.SC_UNAUTHORIZED, response.getResponseCode() );
469 // test get with valid user with read access to repo
470 public void testGetWithAValidUserWithReadAccess()
473 String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
474 String expectedArtifactContents = "dummy-commons-lang-artifact";
476 File artifactFile = new File( repoRootInternal, commonsLangJar );
477 artifactFile.getParentFile().mkdirs();
479 FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, null );
481 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/" + commonsLangJar );
482 InvocationContext ic = sc.newInvocation( request );
483 servlet = (RepositoryServlet) ic.getServlet();
484 servlet.setDavSessionProvider( davSessionProvider );
486 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
487 archivaDavResourceFactory.setHttpAuth( httpAuth );
488 archivaDavResourceFactory.setServletAuth( servletAuth );
490 servlet.setResourceFactory( archivaDavResourceFactory );
492 AuthenticationResult result = new AuthenticationResult();
493 httpAuthControl.expectAndReturn( httpAuth.getAuthenticationResult( null, null ), result );
494 servletAuthControl.expectAndReturn( servletAuth.isAuthenticated( null, null ), true );
496 // ArchivaDavResourceFactory#isAuthorized()
497 SecuritySession session = new DefaultSecuritySession();
498 httpAuthControl.expectAndReturn( httpAuth.getAuthenticationResult( null, null ), result );
499 httpAuthControl.expectAndReturn( httpAuth.getSecuritySession( ic.getRequest().getSession( true ) ), session );
500 httpAuthControl.expectAndReturn( httpAuth.getSessionUser( ic.getRequest().getSession() ), new SimpleUser() );
501 servletAuthControl.expectAndReturn( servletAuth.isAuthenticated( null, result ), true );
502 servletAuthControl.expectAndReturn(
503 servletAuth.isAuthorized( null, session, "internal",
504 ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD ),
507 httpAuthControl.replay();
508 servletAuthControl.replay();
510 WebResponse response = sc.getResponse( request );
512 httpAuthControl.verify();
513 servletAuthControl.verify();
515 assertEquals( HttpServletResponse.SC_OK, response.getResponseCode() );
516 assertEquals( "Expected file contents", expectedArtifactContents, response.getText() );
519 // test get with valid user with no read access to repo
520 public void testGetWithAValidUserWithNoReadAccess()
523 String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
524 String expectedArtifactContents = "dummy-commons-lang-artifact";
526 File artifactFile = new File( repoRootInternal, commonsLangJar );
527 artifactFile.getParentFile().mkdirs();
529 FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, null );
531 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/" + commonsLangJar );
532 InvocationContext ic = sc.newInvocation( request );
533 servlet = (RepositoryServlet) ic.getServlet();
534 servlet.setDavSessionProvider( davSessionProvider );
536 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
537 archivaDavResourceFactory.setHttpAuth( httpAuth );
538 archivaDavResourceFactory.setServletAuth( servletAuth );
540 servlet.setResourceFactory( archivaDavResourceFactory );
542 AuthenticationResult result = new AuthenticationResult();
543 httpAuthControl.expectAndReturn( httpAuth.getAuthenticationResult( null, null ), result );
544 servletAuthControl.expectAndReturn( servletAuth.isAuthenticated( null, null ), true );
546 // ArchivaDavResourceFactory#isAuthorized()
547 SecuritySession session = new DefaultSecuritySession();
548 httpAuthControl.expectAndReturn( httpAuth.getAuthenticationResult( null, null ), result );
549 httpAuthControl.expectAndReturn( httpAuth.getSecuritySession( ic.getRequest().getSession( true ) ), session );
550 httpAuthControl.expectAndReturn( httpAuth.getSessionUser( ic.getRequest().getSession() ), new SimpleUser() );
551 servletAuthControl.expectAndReturn( servletAuth.isAuthenticated( null, result ), true );
552 servletAuthControl.expectAndThrow(
553 servletAuth.isAuthorized( null, session, "internal",
554 ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD ),
555 new UnauthorizedException( "User not authorized to read repository." ) );
557 httpAuthControl.replay();
558 servletAuthControl.replay();
560 WebResponse response = sc.getResponse( request );
562 httpAuthControl.verify();
563 servletAuthControl.verify();
565 assertEquals( HttpServletResponse.SC_UNAUTHORIZED, response.getResponseCode() );