Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

RepositoryServletSecurityTest.java 32KB

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