Author: bdelacretaz
Date: Tue Jun 4 15:12:25 2013
New Revision: 1489464
URL: http://svn.apache.org/r1489464
Log:
SLING-2822 - authorize only admin to execute sling hc rules
Added:
sling/trunk/contrib/extensions/healthcheck/hc-sling/src/main/java/org/apache/sling/hc/sling/impl/RulesExecutionPermission.java
Modified:
sling/trunk/contrib/extensions/healthcheck/hc-sling/src/main/java/org/apache/sling/hc/sling/impl/RulesResourceParserImpl.java
sling/trunk/contrib/extensions/healthcheck/hc-sling/src/main/java/org/apache/sling/hc/sling/impl/SlingHealthCheckServlet.java
sling/trunk/contrib/extensions/healthcheck/hc-sling/src/test/java/org/apache/sling/hc/sling/MockResolver.java
sling/trunk/contrib/extensions/healthcheck/hc-sling/src/test/java/org/apache/sling/hc/sling/RulesResourceParserTest.java
Added:
sling/trunk/contrib/extensions/healthcheck/hc-sling/src/main/java/org/apache/sling/hc/sling/impl/RulesExecutionPermission.java
URL:
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/healthcheck/hc-sling/src/main/java/org/apache/sling/hc/sling/impl/RulesExecutionPermission.java?rev=1489464&view=auto
==============================================================================
---
sling/trunk/contrib/extensions/healthcheck/hc-sling/src/main/java/org/apache/sling/hc/sling/impl/RulesExecutionPermission.java
(added)
+++
sling/trunk/contrib/extensions/healthcheck/hc-sling/src/main/java/org/apache/sling/hc/sling/impl/RulesExecutionPermission.java
Tue Jun 4 15:12:25 2013
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The SF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ */
+package org.apache.sling.hc.sling.impl;
+
+import javax.jcr.AccessDeniedException;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+
+import org.apache.sling.api.resource.Resource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** Used to restrict the execution of health check rules
+ * to authorized users.
+ */
+public class RulesExecutionPermission {
+ /** For now, to be authorized to execute our rules, the current
+ * user needs write access to /libs - it's a realistic
+ * way to check that they are admin, and if they have write access
+ * there they can create a lot of trouble anyway.
+ */
+ public static final String REF_PATH = "/libs";
+ public static final String REQUIRED_PERMISSION = "add_node";
+
+ private final Logger log = LoggerFactory.getLogger(getClass());
+
+ /** Check if the user to which r points is authorized to execute our Rules
*/
+ public void checkPermission(Resource r) throws RepositoryException {
+ final Session s = r.getResourceResolver().adaptTo(Session.class);
+ if(s == null) {
+ log.warn("Adapting {} to a Session returns null, cannot check
permissions", r);
+ throw new AccessDeniedException("No Session, cannot check
permissions");
+ }
+ s.checkPermission(REF_PATH, REQUIRED_PERMISSION);
+ }
+}
Modified:
sling/trunk/contrib/extensions/healthcheck/hc-sling/src/main/java/org/apache/sling/hc/sling/impl/RulesResourceParserImpl.java
URL:
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/healthcheck/hc-sling/src/main/java/org/apache/sling/hc/sling/impl/RulesResourceParserImpl.java?rev=1489464&r1=1489463&r2=1489464&view=diff
==============================================================================
---
sling/trunk/contrib/extensions/healthcheck/hc-sling/src/main/java/org/apache/sling/hc/sling/impl/RulesResourceParserImpl.java
(original)
+++
sling/trunk/contrib/extensions/healthcheck/hc-sling/src/main/java/org/apache/sling/hc/sling/impl/RulesResourceParserImpl.java
Tue Jun 4 15:12:25 2013
@@ -17,23 +17,27 @@
*/
package org.apache.sling.hc.sling.impl;
-import org.apache.sling.engine.SlingRequestProcessor;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
+import javax.jcr.RepositoryException;
+
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.api.SlingException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.scripting.SlingScript;
+import org.apache.sling.engine.SlingRequestProcessor;
import org.apache.sling.hc.api.HealthCheckFacade;
import org.apache.sling.hc.api.Rule;
import org.apache.sling.hc.api.RuleBuilder;
import org.apache.sling.hc.sling.api.RulesResourceParser;
-/** Parses a Resource into a list of Rule. See unit tests for details */
+/** Parses a Resource into a list of Rule. See unit tests for details.
+ * TODO should probably be an Adapter instead */
@Component
@Service(value=RulesResourceParser.class)
public class RulesResourceParserImpl implements RulesResourceParser {
@@ -44,8 +48,22 @@ public class RulesResourceParserImpl imp
@Reference
private SlingRequestProcessor requestProcessor;
+ @SuppressWarnings("serial")
+ public static class UnauthorizedException extends SlingException {
+ UnauthorizedException() {
+ super("Current user is not authorized to execute health check
Rules");
+ }
+ };
+
@Override
public List<Rule> parseResource(Resource r) {
+
+ try {
+ new RulesExecutionPermission().checkPermission(r);
+ } catch(RepositoryException rex) {
+ throw new UnauthorizedException();
+ }
+
final List<Rule> result = new ArrayList<Rule>();
recursivelyParseResource(result, r);
return result;
Modified:
sling/trunk/contrib/extensions/healthcheck/hc-sling/src/main/java/org/apache/sling/hc/sling/impl/SlingHealthCheckServlet.java
URL:
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/healthcheck/hc-sling/src/main/java/org/apache/sling/hc/sling/impl/SlingHealthCheckServlet.java?rev=1489464&r1=1489463&r2=1489464&view=diff
==============================================================================
---
sling/trunk/contrib/extensions/healthcheck/hc-sling/src/main/java/org/apache/sling/hc/sling/impl/SlingHealthCheckServlet.java
(original)
+++
sling/trunk/contrib/extensions/healthcheck/hc-sling/src/main/java/org/apache/sling/hc/sling/impl/SlingHealthCheckServlet.java
Tue Jun 4 15:12:25 2013
@@ -66,8 +66,6 @@ public class SlingHealthCheckServlet ext
protected void doGet(SlingHttpServletRequest request,
SlingHttpServletResponse response)
throws ServletException,IOException {
- // TODO restrict execution to admin?
-
// TODO we could cache the engine + rules, not sure if it's worth it...
final RulesEngine engine = healthcheck.getNewRulesEngine();
engine.addRules(parser.parseResource(request.getResource()));
Modified:
sling/trunk/contrib/extensions/healthcheck/hc-sling/src/test/java/org/apache/sling/hc/sling/MockResolver.java
URL:
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/healthcheck/hc-sling/src/test/java/org/apache/sling/hc/sling/MockResolver.java?rev=1489464&r1=1489463&r2=1489464&view=diff
==============================================================================
---
sling/trunk/contrib/extensions/healthcheck/hc-sling/src/test/java/org/apache/sling/hc/sling/MockResolver.java
(original)
+++
sling/trunk/contrib/extensions/healthcheck/hc-sling/src/test/java/org/apache/sling/hc/sling/MockResolver.java
Tue Jun 4 15:12:25 2013
@@ -22,21 +22,42 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
import javax.servlet.http.HttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
+import org.mockito.Matchers;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
class MockResolver implements ResourceResolver {
private final List<MockResource> resources = new ArrayList<MockResource>();
+
+ @Mock
+ private Session session;
void addResource(MockResource r) {
resources.add(r);
}
+ MockResolver() {
+ MockitoAnnotations.initMocks(this);
+ }
+
+ void setUnauthorized() throws RepositoryException {
+
Mockito.doThrow(RepositoryException.class).when(session).checkPermission(Matchers.anyString(),
Matchers.anyString());
+ }
+
+ @SuppressWarnings("unchecked")
@Override
- public <AdapterType> AdapterType adaptTo(Class<AdapterType> arg0) {
+ public <AdapterType> AdapterType adaptTo(Class<AdapterType> target) {
+ if(target == Session.class) {
+ return (AdapterType)session;
+ }
return null;
}
Modified:
sling/trunk/contrib/extensions/healthcheck/hc-sling/src/test/java/org/apache/sling/hc/sling/RulesResourceParserTest.java
URL:
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/healthcheck/hc-sling/src/test/java/org/apache/sling/hc/sling/RulesResourceParserTest.java?rev=1489464&r1=1489463&r2=1489464&view=diff
==============================================================================
---
sling/trunk/contrib/extensions/healthcheck/hc-sling/src/test/java/org/apache/sling/hc/sling/RulesResourceParserTest.java
(original)
+++
sling/trunk/contrib/extensions/healthcheck/hc-sling/src/test/java/org/apache/sling/hc/sling/RulesResourceParserTest.java
Tue Jun 4 15:12:25 2013
@@ -26,6 +26,9 @@ import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
+import javax.jcr.RepositoryException;
+
+import org.apache.sling.api.SlingException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.hc.api.HealthCheckFacade;
import org.apache.sling.hc.api.Rule;
@@ -143,4 +146,11 @@ public class RulesResourceParserTest {
assertTrue("Expecting rules list (" + allText + ") to contain " +
resText, allText.indexOf(resText) >= 0);
}
}
+
+ @Test(expected=SlingException.class)
+ public void testUnauthorized() throws RepositoryException {
+ resolver.setUnauthorized();
+ final Resource root = new MockResource(resolver, "/foo", "test",
"constant", "5", "> 3");
+ parser.parseResource(root);
+ }
}