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.security.ServletAuthenticator;
39 import org.apache.archiva.security.common.ArchivaRoleConstants;
40 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
41 import org.apache.commons.io.FileUtils;
42 import org.apache.commons.io.IOUtils;
43 import org.apache.commons.lang3.StringUtils;
44 import org.apache.jackrabbit.webdav.DavSessionProvider;
45 import org.easymock.EasyMock;
46 import org.easymock.IMocksControl;
47 import org.junit.After;
48 import org.junit.Before;
49 import org.junit.Rule;
50 import org.junit.Test;
51 import org.junit.runner.RunWith;
52 import org.springframework.context.ApplicationContext;
53 import org.springframework.mock.web.MockHttpServletRequest;
54 import org.springframework.mock.web.MockHttpServletResponse;
55 import org.springframework.mock.web.MockServletConfig;
56 import org.springframework.mock.web.MockServletContext;
57 import org.springframework.test.context.ContextConfiguration;
58 import org.springframework.web.context.WebApplicationContext;
60 import javax.inject.Inject;
61 import javax.servlet.ServletContext;
62 import javax.servlet.http.HttpServletRequest;
63 import javax.servlet.http.HttpServletResponse;
64 import javax.servlet.http.HttpSession;
65 import java.io.InputStream;
66 import java.nio.charset.Charset;
67 import java.nio.file.Files;
68 import java.nio.file.Path;
69 import java.nio.file.Paths;
70 import java.util.ArrayList;
71 import java.util.List;
72 import java.util.concurrent.atomic.AtomicReference;
74 import static org.easymock.EasyMock.anyObject;
75 import static org.easymock.EasyMock.eq;
78 * RepositoryServletSecurityTest Test the flow of the authentication and authorization checks. This does not necessarily
79 * perform redback security checking.
81 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
82 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context-servlet-security-test.xml" } )
83 public class RepositoryServletSecurityTest
86 protected static final String REPOID_INTERNAL = "internal";
89 protected ArchivaConfiguration archivaConfiguration;
92 protected RepositoryRegistry repositoryRegistry;
94 private DavSessionProvider davSessionProvider;
96 private IMocksControl servletAuthControl;
98 private ServletAuthenticator servletAuth;
100 private IMocksControl httpAuthControl;
102 private HttpAuthenticator httpAuth;
104 private RepositoryServlet servlet;
107 ApplicationContext applicationContext;
111 public ArchivaTemporaryFolderRule repoRootInternal = new ArchivaTemporaryFolderRule();
113 private AtomicReference<Path> projectBase = new AtomicReference<>( );
115 public Path getProjectBase() {
116 if (this.projectBase.get()==null) {
117 String pathVal = System.getProperty("mvn.project.base.dir");
119 if ( StringUtils.isEmpty(pathVal)) {
120 baseDir= Paths.get("").toAbsolutePath();
122 baseDir = Paths.get(pathVal).toAbsolutePath();
124 this.projectBase.compareAndSet(null, baseDir);
126 return this.projectBase.get();
137 String appserverBase =
138 System.getProperty( "appserver.base", getProjectBase().resolve( "target/appserver-base" ).toAbsolutePath().toString() );
140 Path testConf = getProjectBase().resolve( "src/test/resources/repository-archiva.xml" );
141 Path testConfDest = Paths.get(appserverBase, "conf/archiva.xml" );
142 FileUtils.copyFile( testConf.toFile(), testConfDest.toFile() );
146 Configuration config = archivaConfiguration.getConfiguration();
147 // clear managed repository
148 List<ManagedRepositoryConfiguration> f1 = new ArrayList<>(config.getManagedRepositories());
149 for (ManagedRepositoryConfiguration f: f1 ) {
150 config.removeManagedRepository(f);
152 assertEquals(0,config.getManagedRepositories().size());
154 config.addManagedRepository(
155 createManagedRepository( REPOID_INTERNAL, "Internal Test Repo", repoRootInternal.getRoot() ) );
157 saveConfiguration( archivaConfiguration );
159 CacheManager.getInstance().clearAll();
162 servletAuthControl = EasyMock.createControl();
164 servletAuth = servletAuthControl.createMock( ServletAuthenticator.class );
166 httpAuthControl = EasyMock.createControl();
168 httpAuth = httpAuthControl.createMock( HttpAuthenticator.class );
170 davSessionProvider = new ArchivaDavSessionProvider( servletAuth, httpAuth );
172 final MockServletContext mockServletContext = new MockServletContext();
174 WebApplicationContext webApplicationContext =
175 new AbstractRepositoryServletTestCase.TestWebapplicationContext( applicationContext, mockServletContext );
177 mockServletContext.setAttribute( WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
178 webApplicationContext );
180 MockServletConfig mockServletConfig = new MockServletConfig()
183 public ServletContext getServletContext()
185 return mockServletContext;
189 servlet = new RepositoryServlet();
191 servlet.init( mockServletConfig );
194 protected ManagedRepositoryConfiguration createManagedRepository( String id, String name, Path location )
196 ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
198 repo.setName( name );
199 repo.setLocation( location.toAbsolutePath().toString() );
203 /*protected void saveConfiguration()
206 saveConfiguration( archivaConfiguration );
209 protected void saveConfiguration( ArchivaConfiguration archivaConfiguration )
212 repositoryRegistry.reload();
213 archivaConfiguration.save( archivaConfiguration.getConfiguration() );
216 /*protected void setupCleanRepo( File repoRootDir )
223 public void tearDown()
227 /* if ( repoRootInternal.exists() )
229 FileUtils.deleteDirectory( repoRootInternal );
233 String appBaseProp = System.getProperty( "appserver.base" );
234 if (StringUtils.isNotEmpty( appBaseProp )) {
235 org.apache.archiva.common.utils.FileUtils.deleteDirectory( Paths.get(appBaseProp) );
241 // test deploy with invalid user, and guest has no write access to repo
242 // 401 must be returned
244 public void testPutWithInvalidUserAndGuestHasNoWriteAccess()
248 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
249 assertNotNull( "artifact.jar inputstream", is );
251 servlet.setDavSessionProvider( davSessionProvider );
253 AuthenticationResult result = new AuthenticationResult();
255 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
256 anyObject( HttpServletResponse.class ) ) ).andReturn(
259 servletAuth.isAuthenticated( EasyMock.anyObject( HttpServletRequest.class ),
260 EasyMock.anyObject( AuthenticationResult.class ) );
261 EasyMock.expectLastCall().andThrow( new AuthenticationException( "Authentication error" ) );
263 servletAuth.isAuthorized( "guest", "internal", ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD );
265 EasyMock.expectLastCall().andThrow( new UnauthorizedException( "'guest' has no write access to repository" ) );
267 httpAuthControl.replay();
268 servletAuthControl.replay();
269 MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
270 mockHttpServletRequest.addHeader( "User-Agent", "foo" );
271 mockHttpServletRequest.setMethod( "PUT" );
272 mockHttpServletRequest.setRequestURI( "/repository/internal/path/to/artifact.jar" );
273 mockHttpServletRequest.setContent( IOUtils.toByteArray( is ) );
274 mockHttpServletRequest.setContentType( "application/octet-stream" );
276 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
278 servlet.service( mockHttpServletRequest, mockHttpServletResponse );
280 httpAuthControl.verify();
281 servletAuthControl.verify();
283 assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() );
286 // test deploy with invalid user, but guest has write access to repo
288 public void testPutWithInvalidUserAndGuestHasWriteAccess()
292 servlet.setDavSessionProvider( davSessionProvider );
294 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
295 archivaDavResourceFactory.setHttpAuth( httpAuth );
296 archivaDavResourceFactory.setServletAuth( servletAuth );
298 servlet.setResourceFactory( archivaDavResourceFactory );
300 AuthenticationResult result = new AuthenticationResult();
302 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
303 anyObject( HttpServletResponse.class ) ) ).andReturn(
306 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
307 anyObject( AuthenticationResult.class ) ) ).andThrow(
308 new AuthenticationException( "Authentication error" ) );
310 EasyMock.expect( servletAuth.isAuthorized( "guest", "internal",
311 ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD ) ).andReturn(
314 // ArchivaDavResourceFactory#isAuthorized()
315 SecuritySession session = new DefaultSecuritySession();
317 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
318 anyObject( HttpServletResponse.class ) ) ).andReturn(
321 EasyMock.expect( httpAuth.getSecuritySession( anyObject( HttpSession.class ) ) ).andReturn( session );
323 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq( result ) ) ).andThrow(
324 new AuthenticationException( "Authentication error" ) );
326 EasyMock.expect( httpAuth.getSessionUser( anyObject( HttpSession.class ) ) ).andReturn( null );
328 // check if guest has write access
329 EasyMock.expect( servletAuth.isAuthorized( "guest", "internal",
330 ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD ) ).andReturn(
333 httpAuthControl.replay();
334 servletAuthControl.replay();
336 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
337 assertNotNull( "artifact.jar inputstream", is );
339 MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
340 mockHttpServletRequest.addHeader( "User-Agent", "foo" );
341 mockHttpServletRequest.setMethod( "PUT" );
342 mockHttpServletRequest.setRequestURI( "/repository/internal/path/to/artifact.jar" );
343 mockHttpServletRequest.setContent( IOUtils.toByteArray( is ) );
344 mockHttpServletRequest.setContentType( "application/octet-stream" );
346 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
348 servlet.service( mockHttpServletRequest, mockHttpServletResponse );
350 httpAuthControl.verify();
351 servletAuthControl.verify();
353 assertEquals( HttpServletResponse.SC_CREATED, mockHttpServletResponse.getStatus() );
356 // test deploy with a valid user with no write access
358 public void testPutWithValidUserWithNoWriteAccess()
362 servlet.setDavSessionProvider( davSessionProvider );
364 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
365 archivaDavResourceFactory.setHttpAuth( httpAuth );
366 archivaDavResourceFactory.setServletAuth( servletAuth );
367 servlet.setResourceFactory( archivaDavResourceFactory );
369 AuthenticationResult result = new AuthenticationResult();
371 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
372 anyObject( HttpServletResponse.class ) ) ).andReturn(
375 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
376 anyObject( AuthenticationResult.class ) ) ).andReturn( true );
378 // ArchivaDavResourceFactory#isAuthorized()
379 SecuritySession session = new DefaultSecuritySession();
381 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
382 anyObject( HttpServletResponse.class ) ) ).andReturn(
385 MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
387 EasyMock.expect( httpAuth.getSecuritySession( mockHttpServletRequest.getSession( true ) ) ).andReturn(
390 EasyMock.expect( httpAuth.getSessionUser( mockHttpServletRequest.getSession() ) ).andReturn( new SimpleUser() );
392 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq( result ) ) ).andReturn(
396 servletAuth.isAuthorized( anyObject( HttpServletRequest.class ), eq( session ), eq( "internal" ),
397 eq( ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD ) ) ).andThrow(
398 new UnauthorizedException( "User not authorized" ) );
399 httpAuthControl.replay();
400 servletAuthControl.replay();
402 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
403 assertNotNull( "artifact.jar inputstream", is );
405 mockHttpServletRequest.addHeader( "User-Agent", "foo" );
406 mockHttpServletRequest.setMethod( "PUT" );
407 mockHttpServletRequest.setRequestURI( "/repository/internal/path/to/artifact.jar" );
408 mockHttpServletRequest.setContent( IOUtils.toByteArray( is ) );
409 mockHttpServletRequest.setContentType( "application/octet-stream" );
411 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
413 servlet.service( mockHttpServletRequest, mockHttpServletResponse );
415 httpAuthControl.verify();
416 servletAuthControl.verify();
418 assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() );
421 // test deploy with a valid user with write access
423 public void testPutWithValidUserWithWriteAccess()
426 assertTrue( Files.exists(repoRootInternal.getRoot()) );
428 MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
429 String putUrl = "http://machine.com/repository/internal/path/to/artifact.jar";
430 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
431 assertNotNull( "artifact.jar inputstream", is );
433 servlet.setDavSessionProvider( davSessionProvider );
435 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
436 archivaDavResourceFactory.setHttpAuth( httpAuth );
437 archivaDavResourceFactory.setServletAuth( servletAuth );
439 TestAuditListener listener = new TestAuditListener();
440 archivaDavResourceFactory.addAuditListener( listener );
441 servlet.setResourceFactory( archivaDavResourceFactory );
443 AuthenticationResult result = new AuthenticationResult();
445 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
446 anyObject( HttpServletResponse.class ) ) ).andReturn(
449 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
450 anyObject( AuthenticationResult.class ) ) ).andReturn( true );
452 User user = new SimpleUser();
453 user.setUsername( "admin" );
455 // ArchivaDavResourceFactory#isAuthorized()
456 SecuritySession session = new DefaultSecuritySession();
458 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
459 anyObject( HttpServletResponse.class ) ) ).andReturn(
462 EasyMock.expect( httpAuth.getSecuritySession( mockHttpServletRequest.getSession() ) ).andReturn( session );
464 EasyMock.expect( httpAuth.getSessionUser( mockHttpServletRequest.getSession() ) ).andReturn( user );
466 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq( result ) ) ).andReturn(
470 servletAuth.isAuthorized( anyObject( HttpServletRequest.class ), eq( session ), eq( "internal" ),
471 eq( ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD ) ) ).andReturn( true );
473 httpAuthControl.replay();
474 servletAuthControl.replay();
476 mockHttpServletRequest.addHeader( "User-Agent", "foo" );
477 mockHttpServletRequest.setMethod( "PUT" );
478 mockHttpServletRequest.setRequestURI( "/repository/internal/path/to/artifact.jar" );
479 mockHttpServletRequest.setContent( IOUtils.toByteArray( is ) );
480 mockHttpServletRequest.setContentType( "application/octet-stream" );
482 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
484 servlet.service( mockHttpServletRequest, mockHttpServletResponse );
486 httpAuthControl.verify();
487 servletAuthControl.verify();
489 assertEquals( HttpServletResponse.SC_CREATED, mockHttpServletResponse.getStatus() );
491 assertEquals( "admin", listener.getEvents().get( 0 ).getUserId() );
494 // test get with invalid user, and guest has read access to repo
496 public void testGetWithInvalidUserAndGuestHasReadAccess()
499 String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
500 String expectedArtifactContents = "dummy-commons-lang-artifact";
502 Path artifactFile = repoRootInternal.getRoot().resolve( commonsLangJar );
503 Files.createDirectories(artifactFile.getParent());
505 org.apache.archiva.common.utils.FileUtils.writeStringToFile( artifactFile, Charset.defaultCharset() , expectedArtifactContents);
507 servlet.setDavSessionProvider( davSessionProvider );
509 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
510 archivaDavResourceFactory.setHttpAuth( httpAuth );
511 archivaDavResourceFactory.setServletAuth( servletAuth );
513 servlet.setResourceFactory( archivaDavResourceFactory );
515 AuthenticationResult result = new AuthenticationResult();
517 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
518 anyObject( HttpServletResponse.class ) ) ).andReturn(
521 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
522 anyObject( AuthenticationResult.class ) ) ).andThrow(
523 new AuthenticationException( "Authentication error" ) );
525 EasyMock.expect( servletAuth.isAuthorized( "guest", "internal",
526 ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS ) ).andReturn(
529 // ArchivaDavResourceFactory#isAuthorized()
530 SecuritySession session = new DefaultSecuritySession();
532 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
533 anyObject( HttpServletResponse.class ) ) ).andReturn(
536 EasyMock.expect( httpAuth.getSecuritySession( anyObject( HttpSession.class ) ) ).andReturn( session );
538 EasyMock.expect( httpAuth.getSessionUser( anyObject( HttpSession.class ) ) ).andReturn( null );
540 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq( result ) ) ).andReturn(
544 servletAuth.isAuthorized( anyObject( HttpServletRequest.class ), eq( session ), eq( "internal" ),
545 eq( ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS ) ) ).andReturn( true );
546 httpAuthControl.replay();
547 servletAuthControl.replay();
549 MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
550 mockHttpServletRequest.addHeader( "User-Agent", "foo" );
551 mockHttpServletRequest.setMethod( "GET" );
552 mockHttpServletRequest.setRequestURI( "/repository/internal/" + commonsLangJar );
554 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
556 servlet.service( mockHttpServletRequest, mockHttpServletResponse );
558 httpAuthControl.verify();
559 servletAuthControl.verify();
561 assertEquals( HttpServletResponse.SC_OK, mockHttpServletResponse.getStatus() );
563 assertEquals( "Expected file contents", expectedArtifactContents, mockHttpServletResponse.getContentAsString() );
566 // test get with invalid user, and guest has no read access to repo
568 public void testGetWithInvalidUserAndGuestHasNoReadAccess()
571 String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
572 String expectedArtifactContents = "dummy-commons-lang-artifact";
574 Path artifactFile = repoRootInternal.getRoot().resolve( commonsLangJar );
575 Files.createDirectories(artifactFile.getParent());
577 org.apache.archiva.common.utils.FileUtils.writeStringToFile( artifactFile, Charset.defaultCharset() , expectedArtifactContents);
579 servlet.setDavSessionProvider( davSessionProvider );
581 AuthenticationResult result = new AuthenticationResult();
583 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
584 anyObject( HttpServletResponse.class ) ) ).andReturn(
587 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
588 anyObject( AuthenticationResult.class ) ) ).andThrow(
589 new AuthenticationException( "Authentication error" ) );
591 EasyMock.expect( servletAuth.isAuthorized( "guest", "internal",
592 ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS ) ).andReturn(
594 httpAuthControl.replay();
595 servletAuthControl.replay();
597 MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
598 mockHttpServletRequest.addHeader( "User-Agent", "foo" );
599 mockHttpServletRequest.setMethod( "GET" );
600 mockHttpServletRequest.setRequestURI( "/repository/internal/" + commonsLangJar );
602 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
604 servlet.service( mockHttpServletRequest, mockHttpServletResponse );
606 httpAuthControl.verify();
607 servletAuthControl.verify();
609 assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() );
612 // test get with valid user with read access to repo
614 public void testGetWithAValidUserWithReadAccess()
617 String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
618 String expectedArtifactContents = "dummy-commons-lang-artifact";
620 Path artifactFile = repoRootInternal.getRoot().resolve( commonsLangJar );
621 Files.createDirectories(artifactFile.getParent());
623 org.apache.archiva.common.utils.FileUtils.writeStringToFile( artifactFile, Charset.defaultCharset() , expectedArtifactContents);
625 servlet.setDavSessionProvider( davSessionProvider );
627 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
628 archivaDavResourceFactory.setHttpAuth( httpAuth );
629 archivaDavResourceFactory.setServletAuth( servletAuth );
631 servlet.setResourceFactory( archivaDavResourceFactory );
633 AuthenticationResult result = new AuthenticationResult();
635 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
636 anyObject( HttpServletResponse.class ) ) ).andReturn(
639 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
640 anyObject( AuthenticationResult.class ) ) ).andReturn( true );
641 // ArchivaDavResourceFactory#isAuthorized()
642 SecuritySession session = new DefaultSecuritySession();
644 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
645 anyObject( HttpServletResponse.class ) ) ).andReturn(
648 EasyMock.expect( httpAuth.getSecuritySession( anyObject( HttpSession.class ) ) ).andReturn( session );
650 EasyMock.expect( httpAuth.getSessionUser( anyObject( HttpSession.class ) ) ).andReturn( new SimpleUser() );
652 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq( result ) ) ).andReturn(
656 servletAuth.isAuthorized( anyObject( HttpServletRequest.class ), eq( session ), eq( "internal" ),
657 eq( ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS ) ) ).andReturn( true );
659 httpAuthControl.replay();
660 servletAuthControl.replay();
662 MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
663 mockHttpServletRequest.addHeader( "User-Agent", "foo" );
664 mockHttpServletRequest.setMethod( "GET" );
665 mockHttpServletRequest.setRequestURI( "/repository/internal/" + commonsLangJar );
667 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
669 servlet.service( mockHttpServletRequest, mockHttpServletResponse );
671 httpAuthControl.verify();
672 servletAuthControl.verify();
674 assertEquals( HttpServletResponse.SC_OK, mockHttpServletResponse.getStatus() );
675 assertEquals( "Expected file contents", expectedArtifactContents, mockHttpServletResponse.getContentAsString() );
678 // test get with valid user with no read access to repo
680 public void testGetWithAValidUserWithNoReadAccess()
683 String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
684 String expectedArtifactContents = "dummy-commons-lang-artifact";
686 Path artifactFile = repoRootInternal.getRoot().resolve( commonsLangJar );
687 Files.createDirectories(artifactFile.getParent());
689 org.apache.archiva.common.utils.FileUtils.writeStringToFile( artifactFile, Charset.defaultCharset() , expectedArtifactContents);
691 servlet.setDavSessionProvider( davSessionProvider );
693 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
694 archivaDavResourceFactory.setHttpAuth( httpAuth );
695 archivaDavResourceFactory.setServletAuth( servletAuth );
697 servlet.setResourceFactory( archivaDavResourceFactory );
699 AuthenticationResult result = new AuthenticationResult();
701 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
702 anyObject( HttpServletResponse.class ) ) ).andReturn(
705 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
706 anyObject( AuthenticationResult.class ) ) ).andReturn( true );
708 // ArchivaDavResourceFactory#isAuthorized()
709 SecuritySession session = new DefaultSecuritySession();
711 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
712 anyObject( HttpServletResponse.class ) ) ).andReturn(
715 EasyMock.expect( httpAuth.getSecuritySession( anyObject( HttpSession.class ) ) ).andReturn( session );
717 EasyMock.expect( httpAuth.getSessionUser( anyObject( HttpSession.class ) ) ).andReturn( new SimpleUser() );
719 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq( result ) ) ).andReturn(
723 servletAuth.isAuthorized( anyObject( HttpServletRequest.class ), eq( session ), eq( "internal" ),
724 eq( ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS ) ) ).andThrow(
725 new UnauthorizedException( "User not authorized to read repository." ) );
726 httpAuthControl.replay();
727 servletAuthControl.replay();
729 MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
730 mockHttpServletRequest.addHeader( "User-Agent", "foo" );
731 mockHttpServletRequest.setMethod( "GET" );
732 mockHttpServletRequest.setRequestURI( "/repository/internal/" + commonsLangJar );
735 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
737 servlet.service( mockHttpServletRequest, mockHttpServletResponse );
739 httpAuthControl.verify();
740 servletAuthControl.verify();
742 assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() );