1 package org.apache.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 junit.framework.TestCase;
24 import net.sf.ehcache.CacheManager;
25 import org.apache.archiva.configuration.ArchivaConfiguration;
26 import org.apache.archiva.configuration.Configuration;
27 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
28 import org.apache.archiva.redback.authentication.AuthenticationException;
29 import org.apache.archiva.redback.authentication.AuthenticationResult;
30 import org.apache.archiva.redback.authorization.UnauthorizedException;
31 import org.apache.archiva.redback.integration.filter.authentication.HttpAuthenticator;
32 import org.apache.archiva.redback.system.DefaultSecuritySession;
33 import org.apache.archiva.redback.system.SecuritySession;
34 import org.apache.archiva.redback.users.User;
35 import org.apache.archiva.redback.users.memory.SimpleUser;
36 import org.apache.archiva.repository.RepositoryRegistry;
37 import org.apache.archiva.metadata.audit.TestAuditListener;
38 import org.apache.archiva.repository.base.group.RepositoryGroupHandler;
39 import org.apache.archiva.security.ServletAuthenticator;
40 import org.apache.archiva.security.common.ArchivaRoleConstants;
41 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
42 import org.apache.commons.io.FileUtils;
43 import org.apache.commons.io.IOUtils;
44 import org.apache.commons.lang3.StringUtils;
45 import org.apache.jackrabbit.webdav.DavSessionProvider;
46 import org.easymock.EasyMock;
47 import org.easymock.IMocksControl;
48 import org.junit.After;
49 import org.junit.Before;
50 import org.junit.Rule;
51 import org.junit.Test;
52 import org.junit.runner.RunWith;
53 import org.springframework.context.ApplicationContext;
54 import org.springframework.mock.web.MockHttpServletRequest;
55 import org.springframework.mock.web.MockHttpServletResponse;
56 import org.springframework.mock.web.MockServletConfig;
57 import org.springframework.mock.web.MockServletContext;
58 import org.springframework.test.context.ContextConfiguration;
59 import org.springframework.web.context.WebApplicationContext;
61 import javax.inject.Inject;
62 import javax.servlet.ServletContext;
63 import javax.servlet.http.HttpServletRequest;
64 import javax.servlet.http.HttpServletResponse;
65 import javax.servlet.http.HttpSession;
66 import java.io.InputStream;
67 import java.nio.charset.Charset;
68 import java.nio.file.Files;
69 import java.nio.file.Path;
70 import java.nio.file.Paths;
71 import java.util.ArrayList;
72 import java.util.List;
73 import java.util.concurrent.atomic.AtomicReference;
75 import static org.easymock.EasyMock.anyObject;
76 import static org.easymock.EasyMock.eq;
79 * RepositoryServletSecurityTest Test the flow of the authentication and authorization checks. This does not necessarily
80 * perform redback security checking.
82 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
83 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context-servlet-security-test.xml" } )
84 public class RepositoryServletSecurityTest
87 protected static final String REPOID_INTERNAL = "internal";
90 protected ArchivaConfiguration archivaConfiguration;
93 protected RepositoryRegistry repositoryRegistry;
95 @SuppressWarnings( "unused" )
97 RepositoryGroupHandler repositoryGroupHandler;
99 private DavSessionProvider davSessionProvider;
101 private IMocksControl servletAuthControl;
103 private ServletAuthenticator servletAuth;
105 private IMocksControl httpAuthControl;
107 private HttpAuthenticator httpAuth;
109 private RepositoryServlet servlet;
112 ApplicationContext applicationContext;
116 public ArchivaTemporaryFolderRule repoRootInternal = new ArchivaTemporaryFolderRule();
118 private AtomicReference<Path> projectBase = new AtomicReference<>( );
120 public Path getProjectBase() {
121 if (this.projectBase.get()==null) {
122 String pathVal = System.getProperty("mvn.project.base.dir");
124 if ( StringUtils.isEmpty(pathVal)) {
125 baseDir= Paths.get("").toAbsolutePath();
127 baseDir = Paths.get(pathVal).toAbsolutePath();
129 this.projectBase.compareAndSet(null, baseDir);
131 return this.projectBase.get();
142 String appserverBase =
143 System.getProperty( "appserver.base", getProjectBase().resolve( "target/appserver-base" ).toAbsolutePath().toString() );
145 Path testConf = getProjectBase().resolve( "src/test/resources/repository-archiva.xml" );
146 Path testConfDest = Paths.get(appserverBase, "conf/archiva.xml" );
147 FileUtils.copyFile( testConf.toFile(), testConfDest.toFile() );
151 Configuration config = archivaConfiguration.getConfiguration();
152 // clear managed repository
153 List<ManagedRepositoryConfiguration> f1 = new ArrayList<>(config.getManagedRepositories());
154 for (ManagedRepositoryConfiguration f: f1 ) {
155 config.removeManagedRepository(f);
157 assertEquals(0,config.getManagedRepositories().size());
159 config.addManagedRepository(
160 createManagedRepository( REPOID_INTERNAL, "Internal Test Repo", repoRootInternal.getRoot() ) );
162 saveConfiguration( archivaConfiguration );
164 CacheManager.getInstance().clearAll();
167 servletAuthControl = EasyMock.createControl();
169 servletAuth = servletAuthControl.createMock( ServletAuthenticator.class );
171 httpAuthControl = EasyMock.createControl();
173 httpAuth = httpAuthControl.createMock( HttpAuthenticator.class );
175 davSessionProvider = new ArchivaDavSessionProvider( servletAuth, httpAuth );
177 final MockServletContext mockServletContext = new MockServletContext();
179 WebApplicationContext webApplicationContext =
180 new AbstractRepositoryServletTestCase.TestWebapplicationContext( applicationContext, mockServletContext );
182 mockServletContext.setAttribute( WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
183 webApplicationContext );
185 MockServletConfig mockServletConfig = new MockServletConfig()
188 public ServletContext getServletContext()
190 return mockServletContext;
194 servlet = new RepositoryServlet();
196 servlet.init( mockServletConfig );
199 protected ManagedRepositoryConfiguration createManagedRepository( String id, String name, Path location )
201 ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
203 repo.setName( name );
204 repo.setLocation( location.toAbsolutePath().toString() );
208 /*protected void saveConfiguration()
211 saveConfiguration( archivaConfiguration );
214 protected void saveConfiguration( ArchivaConfiguration archivaConfiguration )
217 repositoryRegistry.reload();
218 archivaConfiguration.save( archivaConfiguration.getConfiguration() );
221 /*protected void setupCleanRepo( File repoRootDir )
228 public void tearDown()
232 /* if ( repoRootInternal.exists() )
234 FileUtils.deleteDirectory( repoRootInternal );
238 String appBaseProp = System.getProperty( "appserver.base" );
239 if (StringUtils.isNotEmpty( appBaseProp )) {
240 org.apache.archiva.common.utils.FileUtils.deleteDirectory( Paths.get(appBaseProp) );
246 // test deploy with invalid user, and guest has no write access to repo
247 // 401 must be returned
249 public void testPutWithInvalidUserAndGuestHasNoWriteAccess()
253 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
254 assertNotNull( "artifact.jar inputstream", is );
256 servlet.setDavSessionProvider( davSessionProvider );
258 AuthenticationResult result = new AuthenticationResult();
260 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
261 anyObject( HttpServletResponse.class ) ) ).andReturn(
264 servletAuth.isAuthenticated( EasyMock.anyObject( HttpServletRequest.class ),
265 EasyMock.anyObject( AuthenticationResult.class ) );
266 EasyMock.expectLastCall().andThrow( new AuthenticationException( "Authentication error" ) );
268 servletAuth.isAuthorized( "guest", "internal", ArchivaRoleConstants.OPERATION_ADD_ARTIFACT );
270 EasyMock.expectLastCall().andThrow( new UnauthorizedException( "'guest' has no write access to repository" ) );
272 httpAuthControl.replay();
273 servletAuthControl.replay();
274 MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
275 mockHttpServletRequest.addHeader( "User-Agent", "foo" );
276 mockHttpServletRequest.setMethod( "PUT" );
277 mockHttpServletRequest.setRequestURI( "/repository/internal/path/to/artifact.jar" );
278 mockHttpServletRequest.setContent( IOUtils.toByteArray( is ) );
279 mockHttpServletRequest.setContentType( "application/octet-stream" );
281 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
283 servlet.service( mockHttpServletRequest, mockHttpServletResponse );
285 httpAuthControl.verify();
286 servletAuthControl.verify();
288 assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() );
291 // test deploy with invalid user, but guest has write access to repo
293 public void testPutWithInvalidUserAndGuestHasWriteAccess()
297 servlet.setDavSessionProvider( davSessionProvider );
299 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
300 archivaDavResourceFactory.setHttpAuth( httpAuth );
301 archivaDavResourceFactory.setServletAuth( servletAuth );
303 servlet.setResourceFactory( archivaDavResourceFactory );
305 AuthenticationResult result = new AuthenticationResult();
307 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
308 anyObject( HttpServletResponse.class ) ) ).andReturn(
311 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
312 anyObject( AuthenticationResult.class ) ) ).andThrow(
313 new AuthenticationException( "Authentication error" ) );
315 EasyMock.expect( servletAuth.isAuthorized( "guest", "internal",
316 ArchivaRoleConstants.OPERATION_ADD_ARTIFACT ) ).andReturn(
319 // ArchivaDavResourceFactory#isAuthorized()
320 SecuritySession session = new DefaultSecuritySession();
322 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
323 anyObject( HttpServletResponse.class ) ) ).andReturn(
326 EasyMock.expect( httpAuth.getSecuritySession( anyObject( HttpSession.class ) ) ).andReturn( session );
328 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq( result ) ) ).andThrow(
329 new AuthenticationException( "Authentication error" ) );
331 EasyMock.expect( httpAuth.getSessionUser( anyObject( HttpSession.class ) ) ).andReturn( null );
333 // check if guest has write access
334 EasyMock.expect( servletAuth.isAuthorized( "guest", "internal",
335 ArchivaRoleConstants.OPERATION_ADD_ARTIFACT ) ).andReturn(
338 httpAuthControl.replay();
339 servletAuthControl.replay();
341 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
342 assertNotNull( "artifact.jar inputstream", is );
344 MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
345 mockHttpServletRequest.addHeader( "User-Agent", "foo" );
346 mockHttpServletRequest.setMethod( "PUT" );
347 mockHttpServletRequest.setRequestURI( "/repository/internal/path/to/artifact.jar" );
348 mockHttpServletRequest.setContent( IOUtils.toByteArray( is ) );
349 mockHttpServletRequest.setContentType( "application/octet-stream" );
351 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
353 servlet.service( mockHttpServletRequest, mockHttpServletResponse );
355 httpAuthControl.verify();
356 servletAuthControl.verify();
358 assertEquals( HttpServletResponse.SC_CREATED, mockHttpServletResponse.getStatus() );
361 // test deploy with a valid user with no write access
363 public void testPutWithValidUserWithNoWriteAccess()
367 servlet.setDavSessionProvider( davSessionProvider );
369 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
370 archivaDavResourceFactory.setHttpAuth( httpAuth );
371 archivaDavResourceFactory.setServletAuth( servletAuth );
372 servlet.setResourceFactory( archivaDavResourceFactory );
374 AuthenticationResult result = new AuthenticationResult();
376 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
377 anyObject( HttpServletResponse.class ) ) ).andReturn(
380 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
381 anyObject( AuthenticationResult.class ) ) ).andReturn( true );
383 // ArchivaDavResourceFactory#isAuthorized()
384 SecuritySession session = new DefaultSecuritySession();
386 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
387 anyObject( HttpServletResponse.class ) ) ).andReturn(
390 MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
392 EasyMock.expect( httpAuth.getSecuritySession( mockHttpServletRequest.getSession( true ) ) ).andReturn(
395 EasyMock.expect( httpAuth.getSessionUser( mockHttpServletRequest.getSession() ) ).andReturn( new SimpleUser() );
397 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq( result ) ) ).andReturn(
401 servletAuth.isAuthorized( anyObject( HttpServletRequest.class ), eq( session ), eq( "internal" ),
402 eq( ArchivaRoleConstants.OPERATION_ADD_ARTIFACT ) ) ).andThrow(
403 new UnauthorizedException( "User not authorized" ) );
404 httpAuthControl.replay();
405 servletAuthControl.replay();
407 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
408 assertNotNull( "artifact.jar inputstream", is );
410 mockHttpServletRequest.addHeader( "User-Agent", "foo" );
411 mockHttpServletRequest.setMethod( "PUT" );
412 mockHttpServletRequest.setRequestURI( "/repository/internal/path/to/artifact.jar" );
413 mockHttpServletRequest.setContent( IOUtils.toByteArray( is ) );
414 mockHttpServletRequest.setContentType( "application/octet-stream" );
416 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
418 servlet.service( mockHttpServletRequest, mockHttpServletResponse );
420 httpAuthControl.verify();
421 servletAuthControl.verify();
423 assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() );
426 // test deploy with a valid user with write access
428 public void testPutWithValidUserWithWriteAccess()
431 assertTrue( Files.exists(repoRootInternal.getRoot()) );
433 MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
434 String putUrl = "http://machine.com/repository/internal/path/to/artifact.jar";
435 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
436 assertNotNull( "artifact.jar inputstream", is );
438 servlet.setDavSessionProvider( davSessionProvider );
440 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
441 archivaDavResourceFactory.setHttpAuth( httpAuth );
442 archivaDavResourceFactory.setServletAuth( servletAuth );
444 TestAuditListener listener = new TestAuditListener();
445 archivaDavResourceFactory.addAuditListener( listener );
446 servlet.setResourceFactory( archivaDavResourceFactory );
448 AuthenticationResult result = new AuthenticationResult();
450 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
451 anyObject( HttpServletResponse.class ) ) ).andReturn(
454 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
455 anyObject( AuthenticationResult.class ) ) ).andReturn( true );
457 User user = new SimpleUser();
458 user.setUsername( "admin" );
460 // ArchivaDavResourceFactory#isAuthorized()
461 SecuritySession session = new DefaultSecuritySession();
463 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
464 anyObject( HttpServletResponse.class ) ) ).andReturn(
467 EasyMock.expect( httpAuth.getSecuritySession( mockHttpServletRequest.getSession() ) ).andReturn( session );
469 EasyMock.expect( httpAuth.getSessionUser( mockHttpServletRequest.getSession() ) ).andReturn( user );
471 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq( result ) ) ).andReturn(
475 servletAuth.isAuthorized( anyObject( HttpServletRequest.class ), eq( session ), eq( "internal" ),
476 eq( ArchivaRoleConstants.OPERATION_ADD_ARTIFACT ) ) ).andReturn( true );
478 httpAuthControl.replay();
479 servletAuthControl.replay();
481 mockHttpServletRequest.addHeader( "User-Agent", "foo" );
482 mockHttpServletRequest.setMethod( "PUT" );
483 mockHttpServletRequest.setRequestURI( "/repository/internal/path/to/artifact.jar" );
484 mockHttpServletRequest.setContent( IOUtils.toByteArray( is ) );
485 mockHttpServletRequest.setContentType( "application/octet-stream" );
487 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
489 servlet.service( mockHttpServletRequest, mockHttpServletResponse );
491 httpAuthControl.verify();
492 servletAuthControl.verify();
494 assertEquals( HttpServletResponse.SC_CREATED, mockHttpServletResponse.getStatus() );
496 assertEquals( "admin", listener.getEvents().get( 0 ).getUserId() );
499 // test get with invalid user, and guest has read access to repo
501 public void testGetWithInvalidUserAndGuestHasReadAccess()
504 String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
505 String expectedArtifactContents = "dummy-commons-lang-artifact";
507 Path artifactFile = repoRootInternal.getRoot().resolve( commonsLangJar );
508 Files.createDirectories(artifactFile.getParent());
510 org.apache.archiva.common.utils.FileUtils.writeStringToFile( artifactFile, Charset.defaultCharset() , expectedArtifactContents);
512 servlet.setDavSessionProvider( davSessionProvider );
514 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
515 archivaDavResourceFactory.setHttpAuth( httpAuth );
516 archivaDavResourceFactory.setServletAuth( servletAuth );
518 servlet.setResourceFactory( archivaDavResourceFactory );
520 AuthenticationResult result = new AuthenticationResult();
522 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
523 anyObject( HttpServletResponse.class ) ) ).andReturn(
526 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
527 anyObject( AuthenticationResult.class ) ) ).andThrow(
528 new AuthenticationException( "Authentication error" ) );
530 EasyMock.expect( servletAuth.isAuthorized( "guest", "internal",
531 ArchivaRoleConstants.OPERATION_READ_REPOSITORY ) ).andReturn(
534 // ArchivaDavResourceFactory#isAuthorized()
535 SecuritySession session = new DefaultSecuritySession();
537 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
538 anyObject( HttpServletResponse.class ) ) ).andReturn(
541 EasyMock.expect( httpAuth.getSecuritySession( anyObject( HttpSession.class ) ) ).andReturn( session );
543 EasyMock.expect( httpAuth.getSessionUser( anyObject( HttpSession.class ) ) ).andReturn( null );
545 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq( result ) ) ).andReturn(
549 servletAuth.isAuthorized( anyObject( HttpServletRequest.class ), eq( session ), eq( "internal" ),
550 eq( ArchivaRoleConstants.OPERATION_READ_REPOSITORY ) ) ).andReturn( true );
551 httpAuthControl.replay();
552 servletAuthControl.replay();
554 MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
555 mockHttpServletRequest.addHeader( "User-Agent", "foo" );
556 mockHttpServletRequest.setMethod( "GET" );
557 mockHttpServletRequest.setRequestURI( "/repository/internal/" + commonsLangJar );
559 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
561 servlet.service( mockHttpServletRequest, mockHttpServletResponse );
563 httpAuthControl.verify();
564 servletAuthControl.verify();
566 assertEquals( HttpServletResponse.SC_OK, mockHttpServletResponse.getStatus() );
568 assertEquals( "Expected file contents", expectedArtifactContents, mockHttpServletResponse.getContentAsString() );
571 // test get with invalid user, and guest has no read access to repo
573 public void testGetWithInvalidUserAndGuestHasNoReadAccess()
576 String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
577 String expectedArtifactContents = "dummy-commons-lang-artifact";
579 Path artifactFile = repoRootInternal.getRoot().resolve( commonsLangJar );
580 Files.createDirectories(artifactFile.getParent());
582 org.apache.archiva.common.utils.FileUtils.writeStringToFile( artifactFile, Charset.defaultCharset() , expectedArtifactContents);
584 servlet.setDavSessionProvider( davSessionProvider );
586 AuthenticationResult result = new AuthenticationResult();
588 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
589 anyObject( HttpServletResponse.class ) ) ).andReturn(
592 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
593 anyObject( AuthenticationResult.class ) ) ).andThrow(
594 new AuthenticationException( "Authentication error" ) );
596 EasyMock.expect( servletAuth.isAuthorized( "guest", "internal",
597 ArchivaRoleConstants.OPERATION_READ_REPOSITORY ) ).andReturn(
599 httpAuthControl.replay();
600 servletAuthControl.replay();
602 MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
603 mockHttpServletRequest.addHeader( "User-Agent", "foo" );
604 mockHttpServletRequest.setMethod( "GET" );
605 mockHttpServletRequest.setRequestURI( "/repository/internal/" + commonsLangJar );
607 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
609 servlet.service( mockHttpServletRequest, mockHttpServletResponse );
611 httpAuthControl.verify();
612 servletAuthControl.verify();
614 assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() );
617 // test get with valid user with read access to repo
619 public void testGetWithAValidUserWithReadAccess()
622 String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
623 String expectedArtifactContents = "dummy-commons-lang-artifact";
625 Path artifactFile = repoRootInternal.getRoot().resolve( commonsLangJar );
626 Files.createDirectories(artifactFile.getParent());
628 org.apache.archiva.common.utils.FileUtils.writeStringToFile( artifactFile, Charset.defaultCharset() , expectedArtifactContents);
630 servlet.setDavSessionProvider( davSessionProvider );
632 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
633 archivaDavResourceFactory.setHttpAuth( httpAuth );
634 archivaDavResourceFactory.setServletAuth( servletAuth );
636 servlet.setResourceFactory( archivaDavResourceFactory );
638 AuthenticationResult result = new AuthenticationResult();
640 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
641 anyObject( HttpServletResponse.class ) ) ).andReturn(
644 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
645 anyObject( AuthenticationResult.class ) ) ).andReturn( true );
646 // ArchivaDavResourceFactory#isAuthorized()
647 SecuritySession session = new DefaultSecuritySession();
649 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
650 anyObject( HttpServletResponse.class ) ) ).andReturn(
653 EasyMock.expect( httpAuth.getSecuritySession( anyObject( HttpSession.class ) ) ).andReturn( session );
655 EasyMock.expect( httpAuth.getSessionUser( anyObject( HttpSession.class ) ) ).andReturn( new SimpleUser() );
657 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq( result ) ) ).andReturn(
661 servletAuth.isAuthorized( anyObject( HttpServletRequest.class ), eq( session ), eq( "internal" ),
662 eq( ArchivaRoleConstants.OPERATION_READ_REPOSITORY ) ) ).andReturn( true );
664 httpAuthControl.replay();
665 servletAuthControl.replay();
667 MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
668 mockHttpServletRequest.addHeader( "User-Agent", "foo" );
669 mockHttpServletRequest.setMethod( "GET" );
670 mockHttpServletRequest.setRequestURI( "/repository/internal/" + commonsLangJar );
672 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
674 servlet.service( mockHttpServletRequest, mockHttpServletResponse );
676 httpAuthControl.verify();
677 servletAuthControl.verify();
679 assertEquals( HttpServletResponse.SC_OK, mockHttpServletResponse.getStatus() );
680 assertEquals( "Expected file contents", expectedArtifactContents, mockHttpServletResponse.getContentAsString() );
683 // test get with valid user with no read access to repo
685 public void testGetWithAValidUserWithNoReadAccess()
688 String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
689 String expectedArtifactContents = "dummy-commons-lang-artifact";
691 Path artifactFile = repoRootInternal.getRoot().resolve( commonsLangJar );
692 Files.createDirectories(artifactFile.getParent());
694 org.apache.archiva.common.utils.FileUtils.writeStringToFile( artifactFile, Charset.defaultCharset() , expectedArtifactContents);
696 servlet.setDavSessionProvider( davSessionProvider );
698 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
699 archivaDavResourceFactory.setHttpAuth( httpAuth );
700 archivaDavResourceFactory.setServletAuth( servletAuth );
702 servlet.setResourceFactory( archivaDavResourceFactory );
704 AuthenticationResult result = new AuthenticationResult();
706 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
707 anyObject( HttpServletResponse.class ) ) ).andReturn(
710 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
711 anyObject( AuthenticationResult.class ) ) ).andReturn( true );
713 // ArchivaDavResourceFactory#isAuthorized()
714 SecuritySession session = new DefaultSecuritySession();
716 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
717 anyObject( HttpServletResponse.class ) ) ).andReturn(
720 EasyMock.expect( httpAuth.getSecuritySession( anyObject( HttpSession.class ) ) ).andReturn( session );
722 EasyMock.expect( httpAuth.getSessionUser( anyObject( HttpSession.class ) ) ).andReturn( new SimpleUser() );
724 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq( result ) ) ).andReturn(
728 servletAuth.isAuthorized( anyObject( HttpServletRequest.class ), eq( session ), eq( "internal" ),
729 eq( ArchivaRoleConstants.OPERATION_READ_REPOSITORY ) ) ).andThrow(
730 new UnauthorizedException( "User not authorized to read repository." ) );
731 httpAuthControl.replay();
732 servletAuthControl.replay();
734 MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
735 mockHttpServletRequest.addHeader( "User-Agent", "foo" );
736 mockHttpServletRequest.setMethod( "GET" );
737 mockHttpServletRequest.setRequestURI( "/repository/internal/" + commonsLangJar );
740 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
742 servlet.service( mockHttpServletRequest, mockHttpServletResponse );
744 httpAuthControl.verify();
745 servletAuthControl.verify();
747 assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() );