JBoss Identity SVN: r815 - authz/trunk/agent/src/test/java/org/jboss/security/authz/agent/test/integration.
by jboss-identity-commits@lists.jboss.org
Author: sohil.shah(a)jboss.com
Date: 2009-10-04 10:13:31 -0400 (Sun, 04 Oct 2009)
New Revision: 815
Modified:
authz/trunk/agent/src/test/java/org/jboss/security/authz/agent/test/integration/TestTimeOfDay.java
Log:
experimenting with introducing environment based components (currentTime)
Modified: authz/trunk/agent/src/test/java/org/jboss/security/authz/agent/test/integration/TestTimeOfDay.java
===================================================================
--- authz/trunk/agent/src/test/java/org/jboss/security/authz/agent/test/integration/TestTimeOfDay.java 2009-10-04 14:07:49 UTC (rev 814)
+++ authz/trunk/agent/src/test/java/org/jboss/security/authz/agent/test/integration/TestTimeOfDay.java 2009-10-04 14:13:31 UTC (rev 815)
@@ -99,6 +99,66 @@
this.enforce(this
.createEnforcementContext(resource, new Read(), accessTime), false);
}
+
+ public void testPermitAfterSpecifiedTime() throws Exception
+ {
+ // SetUp Resource
+ URIResource resource = new URIResource();
+ resource.setUri(new URI("/blah/index.html"));
+
+ Read action = new Read();
+
+ TimeOfDay timeOfDay = new TimeOfDay();
+ Calendar restrictiveCondition = Calendar.getInstance();
+ timeOfDay.setTimeofDay(restrictiveCondition);
+
+ // Setup the Context for the Composition with these components
+ CompositionContext context = new CompositionContext();
+ context.setPolicyTarget(resource);
+ context.addPolicyRule(Effect.PERMIT, action, timeOfDay, "matchIfAfter");
+
+ // Store the policy into the Policy Server
+ this.provisioner.deploy(context);
+
+ this.assertServerState();
+
+ // Go ahead and produce a RequestContext for a "Permit" Enforcement
+ Calendar accessTime = Calendar.getInstance();
+ int hour = accessTime.get(Calendar.HOUR_OF_DAY);
+ accessTime.set(Calendar.HOUR_OF_DAY, (hour + 1));
+ this.enforce(this
+ .createEnforcementContext(resource, new Read(), accessTime), true);
+ }
+
+ public void testDeniedAfterSpecifiedTime() throws Exception
+ {
+ // SetUp Resource
+ URIResource resource = new URIResource();
+ resource.setUri(new URI("/blah/index.html"));
+
+ Read action = new Read();
+
+ TimeOfDay timeOfDay = new TimeOfDay();
+ Calendar restrictiveCondition = Calendar.getInstance();
+ timeOfDay.setTimeofDay(restrictiveCondition);
+
+ // Setup the Context for the Composition with these components
+ CompositionContext context = new CompositionContext();
+ context.setPolicyTarget(resource);
+ context.addPolicyRule(Effect.PERMIT, action, timeOfDay, "matchIfAfter");
+
+ // Store the policy into the Policy Server
+ this.provisioner.deploy(context);
+
+ this.assertServerState();
+
+ // Go ahead and produce a RequestContext for a "Permit" Enforcement
+ Calendar accessTime = Calendar.getInstance();
+ int hour = accessTime.get(Calendar.HOUR_OF_DAY);
+ accessTime.set(Calendar.HOUR_OF_DAY, (hour - 1));
+ this.enforce(this
+ .createEnforcementContext(resource, new Read(), accessTime), false);
+ }
// ------------------------------------------------------------------------------------------------------------------------------------------------------
private EnforcementContext createEnforcementContext(URIResource uriResource,
Operation operation, Calendar accessTime) throws Exception
16 years, 9 months
JBoss Identity SVN: r814 - in authz/trunk: agent/src/test/java/org/jboss/security/authz/agent/test/integration and 1 other directories.
by jboss-identity-commits@lists.jboss.org
Author: sohil.shah(a)jboss.com
Date: 2009-10-04 10:07:49 -0400 (Sun, 04 Oct 2009)
New Revision: 814
Modified:
authz/trunk/agent/src/main/java/org/jboss/security/authz/agent/services/EnforcementStateGenerator.java
authz/trunk/agent/src/test/java/org/jboss/security/authz/agent/test/integration/TestTimeOfDay.java
authz/trunk/policy-server/src/main/java/org/jboss/security/authz/policy/server/plugin/DroolsFunction.java
Log:
experimenting with introducing environment based components (currentTime)
Modified: authz/trunk/agent/src/main/java/org/jboss/security/authz/agent/services/EnforcementStateGenerator.java
===================================================================
--- authz/trunk/agent/src/main/java/org/jboss/security/authz/agent/services/EnforcementStateGenerator.java 2009-10-02 22:09:20 UTC (rev 813)
+++ authz/trunk/agent/src/main/java/org/jboss/security/authz/agent/services/EnforcementStateGenerator.java 2009-10-04 14:07:49 UTC (rev 814)
@@ -27,6 +27,7 @@
import java.util.ArrayList;
import java.util.Map;
import java.util.Set;
+import java.util.Calendar;
import org.apache.log4j.Logger;
@@ -36,6 +37,7 @@
import org.jboss.security.authz.model.Resource;
import org.jboss.security.authz.model.Subject;
import org.jboss.security.authz.model.Action;
+import org.jboss.security.authz.model.Environment;
import org.jboss.security.authz.model.Attribute;
import org.jboss.security.authz.components.subject.Identity;
import org.jboss.security.authz.components.subject.Roles;
@@ -91,6 +93,10 @@
{
input = SecurityContextDataProcessor.processEnforcementState((Map)input);
}
+ else if(Calendar.class.isAssignableFrom(input.getClass()))
+ {
+ //do nothing..
+ }
else
{
input = input.toString();
@@ -125,6 +131,10 @@
cour.add(this.generateRoles((Set<String>)input));
}
}
+ else if(componentCategory.getAttributeCategory().equals(XACMLConstants.ATTRIBUTEID_CURRENT_TIME))
+ {
+ cour.add(this.generateCurrentTime((Calendar)input));
+ }
}
enforcementState = cour.toArray(new AbstractContextObject[0]);
@@ -213,5 +223,22 @@
}
return subject;
- }
+ }
+
+ private Environment generateCurrentTime(Calendar currentTime)
+ {
+ Environment env = new Environment();
+
+ //TODO: fix the DATATYPE_TIME parsing issues in the xacml engine...so for now just use this
+ //as a simple string type
+ //Attribute attribute = new Attribute(XACMLConstants.ATTRIBUTEID_CURRENT_TIME,
+ // XMLSchemaConstants.DATATYPE_TIME, ""+currentTime.getTime().getTime());
+ //env.addAttribute(attribute);
+
+ Attribute attribute = new Attribute(XACMLConstants.ATTRIBUTEID_CURRENT_TIME,
+ XMLSchemaConstants.DATATYPE_STRING, ""+currentTime.getTime().getTime());
+ env.addAttribute(attribute);
+
+ return env;
+ }
}
Modified: authz/trunk/agent/src/test/java/org/jboss/security/authz/agent/test/integration/TestTimeOfDay.java
===================================================================
--- authz/trunk/agent/src/test/java/org/jboss/security/authz/agent/test/integration/TestTimeOfDay.java 2009-10-02 22:09:20 UTC (rev 813)
+++ authz/trunk/agent/src/test/java/org/jboss/security/authz/agent/test/integration/TestTimeOfDay.java 2009-10-04 14:07:49 UTC (rev 814)
@@ -25,14 +25,11 @@
import java.util.Calendar;
import org.jboss.security.authz.model.Effect;
-import org.jboss.security.authz.model.PolicyMetaData;
import org.jboss.security.authz.components.resource.URIResource;
import org.jboss.security.authz.components.subject.Roles;
import org.jboss.security.authz.components.action.Operation;
import org.jboss.security.authz.components.action.Read;
-import org.jboss.security.authz.components.action.Write;
-import org.jboss.security.authz.components.action.Manage;
import org.jboss.security.authz.components.environment.TimeOfDay;
import org.jboss.security.authz.agent.enforcement.EnforcementContext;
@@ -43,38 +40,68 @@
*/
public class TestTimeOfDay extends AbstractIntegrationTest
{
- public void testAccessBeforeSpecifiedTime() throws Exception
+ public void testPermitBeforeSpecifiedTime() throws Exception
{
// SetUp Resource
URIResource resource = new URIResource();
resource.setUri(new URI("/blah/index.html"));
Read action = new Read();
-
+
TimeOfDay timeOfDay = new TimeOfDay();
Calendar restrictiveCondition = Calendar.getInstance();
- int hour = restrictiveCondition.get(Calendar.HOUR_OF_DAY);
- restrictiveCondition.set(Calendar.HOUR_OF_DAY, (hour + 1));
timeOfDay.setTimeofDay(restrictiveCondition);
-
// Setup the Context for the Composition with these components
CompositionContext context = new CompositionContext();
context.setPolicyTarget(resource);
- context.addPolicyRule(Effect.PERMIT, action, timeOfDay,
- "matchIfBefore");
+ context.addPolicyRule(Effect.PERMIT, action, timeOfDay, "matchIfBefore");
- // Store the policy into the Policy Server
+ // Store the policy into the Policy Server
this.provisioner.deploy(context);
this.assertServerState();
// Go ahead and produce a RequestContext for a "Permit" Enforcement
- this.enforce(this.createEnforcementContext(resource, new Read()), true);
- }
+ Calendar accessTime = Calendar.getInstance();
+ int hour = accessTime.get(Calendar.HOUR_OF_DAY);
+ accessTime.set(Calendar.HOUR_OF_DAY, (hour - 1));
+ this.enforce(this
+ .createEnforcementContext(resource, new Read(), accessTime), true);
+ }
+
+ public void testDeniedBeforeSpecifiedTime() throws Exception
+ {
+ // SetUp Resource
+ URIResource resource = new URIResource();
+ resource.setUri(new URI("/blah/index.html"));
+
+ Read action = new Read();
+
+ TimeOfDay timeOfDay = new TimeOfDay();
+ Calendar restrictiveCondition = Calendar.getInstance();
+ timeOfDay.setTimeofDay(restrictiveCondition);
+
+ // Setup the Context for the Composition with these components
+ CompositionContext context = new CompositionContext();
+ context.setPolicyTarget(resource);
+ context.addPolicyRule(Effect.PERMIT, action, timeOfDay, "matchIfBefore");
+
+ // Store the policy into the Policy Server
+ this.provisioner.deploy(context);
+
+ this.assertServerState();
+
+ // Go ahead and produce a RequestContext for a "Permit" Enforcement
+ Calendar accessTime = Calendar.getInstance();
+ int hour = accessTime.get(Calendar.HOUR_OF_DAY);
+ accessTime.set(Calendar.HOUR_OF_DAY, (hour + 1));
+ this.enforce(this
+ .createEnforcementContext(resource, new Read(), accessTime), false);
+ }
// ------------------------------------------------------------------------------------------------------------------------------------------------------
private EnforcementContext createEnforcementContext(URIResource uriResource,
- Operation operation) throws Exception
+ Operation operation, Calendar accessTime) throws Exception
{
// Create an EnforcementContext
EnforcementContext context = new EnforcementContext();
@@ -89,9 +116,9 @@
// Create Action
context.setAttribute("action", operation);
-
+
TimeOfDay timeOfDay = new TimeOfDay();
- timeOfDay.setTimeofDay(Calendar.getInstance());
+ timeOfDay.setTimeofDay(accessTime);
context.setAttribute("currentTime", timeOfDay);
return context;
Modified: authz/trunk/policy-server/src/main/java/org/jboss/security/authz/policy/server/plugin/DroolsFunction.java
===================================================================
--- authz/trunk/policy-server/src/main/java/org/jboss/security/authz/policy/server/plugin/DroolsFunction.java 2009-10-02 22:09:20 UTC (rev 813)
+++ authz/trunk/policy-server/src/main/java/org/jboss/security/authz/policy/server/plugin/DroolsFunction.java 2009-10-04 14:07:49 UTC (rev 814)
@@ -177,9 +177,11 @@
//Inject Roles
Set<String> roles = new HashSet<String>();
- EvaluationResult roleResult = context.getSubjectAttribute(new URI(XMLSchemaConstants.DATATYPE_STRING),
- new URI(XACMLConstants.ATTRIBUTEID_ROLE),
- new URI(XACMLConstants.ATTRIBUTEID_ACCESS_SUBJECT));
+ EvaluationResult roleResult = context.getSubjectAttribute(
+ new URI(XMLSchemaConstants.DATATYPE_STRING), //type
+ new URI(XACMLConstants.ATTRIBUTEID_ROLE), //id
+ new URI(XACMLConstants.ATTRIBUTEID_ACCESS_SUBJECT) //category
+ );
BagAttribute roleValues = (BagAttribute)roleResult.getAttributeValue();
Iterator itr = roleValues.iterator();
@@ -187,12 +189,21 @@
{
StringAttribute str = (StringAttribute)itr.next();
roles.add(str.getValue().toLowerCase());
- }
-
-
+ }
workingMemory.insert(roles);
- //FIXME: hack job
- workingMemory.insert(new Date());
+ //extract the current time attribute if one is specified
+ EvaluationResult timeResult = context.getEnvironmentAttribute(new URI(
+ XMLSchemaConstants.DATATYPE_STRING), //type
+ new URI(XACMLConstants.ATTRIBUTEID_CURRENT_TIME), //id
+ null //issuer
+ );
+ BagAttribute currentTime = (BagAttribute)timeResult.getAttributeValue();
+ itr = currentTime.iterator();
+ while(itr.hasNext())
+ {
+ StringAttribute cour = (StringAttribute)itr.next();
+ workingMemory.insert(new Date(Long.parseLong(cour.getValue())));
+ }
}
}
16 years, 9 months
JBoss Identity SVN: r813 - in authz/trunk: agent/src/test/java/org/jboss/security/authz/agent/test/integration and 6 other directories.
by jboss-identity-commits@lists.jboss.org
Author: sohil.shah(a)jboss.com
Date: 2009-10-02 18:09:20 -0400 (Fri, 02 Oct 2009)
New Revision: 813
Added:
authz/trunk/agent/src/test/java/org/jboss/security/authz/agent/test/integration/
authz/trunk/agent/src/test/java/org/jboss/security/authz/agent/test/integration/AbstractIntegrationTest.java
authz/trunk/agent/src/test/java/org/jboss/security/authz/agent/test/integration/TestTimeOfDay.java
authz/trunk/core-components/src/main/java/org/jboss/security/authz/components/environment/
authz/trunk/core-components/src/main/java/org/jboss/security/authz/components/environment/TimeOfDay.java
authz/trunk/core-components/src/test/java/org/jboss/security/authz/components/environment/
authz/trunk/core-components/src/test/java/org/jboss/security/authz/components/environment/TestTimeDroolsRules.java
Modified:
authz/trunk/component-spec/src/main/java/org/jboss/security/authz/component/ComponentCategory.java
authz/trunk/policy-server/src/main/java/org/jboss/security/authz/policy/server/plugin/DroolsFunction.java
Log:
experimenting with introducing environment based components
Added: authz/trunk/agent/src/test/java/org/jboss/security/authz/agent/test/integration/AbstractIntegrationTest.java
===================================================================
--- authz/trunk/agent/src/test/java/org/jboss/security/authz/agent/test/integration/AbstractIntegrationTest.java (rev 0)
+++ authz/trunk/agent/src/test/java/org/jboss/security/authz/agent/test/integration/AbstractIntegrationTest.java 2009-10-02 22:09:20 UTC (rev 813)
@@ -0,0 +1,152 @@
+/*
+* JBoss, a division of Red Hat
+* Copyright 2006, Red Hat Middleware, LLC, and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.security.authz.agent.test.integration;
+
+import java.util.Set;
+
+import junit.framework.TestCase;
+
+import org.apache.log4j.Logger;
+
+import org.hibernate.Session;
+import org.hibernate.Transaction;
+import org.jboss.security.authz.bootstrap.ServiceContainer;
+import org.jboss.security.authz.agent.enforcement.PolicyEnforcementPoint;
+import org.jboss.security.authz.agent.provisioning.PolicyProvisioner;
+import org.jboss.security.authz.agent.enforcement.EnforcementResponse;
+import org.jboss.security.authz.agent.enforcement.EnforcementContext;
+import org.jboss.security.authz.agent.services.PolicyComposer;
+import org.jboss.security.authz.model.Policy;
+
+import org.jboss.security.authz.policy.server.tools.HibernateService;
+
+/**
+ * @author <a href="mailto:sshah@redhat.com">Sohil Shah</a>
+ */
+public abstract class AbstractIntegrationTest extends TestCase
+{
+ private static Logger log = Logger.getLogger(AbstractIntegrationTest.class);
+
+ PolicyComposer policyComposer;
+ PolicyEnforcementPoint enforcer;
+ PolicyProvisioner provisioner;
+ HibernateService hibernateService;
+
+
+ public void setUp() throws Exception
+ {
+ ServiceContainer.bootstrap();
+
+ this.policyComposer = (PolicyComposer)ServiceContainer.lookup("/agent/PolicyComposer");
+ this.enforcer = (PolicyEnforcementPoint)ServiceContainer.lookup("/agent/LocalEnforcementPoint");
+ this.provisioner = (PolicyProvisioner)ServiceContainer.lookup("/agent/LocalPolicyProvisioner");
+ this.hibernateService = (HibernateService)ServiceContainer.lookup("/policy-server/HibernateService");
+ }
+
+ public void tearDown() throws Exception
+ {
+ ServiceContainer.shutdown();
+ }
+
+ protected void assertServerState() throws Exception
+ {
+ //Assert Policy State of the Server
+ Set<Policy> policies = this.provisioner.readAllPolicies();
+
+ assertTrue("Policy Store must not be empty!!", (policies != null && !policies.isEmpty()));
+ for(Policy policy: policies)
+ {
+ log.info("------------------------------------------------------------------------------");
+ log.info(policy.generateSystemPolicy());
+ }
+ }
+
+ protected void enforce(EnforcementContext enforcementContext,
+ boolean mustBePermitted) throws Exception
+ {
+ EnforcementResponse response = this.enforcer
+ .checkAccess(enforcementContext);
+
+ assertNotNull(response);
+ log.info("-----------------------------------");
+ log.info("Decision=" + response.getMessage());
+
+ if (mustBePermitted)
+ {
+ assertTrue("Access must be granted!!!", response.isAccessGranted());
+ }
+ else
+ {
+ assertFalse("Access must be denied!!!", response.isAccessGranted());
+ }
+ }
+
+ protected EnforcementResponse enforceWithResponse(EnforcementContext enforcementContext,
+ boolean mustBePermitted) throws Exception
+ {
+ EnforcementResponse response = this.enforcer
+ .checkAccess(enforcementContext);
+
+ assertNotNull(response);
+ log.info("-----------------------------------");
+ log.info("Decision=" + response.getMessage());
+
+ if (mustBePermitted)
+ {
+ assertTrue("Access must be granted!!!", response.isAccessGranted());
+ }
+ else
+ {
+ assertFalse("Access must be denied!!!", response.isAccessGranted());
+ }
+ return response;
+ }
+
+ /*protected void deleteAllPolicies()
+ {
+ Session session = null;
+ Transaction tx = null;
+ try
+ {
+ //Join or Create a New Session/Transaction
+ session = hibernateService.getCurrentSession();
+ tx = session.beginTransaction();
+
+ session.createQuery("delete XACMLPolicy").executeUpdate();
+
+ tx.commit();
+ }
+ catch(Throwable t)
+ {
+ log.error(this, t);
+ tx.rollback();
+ throw new RuntimeException(t);
+ }
+ finally
+ {
+ if(session.isOpen())
+ {
+ session.close();
+ }
+ }
+ }*/
+}
Added: authz/trunk/agent/src/test/java/org/jboss/security/authz/agent/test/integration/TestTimeOfDay.java
===================================================================
--- authz/trunk/agent/src/test/java/org/jboss/security/authz/agent/test/integration/TestTimeOfDay.java (rev 0)
+++ authz/trunk/agent/src/test/java/org/jboss/security/authz/agent/test/integration/TestTimeOfDay.java 2009-10-02 22:09:20 UTC (rev 813)
@@ -0,0 +1,99 @@
+/*
+ * JBoss, a division of Red Hat
+ * Copyright 2006, Red Hat Middleware, LLC, and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.security.authz.agent.test.integration;
+
+import java.net.URI;
+import java.util.Calendar;
+
+import org.jboss.security.authz.model.Effect;
+import org.jboss.security.authz.model.PolicyMetaData;
+
+import org.jboss.security.authz.components.resource.URIResource;
+import org.jboss.security.authz.components.subject.Roles;
+import org.jboss.security.authz.components.action.Operation;
+import org.jboss.security.authz.components.action.Read;
+import org.jboss.security.authz.components.action.Write;
+import org.jboss.security.authz.components.action.Manage;
+import org.jboss.security.authz.components.environment.TimeOfDay;
+
+import org.jboss.security.authz.agent.enforcement.EnforcementContext;
+import org.jboss.security.authz.agent.services.CompositionContext;
+
+/**
+ * @author <a href="mailto:sshah@redhat.com">Sohil Shah</a>
+ */
+public class TestTimeOfDay extends AbstractIntegrationTest
+{
+ public void testAccessBeforeSpecifiedTime() throws Exception
+ {
+ // SetUp Resource
+ URIResource resource = new URIResource();
+ resource.setUri(new URI("/blah/index.html"));
+
+ Read action = new Read();
+
+ TimeOfDay timeOfDay = new TimeOfDay();
+ Calendar restrictiveCondition = Calendar.getInstance();
+ int hour = restrictiveCondition.get(Calendar.HOUR_OF_DAY);
+ restrictiveCondition.set(Calendar.HOUR_OF_DAY, (hour + 1));
+ timeOfDay.setTimeofDay(restrictiveCondition);
+
+
+ // Setup the Context for the Composition with these components
+ CompositionContext context = new CompositionContext();
+ context.setPolicyTarget(resource);
+ context.addPolicyRule(Effect.PERMIT, action, timeOfDay,
+ "matchIfBefore");
+
+ // Store the policy into the Policy Server
+ this.provisioner.deploy(context);
+
+ this.assertServerState();
+
+ // Go ahead and produce a RequestContext for a "Permit" Enforcement
+ this.enforce(this.createEnforcementContext(resource, new Read()), true);
+ }
+ // ------------------------------------------------------------------------------------------------------------------------------------------------------
+ private EnforcementContext createEnforcementContext(URIResource uriResource,
+ Operation operation) throws Exception
+ {
+ // Create an EnforcementContext
+ EnforcementContext context = new EnforcementContext();
+
+ // Create Resource
+ context.setAttribute("uri-resource", uriResource);
+
+ // Create Subjects
+ Roles roles = new Roles();
+ roles.addName("user");
+ context.setAttribute("roles", roles);
+
+ // Create Action
+ context.setAttribute("action", operation);
+
+ TimeOfDay timeOfDay = new TimeOfDay();
+ timeOfDay.setTimeofDay(Calendar.getInstance());
+ context.setAttribute("currentTime", timeOfDay);
+
+ return context;
+ }
+}
Modified: authz/trunk/component-spec/src/main/java/org/jboss/security/authz/component/ComponentCategory.java
===================================================================
--- authz/trunk/component-spec/src/main/java/org/jboss/security/authz/component/ComponentCategory.java 2009-09-28 19:04:19 UTC (rev 812)
+++ authz/trunk/component-spec/src/main/java/org/jboss/security/authz/component/ComponentCategory.java 2009-10-02 22:09:20 UTC (rev 813)
@@ -82,9 +82,20 @@
{
target.addActionMatch(expression);
}
- };
+ },
//------Environment-------------------------------------------------------------------------------------------------------------------------------------------
+ CURRENT_TIME
+ {
+ public String getAttributeCategory()
+ {
+ return XACMLConstants.ATTRIBUTEID_CURRENT_TIME;
+ }
+ public void setExpression(Target target, AttributeExpression expression)
+ {
+ target.addEnvironmentMatch(expression);
+ }
+ };
//Define the enum behavior
public abstract String getAttributeCategory();
Added: authz/trunk/core-components/src/main/java/org/jboss/security/authz/components/environment/TimeOfDay.java
===================================================================
--- authz/trunk/core-components/src/main/java/org/jboss/security/authz/components/environment/TimeOfDay.java (rev 0)
+++ authz/trunk/core-components/src/main/java/org/jboss/security/authz/components/environment/TimeOfDay.java 2009-10-02 22:09:20 UTC (rev 813)
@@ -0,0 +1,163 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.security.authz.components.environment;
+
+import java.util.Calendar;
+
+import org.jboss.security.authz.tools.GeneralTool;
+
+import org.jboss.security.authz.component.Component;
+import org.jboss.security.authz.component.ComponentType;
+import org.jboss.security.authz.component.ComponentCategory;
+import org.jboss.security.authz.component.SecurityContextData;
+import org.jboss.security.authz.component.BooleanExpression;
+
+/**
+ * The TimeOfDay Security Component. Represents the "Time of the Day" that is to be used to make an access decision
+ *
+ *
+ * @author <a href="mailto:sshah@redhat.com">Sohil Shah</a>
+ *
+ */
+@Component(
+ name="timeofday",
+ type=ComponentType.LOGIC,
+ category=ComponentCategory.CURRENT_TIME
+)
+public class TimeOfDay
+{
+ protected static final String matchIfBeforeLogic =
+ "import java.util.Date\n"+
+ "import java.util.Calendar\n"+
+
+ "function boolean before(Date currentTime)\n"+
+ "{\n"+
+ "Calendar allowedTime = Calendar.getInstance();\n"+
+ "allowedTime.set(Calendar.HOUR_OF_DAY, <hour_of_day>);\n"+
+ "allowedTime.set(Calendar.MINUTE, <minute>);\n"+
+
+ "allowedTime.set(Calendar.SECOND, 0);\n"+
+ "allowedTime.set(Calendar.MILLISECOND, 0);\n"+
+
+ "return currentTime.before(allowedTime.getTime());\n"+
+ "}\n"+
+
+ "rule \"<reference>\"\n"+
+ "when\n"+
+ "$ruleName: String()\n"+
+ "$currentTime: Date()\n"+
+ "eval($ruleName.contains(\"<reference>\"))\n"+
+ "eval(before($currentTime))\n"+
+ "then\n"+
+ "insert(Boolean.TRUE);\n"+
+ "end\n";
+
+ protected static final String matchIfAfterLogic =
+ "import java.util.Date\n"+
+ "import java.util.Calendar\n"+
+
+ "function boolean after(Date currentTime)\n"+
+ "{\n"+
+ "Calendar allowedTime = Calendar.getInstance();\n"+
+ "allowedTime.set(Calendar.HOUR_OF_DAY, <hour_of_day>);\n"+
+ "allowedTime.set(Calendar.MINUTE, <minute>);\n"+
+
+ "allowedTime.set(Calendar.SECOND, 0);\n"+
+ "allowedTime.set(Calendar.MILLISECOND, 0);\n"+
+
+ "return currentTime.after(allowedTime.getTime());\n"+
+ "}\n"+
+
+ "rule \"<reference>\"\n"+
+ "when\n"+
+ "$ruleName: String()\n"+
+ "$currentTime: Date()\n"+
+ "eval($ruleName.contains(\"<reference>\"))\n"+
+ "eval(after($currentTime))\n"+
+ "then\n"+
+ "insert(Boolean.TRUE);\n"+
+ "end\n";
+
+
+
+ /**
+ * Role Names
+ */
+ @SecurityContextData
+ protected Calendar timeofDay;
+
+
+ public TimeOfDay()
+ {
+
+ }
+
+
+ public Calendar getTimeofDay()
+ {
+ return timeofDay;
+ }
+
+
+
+ public void setTimeofDay(Calendar timeofDay)
+ {
+ this.timeofDay = timeofDay;
+ }
+ // ------------------------------------------------------------------------------------------------------------------------------------------------------------
+ /**
+ * Creates a Policy Rule expression suggesting that access should be granted if the its performed *before* the specified time of the day
+ *
+ * @return the expression
+ */
+ @BooleanExpression
+ public String[] matchIfBefore()
+ {
+ String ruleReference = "timeofday://matchIfBefore/"
+ + GeneralTool.generateUniqueId();
+
+ String ruleLogic = TimeOfDay.matchIfBeforeLogic.replaceAll("<hour_of_day>", ""+this.timeofDay.get(Calendar.HOUR_OF_DAY));
+ ruleLogic = ruleLogic.replaceAll("<minute>", ""+this.timeofDay.get(Calendar.MINUTE));
+ ruleLogic = ruleLogic.replaceAll("<reference>", ""+ruleReference);
+
+ return new String[] { ruleReference, ruleLogic };
+ }
+
+ /**
+ * Creates a Policy Rule expression suggesting that access should be granted if the its performed *after* the specified time of the day
+ *
+ * @return the expression
+ */
+ @BooleanExpression
+ public String[] matchIfAfter()
+ {
+ String ruleReference = "timeofday://matchIfAfter/"
+ + GeneralTool.generateUniqueId();
+
+ String ruleLogic = TimeOfDay.matchIfAfterLogic.replaceAll("<hour_of_day>", ""+this.timeofDay.get(Calendar.HOUR_OF_DAY));
+ ruleLogic = ruleLogic.replaceAll("<minute>", ""+this.timeofDay.get(Calendar.MINUTE));
+ ruleLogic = ruleLogic.replaceAll("<reference>", ""+ruleReference);
+
+ return new String[] { ruleReference, ruleLogic };
+ }
+}
Added: authz/trunk/core-components/src/test/java/org/jboss/security/authz/components/environment/TestTimeDroolsRules.java
===================================================================
--- authz/trunk/core-components/src/test/java/org/jboss/security/authz/components/environment/TestTimeDroolsRules.java (rev 0)
+++ authz/trunk/core-components/src/test/java/org/jboss/security/authz/components/environment/TestTimeDroolsRules.java 2009-10-02 22:09:20 UTC (rev 813)
@@ -0,0 +1,198 @@
+/*
+ * JBoss, a division of Red Hat
+ * Copyright 2006, Red Hat Middleware, LLC, and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.security.authz.components.environment;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.util.Iterator;
+import java.util.Date;
+import java.util.Calendar;
+
+import junit.framework.TestCase;
+
+import org.apache.log4j.Logger;
+
+import org.drools.RuleBase;
+import org.drools.RuleBaseFactory;
+import org.drools.StatefulSession;
+import org.drools.WorkingMemory;
+import org.drools.compiler.PackageBuilder;
+
+import org.jboss.security.authz.components.environment.TimeOfDay;
+
+/**
+ * @author <a href="mailto:sshah@redhat.com">Sohil Shah</a>
+ */
+public class TestTimeDroolsRules extends TestCase
+{
+ private static Logger log = Logger.getLogger(TestTimeDroolsRules.class);
+ private static String rulePkg = "package security\n"
+ + "import org.jboss.security.authz.model.*;\n"
+ + "import org.jboss.security.xacml.interfaces.XACMLConstants;\n";
+
+ private RuleBase activeRuleBase;
+
+ private String matchIfBefore;
+ private String matchIfAfter;
+
+ public void setUp() throws Exception
+ {
+ StringBuilder buffer = new StringBuilder();
+
+ buffer.append(rulePkg + "\n");
+
+ Calendar calendar = Calendar.getInstance();
+
+ buffer.append(this.getMatchIfBefore() + "\n");
+ buffer.append(this.getMatchIfAfter() + "\n");
+
+ Reader source = new InputStreamReader(new ByteArrayInputStream(buffer
+ .toString().getBytes()));
+ try
+ {
+ PackageBuilder packageBuilder = new PackageBuilder();
+ packageBuilder.addPackageFromDrl(source);
+
+ if (this.activeRuleBase == null)
+ {
+ this.activeRuleBase = RuleBaseFactory.newRuleBase();
+ }
+
+ // Perform the reloading of the RuleBase with the updated Rules
+ this.activeRuleBase.lock();
+ this.activeRuleBase.addPackage(packageBuilder.getPackage());
+ this.activeRuleBase.unlock();
+ }
+ finally
+ {
+ source.close();
+ }
+ }
+
+ public void tearDown() throws Exception
+ {
+ this.activeRuleBase = null;
+ }
+
+ public void testMatchIfBefore() throws Exception
+ {
+ log.info("Executing----------" + this.matchIfBefore);
+ WorkingMemory workingMemory = this.activeRuleBase.newStatefulSession();
+
+ // SetUp the context data
+ workingMemory.insert(this.matchIfBefore);
+ workingMemory.insert(new Date());
+
+ workingMemory.fireAllRules();
+
+ // Extract result
+ boolean success = false;
+ Iterator itr = workingMemory.iterateObjects();
+ while (itr.hasNext())
+ {
+ Object curr = itr.next();
+ if (curr instanceof Boolean)
+ {
+ success = ((Boolean) curr).booleanValue();
+ }
+ }
+
+ ((StatefulSession) workingMemory).dispose();
+
+ assertTrue("Rule did not execute!!", success);
+ }
+
+ public void testMatchIfAfter() throws Exception
+ {
+ log.info("Executing----------" + this.matchIfAfter);
+ WorkingMemory workingMemory = this.activeRuleBase.newStatefulSession();
+
+ // SetUp the context data
+ workingMemory.insert(this.matchIfAfter);
+ workingMemory.insert(new Date());
+
+ workingMemory.fireAllRules();
+
+ // Extract result
+ boolean success = false;
+ Iterator itr = workingMemory.iterateObjects();
+ while (itr.hasNext())
+ {
+ Object curr = itr.next();
+ if (curr instanceof Boolean)
+ {
+ success = ((Boolean) curr).booleanValue();
+ }
+ }
+
+ ((StatefulSession) workingMemory).dispose();
+ assertTrue("Rule did not execute!!", success);
+ }
+
+ // ------------------------------------------------------------------------------------------------------------------------------------------------------
+ private String getMatchIfBefore()
+ {
+ String timerule = null;
+
+ TimeOfDay timeOfDay = new TimeOfDay();
+ Calendar restrictiveCondition = Calendar.getInstance();
+ int hour = restrictiveCondition.get(Calendar.HOUR_OF_DAY);
+ restrictiveCondition.set(Calendar.HOUR_OF_DAY, (hour + 1));
+ timeOfDay.setTimeofDay(restrictiveCondition);
+
+ String[] booleanExpression = timeOfDay.matchIfBefore();
+
+ this.matchIfBefore = booleanExpression[0];
+ timerule = booleanExpression[1];
+ log
+ .info("--------------------------------------------------------------------------------");
+ log.info(timerule);
+ log
+ .info("--------------------------------------------------------------------------------");
+
+ return timerule;
+ }
+
+ private String getMatchIfAfter()
+ {
+ String timerule = null;
+
+ TimeOfDay timeOfDay = new TimeOfDay();
+ Calendar restrictiveCondition = Calendar.getInstance();
+ int hour = restrictiveCondition.get(Calendar.HOUR_OF_DAY);
+ restrictiveCondition.set(Calendar.HOUR_OF_DAY, (hour -1));
+ timeOfDay.setTimeofDay(restrictiveCondition);
+
+ String[] booleanExpression = timeOfDay.matchIfAfter();
+
+ this.matchIfAfter = booleanExpression[0];
+ timerule = booleanExpression[1];
+ log
+ .info("--------------------------------------------------------------------------------");
+ log.info(timerule);
+ log
+ .info("--------------------------------------------------------------------------------");
+
+ return timerule;
+ }
+}
\ No newline at end of file
Modified: authz/trunk/policy-server/src/main/java/org/jboss/security/authz/policy/server/plugin/DroolsFunction.java
===================================================================
--- authz/trunk/policy-server/src/main/java/org/jboss/security/authz/policy/server/plugin/DroolsFunction.java 2009-09-28 19:04:19 UTC (rev 812)
+++ authz/trunk/policy-server/src/main/java/org/jboss/security/authz/policy/server/plugin/DroolsFunction.java 2009-10-02 22:09:20 UTC (rev 813)
@@ -22,6 +22,7 @@
******************************************************************************/
package org.jboss.security.authz.policy.server.plugin;
+import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
@@ -189,6 +190,9 @@
}
- workingMemory.insert(roles);
+ workingMemory.insert(roles);
+
+ //FIXME: hack job
+ workingMemory.insert(new Date());
}
}
16 years, 9 months