Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

RepositoryServletSecurityTest.java 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. package org.apache.maven.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 java.io.File;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import javax.servlet.http.HttpServletResponse;
  24. import net.sf.ehcache.CacheManager;
  25. import org.apache.commons.io.FileUtils;
  26. import org.apache.jackrabbit.webdav.DavSessionProvider;
  27. import org.apache.maven.archiva.configuration.ArchivaConfiguration;
  28. import org.apache.maven.archiva.configuration.Configuration;
  29. import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
  30. import org.apache.maven.archiva.security.ArchivaRoleConstants;
  31. import org.apache.maven.archiva.security.ServletAuthenticator;
  32. import org.codehaus.plexus.redback.authentication.AuthenticationException;
  33. import org.codehaus.plexus.redback.authentication.AuthenticationResult;
  34. import org.codehaus.plexus.redback.authorization.UnauthorizedException;
  35. import org.codehaus.plexus.redback.system.DefaultSecuritySession;
  36. import org.codehaus.plexus.redback.system.SecuritySession;
  37. import org.codehaus.plexus.redback.users.memory.SimpleUser;
  38. import org.codehaus.plexus.spring.PlexusInSpringTestCase;
  39. import org.codehaus.redback.integration.filter.authentication.HttpAuthenticator;
  40. import org.codehaus.redback.integration.filter.authentication.basic.HttpBasicAuthentication;
  41. import org.easymock.MockControl;
  42. import org.easymock.classextension.MockClassControl;
  43. import com.meterware.httpunit.GetMethodWebRequest;
  44. import com.meterware.httpunit.HttpUnitOptions;
  45. import com.meterware.httpunit.PutMethodWebRequest;
  46. import com.meterware.httpunit.WebRequest;
  47. import com.meterware.httpunit.WebResponse;
  48. import com.meterware.servletunit.InvocationContext;
  49. import com.meterware.servletunit.ServletRunner;
  50. import com.meterware.servletunit.ServletUnitClient;
  51. /**
  52. * RepositoryServletSecurityTest Test the flow of the authentication and authorization checks. This does not necessarily
  53. * perform redback security checking.
  54. *
  55. * @version $Id$
  56. */
  57. public class RepositoryServletSecurityTest
  58. extends PlexusInSpringTestCase
  59. {
  60. protected static final String REPOID_INTERNAL = "internal";
  61. protected ServletUnitClient sc;
  62. protected File repoRootInternal;
  63. private ServletRunner sr;
  64. protected ArchivaConfiguration archivaConfiguration;
  65. private DavSessionProvider davSessionProvider;
  66. private MockControl servletAuthControl;
  67. private ServletAuthenticator servletAuth;
  68. private MockClassControl httpAuthControl;
  69. private HttpAuthenticator httpAuth;
  70. private RepositoryServlet servlet;
  71. public void setUp()
  72. throws Exception
  73. {
  74. super.setUp();
  75. String appserverBase = getTestFile( "target/appserver-base" ).getAbsolutePath();
  76. System.setProperty( "appserver.base", appserverBase );
  77. File testConf = getTestFile( "src/test/resources/repository-archiva.xml" );
  78. File testConfDest = new File( appserverBase, "conf/archiva.xml" );
  79. FileUtils.copyFile( testConf, testConfDest );
  80. archivaConfiguration = (ArchivaConfiguration) lookup( ArchivaConfiguration.class );
  81. repoRootInternal = new File( appserverBase, "data/repositories/internal" );
  82. Configuration config = archivaConfiguration.getConfiguration();
  83. config.addManagedRepository( createManagedRepository( REPOID_INTERNAL, "Internal Test Repo", repoRootInternal ) );
  84. saveConfiguration( archivaConfiguration );
  85. CacheManager.getInstance().removeCache( "url-failures-cache" );
  86. HttpUnitOptions.setExceptionsThrownOnErrorStatus( false );
  87. sr = new ServletRunner( getTestFile( "src/test/resources/WEB-INF/repository-servlet-security-test/web.xml" ) );
  88. sr.registerServlet( "/repository/*", RepositoryServlet.class.getName() );
  89. sc = sr.newClient();
  90. servletAuthControl = MockControl.createControl( ServletAuthenticator.class );
  91. servletAuthControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
  92. servletAuth = (ServletAuthenticator) servletAuthControl.getMock();
  93. httpAuthControl =
  94. MockClassControl.createControl( HttpBasicAuthentication.class, HttpBasicAuthentication.class.getMethods() );
  95. httpAuthControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
  96. httpAuth = (HttpAuthenticator) httpAuthControl.getMock();
  97. davSessionProvider = new ArchivaDavSessionProvider( servletAuth, httpAuth );
  98. }
  99. protected ManagedRepositoryConfiguration createManagedRepository( String id, String name, File location )
  100. {
  101. ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
  102. repo.setId( id );
  103. repo.setName( name );
  104. repo.setLocation( location.getAbsolutePath() );
  105. return repo;
  106. }
  107. protected void saveConfiguration()
  108. throws Exception
  109. {
  110. saveConfiguration( archivaConfiguration );
  111. }
  112. protected void saveConfiguration( ArchivaConfiguration archivaConfiguration )
  113. throws Exception
  114. {
  115. archivaConfiguration.save( archivaConfiguration.getConfiguration() );
  116. }
  117. protected void setupCleanRepo( File repoRootDir )
  118. throws IOException
  119. {
  120. FileUtils.deleteDirectory( repoRootDir );
  121. if ( !repoRootDir.exists() )
  122. {
  123. repoRootDir.mkdirs();
  124. }
  125. }
  126. @Override
  127. protected String getPlexusConfigLocation()
  128. {
  129. return "org/apache/maven/archiva/webdav/RepositoryServletSecurityTest.xml";
  130. }
  131. @Override
  132. protected void tearDown()
  133. throws Exception
  134. {
  135. if ( sc != null )
  136. {
  137. sc.clearContents();
  138. }
  139. if ( sr != null )
  140. {
  141. sr.shutDown();
  142. }
  143. if ( repoRootInternal.exists() )
  144. {
  145. FileUtils.deleteDirectory( repoRootInternal );
  146. }
  147. servlet = null;
  148. super.tearDown();
  149. }
  150. // test deploy with invalid user, and guest has no write access to repo
  151. // 401 must be returned
  152. public void testPutWithInvalidUserAndGuestHasNoWriteAccess()
  153. throws Exception
  154. {
  155. setupCleanRepo( repoRootInternal );
  156. String putUrl = "http://machine.com/repository/internal/path/to/artifact.jar";
  157. InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
  158. assertNotNull( "artifact.jar inputstream", is );
  159. WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
  160. InvocationContext ic = sc.newInvocation( request );
  161. servlet = (RepositoryServlet) ic.getServlet();
  162. servlet.setDavSessionProvider( davSessionProvider );
  163. AuthenticationResult result = new AuthenticationResult();
  164. httpAuthControl.expectAndReturn( httpAuth.getAuthenticationResult( null, null ), result );
  165. servletAuthControl.expectAndThrow( servletAuth.isAuthenticated( null, null ),
  166. new AuthenticationException( "Authentication error" ) );
  167. servletAuth.isAuthorized( "guest", "internal", ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD );
  168. servletAuthControl.setMatcher( MockControl.EQUALS_MATCHER );
  169. servletAuthControl.setThrowable( new UnauthorizedException( "'guest' has no write access to repository" ) );
  170. httpAuthControl.replay();
  171. servletAuthControl.replay();
  172. servlet.service( ic.getRequest(), ic.getResponse() );
  173. httpAuthControl.verify();
  174. servletAuthControl.verify();
  175. // assertEquals(HttpServletResponse.SC_UNAUTHORIZED, response.getResponseCode());
  176. }
  177. // test deploy with invalid user, but guest has write access to repo
  178. public void testPutWithInvalidUserAndGuestHasWriteAccess()
  179. throws Exception
  180. {
  181. setupCleanRepo( repoRootInternal );
  182. String putUrl = "http://machine.com/repository/internal/path/to/artifact.jar";
  183. InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
  184. assertNotNull( "artifact.jar inputstream", is );
  185. WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
  186. InvocationContext ic = sc.newInvocation( request );
  187. servlet = (RepositoryServlet) ic.getServlet();
  188. servlet.setDavSessionProvider( davSessionProvider );
  189. ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
  190. archivaDavResourceFactory.setHttpAuth( httpAuth );
  191. archivaDavResourceFactory.setServletAuth( servletAuth );
  192. servlet.setResourceFactory( archivaDavResourceFactory );
  193. AuthenticationResult result = new AuthenticationResult();
  194. httpAuthControl.expectAndReturn( httpAuth.getAuthenticationResult( null, null ), result );
  195. servletAuthControl.expectAndThrow( servletAuth.isAuthenticated( null, null ),
  196. new AuthenticationException( "Authentication error" ) );
  197. servletAuth.isAuthorized( "guest", "internal", ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD );
  198. servletAuthControl.setMatcher( MockControl.EQUALS_MATCHER );
  199. servletAuthControl.setReturnValue( true );
  200. // ArchivaDavResourceFactory#isAuthorized()
  201. SecuritySession session = new DefaultSecuritySession();
  202. httpAuthControl.expectAndReturn( httpAuth.getAuthenticationResult( null, null ), result );
  203. httpAuthControl.expectAndReturn( httpAuth.getSecuritySession( ic.getRequest().getSession( true ) ), session );
  204. servletAuthControl.expectAndThrow( servletAuth.isAuthenticated( null, result ),
  205. new AuthenticationException( "Authentication error" ) );
  206. httpAuthControl.expectAndReturn( httpAuth.getSessionUser( ic.getRequest().getSession() ), null );
  207. // check if guest has write access
  208. servletAuth.isAuthorized( "guest", "internal", ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD );
  209. servletAuthControl.setMatcher( MockControl.EQUALS_MATCHER );
  210. servletAuthControl.setReturnValue( true );
  211. httpAuthControl.replay();
  212. servletAuthControl.replay();
  213. servlet.service( ic.getRequest(), ic.getResponse() );
  214. httpAuthControl.verify();
  215. servletAuthControl.verify();
  216. // assertEquals( HttpServletResponse.SC_CREATED, response.getResponseCode() );
  217. }
  218. // test deploy with a valid user with no write access
  219. public void testPutWithValidUserWithNoWriteAccess()
  220. throws Exception
  221. {
  222. setupCleanRepo( repoRootInternal );
  223. String putUrl = "http://machine.com/repository/internal/path/to/artifact.jar";
  224. InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
  225. assertNotNull( "artifact.jar inputstream", is );
  226. WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
  227. InvocationContext ic = sc.newInvocation( request );
  228. servlet = (RepositoryServlet) ic.getServlet();
  229. servlet.setDavSessionProvider( davSessionProvider );
  230. ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
  231. archivaDavResourceFactory.setHttpAuth( httpAuth );
  232. archivaDavResourceFactory.setServletAuth( servletAuth );
  233. servlet.setResourceFactory( archivaDavResourceFactory );
  234. AuthenticationResult result = new AuthenticationResult();
  235. httpAuthControl.expectAndReturn( httpAuth.getAuthenticationResult( null, null ), result );
  236. servletAuthControl.expectAndReturn( servletAuth.isAuthenticated( null, null ), true );
  237. // ArchivaDavResourceFactory#isAuthorized()
  238. SecuritySession session = new DefaultSecuritySession();
  239. httpAuthControl.expectAndReturn( httpAuth.getAuthenticationResult( null, null ), result );
  240. httpAuthControl.expectAndReturn( httpAuth.getSecuritySession( ic.getRequest().getSession( true ) ), session );
  241. httpAuthControl.expectAndReturn( httpAuth.getSessionUser( ic.getRequest().getSession() ), new SimpleUser() );
  242. servletAuthControl.expectAndReturn( servletAuth.isAuthenticated( null, result ), true );
  243. servletAuthControl.expectAndThrow(
  244. servletAuth.isAuthorized( null, session, "internal",
  245. ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD ),
  246. new UnauthorizedException( "User not authorized" ) );
  247. httpAuthControl.replay();
  248. servletAuthControl.replay();
  249. servlet.service( ic.getRequest(), ic.getResponse() );
  250. httpAuthControl.verify();
  251. servletAuthControl.verify();
  252. // assertEquals(HttpServletResponse.SC_UNAUTHORIZED, response.getResponseCode());
  253. }
  254. // test deploy with a valid user with write access
  255. public void testPutWithValidUserWithWriteAccess()
  256. throws Exception
  257. {
  258. setupCleanRepo( repoRootInternal );
  259. assertTrue( repoRootInternal.exists() );
  260. String putUrl = "http://machine.com/repository/internal/path/to/artifact.jar";
  261. InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
  262. assertNotNull( "artifact.jar inputstream", is );
  263. WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
  264. InvocationContext ic = sc.newInvocation( request );
  265. servlet = (RepositoryServlet) ic.getServlet();
  266. servlet.setDavSessionProvider( davSessionProvider );
  267. ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
  268. archivaDavResourceFactory.setHttpAuth( httpAuth );
  269. archivaDavResourceFactory.setServletAuth( servletAuth );
  270. servlet.setResourceFactory( archivaDavResourceFactory );
  271. AuthenticationResult result = new AuthenticationResult();
  272. httpAuthControl.expectAndReturn( httpAuth.getAuthenticationResult( null, null ), result );
  273. servletAuthControl.expectAndReturn( servletAuth.isAuthenticated( null, null ), true );
  274. // ArchivaDavResourceFactory#isAuthorized()
  275. SecuritySession session = new DefaultSecuritySession();
  276. httpAuthControl.expectAndReturn( httpAuth.getAuthenticationResult( null, null ), result );
  277. httpAuthControl.expectAndReturn( httpAuth.getSecuritySession( ic.getRequest().getSession( true ) ), session );
  278. httpAuthControl.expectAndReturn( httpAuth.getSessionUser( ic.getRequest().getSession() ), new SimpleUser() );
  279. servletAuthControl.expectAndReturn( servletAuth.isAuthenticated( null, result ), true );
  280. servletAuthControl.expectAndReturn(
  281. servletAuth.isAuthorized( null, session, "internal",
  282. ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD ),
  283. true );
  284. httpAuthControl.replay();
  285. servletAuthControl.replay();
  286. servlet.service( ic.getRequest(), ic.getResponse() );
  287. httpAuthControl.verify();
  288. servletAuthControl.verify();
  289. // assertEquals(HttpServletResponse.SC_CREATED, response.getResponseCode());
  290. }
  291. // test get with invalid user, and guest has read access to repo
  292. public void testGetWithInvalidUserAndGuestHasReadAccess()
  293. throws Exception
  294. {
  295. String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
  296. String expectedArtifactContents = "dummy-commons-lang-artifact";
  297. File artifactFile = new File( repoRootInternal, commonsLangJar );
  298. artifactFile.getParentFile().mkdirs();
  299. FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, null );
  300. WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/" + commonsLangJar );
  301. InvocationContext ic = sc.newInvocation( request );
  302. servlet = (RepositoryServlet) ic.getServlet();
  303. servlet.setDavSessionProvider( davSessionProvider );
  304. ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
  305. archivaDavResourceFactory.setHttpAuth( httpAuth );
  306. archivaDavResourceFactory.setServletAuth( servletAuth );
  307. servlet.setResourceFactory( archivaDavResourceFactory );
  308. AuthenticationResult result = new AuthenticationResult();
  309. httpAuthControl.expectAndReturn( httpAuth.getAuthenticationResult( null, null ), result );
  310. servletAuthControl.expectAndThrow( servletAuth.isAuthenticated( null, null ),
  311. new AuthenticationException( "Authentication error" ) );
  312. servletAuthControl.expectAndReturn(
  313. servletAuth.isAuthorized( "guest", "internal",
  314. ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS ),
  315. true );
  316. // ArchivaDavResourceFactory#isAuthorized()
  317. SecuritySession session = new DefaultSecuritySession();
  318. httpAuthControl.expectAndReturn( httpAuth.getAuthenticationResult( null, null ), result );
  319. httpAuthControl.expectAndReturn( httpAuth.getSecuritySession( ic.getRequest().getSession( true ) ), session );
  320. httpAuthControl.expectAndReturn( httpAuth.getSessionUser( ic.getRequest().getSession() ), null );
  321. servletAuthControl.expectAndReturn( servletAuth.isAuthenticated( null, result ), true );
  322. servletAuthControl.expectAndReturn(
  323. servletAuth.isAuthorized( null, session, "internal",
  324. ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD ),
  325. true );
  326. httpAuthControl.replay();
  327. servletAuthControl.replay();
  328. WebResponse response = sc.getResponse( request );
  329. httpAuthControl.verify();
  330. servletAuthControl.verify();
  331. assertEquals( HttpServletResponse.SC_OK, response.getResponseCode() );
  332. assertEquals( "Expected file contents", expectedArtifactContents, response.getText() );
  333. }
  334. // test get with invalid user, and guest has no read access to repo
  335. public void testGetWithInvalidUserAndGuestHasNoReadAccess()
  336. throws Exception
  337. {
  338. String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
  339. String expectedArtifactContents = "dummy-commons-lang-artifact";
  340. File artifactFile = new File( repoRootInternal, commonsLangJar );
  341. artifactFile.getParentFile().mkdirs();
  342. FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, null );
  343. WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/" + commonsLangJar );
  344. InvocationContext ic = sc.newInvocation( request );
  345. servlet = (RepositoryServlet) ic.getServlet();
  346. servlet.setDavSessionProvider( davSessionProvider );
  347. AuthenticationResult result = new AuthenticationResult();
  348. httpAuthControl.expectAndReturn( httpAuth.getAuthenticationResult( null, null ), result );
  349. servletAuthControl.expectAndThrow( servletAuth.isAuthenticated( null, null ),
  350. new AuthenticationException( "Authentication error" ) );
  351. servletAuthControl.expectAndReturn(
  352. servletAuth.isAuthorized( "guest", "internal",
  353. ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS ),
  354. false );
  355. httpAuthControl.replay();
  356. servletAuthControl.replay();
  357. WebResponse response = sc.getResponse( request );
  358. httpAuthControl.verify();
  359. servletAuthControl.verify();
  360. assertEquals( HttpServletResponse.SC_UNAUTHORIZED, response.getResponseCode() );
  361. }
  362. // test get with valid user with read access to repo
  363. public void testGetWithAValidUserWithReadAccess()
  364. throws Exception
  365. {
  366. String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
  367. String expectedArtifactContents = "dummy-commons-lang-artifact";
  368. File artifactFile = new File( repoRootInternal, commonsLangJar );
  369. artifactFile.getParentFile().mkdirs();
  370. FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, null );
  371. WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/" + commonsLangJar );
  372. InvocationContext ic = sc.newInvocation( request );
  373. servlet = (RepositoryServlet) ic.getServlet();
  374. servlet.setDavSessionProvider( davSessionProvider );
  375. ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
  376. archivaDavResourceFactory.setHttpAuth( httpAuth );
  377. archivaDavResourceFactory.setServletAuth( servletAuth );
  378. servlet.setResourceFactory( archivaDavResourceFactory );
  379. AuthenticationResult result = new AuthenticationResult();
  380. httpAuthControl.expectAndReturn( httpAuth.getAuthenticationResult( null, null ), result );
  381. servletAuthControl.expectAndReturn( servletAuth.isAuthenticated( null, null ), true );
  382. // ArchivaDavResourceFactory#isAuthorized()
  383. SecuritySession session = new DefaultSecuritySession();
  384. httpAuthControl.expectAndReturn( httpAuth.getAuthenticationResult( null, null ), result );
  385. httpAuthControl.expectAndReturn( httpAuth.getSecuritySession( ic.getRequest().getSession( true ) ), session );
  386. httpAuthControl.expectAndReturn( httpAuth.getSessionUser( ic.getRequest().getSession() ), new SimpleUser() );
  387. servletAuthControl.expectAndReturn( servletAuth.isAuthenticated( null, result ), true );
  388. servletAuthControl.expectAndReturn(
  389. servletAuth.isAuthorized( null, session, "internal",
  390. ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD ),
  391. true );
  392. httpAuthControl.replay();
  393. servletAuthControl.replay();
  394. WebResponse response = sc.getResponse( request );
  395. httpAuthControl.verify();
  396. servletAuthControl.verify();
  397. assertEquals( HttpServletResponse.SC_OK, response.getResponseCode() );
  398. assertEquals( "Expected file contents", expectedArtifactContents, response.getText() );
  399. }
  400. // test get with valid user with no read access to repo
  401. public void testGetWithAValidUserWithNoReadAccess()
  402. throws Exception
  403. {
  404. String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
  405. String expectedArtifactContents = "dummy-commons-lang-artifact";
  406. File artifactFile = new File( repoRootInternal, commonsLangJar );
  407. artifactFile.getParentFile().mkdirs();
  408. FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, null );
  409. WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/" + commonsLangJar );
  410. InvocationContext ic = sc.newInvocation( request );
  411. servlet = (RepositoryServlet) ic.getServlet();
  412. servlet.setDavSessionProvider( davSessionProvider );
  413. ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
  414. archivaDavResourceFactory.setHttpAuth( httpAuth );
  415. archivaDavResourceFactory.setServletAuth( servletAuth );
  416. servlet.setResourceFactory( archivaDavResourceFactory );
  417. AuthenticationResult result = new AuthenticationResult();
  418. httpAuthControl.expectAndReturn( httpAuth.getAuthenticationResult( null, null ), result );
  419. servletAuthControl.expectAndReturn( servletAuth.isAuthenticated( null, null ), true );
  420. // ArchivaDavResourceFactory#isAuthorized()
  421. SecuritySession session = new DefaultSecuritySession();
  422. httpAuthControl.expectAndReturn( httpAuth.getAuthenticationResult( null, null ), result );
  423. httpAuthControl.expectAndReturn( httpAuth.getSecuritySession( ic.getRequest().getSession( true ) ), session );
  424. httpAuthControl.expectAndReturn( httpAuth.getSessionUser( ic.getRequest().getSession() ), new SimpleUser() );
  425. servletAuthControl.expectAndReturn( servletAuth.isAuthenticated( null, result ), true );
  426. servletAuthControl.expectAndThrow(
  427. servletAuth.isAuthorized( null, session, "internal",
  428. ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD ),
  429. new UnauthorizedException( "User not authorized to read repository." ) );
  430. httpAuthControl.replay();
  431. servletAuthControl.replay();
  432. WebResponse response = sc.getResponse( request );
  433. httpAuthControl.verify();
  434. servletAuthControl.verify();
  435. assertEquals( HttpServletResponse.SC_UNAUTHORIZED, response.getResponseCode() );
  436. }
  437. }