You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RepositoryServletSecurityTest.java 32KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. package org.apache.archiva.webdav;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import junit.framework.TestCase;
  21. import net.sf.ehcache.CacheManager;
  22. import org.apache.archiva.configuration.ArchivaConfiguration;
  23. import org.apache.archiva.configuration.Configuration;
  24. import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
  25. import org.apache.archiva.redback.authentication.AuthenticationException;
  26. import org.apache.archiva.redback.authentication.AuthenticationResult;
  27. import org.apache.archiva.redback.authorization.UnauthorizedException;
  28. import org.apache.archiva.redback.integration.filter.authentication.HttpAuthenticator;
  29. import org.apache.archiva.redback.system.DefaultSecuritySession;
  30. import org.apache.archiva.redback.system.SecuritySession;
  31. import org.apache.archiva.redback.users.User;
  32. import org.apache.archiva.redback.users.memory.SimpleUser;
  33. import org.apache.archiva.repository.RepositoryRegistry;
  34. import org.apache.archiva.metadata.audit.TestAuditListener;
  35. import org.apache.archiva.repository.base.group.RepositoryGroupHandler;
  36. import org.apache.archiva.security.ServletAuthenticator;
  37. import org.apache.archiva.security.common.ArchivaRoleConstants;
  38. import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
  39. import org.apache.commons.io.FileUtils;
  40. import org.apache.commons.io.IOUtils;
  41. import org.apache.commons.lang3.StringUtils;
  42. import org.apache.jackrabbit.webdav.DavSessionProvider;
  43. import org.easymock.EasyMock;
  44. import org.easymock.IMocksControl;
  45. import org.junit.After;
  46. import org.junit.Before;
  47. import org.junit.Rule;
  48. import org.junit.Test;
  49. import org.junit.runner.RunWith;
  50. import org.springframework.context.ApplicationContext;
  51. import org.springframework.mock.web.MockHttpServletRequest;
  52. import org.springframework.mock.web.MockHttpServletResponse;
  53. import org.springframework.mock.web.MockServletConfig;
  54. import org.springframework.mock.web.MockServletContext;
  55. import org.springframework.test.context.ContextConfiguration;
  56. import org.springframework.web.context.WebApplicationContext;
  57. import javax.inject.Inject;
  58. import javax.servlet.ServletContext;
  59. import javax.servlet.http.HttpServletRequest;
  60. import javax.servlet.http.HttpServletResponse;
  61. import javax.servlet.http.HttpSession;
  62. import java.io.InputStream;
  63. import java.nio.charset.Charset;
  64. import java.nio.file.Files;
  65. import java.nio.file.Path;
  66. import java.nio.file.Paths;
  67. import java.util.ArrayList;
  68. import java.util.List;
  69. import java.util.concurrent.atomic.AtomicReference;
  70. import static org.easymock.EasyMock.anyObject;
  71. import static org.easymock.EasyMock.eq;
  72. /**
  73. * RepositoryServletSecurityTest Test the flow of the authentication and authorization checks. This does not necessarily
  74. * perform redback security checking.
  75. */
  76. @RunWith( ArchivaSpringJUnit4ClassRunner.class )
  77. @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context-servlet-security-test.xml" } )
  78. public class RepositoryServletSecurityTest
  79. extends TestCase
  80. {
  81. protected static final String REPOID_INTERNAL = "internal";
  82. @Inject
  83. protected ArchivaConfiguration archivaConfiguration;
  84. @Inject
  85. protected RepositoryRegistry repositoryRegistry;
  86. @SuppressWarnings( "unused" )
  87. @Inject
  88. RepositoryGroupHandler repositoryGroupHandler;
  89. private DavSessionProvider davSessionProvider;
  90. private IMocksControl servletAuthControl;
  91. private ServletAuthenticator servletAuth;
  92. private IMocksControl httpAuthControl;
  93. private HttpAuthenticator httpAuth;
  94. private RepositoryServlet servlet;
  95. @Inject
  96. ApplicationContext applicationContext;
  97. @Rule
  98. public ArchivaTemporaryFolderRule repoRootInternal = new ArchivaTemporaryFolderRule();
  99. private AtomicReference<Path> projectBase = new AtomicReference<>( );
  100. public Path getProjectBase() {
  101. if (this.projectBase.get()==null) {
  102. String pathVal = System.getProperty("mvn.project.base.dir");
  103. Path baseDir;
  104. if ( StringUtils.isEmpty(pathVal)) {
  105. baseDir= Paths.get("").toAbsolutePath();
  106. } else {
  107. baseDir = Paths.get(pathVal).toAbsolutePath();
  108. }
  109. this.projectBase.compareAndSet(null, baseDir);
  110. }
  111. return this.projectBase.get();
  112. }
  113. @Before
  114. @Override
  115. public void setUp()
  116. throws Exception
  117. {
  118. super.setUp();
  119. String appserverBase =
  120. System.getProperty( "appserver.base", getProjectBase().resolve( "target/appserver-base" ).toAbsolutePath().toString() );
  121. Path testConf = getProjectBase().resolve( "src/test/resources/repository-archiva.xml" );
  122. Path testConfDest = Paths.get(appserverBase, "conf/archiva.xml" );
  123. FileUtils.copyFile( testConf.toFile(), testConfDest.toFile() );
  124. Configuration config = archivaConfiguration.getConfiguration();
  125. // clear managed repository
  126. List<ManagedRepositoryConfiguration> f1 = new ArrayList<>(config.getManagedRepositories());
  127. for (ManagedRepositoryConfiguration f: f1 ) {
  128. config.removeManagedRepository(f);
  129. }
  130. assertEquals(0,config.getManagedRepositories().size());
  131. // add internal repo
  132. config.addManagedRepository(
  133. createManagedRepository( REPOID_INTERNAL, "Internal Test Repo", repoRootInternal.getRoot() ) );
  134. saveConfiguration( archivaConfiguration );
  135. CacheManager.getInstance().clearAll();
  136. servletAuthControl = EasyMock.createControl();
  137. servletAuth = servletAuthControl.createMock( ServletAuthenticator.class );
  138. httpAuthControl = EasyMock.createControl();
  139. httpAuth = httpAuthControl.createMock( HttpAuthenticator.class );
  140. davSessionProvider = new ArchivaDavSessionProvider( servletAuth, httpAuth );
  141. final MockServletContext mockServletContext = new MockServletContext();
  142. WebApplicationContext webApplicationContext =
  143. new AbstractRepositoryServletTestCase.TestWebapplicationContext( applicationContext, mockServletContext );
  144. mockServletContext.setAttribute( WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
  145. webApplicationContext );
  146. MockServletConfig mockServletConfig = new MockServletConfig()
  147. {
  148. @Override
  149. public ServletContext getServletContext()
  150. {
  151. return mockServletContext;
  152. }
  153. };
  154. servlet = new RepositoryServlet();
  155. servlet.init( mockServletConfig );
  156. }
  157. protected ManagedRepositoryConfiguration createManagedRepository( String id, String name, Path location )
  158. {
  159. ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
  160. repo.setId( id );
  161. repo.setName( name );
  162. repo.setLocation( location.toAbsolutePath().toString() );
  163. return repo;
  164. }
  165. /*protected void saveConfiguration()
  166. throws Exception
  167. {
  168. saveConfiguration( archivaConfiguration );
  169. }*/
  170. protected void saveConfiguration( ArchivaConfiguration archivaConfiguration )
  171. throws Exception
  172. {
  173. repositoryRegistry.reload();
  174. archivaConfiguration.save( archivaConfiguration.getConfiguration() );
  175. }
  176. /*protected void setupCleanRepo( File repoRootDir )
  177. throws IOException
  178. {
  179. }*/
  180. @Override
  181. @After
  182. public void tearDown()
  183. throws Exception
  184. {
  185. /* if ( repoRootInternal.exists() )
  186. {
  187. FileUtils.deleteDirectory( repoRootInternal );
  188. }*/
  189. super.tearDown();
  190. String appBaseProp = System.getProperty( "appserver.base" );
  191. if (StringUtils.isNotEmpty( appBaseProp )) {
  192. org.apache.archiva.common.utils.FileUtils.deleteDirectory( Paths.get(appBaseProp) );
  193. }
  194. }
  195. // test deploy with invalid user, and guest has no write access to repo
  196. // 401 must be returned
  197. @Test
  198. public void testPutWithInvalidUserAndGuestHasNoWriteAccess()
  199. throws Exception
  200. {
  201. InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
  202. assertNotNull( "artifact.jar inputstream", is );
  203. servlet.setDavSessionProvider( davSessionProvider );
  204. AuthenticationResult result = new AuthenticationResult();
  205. EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
  206. anyObject( HttpServletResponse.class ) ) ).andReturn(
  207. result );
  208. servletAuth.isAuthenticated( EasyMock.anyObject( HttpServletRequest.class ),
  209. EasyMock.anyObject( AuthenticationResult.class ) );
  210. EasyMock.expectLastCall().andThrow( new AuthenticationException( "Authentication error" ) );
  211. servletAuth.isAuthorized( "guest", "internal", ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD );
  212. EasyMock.expectLastCall().andThrow( new UnauthorizedException( "'guest' has no write access to repository" ) );
  213. httpAuthControl.replay();
  214. servletAuthControl.replay();
  215. MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
  216. mockHttpServletRequest.addHeader( "User-Agent", "foo" );
  217. mockHttpServletRequest.setMethod( "PUT" );
  218. mockHttpServletRequest.setRequestURI( "/repository/internal/path/to/artifact.jar" );
  219. mockHttpServletRequest.setContent( IOUtils.toByteArray( is ) );
  220. mockHttpServletRequest.setContentType( "application/octet-stream" );
  221. MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
  222. servlet.service( mockHttpServletRequest, mockHttpServletResponse );
  223. httpAuthControl.verify();
  224. servletAuthControl.verify();
  225. assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() );
  226. }
  227. // test deploy with invalid user, but guest has write access to repo
  228. @Test
  229. public void testPutWithInvalidUserAndGuestHasWriteAccess()
  230. throws Exception
  231. {
  232. servlet.setDavSessionProvider( davSessionProvider );
  233. ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
  234. archivaDavResourceFactory.setHttpAuth( httpAuth );
  235. archivaDavResourceFactory.setServletAuth( servletAuth );
  236. servlet.setResourceFactory( archivaDavResourceFactory );
  237. AuthenticationResult result = new AuthenticationResult();
  238. EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
  239. anyObject( HttpServletResponse.class ) ) ).andReturn(
  240. result );
  241. EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
  242. anyObject( AuthenticationResult.class ) ) ).andThrow(
  243. new AuthenticationException( "Authentication error" ) );
  244. EasyMock.expect( servletAuth.isAuthorized( "guest", "internal",
  245. ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD ) ).andReturn(
  246. true );
  247. // ArchivaDavResourceFactory#isAuthorized()
  248. SecuritySession session = new DefaultSecuritySession();
  249. EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
  250. anyObject( HttpServletResponse.class ) ) ).andReturn(
  251. result );
  252. EasyMock.expect( httpAuth.getSecuritySession( anyObject( HttpSession.class ) ) ).andReturn( session );
  253. EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq( result ) ) ).andThrow(
  254. new AuthenticationException( "Authentication error" ) );
  255. EasyMock.expect( httpAuth.getSessionUser( anyObject( HttpSession.class ) ) ).andReturn( null );
  256. // check if guest has write access
  257. EasyMock.expect( servletAuth.isAuthorized( "guest", "internal",
  258. ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD ) ).andReturn(
  259. true );
  260. httpAuthControl.replay();
  261. servletAuthControl.replay();
  262. InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
  263. assertNotNull( "artifact.jar inputstream", is );
  264. MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
  265. mockHttpServletRequest.addHeader( "User-Agent", "foo" );
  266. mockHttpServletRequest.setMethod( "PUT" );
  267. mockHttpServletRequest.setRequestURI( "/repository/internal/path/to/artifact.jar" );
  268. mockHttpServletRequest.setContent( IOUtils.toByteArray( is ) );
  269. mockHttpServletRequest.setContentType( "application/octet-stream" );
  270. MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
  271. servlet.service( mockHttpServletRequest, mockHttpServletResponse );
  272. httpAuthControl.verify();
  273. servletAuthControl.verify();
  274. assertEquals( HttpServletResponse.SC_CREATED, mockHttpServletResponse.getStatus() );
  275. }
  276. // test deploy with a valid user with no write access
  277. @Test
  278. public void testPutWithValidUserWithNoWriteAccess()
  279. throws Exception
  280. {
  281. servlet.setDavSessionProvider( davSessionProvider );
  282. ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
  283. archivaDavResourceFactory.setHttpAuth( httpAuth );
  284. archivaDavResourceFactory.setServletAuth( servletAuth );
  285. servlet.setResourceFactory( archivaDavResourceFactory );
  286. AuthenticationResult result = new AuthenticationResult();
  287. EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
  288. anyObject( HttpServletResponse.class ) ) ).andReturn(
  289. result );
  290. EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
  291. anyObject( AuthenticationResult.class ) ) ).andReturn( true );
  292. // ArchivaDavResourceFactory#isAuthorized()
  293. SecuritySession session = new DefaultSecuritySession();
  294. EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
  295. anyObject( HttpServletResponse.class ) ) ).andReturn(
  296. result );
  297. MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
  298. EasyMock.expect( httpAuth.getSecuritySession( mockHttpServletRequest.getSession( true ) ) ).andReturn(
  299. session );
  300. EasyMock.expect( httpAuth.getSessionUser( mockHttpServletRequest.getSession() ) ).andReturn( new SimpleUser() );
  301. EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq( result ) ) ).andReturn(
  302. true );
  303. EasyMock.expect(
  304. servletAuth.isAuthorized( anyObject( HttpServletRequest.class ), eq( session ), eq( "internal" ),
  305. eq( ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD ) ) ).andThrow(
  306. new UnauthorizedException( "User not authorized" ) );
  307. httpAuthControl.replay();
  308. servletAuthControl.replay();
  309. InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
  310. assertNotNull( "artifact.jar inputstream", is );
  311. mockHttpServletRequest.addHeader( "User-Agent", "foo" );
  312. mockHttpServletRequest.setMethod( "PUT" );
  313. mockHttpServletRequest.setRequestURI( "/repository/internal/path/to/artifact.jar" );
  314. mockHttpServletRequest.setContent( IOUtils.toByteArray( is ) );
  315. mockHttpServletRequest.setContentType( "application/octet-stream" );
  316. MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
  317. servlet.service( mockHttpServletRequest, mockHttpServletResponse );
  318. httpAuthControl.verify();
  319. servletAuthControl.verify();
  320. assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() );
  321. }
  322. // test deploy with a valid user with write access
  323. @Test
  324. public void testPutWithValidUserWithWriteAccess()
  325. throws Exception
  326. {
  327. assertTrue( Files.exists(repoRootInternal.getRoot()) );
  328. MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
  329. String putUrl = "http://machine.com/repository/internal/path/to/artifact.jar";
  330. InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
  331. assertNotNull( "artifact.jar inputstream", is );
  332. servlet.setDavSessionProvider( davSessionProvider );
  333. ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
  334. archivaDavResourceFactory.setHttpAuth( httpAuth );
  335. archivaDavResourceFactory.setServletAuth( servletAuth );
  336. TestAuditListener listener = new TestAuditListener();
  337. archivaDavResourceFactory.addAuditListener( listener );
  338. servlet.setResourceFactory( archivaDavResourceFactory );
  339. AuthenticationResult result = new AuthenticationResult();
  340. EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
  341. anyObject( HttpServletResponse.class ) ) ).andReturn(
  342. result );
  343. EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
  344. anyObject( AuthenticationResult.class ) ) ).andReturn( true );
  345. User user = new SimpleUser();
  346. user.setUsername( "admin" );
  347. // ArchivaDavResourceFactory#isAuthorized()
  348. SecuritySession session = new DefaultSecuritySession();
  349. EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
  350. anyObject( HttpServletResponse.class ) ) ).andReturn(
  351. result );
  352. EasyMock.expect( httpAuth.getSecuritySession( mockHttpServletRequest.getSession() ) ).andReturn( session );
  353. EasyMock.expect( httpAuth.getSessionUser( mockHttpServletRequest.getSession() ) ).andReturn( user );
  354. EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq( result ) ) ).andReturn(
  355. true );
  356. EasyMock.expect(
  357. servletAuth.isAuthorized( anyObject( HttpServletRequest.class ), eq( session ), eq( "internal" ),
  358. eq( ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD ) ) ).andReturn( true );
  359. httpAuthControl.replay();
  360. servletAuthControl.replay();
  361. mockHttpServletRequest.addHeader( "User-Agent", "foo" );
  362. mockHttpServletRequest.setMethod( "PUT" );
  363. mockHttpServletRequest.setRequestURI( "/repository/internal/path/to/artifact.jar" );
  364. mockHttpServletRequest.setContent( IOUtils.toByteArray( is ) );
  365. mockHttpServletRequest.setContentType( "application/octet-stream" );
  366. MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
  367. servlet.service( mockHttpServletRequest, mockHttpServletResponse );
  368. httpAuthControl.verify();
  369. servletAuthControl.verify();
  370. assertEquals( HttpServletResponse.SC_CREATED, mockHttpServletResponse.getStatus() );
  371. assertEquals( "admin", listener.getEvents().get( 0 ).getUserId() );
  372. }
  373. // test get with invalid user, and guest has read access to repo
  374. @Test
  375. public void testGetWithInvalidUserAndGuestHasReadAccess()
  376. throws Exception
  377. {
  378. String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
  379. String expectedArtifactContents = "dummy-commons-lang-artifact";
  380. Path artifactFile = repoRootInternal.getRoot().resolve( commonsLangJar );
  381. Files.createDirectories(artifactFile.getParent());
  382. org.apache.archiva.common.utils.FileUtils.writeStringToFile( artifactFile, Charset.defaultCharset() , expectedArtifactContents);
  383. servlet.setDavSessionProvider( davSessionProvider );
  384. ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
  385. archivaDavResourceFactory.setHttpAuth( httpAuth );
  386. archivaDavResourceFactory.setServletAuth( servletAuth );
  387. servlet.setResourceFactory( archivaDavResourceFactory );
  388. AuthenticationResult result = new AuthenticationResult();
  389. EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
  390. anyObject( HttpServletResponse.class ) ) ).andReturn(
  391. result );
  392. EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
  393. anyObject( AuthenticationResult.class ) ) ).andThrow(
  394. new AuthenticationException( "Authentication error" ) );
  395. EasyMock.expect( servletAuth.isAuthorized( "guest", "internal",
  396. ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS ) ).andReturn(
  397. true );
  398. // ArchivaDavResourceFactory#isAuthorized()
  399. SecuritySession session = new DefaultSecuritySession();
  400. EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
  401. anyObject( HttpServletResponse.class ) ) ).andReturn(
  402. result );
  403. EasyMock.expect( httpAuth.getSecuritySession( anyObject( HttpSession.class ) ) ).andReturn( session );
  404. EasyMock.expect( httpAuth.getSessionUser( anyObject( HttpSession.class ) ) ).andReturn( null );
  405. EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq( result ) ) ).andReturn(
  406. true );
  407. EasyMock.expect(
  408. servletAuth.isAuthorized( anyObject( HttpServletRequest.class ), eq( session ), eq( "internal" ),
  409. eq( ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS ) ) ).andReturn( true );
  410. httpAuthControl.replay();
  411. servletAuthControl.replay();
  412. MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
  413. mockHttpServletRequest.addHeader( "User-Agent", "foo" );
  414. mockHttpServletRequest.setMethod( "GET" );
  415. mockHttpServletRequest.setRequestURI( "/repository/internal/" + commonsLangJar );
  416. MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
  417. servlet.service( mockHttpServletRequest, mockHttpServletResponse );
  418. httpAuthControl.verify();
  419. servletAuthControl.verify();
  420. assertEquals( HttpServletResponse.SC_OK, mockHttpServletResponse.getStatus() );
  421. assertEquals( "Expected file contents", expectedArtifactContents, mockHttpServletResponse.getContentAsString() );
  422. }
  423. // test get with invalid user, and guest has no read access to repo
  424. @Test
  425. public void testGetWithInvalidUserAndGuestHasNoReadAccess()
  426. throws Exception
  427. {
  428. String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
  429. String expectedArtifactContents = "dummy-commons-lang-artifact";
  430. Path artifactFile = repoRootInternal.getRoot().resolve( commonsLangJar );
  431. Files.createDirectories(artifactFile.getParent());
  432. org.apache.archiva.common.utils.FileUtils.writeStringToFile( artifactFile, Charset.defaultCharset() , expectedArtifactContents);
  433. servlet.setDavSessionProvider( davSessionProvider );
  434. AuthenticationResult result = new AuthenticationResult();
  435. EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
  436. anyObject( HttpServletResponse.class ) ) ).andReturn(
  437. result );
  438. EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
  439. anyObject( AuthenticationResult.class ) ) ).andThrow(
  440. new AuthenticationException( "Authentication error" ) );
  441. EasyMock.expect( servletAuth.isAuthorized( "guest", "internal",
  442. ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS ) ).andReturn(
  443. false );
  444. httpAuthControl.replay();
  445. servletAuthControl.replay();
  446. MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
  447. mockHttpServletRequest.addHeader( "User-Agent", "foo" );
  448. mockHttpServletRequest.setMethod( "GET" );
  449. mockHttpServletRequest.setRequestURI( "/repository/internal/" + commonsLangJar );
  450. MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
  451. servlet.service( mockHttpServletRequest, mockHttpServletResponse );
  452. httpAuthControl.verify();
  453. servletAuthControl.verify();
  454. assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() );
  455. }
  456. // test get with valid user with read access to repo
  457. @Test
  458. public void testGetWithAValidUserWithReadAccess()
  459. throws Exception
  460. {
  461. String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
  462. String expectedArtifactContents = "dummy-commons-lang-artifact";
  463. Path artifactFile = repoRootInternal.getRoot().resolve( commonsLangJar );
  464. Files.createDirectories(artifactFile.getParent());
  465. org.apache.archiva.common.utils.FileUtils.writeStringToFile( artifactFile, Charset.defaultCharset() , expectedArtifactContents);
  466. servlet.setDavSessionProvider( davSessionProvider );
  467. ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
  468. archivaDavResourceFactory.setHttpAuth( httpAuth );
  469. archivaDavResourceFactory.setServletAuth( servletAuth );
  470. servlet.setResourceFactory( archivaDavResourceFactory );
  471. AuthenticationResult result = new AuthenticationResult();
  472. EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
  473. anyObject( HttpServletResponse.class ) ) ).andReturn(
  474. result );
  475. EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
  476. anyObject( AuthenticationResult.class ) ) ).andReturn( true );
  477. // ArchivaDavResourceFactory#isAuthorized()
  478. SecuritySession session = new DefaultSecuritySession();
  479. EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
  480. anyObject( HttpServletResponse.class ) ) ).andReturn(
  481. result );
  482. EasyMock.expect( httpAuth.getSecuritySession( anyObject( HttpSession.class ) ) ).andReturn( session );
  483. EasyMock.expect( httpAuth.getSessionUser( anyObject( HttpSession.class ) ) ).andReturn( new SimpleUser() );
  484. EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq( result ) ) ).andReturn(
  485. true );
  486. EasyMock.expect(
  487. servletAuth.isAuthorized( anyObject( HttpServletRequest.class ), eq( session ), eq( "internal" ),
  488. eq( ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS ) ) ).andReturn( true );
  489. httpAuthControl.replay();
  490. servletAuthControl.replay();
  491. MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
  492. mockHttpServletRequest.addHeader( "User-Agent", "foo" );
  493. mockHttpServletRequest.setMethod( "GET" );
  494. mockHttpServletRequest.setRequestURI( "/repository/internal/" + commonsLangJar );
  495. MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
  496. servlet.service( mockHttpServletRequest, mockHttpServletResponse );
  497. httpAuthControl.verify();
  498. servletAuthControl.verify();
  499. assertEquals( HttpServletResponse.SC_OK, mockHttpServletResponse.getStatus() );
  500. assertEquals( "Expected file contents", expectedArtifactContents, mockHttpServletResponse.getContentAsString() );
  501. }
  502. // test get with valid user with no read access to repo
  503. @Test
  504. public void testGetWithAValidUserWithNoReadAccess()
  505. throws Exception
  506. {
  507. String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
  508. String expectedArtifactContents = "dummy-commons-lang-artifact";
  509. Path artifactFile = repoRootInternal.getRoot().resolve( commonsLangJar );
  510. Files.createDirectories(artifactFile.getParent());
  511. org.apache.archiva.common.utils.FileUtils.writeStringToFile( artifactFile, Charset.defaultCharset() , expectedArtifactContents);
  512. servlet.setDavSessionProvider( davSessionProvider );
  513. ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
  514. archivaDavResourceFactory.setHttpAuth( httpAuth );
  515. archivaDavResourceFactory.setServletAuth( servletAuth );
  516. servlet.setResourceFactory( archivaDavResourceFactory );
  517. AuthenticationResult result = new AuthenticationResult();
  518. EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
  519. anyObject( HttpServletResponse.class ) ) ).andReturn(
  520. result );
  521. EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
  522. anyObject( AuthenticationResult.class ) ) ).andReturn( true );
  523. // ArchivaDavResourceFactory#isAuthorized()
  524. SecuritySession session = new DefaultSecuritySession();
  525. EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
  526. anyObject( HttpServletResponse.class ) ) ).andReturn(
  527. result );
  528. EasyMock.expect( httpAuth.getSecuritySession( anyObject( HttpSession.class ) ) ).andReturn( session );
  529. EasyMock.expect( httpAuth.getSessionUser( anyObject( HttpSession.class ) ) ).andReturn( new SimpleUser() );
  530. EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq( result ) ) ).andReturn(
  531. true );
  532. EasyMock.expect(
  533. servletAuth.isAuthorized( anyObject( HttpServletRequest.class ), eq( session ), eq( "internal" ),
  534. eq( ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS ) ) ).andThrow(
  535. new UnauthorizedException( "User not authorized to read repository." ) );
  536. httpAuthControl.replay();
  537. servletAuthControl.replay();
  538. MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
  539. mockHttpServletRequest.addHeader( "User-Agent", "foo" );
  540. mockHttpServletRequest.setMethod( "GET" );
  541. mockHttpServletRequest.setRequestURI( "/repository/internal/" + commonsLangJar );
  542. MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
  543. servlet.service( mockHttpServletRequest, mockHttpServletResponse );
  544. httpAuthControl.verify();
  545. servletAuthControl.verify();
  546. assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() );
  547. }
  548. }